-
Notifications
You must be signed in to change notification settings - Fork 9
/
driver.c
269 lines (249 loc) · 6.41 KB
/
driver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
** Driver for backend
**
** by Oscar Toledo G.
**
** Creation date: Aug/04/2024.
*/
#include <stdio.h>
#include <string.h>
#include "cvbasic.h"
#include "node.h"
#include "cpuz80.h"
#include "cpu6502.h"
#include "cpu9900.h"
static char driver_temp[MAX_LINE_SIZE];
/*
** Dump peephole optimization
*/
void generic_dump(void)
{
if (target == CPU_Z80)
cpuz80_dump();
}
/*
** 8-bit test
*/
void generic_test_8(void)
{
if (target == CPU_6502)
cpu6502_1op("CMP", "#0");
if (target == CPU_9900)
cpu9900_2op("movb", "r0", "r0");
if (target == CPU_Z80)
cpuz80_1op("OR", "A");
}
/*
** 16-bit test
*/
void generic_test_16(void)
{
if (target == CPU_6502) {
cpu6502_1op("STY", "temp");
cpu6502_1op("ORA", "temp");
}
if (target == CPU_9900) {
cpu9900_2op("mov", "r0", "r0");
}
if (target == CPU_Z80) {
cpuz80_2op("LD", "A", "H");
cpuz80_1op("OR", "L");
}
}
/*
** Label
*/
void generic_label(char *label)
{
if (target == CPU_6502)
cpu6502_label(label);
if (target == CPU_9900)
cpu9900_label(label);
if (target == CPU_Z80)
cpuz80_label(label);
}
/*
** Call
*/
void generic_call(char *label)
{
if (target == CPU_6502)
cpu6502_1op("JSR", label);
if (target == CPU_9900) {
cpu9900_1op("bl", "@jsr");
cpu9900_1op("data", label);
}
if (target == CPU_Z80)
cpuz80_1op("CALL", label);
}
/*
** Return
*/
void generic_return(void)
{
if (target == CPU_6502)
cpu6502_noop("RTS");
if (target == CPU_9900) {
/* we don't presume r11 was preserved - it probably wasn't! */
cpu9900_2op("mov", "*r10+", "r0");
cpu9900_1op("b", "*r0");
}
if (target == CPU_Z80)
cpuz80_noop("RET");
}
/*
** Jump
*/
void generic_jump(char *label)
{
if (target == CPU_6502)
cpu6502_1op("JMP", label);
if (target == CPU_9900) {
char temp[256];
sprintf(temp, "@%s", label);
cpu9900_1op("b", temp);
}
if (target == CPU_Z80)
cpuz80_1op("JP", label);
}
/*
** Jump if zero
*/
void generic_jump_zero(char *label)
{
if (target == CPU_6502)
cpu6502_1op("BEQ.L", label);
if (target == CPU_9900) {
char internal_label[256], internal_label2[256];
int number = next_local++;
sprintf(internal_label, INTERNAL_PREFIX "%d", number);
sprintf(internal_label2, "@%s", label);
cpu9900_1op("jne", internal_label);
cpu9900_1op("b", internal_label2);
cpu9900_label(internal_label);
}
if (target == CPU_Z80)
cpuz80_2op("JP", "Z", label);
}
/*
** Generic range comparison (8-bit)
*/
void generic_comparison_8bit(int min, int max, char *label)
{
char value[256];
if (min == max) {
if (target == CPU_Z80) {
sprintf(value, "%d", min);
cpuz80_1op("CP", value);
cpuz80_2op("JP", "NZ", label);
}
if (target == CPU_6502) {
sprintf(value, "#%d", min);
cpu6502_1op("CMP", value);
cpu6502_1op("BNE.L", label);
}
if (target == CPU_9900) {
sprintf(value, "%d", min * 256);
cpu9900_2op("ci", "r0", value);
cpu9900_1op("jne", label);
}
return;
}
if (target == CPU_Z80) {
sprintf(value, "%d", min);
cpuz80_1op("CP", value);
cpuz80_2op("JP", "C", label);
sprintf(value, "%d", max + 1);
cpuz80_1op("CP", value);
cpuz80_2op("JP", "NC", label);
}
if (target == CPU_6502) {
sprintf(value, "#%d", min);
cpu6502_1op("CMP", value);
cpu6502_1op("BCC.L", label);
sprintf(value, "#%d", max + 1);
cpu6502_1op("CMP", value);
cpu6502_1op("BCS.L", label);
}
if (target == CPU_9900) {
sprintf(value, "%d", min * 256);
cpu9900_2op("ci", "r0", value);
cpu9900_1op("jl", label);
sprintf(value, "%d", max * 256);
cpu9900_2op("ci", "r0", value);
cpu9900_1op("jh", label);
}
}
/*
** Generic range comparison (16-bit)
*/
void generic_comparison_16bit(int min, int max, char *label)
{
char value[256];
if (min == max) {
if (target == CPU_Z80) {
sprintf(value, "%d", min);
cpuz80_2op("LD", "DE", value);
cpuz80_1op("OR", "A");
cpuz80_2op("SBC", "HL", "DE");
cpuz80_2op("ADD", "HL", "DE");
cpuz80_2op("JP", "NZ", label);
}
if (target == CPU_6502) {
sprintf(value, "#%d", min & 0xff);
cpu6502_1op("CMP", value);
cpu6502_1op("BNE.L", label);
sprintf(value, "#%d", (min >> 8) & 0xff);
cpu6502_1op("CPY", value);
cpu6502_1op("BNE.L", label);
}
if (target == CPU_9900) {
sprintf(value, "%d", min);
cpu9900_2op("ci", "r0", value);
cpu9900_1op("jne", label);
}
return;
}
if (target == CPU_Z80) {
sprintf(value, "%d", min);
cpuz80_2op("LD", "DE", value);
cpuz80_1op("OR", "A");
cpuz80_2op("SBC", "HL", "DE");
cpuz80_2op("ADD", "HL", "DE");
cpuz80_2op("JP", "C", label);
sprintf(value, "%d", max + 1);
cpuz80_2op("LD", "DE", value);
/* cpuz80_1op("OR", "A"); */ /* Guaranteed */
cpuz80_2op("SBC", "HL", "DE");
cpuz80_2op("ADD", "HL", "DE");
cpuz80_2op("JP", "NC", label);
}
if (target == CPU_6502) {
cpu6502_noop("PHA");
cpu6502_noop("SEC");
sprintf(value, "#%d", min & 0xff);
cpu6502_1op("SBC", value);
cpu6502_noop("TYA");
sprintf(value, "#%d", (min >> 8) & 0xff);
cpu6502_1op("SBC", value);
cpu6502_noop("PLA");
cpu6502_1op("BCC.L", label);
cpu6502_noop("PHA");
/* cpu6502_noop("SEC"); */ /* Guaranteed */
sprintf(value, "#%d", (max + 1) & 0xff);
cpu6502_1op("SBC", value);
cpu6502_noop("TYA");
sprintf(value, "#%d", ((max + 1) >> 8) & 0xff);
cpu6502_1op("SBC", value);
cpu6502_noop("PLA");
cpu6502_1op("BCS.L", label);
}
if (target == CPU_9900) {
sprintf(value, "%d", min);
cpu9900_2op("ci", "r0", value);
cpu9900_1op("jl", label);
sprintf(value, "%d", max);
cpu9900_2op("ci", "r0", value);
cpu9900_1op("jh", label);
}
}