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 }
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("EORA #$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("ANDA %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("EORA %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("ANDA %s", _b );
154 outline1("STA %s", _d );
155 break;
156 case 2:
157 outline1("LDA %s", _a );
158 outline0("TFR A, B" );
159 outline1("LDA %s", _b );
160 outline0("ANDA #$03" );
161 outline1("BNE %smask1", label );
162 outline0("ANDB #$fc" );
163 outhead1("%smask1", label );
164 outline1("LDA %s", _b );
165 outline0("ANDA #$0c" );
166 outline1("BNE %smask2", label );
167 outline0("ANDB #$f3" );
168 outhead1("%smask2", label );
169 outline1("LDA %s", _b );
170 outline0("ANDA #$30" );
171 outline1("BNE %smask3", label );
172 outline0("ANDB #$cf" );
173 outhead1("%smask3", label );
174 outline1("LDA %s", _b );
175 outline0("AND #$c0" );
176 outline1("BNE %smask4", label );
177 outline0("ANDB #$3f" );
178 outhead1("%smask4", label );
179 outline1("STB %s", _d );
180 break;
181 }
182 }
183
184}
185
196void blit_define( Environment * _environment, char * _name, int _sop, int _mop, int _smop, int _iop, int _dop, int _idop, int _top ) {
197
198 char blitLabel[MAX_TEMPORARY_STORAGE]; sprintf( blitLabel, "_%sblit", _name );
199 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _name );
200
201 cpu_jump( _environment, skipLabel );
202 cpu_label( _environment, blitLabel );
203
204 // B = s(x,y)
205 // IYL = d(x, y)
206 // IYH = m(x, y)
207
208 // BLITSU
209 blit_define_bltu( _environment, _sop, "BLITS1", "BLITR0" );
210
211 // BLITMU
212 blit_define_bltu( _environment, _mop, "BLITS2", "BLITR1" );
213
214 // BLITSM
215 blit_define_bltb( _environment, _smop, "BLITR0", "BLITR1", "BLITR2" );
216
217 // BLITSMU
218 blit_define_bltu( _environment, _iop, "BLITR2", "BLITR0" );
219
220 // BLITDU
221 blit_define_bltu( _environment, _dop, "BLITS3", "BLITR1" );
222
223 // BLITT
224 blit_define_bltb( _environment, _idop, "BLITR0", "BLITR1", "BLITR2" );
225
226 // BLITTU
227 blit_define_bltu( _environment, _top, "BLITR2", "BLITR3" );
228
229 outline0("LDA BLITR3");
230
231 cpu_return( _environment );
232 cpu_label( _environment, skipLabel );
233
234}
235
236static const char BLIT_SOURCES_REGISTER[][10] = {
237 "BLITS0", // destination
238 "BLITS1", // source 1
239 "BLITS2", // source 2
240};
241
242void blit_define_begin_compound( Environment * _environment, char * _name ) {
243
244 char blitLabel[MAX_TEMPORARY_STORAGE]; sprintf( blitLabel, "_%sblit", _name );
245 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _name );
246
247 if ( variable_exists( _environment, _name ) ) {
249 }
250 Variable * var = variable_define( _environment, _name, VT_BLIT, 0 );
251
252 memset( &_environment->blit, 0, sizeof( Blit ) );
253
254 _environment->blit.name = strdup( _name );
255 _environment->blit.realName = strdup( var->realName );
256
257 cpu_jump( _environment, skipLabel );
258 cpu_label( _environment, blitLabel );
259
260 cpu_blit_initialize( _environment );
261
262}
263
264void blit_define_compound_operand_to_register( Environment * _environment, int _register, int _source ) {
265
266 char * reg = cpu_blit_register_name( _environment, _register ) ;
267
268 outline1( "LDA %s", &BLIT_SOURCES_REGISTER[_source][0] );
269 outline1( "STA %s", reg );
270
271}
272
273void blit_define_compound_unary( Environment * _environment, int _operation, int _operand, int _result ) {
274
275 blit_define_bltu( _environment, _operation, cpu_blit_register_name( _environment, _operand ), cpu_blit_register_name( _environment, _result ) );
276
277}
278
279void blit_define_compound_binary( Environment * _environment, int _operation, int _operand1, int _operand2, int _result ) {
280
281 blit_define_bltb( _environment, _operation, cpu_blit_register_name( _environment, _operand1 ), cpu_blit_register_name( _environment, _operand2 ), cpu_blit_register_name( _environment, _result ) );
282
283}
284
285void blit_define_end_compound( Environment * _environment, int _result ) {
286
287 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _environment->blit.name );
288 char blitStageAreaName[MAX_TEMPORARY_STORAGE]; sprintf( blitStageAreaName, "%sbs", _environment->blit.name );
289
290 Variable * blitStageArea = variable_define( _environment, blitStageAreaName, VT_BUFFER, 0 );
291 variable_resize_buffer( _environment, blitStageArea->name, _environment->blit.usedMemory );
292
293 char * reg = cpu_blit_register_name( _environment, _result ) ;
294
295 outline1( "LDA %s", reg );
296
297 cpu_blit_finalize( _environment );
298
299 cpu_return( _environment );
300 cpu_label( _environment, skipLabel );
301
302}
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]