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
41extern char DATATYPE_AS_STRING[][16];
42
43void input( Environment * _environment, char * _variable, VariableType _default_type ) {
44
46
47 Variable * result;
48 if ( variable_exists( _environment, _variable ) ) {
49 result = variable_retrieve( _environment, _variable );
50 } else {
51 result = variable_define( _environment, _variable, _default_type, 0 );
52 }
53
54 char repeatLabel[MAX_TEMPORARY_STORAGE]; sprintf(repeatLabel, "%srepeat", label );
55 char finishedLabel[MAX_TEMPORARY_STORAGE]; sprintf(finishedLabel, "%sfinished", label );
56 char doneLabel[MAX_TEMPORARY_STORAGE]; sprintf(doneLabel, "%sdone", label );
57 char backspaceLabel[MAX_TEMPORARY_STORAGE]; sprintf(backspaceLabel, "%sbackspace", label );
58 char skipCursorChangeLabel[MAX_TEMPORARY_STORAGE]; sprintf(skipCursorChangeLabel, "%sskipcc", label );
59 char skipCursorChangeLabel2[MAX_TEMPORARY_STORAGE]; sprintf(skipCursorChangeLabel2, "%sskipcc2", label );
60 char skipCursorChangeLabel3[MAX_TEMPORARY_STORAGE]; sprintf(skipCursorChangeLabel3, "%sskipcc3", label );
61
62 Variable * temporary = variable_temporary( _environment, VT_DSTRING, "(temporary storage for input)");
63 Variable * offset = variable_temporary( _environment, VT_BYTE, "(offset inside temporary storage)");
64
65 Variable * enter = variable_temporary( _environment, VT_CHAR, "(enter)" );
66 Variable * comma = variable_temporary( _environment, VT_CHAR, "(comma)" );
67 Variable * space = variable_temporary( _environment, VT_CHAR, "(space)" );
68 Variable * underscore = variable_temporary( _environment, VT_CHAR, "(underscore)" );
69 Variable * underscoreTimer = variable_temporary( _environment, VT_BYTE, "(underscore timer)" );
70 Variable * backspace = variable_temporary( _environment, VT_CHAR, "(backspace)" );
71 Variable * size = variable_temporary( _environment, VT_BYTE, "(size max)" );
72 Variable * pressed = variable_temporary( _environment, VT_BYTE, "(key pressed?)");
73 Variable * key = variable_temporary( _environment, VT_CHAR, "(key pressed)");
74 Variable * zero = variable_temporary( _environment, VT_BYTE, "(zero)" );
75
76 cpu_store_8bit( _environment, enter->realName, 13 );
77 cpu_store_8bit( _environment, offset->realName, 0 );
78 cpu_store_8bit( _environment, backspace->realName, 8 );
79 cpu_store_8bit( _environment, space->realName, 32 );
80 cpu_store_8bit( _environment, zero->realName, 0 );
81 if ( _environment->lineInput ) {
82 cpu_store_8bit( _environment, comma->realName, 13 );
83 } else {
84 cpu_store_8bit( _environment, comma->realName, _environment->keyboardConfig.separator == 0 ? INPUT_DEFAULT_SEPARATOR : _environment->keyboardConfig.separator );
85 }
86 cpu_store_8bit( _environment, size->realName, _environment->keyboardConfig.size == 0 ? INPUT_DEFAULT_SIZE : _environment->keyboardConfig.size );
87 cpu_store_8bit( _environment, underscore->realName, _environment->keyboardConfig.cursor == 0 ? INPUT_DEFAULT_CURSOR : _environment->keyboardConfig.cursor );
88
89 Variable * address = variable_temporary( _environment, VT_ADDRESS, "(address of DSTRING)");
90 cpu_dsfree( _environment, temporary->realName );
91 cpu_dsalloc( _environment, size->realName, temporary->realName );
92 cpu_dsdescriptor( _environment, temporary->realName, address->realName, pressed->realName );
93
94 cpu_store_8bit( _environment, underscoreTimer->realName, 255 );
95
96 cpu_label( _environment, repeatLabel );
97
98 cpu_dec( _environment, underscoreTimer->realName );
99 cpu_compare_and_branch_8bit_const( _environment, underscoreTimer->realName, 0, skipCursorChangeLabel, 0 );
100 cpu_store_8bit( _environment, underscoreTimer->realName, 32 );
101 cpu_compare_and_branch_8bit_const( _environment, underscore->realName, _environment->keyboardConfig.cursor == 0 ? INPUT_DEFAULT_CURSOR : _environment->keyboardConfig.cursor, skipCursorChangeLabel2, 1 );
102 cpu_label( _environment, skipCursorChangeLabel3 );
103 cpu_store_8bit( _environment, underscore->realName, _environment->keyboardConfig.cursor == 0 ? INPUT_DEFAULT_CURSOR : _environment->keyboardConfig.cursor );
104 cpu_jump( _environment, skipCursorChangeLabel );
105 cpu_label( _environment, skipCursorChangeLabel2 );
106 cpu_store_8bit( _environment, underscore->realName, 32 );
107 cpu_label( _environment, skipCursorChangeLabel );
108
109 print( _environment, underscore->name, 0, _environment->printRaw );
110 cmove_direct( _environment, -1, 0 );
111
112 atari_inkey( _environment, key->realName );
113
114 cpu_bveq( _environment, key->realName, repeatLabel );
115
116 cpu_compare_8bit( _environment, key->realName, backspace->realName, pressed->realName, 1 );
117
118 cpu_bvneq( _environment, pressed->realName, backspaceLabel );
119
120 cpu_compare_8bit( _environment, key->realName, comma->realName, pressed->realName, 1 );
121
122 cpu_bvneq( _environment, pressed->realName, finishedLabel );
123
124 cpu_compare_8bit( _environment, key->realName, enter->realName, pressed->realName, 1 );
125
126 cpu_bvneq( _environment, pressed->realName, finishedLabel );
127
128 print( _environment, key->name, 0, _environment->printRaw );
129
130 cpu_move_8bit_indirect_with_offset2( _environment, key->realName, address->realName, offset->realName );
131
132 cpu_inc( _environment, offset->realName );
133
134 cpu_compare_8bit( _environment, offset->realName, size->realName, pressed->realName, 1 );
135
136 cpu_bveq( _environment, pressed->realName, repeatLabel );
137
138 cpu_jump( _environment, finishedLabel );
139
140 cpu_label( _environment, backspaceLabel );
141
142 cpu_compare_8bit( _environment, offset->realName, zero->realName, pressed->realName, 1 );
143
144 cpu_bvneq( _environment, pressed->realName, repeatLabel );
145
146 cpu_dec( _environment, offset->realName );
147
148 print( _environment, space->name, 0, _environment->printRaw );
149
150 cmove_direct( _environment, -2, 0 );
151
152 print( _environment, space->name, 0, _environment->printRaw );
153
154 cmove_direct( _environment, -1, 0 );
155
156 cpu_jump( _environment, repeatLabel );
157
158 cpu_label( _environment, finishedLabel );
159
160 print( _environment, space->name, 0, _environment->printRaw );
161 cmove_direct( _environment, -1, 0 );
162
163 cpu_compare_8bit( _environment, comma->realName, enter->realName, pressed->realName, 1 );
164 cpu_bveq( _environment, pressed->realName, doneLabel );
165
166 print_newline( _environment );
167
168 cpu_label( _environment, doneLabel );
169
170 cpu_dsresize( _environment, temporary->realName, offset->realName );
171
172 switch( VT_BITWIDTH( result->type ) ) {
173 case 8:
174 case 16:
175 case 32:
176 variable_move( _environment, variable_string_val( _environment, temporary->name )->name, result->name );
177 break;
178 case 0:
179 switch( result->type ) {
180 case VT_DSTRING:
181 variable_move_naked( _environment, temporary->name, result->name );
182 break;
183 default:
184 CRITICAL_INPUT_UNSUPPORTED( _variable, DATATYPE_AS_STRING[result->type] );
185 }
186 }
187
188}
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.
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
void atari_inkey(Environment *_environment, char *_key)
Definition atari.c:66
#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 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]