ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
blit_define.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
43static void blit_define_bltu( Environment * _environment, int _op, char * _a, char * _c ) {
44
45 if ( _op == ( 1 << _environment->currentModeBW ) + 3 ) {
46
48 outline0("; bltu C = (2^bpp-1) if A > 0 (threshold)");
49 switch( _environment->currentModeBW ) {
50 case 1:
51 outline1("LD A, %s", _a );
52 outline1("LD %s, A", _c );
53 break;
54 case 2:
55 outline0("LD A, 0" );
56 outline1("LD %s, A", _c );
57 outline1("LD A, %s", _a );
58 outline0("AND $11" );
59 outline0("CMP 0" );
60 outline1("JR Z, %sthreshold1", label );
61 outline0("LD A, $11" );
62 outline1("LD %s, A", _c );
63 outhead1("%sthreshold1:", label );
64 outline1("LD A, %s", _a );
65 outline0("AND $22" );
66 outline0("CMP 0" );
67 outline1("JR Z, %sthreshold2", label );
68 outline0("LD A, $22" );
69 outline1("OR %s", _c );
70 outline1("LD %s, A", _c );
71 outhead1("%sthreshold2:", label );
72 outline1("LD A, %s", _a );
73 outline0("AND $44" );
74 outline0("CMP 0" );
75 outline1("JR Z, %sthreshold3", label );
76 outline0("LD A, $44" );
77 outline1("OR %s", _c );
78 outline1("LD %s, A", _c );
79 outhead1("%sthreshold3:", label );
80 outline1("LD A, %s", _a );
81 outline0("AND $88" );
82 outline0("CMP 0" );
83 outline1("JR Z, %sthreshold4", label );
84 outline0("LD A, $88" );
85 outline1("OR %s", _c );
86 outline1("LD %s, A", _c );
87 outhead1("%sthreshold4:", label );
88 break;
89 case 4:
90 outline0("LD A, 0" );
91 outline1("LD %s, A", _c );
92 outline1("LD A, %s", _a );
93 outline0("AND $aa" );
94 outline0("CMP 0" );
95 outline1("JR Z, %sthreshold1", label );
96 outline0("LD A, $aa" );
97 outline1("LD %s, A", _c );
98 outhead1("%sthreshold1:", label );
99 outline1("LD A, %s", _a );
100 outline0("AND $55" );
101 outline0("CMP 0" );
102 outline1("JR Z, %sthreshold2", label );
103 outline0("LD A, $55" );
104 outline1("OR %s", _c );
105 outline1("LD %s, A", _c );
106 outhead1("%sthreshold2:", label );
107 break;
108 }
109 } else if ( _op == ( 1 << _environment->currentModeBW ) + 2 ) {
110 outline0("; bltu C = 0 (ignore)");
111 outline0("LD A, 0" );
112 outline1("LD %s, A", _c );
113 } else if ( _op == ( 1 << _environment->currentModeBW ) + 1 ) {
114 outline0("; bltu C = !A (not)");
115 outline1("LD A, %s", _a );
116 outline0("XOR $ff");
117 outline1("LD %s, A", _c );
118 } else if ( _op == ( 1 << _environment->currentModeBW ) ) {
119 outline0("; bltu C = A (copy)");
120 outline1("LD A, %s", _a );
121 outline1("LD %s, A", _c );
122 } else if ( _op < ( 1 << _environment->currentModeBW ) ) {
123 unsigned char value = 0;
124 switch( _environment->currentModeBW ) {
125 case 1:
126 if ( _op ) {
127 value = 0xff;
128 } else {
129 value = 0x00;
130 }
131 break;
132 case 2:
133 value = ( ( _op & 0x1 ) );
134 value |= ( ( ( _op & 0x2 ) ) << 3 );
135 value = value | ( value << 1 ) | ( value << 2 ) | ( value << 3 );
136 break;
137 case 4:
138 value = ( ( _op & 0x8 ) >> 3 );
139 value |= ( ( ( _op & 0x2 ) ) << 1 );
140 value |= ( ( ( _op & 0x4 ) ) << 2 );
141 value |= ( ( ( _op & 0x1 ) ) << 6 );
142 value = value | ( value << 1 );
143 break;
144 }
145 outline0("; bltu C = value");
146 outline1("LD A, $%2.2x", value );
147 outline1("LD %s, A", _c );
148 }
149
150}
151
152static void blit_define_bltb( Environment * _environment, int _op, char * _a, char * _b, char * _d ) {
153
154 if ( _op == 0 ) {
155 outline0("; bltb C = A and B");
156 outline1("LD A, %s", _a );
157 outline1("LD %s, A", _d );
158 outline1("LD A, %s", _b );
159 outline1("AND %s", _d );
160 outline1("LD %s, A", _d );
161 } else if ( _op == 1 ) {
162 outline0("; bltb C = A or B");
163 outline1("LD A, %s", _a );
164 outline1("LD %s, A", _d );
165 outline1("LD A, %s", _b );
166 outline1("OR %s", _d );
167 outline1("LD %s, A", _d );
168 } else if ( _op == 2 ) {
169 outline0("; bltb C = A xor B");
170 outline1("LD A, %s", _a );
171 outline1("LD %s, A", _d );
172 outline1("LD A, %s", _b );
173 outline1("XOR %s", _d );
174 outline1("LD %s, A", _d );
175 } else if ( _op == 3 ) {
176 outline0("; bltb C = A");
177 outline1("LD A, %s", _a );
178 outline1("LD %s, A", _d );
179 } else if ( _op == 4 ) {
180 outline0("; bltb C = B");
181 outline1("LD A, %s", _b );
182 outline1("LD %s, A", _d );
183 } else if ( _op == 5 ) {
184 outline0("; bltb C = A if B>0, 0 if B=0");
186 switch( _environment->currentModeBW ) {
187 case 1:
188 outline1("LD A, %s", _a );
189 outline1("AND %s", _b );
190 outline1("LD %s, A", _d );
191 break;
192 case 2:
193 outline1("LD A, %s", _a );
194 outline1("LD %s, A", _d );
195 outline1("LD A, %s", _b );
196 outline0("AND $11" );
197 outline0("CMP 0" );
198 outline1("JR NZ, %smask1", label );
199 outline1("LD A, %s", _d );
200 outline0("AND $ee" );
201 outline1("LD %s, A", _d );
202 outhead1("%smask1:", label );
203 outline1("LD A, %s", _b );
204 outline0("AND $22" );
205 outline0("CMP 0" );
206 outline1("JR NZ, %smask2", label );
207 outline1("LD A, %s", _d );
208 outline0("AND $dd" );
209 outline1("LD %s, A", _d );
210 outhead1("%smask2:", label );
211 outline1("LD A, %s", _b );
212 outline0("AND $44" );
213 outline0("CMP 0" );
214 outline1("JR NZ, %smask3", label );
215 outline1("LD A, %s", _d );
216 outline0("AND $bb" );
217 outline1("LD %s, A", _d );
218 outhead1("%smask3:", label );
219 outline1("LD A, %s", _b );
220 outline0("AND $88" );
221 outline0("CMP 0" );
222 outline1("JR NZ, %smask4", label );
223 outline1("LD A, %s", _d );
224 outline0("AND $77" );
225 outline1("LD %s, A", _d );
226 outhead1("%smask4:", label );
227 break;
228 case 4:
229 outline1("LD A, %s", _a );
230 outline1("LD %s, A", _d );
231 outline1("LD A, %s", _b );
232 outline0("AND $aa" );
233 outline0("CMP 0" );
234 outline1("JR NZ, %smask1", label );
235 outline1("LD A, %s", _d );
236 outline0("AND $55" );
237 outline1("LD %s, A", _d );
238 outhead1("%smask1:", label );
239 outline1("LD A, %s", _b );
240 outline0("AND $55" );
241 outline0("CMP 0" );
242 outline1("JR NZ, %smask2", label );
243 outline1("LD A, %s", _d );
244 outline0("AND $aa" );
245 outline1("LD %s, A", _d );
246 outhead1("%smask2:", label );
247 break;
248 }
249 }
250
251}
252
263void blit_define( Environment * _environment, char * _name, int _sop, int _mop, int _smop, int _iop, int _dop, int _idop, int _top ) {
264
265 char blitLabel[MAX_TEMPORARY_STORAGE]; sprintf( blitLabel, "_%sblit", _name );
266 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _name );
267
268 cpu_jump( _environment, skipLabel );
269 cpu_label( _environment, blitLabel );
270
271 // B = s(x,y)
272 // IYL = d(x, y)
273 // IYH = m(x, y)
274
275 outline0("PUSH HL");
276 outline0("PUSH DE");
277
278 // BLITSU
279 blit_define_bltu( _environment, _sop, "B", "H" );
280
281 // BLITMU
282 blit_define_bltu( _environment, _mop, "IYH", "L" );
283
284 // BLITSM
285 blit_define_bltb( _environment, _smop, "H", "L", "D" );
286
287 // BLITSMU
288 blit_define_bltu( _environment, _iop, "D", "H" );
289
290 // BLITDU
291 blit_define_bltu( _environment, _dop, "IYL", "L" );
292
293 // BLITT
294 blit_define_bltb( _environment, _idop, "H", "L", "D" );
295
296 // BLITTU
297 blit_define_bltu( _environment, _top, "D", "E" );
298
299 outline0("LD A, E");
300 outline0("POP DE");
301 outline0("POP HL");
302
303 cpu_return( _environment );
304 cpu_label( _environment, skipLabel );
305
306}
307
308static const char BLIT_SOURCES_REGISTER[][4] = {
309 "IYL", // destination
310 "B", // source 1
311 "IYH", // source 2
312};
313
314void blit_define_begin_compound( Environment * _environment, char * _name ) {
315
316 char blitLabel[MAX_TEMPORARY_STORAGE]; sprintf( blitLabel, "_%sblit", _name );
317 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _name );
318
319 if ( variable_exists( _environment, _name ) ) {
321 }
322 Variable * var = variable_define( _environment, _name, VT_BLIT, 0 );
323
324 memset( &_environment->blit, 0, sizeof( Blit ) );
325
326 _environment->blit.name = strdup( _name );
327 _environment->blit.realName = strdup( var->realName );
328
329 cpu_jump( _environment, skipLabel );
330 cpu_label( _environment, blitLabel );
331
332 cpu_blit_initialize( _environment );
333
334}
335
336void blit_define_compound_operand_to_register( Environment * _environment, int _register, int _source ) {
337
338 char * reg = cpu_blit_register_name( _environment, _register ) ;
339
340 outline1( "LD A, %s", &BLIT_SOURCES_REGISTER[_source][0] );
341 outline1( "LD %s, A", reg );
342
343}
344
345void blit_define_compound_unary( Environment * _environment, int _operation, int _operand, int _result ) {
346
347 blit_define_bltu( _environment, _operation, cpu_blit_register_name( _environment, _operand ), cpu_blit_register_name( _environment, _result ) );
348
349}
350
351void blit_define_compound_binary( Environment * _environment, int _operation, int _operand1, int _operand2, int _result ) {
352
353 blit_define_bltb( _environment, _operation, cpu_blit_register_name( _environment, _operand1 ), cpu_blit_register_name( _environment, _operand2 ), cpu_blit_register_name( _environment, _result ) );
354
355}
356
357void blit_define_end_compound( Environment * _environment, int _result ) {
358
359 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _environment->blit.name );
360 char blitStageAreaName[MAX_TEMPORARY_STORAGE]; sprintf( blitStageAreaName, "%sbs", _environment->blit.name );
361
362 Variable * blitStageArea = variable_define( _environment, blitStageAreaName, VT_BUFFER, 0 );
363 variable_resize_buffer( _environment, blitStageArea->name, _environment->blit.usedMemory );
364
365 char * reg = cpu_blit_register_name( _environment, _result ) ;
366
367 outline1( "LD A, %s", reg );
368
369 cpu_blit_finalize( _environment );
370
371 cpu_return( _environment );
372 cpu_label( _environment, skipLabel );
373
374}
char * cpu_blit_register_name(Environment *_environment, int _register)
Definition 6309.c:6417
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void cpu_blit_finalize(Environment *_environment)
Definition 6309.c:6410
void cpu_jump(Environment *_environment, char *_label)
Definition 6309.c:3739
void cpu_return(Environment *_environment)
Definition 6309.c:4030
void cpu_blit_initialize(Environment *_environment)
Definition 6309.c:6403
int variable_exists(Environment *_environment, char *_name)
Variable * variable_resize_buffer(Environment *_environment, char *_destination, int _size)
Resize the (static) size of a buffer.
Variable * variable_define(Environment *_environment, char *_name, VariableType _type, int _value)
Define a variable for the program.
void blit_define(Environment *_environment, char *_name, int _sop, int _mop, int _smop, int _iop, int _dop, int _idop, int _top)
Emit ASM code for BLIT IMAGE [image] AT [int],[int].
void blit_define_compound_binary(Environment *_environment, int _operation, int _operand1, int _operand2, int _result)
void blit_define_compound_unary(Environment *_environment, int _operation, int _operand, int _result)
void blit_define_begin_compound(Environment *_environment, char *_name)
void blit_define_compound_operand_to_register(Environment *_environment, int _register, int _source)
void blit_define_end_compound(Environment *_environment, int _result)
int usedMemory
Definition ugbc.h:2167
char * name
Definition ugbc.h:2164
char * realName
Definition ugbc.h:2165
Blit blit
Definition ugbc.h:2474
int currentModeBW
Definition ugbc.h:2701
char * name
Definition ugbc.h:979
char * realName
Definition ugbc.h:982
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
struct _Blit Blit
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_BLIT
Definition ugbc.h:519
@ VT_BUFFER
Definition ugbc.h:477
#define outline0(s)
Definition ugbc.h:4252
#define CRITICAL_BLIT_ALREADY_DEFINED(n)
Definition ugbc.h:3615
#define outline1(s, a)
Definition ugbc.h:4253
#define MAKE_LABEL
Definition ugbc.h:3351
#define outhead1(s, a)
Definition ugbc.h:4247
char DATATYPE_AS_STRING[][16]