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("AND #$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("AND #$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("AND #$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("AND #$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 }
86 } else if ( _op == ( 1 << _environment->currentModeBW ) + 2 ) {
87 outline0("; bltu C = 0 (ignore)");
88 outline0("LDA #$0" );
89 outline1("STA %s", _c );
90 } else if ( _op == ( 1 << _environment->currentModeBW ) + 1 ) {
91 outline0("; bltu C = !A (not)");
92 outline1("LDA %s", _a );
93 outline0("EOR #$ff");
94 outline1("STA %s", _c );
95 } else if ( _op == ( 1 << _environment->currentModeBW ) ) {
96 outline0("; bltu C = A (copy)");
97 outline1("LDA %s", _a );
98 outline1("STA %s", _c );
99 } else if ( _op < ( 1 << _environment->currentModeBW ) ) {
100 unsigned char value = 0;
101 switch( _environment->currentModeBW ) {
102 case 1:
103 if ( _op ) {
104 value = 0xff;
105 } else {
106 value = 0x00;
107 }
108 break;
109 case 2:
110 value = ( ( _op & 0x1 ) );
111 value |= ( ( ( _op & 0x2 ) ) << 1 );
112 value = value | ( value << 2 ) | ( value << 4 ) | ( value << 6 );
113 break;
114 }
115 outline0("; bltu C = value");
116 outline1("LDA #$%2.2x", value );
117 outline1("STA %s", _c );
118 }
119
120}
121
122static void blit_define_bltb( Environment * _environment, int _op, char * _a, char * _b, char * _d ) {
123
124 if ( _op == 0 ) {
125 outline0("; bltb C = A and B");
126 outline1("LDA %s", _a );
127 outline1("AND %s", _b );
128 outline1("STA %s", _d );
129 } else if ( _op == 1 ) {
130 outline0("; bltb C = A or B");
131 outline1("LDA %s", _a );
132 outline1("ORA %s", _b );
133 outline1("STA %s", _d );
134 } else if ( _op == 2 ) {
135 outline0("; bltb C = A xor B");
136 outline1("LDA %s", _a );
137 outline1("EOR %s", _b );
138 outline1("STA %s", _d );
139 } else if ( _op == 3 ) {
140 outline0("; bltb C = A");
141 outline1("LDA %s", _a );
142 outline1("STA %s", _d );
143 } else if ( _op == 4 ) {
144 outline0("; bltb C = B");
145 outline1("LDA %s", _b );
146 outline1("STA %s", _d );
147 } else if ( _op == 5 ) {
148 outline0("; bltb C = A if B>0, 0 if B=0");
150 switch( _environment->currentModeBW ) {
151 case 1:
152 outline1("LDA %s", _a );
153 outline1("AND %s", _b );
154 outline1("STA %s", _d );
155 break;
156 case 2:
157 outline1("LDA %s", _a );
158 outline1("STA %s", _d );
159 outline1("LDA %s", _b );
160 outline0("AND #$03" );
161 outline1("BNE %smask1", label );
162 outline1("LDA %s", _d );
163 outline0("AND #$fc" );
164 outline1("STA %s", _d );
165 outhead1("%smask1:", label );
166 outline1("LDA %s", _b );
167 outline0("AND #$0c" );
168 outline1("BNE %smask2", label );
169 outline1("LDA %s", _d );
170 outline0("AND #$f3" );
171 outline1("STA %s", _d );
172 outhead1("%smask2:", label );
173 outline1("LDA %s", _b );
174 outline0("AND #$30" );
175 outline1("BNE %smask3", label );
176 outline1("LDA %s", _d );
177 outline0("AND #$cf" );
178 outline1("STA %s", _d );
179 outhead1("%smask3:", label );
180 outline1("LDA %s", _b );
181 outline0("AND #$c0" );
182 outline1("BNE %smask4", label );
183 outline1("LDA %s", _d );
184 outline0("AND #$3f" );
185 outline1("STA %s", _d );
186 outhead1("%smask4:", label );
187 break;
188 }
189 }
190
191}
192
203
204void blit_define( Environment * _environment, char * _name, int _sop, int _mop, int _smop, int _iop, int _dop, int _idop, int _top ) {
205
206 char blitLabel[MAX_TEMPORARY_STORAGE]; sprintf( blitLabel, "_%sblit", _name );
207 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _name );
208
209 cpu_jump( _environment, skipLabel );
210 cpu_label( _environment, blitLabel );
211
212 // B = s(x,y)
213 // IYL = d(x, y)
214 // IYH = m(x, y)
215
216 // BLITSU
217 blit_define_bltu( _environment, _sop, "BLITS1", "BLITR0" );
218
219 // BLITMU
220 blit_define_bltu( _environment, _mop, "BLITS2", "BLITR1" );
221
222 // BLITSM
223 blit_define_bltb( _environment, _smop, "BLITR0", "BLITR1", "BLITR2" );
224
225 // BLITSMU
226 blit_define_bltu( _environment, _iop, "BLITR2", "BLITR0" );
227
228 // BLITDU
229 blit_define_bltu( _environment, _dop, "BLITS3", "BLITR1" );
230
231 // BLITT
232 blit_define_bltb( _environment, _idop, "BLITR0", "BLITR1", "BLITR2" );
233
234 // BLITTU
235 blit_define_bltu( _environment, _top, "BLITR2", "BLITR3" );
236
237 outline0("LDA BLITR3");
238
239 cpu_return( _environment );
240 cpu_label( _environment, skipLabel );
241
242}
243
244static const char BLIT_SOURCES_REGISTER[][10] = {
245 "BLITS0", // destination
246 "BLITS1", // source 1
247 "BLITS2", // source 2
248};
249
250void blit_define_begin_compound( Environment * _environment, char * _name ) {
251
252 char blitLabel[MAX_TEMPORARY_STORAGE]; sprintf( blitLabel, "_%sblit", _name );
253 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _name );
254
255 if ( variable_exists( _environment, _name ) ) {
257 }
258 Variable * var = variable_define( _environment, _name, VT_BLIT, 0 );
259
260 memset( &_environment->blit, 0, sizeof( Blit ) );
261
262 _environment->blit.name = strdup( _name );
263 _environment->blit.realName = strdup( var->realName );
264
265 cpu_jump( _environment, skipLabel );
266 cpu_label( _environment, blitLabel );
267
268 cpu_blit_initialize( _environment );
269
270}
271
272void blit_define_compound_operand_to_register( Environment * _environment, int _register, int _source ) {
273
274 char * reg = cpu_blit_register_name( _environment, _register ) ;
275
276 outline1( "LDA %s", &BLIT_SOURCES_REGISTER[_source][0] );
277 outline1( "STA %s", reg );
278
279}
280
281void blit_define_compound_unary( Environment * _environment, int _operation, int _operand, int _result ) {
282
283 blit_define_bltu( _environment, _operation, cpu_blit_register_name( _environment, _operand ), cpu_blit_register_name( _environment, _result ) );
284
285}
286
287void blit_define_compound_binary( Environment * _environment, int _operation, int _operand1, int _operand2, int _result ) {
288
289 blit_define_bltb( _environment, _operation, cpu_blit_register_name( _environment, _operand1 ), cpu_blit_register_name( _environment, _operand2 ), cpu_blit_register_name( _environment, _result ) );
290
291}
292
293void blit_define_end_compound( Environment * _environment, int _result ) {
294
295 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _environment->blit.name );
296 char blitStageAreaName[MAX_TEMPORARY_STORAGE]; sprintf( blitStageAreaName, "%sbs", _environment->blit.name );
297
298 Variable * blitStageArea = variable_define( _environment, blitStageAreaName, VT_BUFFER, 0 );
299 variable_resize_buffer( _environment, blitStageArea->name, _environment->blit.usedMemory );
300
301 char * reg = cpu_blit_register_name( _environment, _result ) ;
302
303 outline1( "LDA %s", reg );
304
305 cpu_blit_finalize( _environment );
306
307 cpu_return( _environment );
308 cpu_label( _environment, skipLabel );
309
310}
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]