ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
input.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
41#if defined(__coco__) || defined(__coco3__) || defined(__d32__) || defined(__d64__) || \
42 defined(__cocob__) || defined(__coco3b__) || defined(__d32b__) || defined(__d64b__)
43
44extern char DATATYPE_AS_STRING[][16];
45
46void input( Environment * _environment, char * _variable, VariableType _default_type ) {
47
49
50 Variable * result;
51 if ( variable_exists( _environment, _variable ) ) {
52 result = variable_retrieve( _environment, _variable );
53 } else {
54 result = variable_define( _environment, _variable, _default_type, 0 );
55 }
56
57 char repeatLabel[MAX_TEMPORARY_STORAGE]; sprintf(repeatLabel, "%srepeat", label );
58 char repeat2Label[MAX_TEMPORARY_STORAGE]; sprintf(repeat2Label, "%srepeat2", label );
59 char skipColorChangeLabel[MAX_TEMPORARY_STORAGE]; sprintf(skipColorChangeLabel, "%sskipcc", label );
60 char graphicalCursor[MAX_TEMPORARY_STORAGE]; sprintf(graphicalCursor, "%sgrph", label );
61 char finishedLabel[MAX_TEMPORARY_STORAGE]; sprintf(finishedLabel, "%sfinished", label );
62 char doneLabel[MAX_TEMPORARY_STORAGE]; sprintf(doneLabel, "%sdone", label );
63 char backspaceLabel[MAX_TEMPORARY_STORAGE]; sprintf(backspaceLabel, "%sbackspace", label );
64
65 Variable * temporary = variable_temporary( _environment, VT_DSTRING, "(temporary storage for input)");
66 Variable * offset = variable_temporary( _environment, VT_BYTE, "(offset inside temporary storage)");
67
68 Variable * enter = variable_temporary( _environment, VT_CHAR, "(enter)" );
69 Variable * comma = variable_temporary( _environment, VT_CHAR, "(comma)" );
70 Variable * space = variable_temporary( _environment, VT_CHAR, "(space)" );
71 Variable * underscore = variable_temporary( _environment, VT_CHAR, "(underscore)" );
72 Variable * underscoreTimer = variable_temporary( _environment, VT_BYTE, "(underscore timer)" );
73 Variable * backspace = variable_temporary( _environment, VT_CHAR, "(backspace)" );
74 Variable * size = variable_temporary( _environment, VT_BYTE, "(size max)" );
75 Variable * pressed = variable_temporary( _environment, VT_BYTE, "(key pressed?)");
76 Variable * key = variable_temporary( _environment, VT_CHAR, "(key pressed)");
77 Variable * key2 = variable_temporary( _environment, VT_CHAR, "(key pressed)");
78 Variable * zero = variable_temporary( _environment, VT_BYTE, "(zero)" );
79
80 cpu_store_8bit( _environment, enter->realName, 13 );
81 cpu_store_8bit( _environment, offset->realName, 0 );
82#if defined(__coco__) || defined(__coco3__) || defined(__cocob__) || defined(__coco3b__)
83 cpu_store_8bit( _environment, backspace->realName, 8 );
84#else
85 cpu_store_8bit( _environment, backspace->realName, 0xfc );
86#endif
87 cpu_store_8bit( _environment, space->realName, 32 );
88 cpu_store_8bit( _environment, zero->realName, 0 );
89
90 if ( _environment->lineInput ) {
91 cpu_store_8bit( _environment, comma->realName, 13 );
92 } else {
93 cpu_store_8bit( _environment, comma->realName, _environment->keyboardConfig.separator == 0 ? INPUT_DEFAULT_SEPARATOR : _environment->keyboardConfig.separator );
94 }
95
96 cpu_store_8bit( _environment, size->realName, _environment->keyboardConfig.size == 0 ? INPUT_DEFAULT_SIZE : _environment->keyboardConfig.size );
97 cpu_store_8bit( _environment, underscore->realName, _environment->keyboardConfig.cursor == 0 ? INPUT_DEFAULT_CURSOR : _environment->keyboardConfig.cursor );
98 cpu_store_8bit( _environment, underscoreTimer->realName, 143 );
99
100 Variable * address = variable_temporary( _environment, VT_ADDRESS, "(address of DSTRING)");
101 cpu_dsfree( _environment, temporary->realName );
102 cpu_dsalloc( _environment, size->realName, temporary->realName );
103 cpu_dsdescriptor( _environment, temporary->realName, address->realName, pressed->realName );
104
105 cpu_label( _environment, repeatLabel );
106
107 Variable * currentTileMode = variable_retrieve( _environment, "CURRENTTILEMODE" );
108 cpu_compare_and_branch_8bit_const( _environment, currentTileMode->realName, 0, graphicalCursor, 1 );
109
110 // It would be advisable to implement a cursor as similar as possible to the system one
111 // for the COCO target. The cursor blink routine is disassembled in Color BASIC Unravelled ...
112 // there's a frame countdown timer ... something like 11-12 frames the color gets switched
113 // to the next one ... and the characters drawn are 128, then 144, then 160, then 176,
114 // and wrapping around to 128 after drawing character 240:
115
116#if defined( __coco3__ )
117
118 cpu_label( _environment, graphicalCursor );
119 cpu_dec( _environment, underscoreTimer->realName );
120 cpu_compare_and_branch_8bit_const( _environment, underscoreTimer->realName, 0, skipColorChangeLabel, 0 );
121 add_complex( _environment, underscore->name, 10, 32, 42, 0 );
122 cpu_store_8bit( _environment, underscoreTimer->realName, 128 );
123
124#else
125
126 cpu_dec( _environment, underscoreTimer->realName );
127 cpu_compare_and_branch_8bit_const( _environment, underscoreTimer->realName, 0, skipColorChangeLabel, 0 );
128 add_complex( _environment, underscore->name, 16, 143, 224, 0 );
129 cpu_store_8bit( _environment, underscoreTimer->realName, 128 );
130 cpu_jump( _environment, skipColorChangeLabel );
131
132 cpu_label( _environment, graphicalCursor );
133 cpu_dec( _environment, underscoreTimer->realName );
134 cpu_compare_and_branch_8bit_const( _environment, underscoreTimer->realName, 0, skipColorChangeLabel, 0 );
135 add_complex( _environment, underscore->name, 10, 32, 42, 0 );
136 cpu_store_8bit( _environment, underscoreTimer->realName, 128 );
137
138#endif
139
140 cpu_label( _environment, skipColorChangeLabel );
141
142 print( _environment, underscore->name, 0, _environment->printRaw );
143 cmove_direct( _environment, -1, 0 );
144
145 pia_inkey( _environment, key->realName );
146
147 cpu_bveq( _environment, key->realName, repeatLabel );
148
149 cpu_label( _environment, repeat2Label );
150
151 pia_inkey( _environment, key2->realName );
152
153 cpu_bvneq( _environment, key2->realName, repeat2Label );
154
155 cpu_compare_8bit( _environment, key->realName, backspace->realName, pressed->realName, 1 );
156
157 cpu_bvneq( _environment, pressed->realName, backspaceLabel );
158
159 cpu_compare_8bit( _environment, key->realName, comma->realName, pressed->realName, 1 );
160
161 cpu_bvneq( _environment, pressed->realName, finishedLabel );
162
163 cpu_compare_8bit( _environment, key->realName, enter->realName, pressed->realName, 1 );
164
165 cpu_bvneq( _environment, pressed->realName, finishedLabel );
166
167 print( _environment, key->name, 0, _environment->printRaw );
168
169 cpu_move_8bit_indirect_with_offset2( _environment, key->realName, address->realName, offset->realName );
170
171 cpu_inc( _environment, offset->realName );
172
173 cpu_compare_8bit( _environment, offset->realName, size->realName, pressed->realName, 1 );
174
175 cpu_bveq( _environment, pressed->realName, repeatLabel );
176
177 cpu_jump( _environment, finishedLabel );
178
179 cpu_label( _environment, backspaceLabel );
180
181 cpu_compare_8bit( _environment, offset->realName, zero->realName, pressed->realName, 1 );
182
183 cpu_bvneq( _environment, pressed->realName, repeatLabel );
184
185 cpu_dec( _environment, offset->realName );
186
187 print( _environment, space->name, 0, _environment->printRaw );
188
189 cmove_direct( _environment, -2, 0 );
190
191 print( _environment, space->name, 0, _environment->printRaw );
192
193 cmove_direct( _environment, -1, 0 );
194
195 cpu_jump( _environment, repeatLabel );
196
197 cpu_label( _environment, finishedLabel );
198
199 print( _environment, space->name, 0, _environment->printRaw );
200 cmove_direct( _environment, -1, 0 );
201
202 cpu_compare_8bit( _environment, comma->realName, enter->realName, pressed->realName, 1 );
203 cpu_bveq( _environment, pressed->realName, doneLabel );
204
205 print_newline( _environment );
206
207 cpu_label( _environment, doneLabel );
208
209 cpu_dsresize( _environment, temporary->realName, offset->realName );
210
211 switch( VT_BITWIDTH( result->type ) ) {
212 case 8:
213 case 16:
214 case 32:
215 variable_move( _environment, variable_string_val( _environment, temporary->name )->name, result->name );
216 break;
217 case 0:
218 switch( result->type ) {
219 case VT_DSTRING:
220 variable_move_naked( _environment, temporary->name, result->name );
221 break;
222 default:
223 CRITICAL_INPUT_UNSUPPORTED( _variable, DATATYPE_AS_STRING[result->type] );
224 }
225 }
226
227}
228
229#endif
void cpu_dsfree(Environment *_environment, char *_index)
Definition 6309.c:5917
void cpu_bveq(Environment *_environment, char *_value, char *_label)
Definition 6309.c:334
void cpu_dsresize(Environment *_environment, char *_index, char *_resize)
Definition 6309.c:5937
void cpu_compare_8bit(Environment *_environment, char *_source, char *_destination, char *_other, int _positive)
CPU 6309: emit code to compare two 8 bit values
Definition 6309.c:811
void cpu_inc(Environment *_environment, char *_variable)
Definition 6309.c:4555
void cpu_move_8bit_indirect_with_offset2(Environment *_environment, char *_source, char *_value, char *_offset)
Definition 6309.c:5262
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void cpu_dec(Environment *_environment, char *_variable)
Definition 6309.c:4630
void cpu_dsalloc(Environment *_environment, char *_size, char *_index)
Definition 6309.c:5895
void cpu_store_8bit(Environment *_environment, char *_destination, int _value)
CPU 6309: emit code to store 8 bit
Definition 6309.c:761
void cpu_jump(Environment *_environment, char *_label)
Definition 6309.c:3739
void cpu_bvneq(Environment *_environment, char *_value, char *_label)
Definition 6309.c:345
void cpu_dsdescriptor(Environment *_environment, char *_index, char *_address, char *_size)
Definition 6309.c:5977
void cpu_compare_and_branch_8bit_const(Environment *_environment, char *_source, int _destination, char *_label, int _positive)
CPU 6309: emit code to compare two 8 bit values and jump if they are equal/different
Definition 6309.c:876
Variable * variable_retrieve(Environment *_environment, char *_name)
int variable_exists(Environment *_environment, char *_name)
Variable * variable_string_val(Environment *_environment, char *_value)
Emit code for = VAL( ... ).
Variable * variable_move(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable by converting it.
Variable * variable_move_naked(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable without conversion.
Variable * variable_define(Environment *_environment, char *_name, VariableType _type, int _value)
Define a variable for the program.
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
void add_complex(Environment *_environment, char *_variable, int _expression, int _limit_lower, int _limit_upper, int _clamp)
Definition add.c:308
int size
Definition _optimizer.c:678
int offset
Definition _optimizer.c:681
void input(Environment *_environment, char *_variable, VariableType _default_type)
Definition input.c:43
#define INPUT_DEFAULT_SEPARATOR
Definition atari.h:133
#define INPUT_DEFAULT_SIZE
Definition atari.h:134
#define INPUT_DEFAULT_CURSOR
Definition atari.h:135
void cmove_direct(Environment *_environment, int _dx, int _dy)
Definition cmove.c:41
void pia_inkey(Environment *_environment, char *_key)
Definition pia.c:44
void print_newline(Environment *_environment)
Emit code for print a single newline.
Definition print.c:422
void print(Environment *_environment, char *_value, int _new_line, int _raw)
Emit code for PRINT... instruction.
Definition print.c:141
int lineInput
Definition ugbc.h:3118
KeyboardConfig keyboardConfig
Definition ugbc.h:2435
int printRaw
Definition ugbc.h:3274
char separator
Definition ugbc.h:1982
char * name
Definition ugbc.h:979
char * realName
Definition ugbc.h:982
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
struct _Variable Variable
Structure of a single variable.
#define CRITICAL_INPUT_UNSUPPORTED(v, t)
Definition ugbc.h:3490
struct _Environment Environment
Structure of compilation environment.
@ VT_BYTE
Definition ugbc.h:450
@ VT_CHAR
Definition ugbc.h:498
@ VT_ADDRESS
Definition ugbc.h:465
@ VT_DSTRING
Definition ugbc.h:483
enum _VariableType VariableType
Type of variables.
#define VT_BITWIDTH(t)
Definition ugbc.h:595
#define MAKE_LABEL
Definition ugbc.h:3351
char DATATYPE_AS_STRING[][16]