ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
print_buffer.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 PRINT BUFFER
52
53@english
54
55This is a specific variant of ''PRINT'' that can be used to print large amount of text.
56The ''PRINT BUFFER'' instruction does not have the limits of ''PRINT'' strings,
57and is able to print any file that have been loaded. An item or a list of items
58can follow the command. The list of items can consist of any group of buffers.
59Each element in the list must be separated by either a semi-colon '';'' or a comma '',''.
60A semi-colon prints the data immediately after the previous value, whereas a comma first
61moves the cursor to the next ''TAB'' position on the screen.
62
63Normally the cursor will be advanced downwards by a single line after each ''PRINT BUFFER''
64instruction. This can be suppressed by adding a separator after the print. As before, a semicolon
65will preserve the cursor position after the operation, and a comma will place the
66cursor to the next ''TAB'' stop before proceeding.
67
68Finally, it is possible to omit the native conversion of the buffer storage system, if you want
69to print "as is". In that case you need to use the ''RAW'' keyword.
70
71@italian
72
73Questa è una variante specifica di ''PRINT'' che può essere utilizzata per stampare grandi
74quantità di testo. L'istruzione ''PRINT BUFFER'' non ha i limiti delle stringhe ''PRINT'',
75ed è in grado di stampare qualsiasi file che sono stati caricati. Un elemento o un elenco di
76elementi può seguire il comando. L'elenco di elementi può essere costituito da qualsiasi
77gruppo di buffer. Ogni elemento nell'elenco deve essere separato da un punto e virgola '';''
78o da una virgola '',''. Un punto e virgola stampa i dati immediatamente dopo il valore
79precedente, mentre una virgola sposta prima il cursore alla posizione ''TAB'' successiva
80sullo schermo.
81
82Normalmente il cursore verrà avanzato verso il basso di una singola riga dopo ciascuna
83istruzione ''PRINT BUFFER''. Questo può essere eliminato aggiungendo un separatore dopo
84la stampa. Come prima, un punto e virgola manterrà la posizione del cursore dopo l'operazione
85e una virgola posizionerà il cursore al successivo punto "TAB" prima di procedere.
86
87Infine, è possibile omettere la conversione nativa del sistema di stoccaggio del buffer,
88laddove si voglia stampare "as is". In tal caso è necessario utilizzare la parola chiave ''RAW''.
89
90@syntax PRINT BUFFER buf1 [; [buf2] [; ...] ... ]
91@syntax PRINT BUFFER RAW buf1 [; [buf2] [; ...] ... ]
92
93@example PRINT BUFFER description
94@example PRINT BUFFER RAW descriptionRaw
95
96@usedInExample buffers_print_01.bas
97@usedInExample contrib_ascii.bas
98@usedInExample contrib_ascii2.bas
99
100@target all
101@verified
102
103@keyword PRINT BUFFER
104
105@english
106
107
108@italian
109Esiste anche una specifica variante di ''PRINT'' , che può essere utilizzata per
110stampare un grande ammontare di testo. L'istruzione ''PRINT BUFFER'' non ha i limiti
111delle stringhe con il ''PRINT'', e quindi è in grado di stampare qualsiasi file sia stato
112caricato.
113
114@syntax PRINT BUFFER [expression]{;|,[expression]|;[expression]} ...
115
116@example file := load("example.txt")
117@example PRINT BUFFER file
118
119@usedInExample buffers_print_01.bas
120
121@target all
122</usermanual> */
123void print_buffer( Environment * _environment, char * _value, int _new_line, int _printable ) {
124
126
127 const int bufferSizeSegment = 32;
128
129 Variable * value = variable_retrieve( _environment, _value );
130 Variable * dstring = variable_temporary( _environment, VT_DSTRING, "(temporary buffer)" );
131 Variable * targetAddress = variable_temporary( _environment, VT_ADDRESS, "(address)" );
132 Variable * size = variable_temporary( _environment, VT_BYTE, "(size)" );
133 Variable * sourceAddress = variable_temporary( _environment, VT_ADDRESS, "(address)" );
134
135 char printBufferLabel[MAX_TEMPORARY_STORAGE]; sprintf( printBufferLabel, "%sloop", label );
136 char printBufferRestLabel[MAX_TEMPORARY_STORAGE]; sprintf( printBufferRestLabel, "%srest", label );
137
138 if ( value->type != VT_BUFFER ) {
140 }
141
142 value->printable = _printable;
143
144 // char * string = malloc( value->size + 1 );
145 // memset( string, 0, value->size );
146 // memcpy( string, value->valueBuffer, value->size );
147
148 Variable * bufferSize = variable_temporary( _environment, VT_WORD, "(bufferSize)" );
149 Variable * restSize = variable_temporary( _environment, VT_BYTE, "(bufferSize)" );
150
151 if ( value->residentAssigned ) {
152
153 char alreadyLoadedLabel[MAX_TEMPORARY_STORAGE];
154 sprintf(alreadyLoadedLabel, "%salready", label );
155
156 char bankWindowId[MAX_TEMPORARY_STORAGE];
157 sprintf( bankWindowId, "BANKWINDOWID%2.2x", value->residentAssigned );
158
159 char bankWindowName[MAX_TEMPORARY_STORAGE];
160 sprintf( bankWindowName, "BANKWINDOW%2.2x", value->residentAssigned );
161
162 cpu_compare_and_branch_16bit_const( _environment, bankWindowId, value->variableUniqueId, alreadyLoadedLabel, 1 );
163 if ( value->uncompressedSize ) {
164 bank_uncompress_semi_var( _environment, value->bankAssigned, value->absoluteAddress, bankWindowName );
165 variable_store( _environment, bufferSize->name, value->uncompressedSize );
166 } else {
167 bank_read_semi_var( _environment, value->bankAssigned, value->absoluteAddress, bankWindowName, value->size );
168 variable_store( _environment, bufferSize->name, value->size );
169 }
170 cpu_store_16bit(_environment, bankWindowId, value->variableUniqueId );
171 cpu_label( _environment, alreadyLoadedLabel );
172 cpu_addressof_16bit( _environment, bankWindowName, sourceAddress->realName );
173 } else {
174 cpu_addressof_16bit( _environment, value->realName, sourceAddress->realName );
175 variable_store( _environment, bufferSize->name, value->size );
176 }
177
178 int offset = 0;
179
180 cpu_dsalloc_size( _environment, bufferSizeSegment, dstring->realName );
181 cpu_dsdescriptor( _environment, dstring->realName, targetAddress->realName, size->realName );
182
183 cpu_label( _environment, printBufferLabel );
184
185 Variable * compare = variable_greater_than_const( _environment, bufferSize->name, bufferSizeSegment, 1 );
186 cpu_compare_and_branch_8bit_const( _environment, compare->realName, 0, printBufferRestLabel, 1 );
187
188 cpu_mem_move_size( _environment, sourceAddress->realName, targetAddress->realName, bufferSizeSegment );
189 cpu_math_add_16bit_const( _environment, sourceAddress->realName, bufferSizeSegment, sourceAddress->realName );
190 cpu_math_add_16bit_const( _environment, bufferSize->realName, -bufferSizeSegment, bufferSize->realName );
191 text_text( _environment, dstring->name, 0 );
192
193 cpu_jump( _environment, printBufferLabel );
194
195 cpu_label( _environment, printBufferRestLabel );
196
197 variable_move( _environment, bufferSize->name, restSize->name );
198
199 cpu_dsfree( _environment, dstring->realName );
200 cpu_dsalloc( _environment, restSize->realName, dstring->realName );
201 cpu_dsdescriptor( _environment, dstring->realName, targetAddress->realName, size->realName );
202 cpu_mem_move( _environment, sourceAddress->realName, targetAddress->realName, restSize->realName );
203 text_text( _environment, dstring->name, 0 );
204 cpu_dsfree( _environment, dstring->realName );
205
206 if ( _new_line ) {
207 text_newline( _environment );
208 }
209
210}
void cpu_dsfree(Environment *_environment, char *_index)
Definition 6309.c:5917
void cpu_mem_move(Environment *_environment, char *_source, char *_destination, char *_size)
Definition 6309.c:4692
void cpu_store_16bit(Environment *_environment, char *_destination, int _value)
CPU 6309: emit code to store 16 bit
Definition 6309.c:1503
void cpu_dsalloc_size(Environment *_environment, int _size, char *_index)
Definition 6309.c:5906
void cpu_addressof_16bit(Environment *_environment, char *_source, char *_destination)
Definition 6309.c:1485
void cpu_math_add_16bit_const(Environment *_environment, char *_source, int _destination, char *_other)
Definition 6309.c:1674
void cpu_compare_and_branch_16bit_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:1578
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void cpu_dsalloc(Environment *_environment, char *_size, char *_index)
Definition 6309.c:5895
void cpu_jump(Environment *_environment, char *_label)
Definition 6309.c:3739
void cpu_mem_move_size(Environment *_environment, char *_source, char *_destination, int _size)
Definition 6309.c:4781
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)
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_store(Environment *_environment, char *_destination, unsigned int _value)
Store a direct value to a variable.
Variable * variable_greater_than_const(Environment *_environment, char *_source, int _destination, int _equal)
int size
Definition _optimizer.c:678
int offset
Definition _optimizer.c:681
void bank_read_semi_var(Environment *_environment, int _bank, int _address1, char *_address2, int _size)
Emit ASM code for instruction BANK READ ....
Definition bank_read.c:50
void bank_uncompress_semi_var(Environment *_environment, int _bank, int _address1, char *_address2)
Emit ASM code for instruction BANK UNCOMPRESS ....
void print_buffer(Environment *_environment, char *_value, int _new_line, int _printable)
Emit code for PRINT... instruction.
int bankAssigned
Definition ugbc.h:1172
int printable
Definition ugbc.h:1097
int residentAssigned
Definition ugbc.h:1175
int size
Definition ugbc.h:1077
char * name
Definition ugbc.h:979
int absoluteAddress
Definition ugbc.h:1092
VariableType type
Definition ugbc.h:988
int uncompressedSize
Definition ugbc.h:1082
int variableUniqueId
Definition ugbc.h:1178
char * realName
Definition ugbc.h:982
void text_text(Environment *_environment, char *_text, int _raw)
Definition text.c:41
void text_newline(Environment *_environment)
Definition text.c:51
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
#define CRITICAL_PRINT_BUFFER_ON_A_NOT_BUFFER(v)
Definition ugbc.h:3549
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_WORD
Definition ugbc.h:455
@ VT_BYTE
Definition ugbc.h:450
@ VT_BUFFER
Definition ugbc.h:477
@ VT_ADDRESS
Definition ugbc.h:465
@ VT_DSTRING
Definition ugbc.h:483
#define MAKE_LABEL
Definition ugbc.h:3351
char DATATYPE_AS_STRING[][16]