ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
rnd.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
41extern char DATATYPE_AS_STRING[][16];
42
50/* <usermanual>
51@keyword RND
52
53@english
54The ''RND'' function generates integers at random, between zero and any number specified
55in brackets (minus one). If your specified number is greater than zero, random numbers will be generated
56up to that maximum number (minus one).
57
58To better understand how the bounds work for random number extraction, if the value ''max''
59is an integer greater than ''1'', then the call to ''RND(max)'' generates a random integer
60between ''0'' and ''max-1''.
61
62@italian
63La funzione ''RND'' genera numeri interi casuali, compresi tra zero e qualsiasi numero
64specificato tra parentesi (meno uno). Se il numero specificato è maggiore di zero, verranno generati
65numeri casuali fino a quel numero massimo (meno uno).
66
67Per meglio comprendere come funzionano gli estremi per l'estrazione dei numeri casuali,
68se il valore ''max'' è un intero più grande di ''1'', allora la chiamata a ''RND(max)''
69genera un numero intero casuale tra ''0'' e ''max-1''.
70
71@syntax = RND(max)
72
73@example score = score + RND(100)
74
75@usedInExample maths_rand_01.bas
76
77@target all
78</usermanual> */
79Variable * rnd( Environment * _environment, char * _value ) {
80
81 Variable * last_random = variable_temporary( _environment, VT_DWORD, "(last temporary for RND)");
82 last_random->locked = 1;
83
84 Variable * value = variable_retrieve( _environment, _value );
85
86 Variable * result;
87
88 Variable * bresult = variable_temporary( _environment, VT_BYTE, "(temporary for RND)");
89
90 Variable * ignored = variable_temporary( _environment, value->type, "(ignored)");
91 Variable * remainder = variable_temporary( _environment, value->type, "(remainder)");
92
93 Variable * zero = variable_temporary( _environment, value->type, "(0)" );
94 variable_store( _environment, zero->name, 0 );
95
97
98 char endLabel[MAX_TEMPORARY_STORAGE]; sprintf(endLabel, "%send", label );
99 char lastRandomLabel[MAX_TEMPORARY_STORAGE]; sprintf(lastRandomLabel, "%slr", label );
100
101 if_then( _environment, variable_compare( _environment, value->name, zero->name )->name );
102 cpu_jump( _environment, lastRandomLabel );
103 end_if_then( _environment );
104
105 result = random_value( _environment, value->type );
106
107 switch( VT_BITWIDTH( value->type ) ) {
108 case 32:
109 cpu_math_div_32bit_to_16bit( _environment, result->realName, value->realName, ignored->realName, remainder->realName, 0 );
110 cpu_move_16bit( _environment, remainder->realName, result->realName );
111 cpu_move_16bit( _environment, remainder->realName, last_random->realName );
112 cpu_jump( _environment, endLabel );
113 break;
114 case 16:
115 cpu_math_div_16bit_to_16bit( _environment, result->realName, value->realName, ignored->realName, remainder->realName, 0 );
116 cpu_move_16bit( _environment, remainder->realName, result->realName );
117 cpu_move_16bit( _environment, remainder->realName, last_random->realName );
118 cpu_jump( _environment, endLabel );
119 break;
120 case 8:
121 cpu_math_div_8bit_to_8bit( _environment, result->realName, value->realName, ignored->realName, remainder->realName, 0 );
122 cpu_move_8bit( _environment, remainder->realName, result->realName );
123 cpu_move_8bit( _environment, remainder->realName, last_random->realName );
124 cpu_jump( _environment, endLabel );
125 break;
126 case 0:
128 break;
129 }
130
131 cpu_label( _environment, lastRandomLabel );
132
133 variable_move( _environment, last_random->name, result->name );
134
135 cpu_label( _environment, endLabel );
136
137 return result;
138}
void cpu_math_div_16bit_to_16bit(Environment *_environment, char *_source, char *_destination, char *_other, char *_other_remainder, int _signed)
Definition 6309.c:2009
void cpu_math_div_32bit_to_16bit(Environment *_environment, char *_source, char *_destination, char *_other, char *_other_remainder, int _signed)
Definition 6309.c:2554
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void cpu_move_16bit(Environment *_environment, char *_source, char *_destination)
CPU 6309: emit code to move 16 bit
Definition 6309.c:1474
void cpu_jump(Environment *_environment, char *_label)
Definition 6309.c:3739
void cpu_move_8bit(Environment *_environment, char *_source, char *_destination)
CPU 6309: emit code to move 8 bit
Definition 6309.c:743
void cpu_math_div_8bit_to_8bit(Environment *_environment, char *_source, char *_destination, char *_other, char *_other_remainder, int _signed)
Definition 6309.c:1163
Variable * variable_retrieve(Environment *_environment, char *_name)
Variable * variable_move(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable by converting it.
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * variable_compare(Environment *_environment, char *_source, char *_destination)
Compare two variable and return the result of comparation.
Variable * variable_store(Environment *_environment, char *_destination, unsigned int _value)
Store a direct value to a variable.
Variable * random_value(Environment *_environment, VariableType _type)
Definition random.c:41
void end_if_then(Environment *_environment)
Emit ASM code for ENDIF.
Definition end_if_then.c:50
void if_then(Environment *_environment, char *_expression)
Emit ASM code for IF ... THEN ....
Definition if_then.c:123
Variable * rnd(Environment *_environment, char *_value)
Return a random value.
Definition rnd.c:79
int locked
Definition ugbc.h:1009
char * name
Definition ugbc.h:979
VariableType type
Definition ugbc.h:988
char * realName
Definition ugbc.h:982
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
#define CRITICAL_RANDOM_UNSUPPORTED(v, t)
Definition ugbc.h:3494
@ VT_BYTE
Definition ugbc.h:450
@ VT_DWORD
Definition ugbc.h:460
#define VT_BITWIDTH(t)
Definition ugbc.h:595
#define MAKE_LABEL
Definition ugbc.h:3351
char DATATYPE_AS_STRING[][16]