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("LDA %s", _a );
52 outline1("STA %s", _c );
53 break;
54 case 2:
55 outline0("LDA #$0" );
56 outline1("STA %s", _c );
57 outline1("LDA %s", _a );
58 outline0("ANDA #$03" );
59 outline1("BEQ %sthreshold1", label );
60 outline0("LDA #$03" );
61 outline1("STA %s", _c );
62 outhead1("%sthreshold1", label );
63 outline1("LDA %s", _a );
64 outline0("ANDA #$0C" );
65 outline1("BEQ %sthreshold2", label );
66 outline0("LDA #$0C" );
67 outline1("ORA %s", _c );
68 outline1("STA %s", _c );
69 outhead1("%sthreshold2", label );
70 outline1("LDA %s", _a );
71 outline0("ANDA #$30" );
72 outline1("BEQ %sthreshold3", label );
73 outline1("LDA %s", _c );
74 outline0("ORA #$30" );
75 outline1("STA %s", _c );
76 outhead1("%sthreshold3", label );
77 outline1("LDA %s", _a );
78 outline0("ANDA #$C0" );
79 outline1("BEQ %sthreshold4", label );
80 outline1("LDA %s", _c );
81 outline0("ORA #$c0" );
82 outline1("STA %s", _c );
83 outhead1("%sthreshold4", label );
84 break;
85 case 4:
86 outline0("LDA #$0" );
87 outline1("STA %s", _c );
88 outline1("LDA %s", _a );
89 outline0("ANDA #$0f" );
90 outline1("BEQ %sthreshold1", label );
91 outline0("LDA #$0F" );
92 outline1("STA %s", _c );
93 outhead1("%sthreshold1", label );
94 outline1("LDA %s", _a );
95 outline0("ANDA #$F0" );
96 outline1("BEQ %sthreshold2", label );
97 outline0("LDA #$F0" );
98 outline1("ORA %s", _c );
99 outline1("STA %s", _c );
100 outhead1("%sthreshold2", label );
101 break;
102 }
103 } else if ( _op == ( 1 << _environment->currentModeBW ) + 2 ) {
104 outline0("; bltu C = 0 (ignore)");
105 outline0("LDA #$0" );
106 outline1("STA %s", _c );
107 } else if ( _op == ( 1 << _environment->currentModeBW ) + 1 ) {
108 outline0("; bltu C = !A (not)");
109 outline1("LDA %s", _a );
110 outline0("EORA #$ff");
111 outline1("STA %s", _c );
112 } else if ( _op == ( 1 << _environment->currentModeBW ) ) {
113 outline0("; bltu C = A (copy)");
114 outline1("LDA %s", _a );
115 outline1("STA %s", _c );
116 } else if ( _op < ( 1 << _environment->currentModeBW ) ) {
117 unsigned char value = 0;
118 switch( _environment->currentModeBW ) {
119 case 1:
120 if ( _op ) {
121 value = 0xff;
122 } else {
123 value = 0x00;
124 }
125 break;
126 case 2:
127 value = ( ( _op & 0x1 ) );
128 value |= ( ( ( _op & 0x2 ) ) << 1 );
129 value = value | ( value << 2 ) | ( value << 4 ) | ( value << 6 );
130 break;
131 case 4:
132 value = ( ( _op & 0xf ) );
133 value = value | ( value << 4 );
134 break;
135 }
136 outline0("; bltu C = value");
137 outline1("LDA #$%2.2x", value );
138 outline1("STA %s", _c );
139 }
140
141}
142
143static void blit_define_bltb( Environment * _environment, int _op, char * _a, char * _b, char * _d ) {
144
145 if ( _op == 0 ) {
146 outline0("; bltb C = A and B");
147 outline1("LDA %s", _a );
148 outline1("ANDA %s", _b );
149 outline1("STA %s", _d );
150 } else if ( _op == 1 ) {
151 outline0("; bltb C = A or B");
152 outline1("LDA %s", _a );
153 outline1("ORA %s", _b );
154 outline1("STA %s", _d );
155 } else if ( _op == 2 ) {
156 outline0("; bltb C = A xor B");
157 outline1("LDA %s", _a );
158 outline1("EORA %s", _b );
159 outline1("STA %s", _d );
160 } else if ( _op == 3 ) {
161 outline0("; bltb C = A");
162 outline1("LDA %s", _a );
163 outline1("STA %s", _d );
164 } else if ( _op == 4 ) {
165 outline0("; bltb C = B");
166 outline1("LDA %s", _b );
167 outline1("STA %s", _d );
168 } else if ( _op == 5 ) {
169 outline0("; bltb C = A if B>0, 0 if B=0");
171 switch( _environment->currentModeBW ) {
172 case 1:
173 outline1("LDA %s", _a );
174 outline1("ANDA %s", _b );
175 outline1("STA %s", _d );
176 break;
177 case 2:
178 outline1("LDA %s", _a );
179 outline0("TFR A, B" );
180 outline1("LDA %s", _b );
181 outline0("ANDA #$03" );
182 outline1("BNE %smask1", label );
183 outline0("ANDB #$fc" );
184 outhead1("%smask1", label );
185 outline1("LDA %s", _b );
186 outline0("ANDA #$0c" );
187 outline1("BNE %smask2", label );
188 outline0("ANDB #$f3" );
189 outhead1("%smask2", label );
190 outline1("LDA %s", _b );
191 outline0("ANDA #$30" );
192 outline1("BNE %smask3", label );
193 outline0("ANDB #$cf" );
194 outhead1("%smask3", label );
195 outline1("LDA %s", _b );
196 outline0("AND #$c0" );
197 outline1("BNE %smask4", label );
198 outline0("ANDB #$3f" );
199 outhead1("%smask4", label );
200 outline1("STB %s", _d );
201 break;
202 case 4:
203 outline1("LDA %s", _a );
204 outline0("TFR A, B" );
205 outline1("LDA %s", _b );
206 outline0("ANDA #$0f" );
207 outline1("BNE %smask1", label );
208 outline0("ANDB #$f0" );
209 outhead1("%smask1", label );
210 outline1("LDA %s", _b );
211 outline0("ANDA #$f0" );
212 outline1("BNE %smask2", label );
213 outline0("ANDB #$0f" );
214 outhead1("%smask2", label );
215 outline1("STB %s", _d );
216 break;
217
218 }
219 }
220
221}
222
233void blit_define( Environment * _environment, char * _name, int _sop, int _mop, int _smop, int _iop, int _dop, int _idop, int _top ) {
234
235 char blitLabel[MAX_TEMPORARY_STORAGE]; sprintf( blitLabel, "_%sblit", _name );
236 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _name );
237
238 cpu_jump( _environment, skipLabel );
239 cpu_label( _environment, blitLabel );
240
241 // B = s(x,y)
242 // IYL = d(x, y)
243 // IYH = m(x, y)
244
245 // BLITSU
246 blit_define_bltu( _environment, _sop, "BLITS1", "BLITR0" );
247
248 // BLITMU
249 blit_define_bltu( _environment, _mop, "BLITS2", "BLITR1" );
250
251 // BLITSM
252 blit_define_bltb( _environment, _smop, "BLITR0", "BLITR1", "BLITR2" );
253
254 // BLITSMU
255 blit_define_bltu( _environment, _iop, "BLITR2", "BLITR0" );
256
257 // BLITDU
258 blit_define_bltu( _environment, _dop, "BLITS3", "BLITR1" );
259
260 // BLITT
261 blit_define_bltb( _environment, _idop, "BLITR0", "BLITR1", "BLITR2" );
262
263 // BLITTU
264 blit_define_bltu( _environment, _top, "BLITR2", "BLITR3" );
265
266 outline0("LDA BLITR3");
267
268 cpu_return( _environment );
269 cpu_label( _environment, skipLabel );
270
271}
272
273static const char BLIT_SOURCES_REGISTER[][10] = {
274 "BLITS0", // destination
275 "BLITS1", // source 1
276 "BLITS2", // source 2
277};
278
279void blit_define_begin_compound( Environment * _environment, char * _name ) {
280
281 char blitLabel[MAX_TEMPORARY_STORAGE]; sprintf( blitLabel, "_%sblit", _name );
282 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _name );
283
284 if ( variable_exists( _environment, _name ) ) {
286 }
287 Variable * var = variable_define( _environment, _name, VT_BLIT, 0 );
288
289 memset( &_environment->blit, 0, sizeof( Blit ) );
290
291 _environment->blit.name = strdup( _name );
292 _environment->blit.realName = strdup( var->realName );
293
294 cpu_jump( _environment, skipLabel );
295 cpu_label( _environment, blitLabel );
296
297 cpu_blit_initialize( _environment );
298
299}
300
301void blit_define_compound_operand_to_register( Environment * _environment, int _register, int _source ) {
302
303 char * reg = cpu_blit_register_name( _environment, _register ) ;
304
305 outline1( "LDA %s", &BLIT_SOURCES_REGISTER[_source][0] );
306 outline1( "STA %s", reg );
307
308}
309
310void blit_define_compound_unary( Environment * _environment, int _operation, int _operand, int _result ) {
311
312 blit_define_bltu( _environment, _operation, cpu_blit_register_name( _environment, _operand ), cpu_blit_register_name( _environment, _result ) );
313
314}
315
316void blit_define_compound_binary( Environment * _environment, int _operation, int _operand1, int _operand2, int _result ) {
317
318 blit_define_bltb( _environment, _operation, cpu_blit_register_name( _environment, _operand1 ), cpu_blit_register_name( _environment, _operand2 ), cpu_blit_register_name( _environment, _result ) );
319
320}
321
322void blit_define_end_compound( Environment * _environment, int _result ) {
323
324 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _environment->blit.name );
325 char blitStageAreaName[MAX_TEMPORARY_STORAGE]; sprintf( blitStageAreaName, "%sbs", _environment->blit.name );
326
327 Variable * blitStageArea = variable_define( _environment, blitStageAreaName, VT_BUFFER, 0 );
328 variable_resize_buffer( _environment, blitStageArea->name, _environment->blit.usedMemory );
329
330 char * reg = cpu_blit_register_name( _environment, _result ) ;
331
332 outline1( "LDA %s", reg );
333
334 cpu_blit_finalize( _environment );
335
336 cpu_return( _environment );
337 cpu_label( _environment, skipLabel );
338
339}
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]