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 }
55 } else if ( _op == ( 1 << _environment->currentModeBW ) + 2 ) {
56 outline0("; bltu C = 0 (ignore)");
57 outline0("LD A, 0" );
58 outline1("LD %s, A", _c );
59 } else if ( _op == ( 1 << _environment->currentModeBW ) + 1 ) {
60 outline0("; bltu C = !A (not)");
61 outline1("LD A, %s", _a );
62 outline0("XOR $ff");
63 outline1("LD %s, A", _c );
64 } else if ( _op == ( 1 << _environment->currentModeBW ) ) {
65 outline0("; bltu C = A (copy)");
66 outline1("LD A, %s", _a );
67 outline1("LD %s, A", _c );
68 } else if ( _op < ( 1 << _environment->currentModeBW ) ) {
69 unsigned char value = 0;
70 switch( _environment->currentModeBW ) {
71 case 1:
72 if ( _op ) {
73 value = 0xff;
74 } else {
75 value = 0x00;
76 }
77 break;
78 }
79 outline0("; bltu C = value");
80 outline1("LD A, $%2.2x", value );
81 outline1("LD %s, A", _c );
82 }
83
84}
85
86static void blit_define_bltb( Environment * _environment, int _op, char * _a, char * _b, char * _d ) {
87
88 if ( _op == 0 ) {
89 outline0("; bltb C = A and B");
90 outline1("LD A, %s", _a );
91 outline1("LD %s, A", _d );
92 outline1("LD A, %s", _b );
93 outline1("AND %s", _d );
94 outline1("LD %s, A", _d );
95 } else if ( _op == 1 ) {
96 outline0("; bltb C = A or B");
97 outline1("LD A, %s", _a );
98 outline1("LD %s, A", _d );
99 outline1("LD A, %s", _b );
100 outline1("OR %s", _d );
101 outline1("LD %s, A", _d );
102 } else if ( _op == 2 ) {
103 outline0("; bltb C = A xor B");
104 outline1("LD A, %s", _a );
105 outline1("LD %s, A", _d );
106 outline1("LD A, %s", _b );
107 outline1("XOR %s", _d );
108 outline1("LD %s, A", _d );
109 } else if ( _op == 3 ) {
110 outline0("; bltb C = A");
111 outline1("LD A, %s", _a );
112 outline1("LD %s, A", _d );
113 } else if ( _op == 4 ) {
114 outline0("; bltb C = B");
115 outline1("LD A, %s", _b );
116 outline1("LD %s, A", _d );
117 } else if ( _op == 5 ) {
118 outline0("; bltb C = A if B>0, 0 if B=0");
120 switch( _environment->currentModeBW ) {
121 case 1:
122 outline1("LD A, %s", _a );
123 outline1("AND %s", _b );
124 outline1("LD %s, A", _d );
125 break;
126 }
127 }
128
129}
130
141void blit_define( Environment * _environment, char * _name, int _sop, int _mop, int _smop, int _iop, int _dop, int _idop, int _top ) {
142
143 char blitLabel[MAX_TEMPORARY_STORAGE]; sprintf( blitLabel, "_%sblit", _name );
144 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _name );
145
146 cpu_jump( _environment, skipLabel );
147 cpu_label( _environment, blitLabel );
148
149 // B = s(x,y)
150 // IYL = d(x, y)
151 // IYH = m(x, y)
152
153 outline0("PUSH HL");
154 outline0("PUSH DE");
155
156 // BLITSU
157 blit_define_bltu( _environment, _sop, "B", "H" );
158
159 // BLITMU
160 blit_define_bltu( _environment, _mop, "IYH", "L" );
161
162 // BLITSM
163 blit_define_bltb( _environment, _smop, "H", "L", "D" );
164
165 // BLITSMU
166 blit_define_bltu( _environment, _iop, "D", "H" );
167
168 // BLITDU
169 blit_define_bltu( _environment, _dop, "IYL", "L" );
170
171 // BLITT
172 blit_define_bltb( _environment, _idop, "H", "L", "D" );
173
174 // BLITTU
175 blit_define_bltu( _environment, _top, "D", "E" );
176
177 outline0("LD A, E");
178 outline0("POP DE");
179 outline0("POP HL");
180
181 cpu_return( _environment );
182 cpu_label( _environment, skipLabel );
183
184}
185
186static const char BLIT_SOURCES_REGISTER[][4] = {
187 "IYL", // destination
188 "B", // source 1
189 "IYH", // source 2
190};
191
192void blit_define_begin_compound( Environment * _environment, char * _name ) {
193
194 char blitLabel[MAX_TEMPORARY_STORAGE]; sprintf( blitLabel, "_%sblit", _name );
195 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _name );
196
197 if ( variable_exists( _environment, _name ) ) {
199 }
200 Variable * var = variable_define( _environment, _name, VT_BLIT, 0 );
201
202 memset( &_environment->blit, 0, sizeof( Blit ) );
203
204 _environment->blit.name = strdup( _name );
205 _environment->blit.realName = strdup( var->realName );
206
207 cpu_jump( _environment, skipLabel );
208 cpu_label( _environment, blitLabel );
209
210 cpu_blit_initialize( _environment );
211
212}
213
214void blit_define_compound_operand_to_register( Environment * _environment, int _register, int _source ) {
215
216 char * reg = cpu_blit_register_name( _environment, _register ) ;
217
218 outline1( "LD A, %s", &BLIT_SOURCES_REGISTER[_source][0] );
219 outline1( "LD %s, A", reg );
220
221}
222
223void blit_define_compound_unary( Environment * _environment, int _operation, int _operand, int _result ) {
224
225 blit_define_bltu( _environment, _operation, cpu_blit_register_name( _environment, _operand ), cpu_blit_register_name( _environment, _result ) );
226
227}
228
229void blit_define_compound_binary( Environment * _environment, int _operation, int _operand1, int _operand2, int _result ) {
230
231 blit_define_bltb( _environment, _operation, cpu_blit_register_name( _environment, _operand1 ), cpu_blit_register_name( _environment, _operand2 ), cpu_blit_register_name( _environment, _result ) );
232
233}
234
235void blit_define_end_compound( Environment * _environment, int _result ) {
236
237 char skipLabel[MAX_TEMPORARY_STORAGE]; sprintf( skipLabel, "_%sskip", _environment->blit.name );
238 char blitStageAreaName[MAX_TEMPORARY_STORAGE]; sprintf( blitStageAreaName, "%sbs", _environment->blit.name );
239
240 Variable * blitStageArea = variable_define( _environment, blitStageAreaName, VT_BUFFER, 0 );
241 variable_resize_buffer( _environment, blitStageArea->name, _environment->blit.usedMemory );
242
243 char * reg = cpu_blit_register_name( _environment, _result ) ;
244
245 outline1( "LD A, %s", reg );
246
247 cpu_blit_finalize( _environment );
248
249 cpu_return( _environment );
250 cpu_label( _environment, skipLabel );
251
252}
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
char DATATYPE_AS_STRING[][16]