-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstr.c
356 lines (338 loc) · 9.64 KB
/
instr.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "as_wdc6502.h"
#include "instr.h"
#include <ctype.h>
instruction instr_get(char *mnem)
{
const struct instruction *i = instr_get_intern(mnem, strlen(mnem));
return (instruction)i;
}
// Converts addressing mode bit flag to an opcode[16] index in
// `instruction`
static opcode_i am_to_opcode_i(addressing_mode am)
{
int i = -1;
while (am > 0) {
am >>= 1;
i++;
}
return i;
}
static int strcicmp(char const *a, char const *b)
{
for (;; a++, b++) {
int d = tolower((unsigned char)*a) - tolower((unsigned char)*b);
if (d != 0 || !*a)
return d;
}
}
static bool eol(tok t)
{
return t == TOK_NEWL || t == TOK_EOF;
}
static bool val(tok t)
{
return t == TOK_INT || t == TOK_ID;
}
typedef struct {
addressing_mode am;
bool is_imm;
unsigned long long imm;
char *sym;
} operand;
static void set_val(scanner *s, operand *op)
{
if (s->tok == TOK_INT) {
op->imm = s->tok_num;
op->is_imm = true;
} else {
op->sym = s->tok_id;
op->is_imm = false;
}
}
static bool get_op(scanner *s, operand *op)
{
tok t = s->tok;
if (t == TOK_HASH) {
// '#'
t = scan_tok(s);
if (val(t)) {
// '#' val
set_val(s, op);
t = scan_tok(s);
if (eol(t)) {
// '#' val eol
op->am = AM_IMM;
return true;
} else {
// '#' val ?
scan_discard_line(s);
return false;
}
} else {
// '#' ?
scan_discard_line(s);
return false;
}
} else if (val(t)) {
// val
set_val(s, op);
t = scan_tok(s);
if (eol(t)) {
// val eol
op->am = AM_ABS | AM_ZP | AM_PCR;
return true;
} else if (t == TOK_COMMA) {
// val ','
t = scan_tok(s);
if (t == TOK_ID && !strcicmp(s->tok_id, "x")) {
// val ',' 'x'
t = scan_tok(s);
if (eol(t)) {
// val ',' 'x' eol
op->am = AM_ABSX | AM_ZPX;
return true;
} else {
// val ',' 'x' ?
scan_discard_line(s);
return false;
}
} else if (t == TOK_ID && !strcicmp(s->tok_id, "y")) {
// val ',' 'y'
t = scan_tok(s);
if (eol(t)) {
// val ',' 'y' eol
op->am = AM_ABSY | AM_ZPY;
return true;
} else {
// val ',' 'y' ?
scan_discard_line(s);
return false;
}
} else {
// val ',' ?
scan_discard_line(s);
return false;
}
} else {
// val ?
scan_discard_line(s);
return false;
}
} else if (t == TOK_LBRACE) {
// '('
t = scan_tok(s);
if (val(t)) {
// '(' val
set_val(s, op);
t = scan_tok(s);
if (t == TOK_RBRACE) {
// '(' val ')'
t = scan_tok(s);
if (eol(t)) {
// '(' val ')' eol
op->am = AM_ABSI | AM_ZPI;
return true;
} else if (t == TOK_COMMA) {
// '(' val ')' ','
t = scan_tok(s);
if (t == TOK_ID && !strcicmp(s->tok_id, "y")) {
// '(' val ')' ',' 'y'
t = scan_tok(s);
if (eol(t)) {
// '(' val ')' ',' 'y' eol
op->am = AM_ZPIIY;
return true;
} else {
// '(' val ')' ',' 'y' ?
scan_discard_line(s);
return false;
}
} else {
// '(' val ')' ',' ?
scan_discard_line(s);
return false;
}
} else {
// '(' val ')' ',' ?
scan_discard_line(s);
return false;
}
} else if (t == TOK_COMMA) {
// '(' val ','
t = scan_tok(s);
if (t == TOK_ID && !strcicmp(s->tok_id, "x")) {
// '(' val ',' 'x'
t = scan_tok(s);
if (t == TOK_RBRACE) {
// '(' val ',' 'x' ')'
t = scan_tok(s);
if (eol(t)) {
// '(' val ',' 'x' ')' eol
op->am = AM_ABSII | AM_ZPII;
return true;
} else {
// '(' val ',' 'x' ')' ?
scan_discard_line(s);
return false;
}
} else {
// '(' val ',' 'x' ?
scan_discard_line(s);
return false;
}
} else {
// '(' val ',' ?
scan_discard_line(s);
return false;
}
} else {
// '(' val ?
scan_discard_line(s);
return false;
}
} else {
// '(' ?
scan_discard_line(s);
return false;
}
} else if (eol(t)) {
// eol
op->am = AM_IMPL | AM_STK | AM_ACC;
return true;
} else {
// ?
scan_discard_line(s);
return false;
}
}
// Disambiguates any scenarios where an instruction can use both zero-page
// and absolute addressing.
// - If the operand is not immediate (relocatable symbol), use absolute
// addressing
// - If the operand is immediate and smaller than 0xFF, use zero-page addressing
// - If the operand is immediate and larger than 0xFF, use absolute addressing
static void disambiguate(operand *op)
{
if (op->am & (AM_ABS | AM_ZP)) {
// AM_ABS | AM_ZP
if (!op->is_imm || op->imm > BYTE_MAX) {
op->am &= ~AM_ZP;
} else {
op->am &= ~AM_ABS;
}
}
if (op->am & (AM_ABSII | AM_ZPII)) {
// AM_ABSII | AM_ZPII
if (!op->is_imm || op->imm > BYTE_MAX) {
op->am &= ~AM_ZPII;
} else {
op->am &= ~AM_ABSII;
}
}
if (op->am & (AM_ABSX | AM_ZPX)) {
// AM_ABSX | AM_ZPX
if (!op->is_imm || op->imm > BYTE_MAX) {
op->am &= ~AM_ZPX;
} else {
op->am &= ~AM_ABSX;
}
}
if (op->am & (AM_ABSY | AM_ZPY)) {
if (!op->is_imm || op->imm > BYTE_MAX) {
op->am &= ~AM_ZPY;
} else {
op->am &= ~AM_ABSY;
}
}
if (op->am & (AM_ABSI | AM_ZPI)) {
if (!op->is_imm || op->imm > BYTE_MAX) {
op->am &= ~AM_ZPI;
} else {
op->am &= ~AM_ABSI;
}
}
}
void instr_asm(program *p, scanner *s, instruction ptr)
{
struct instruction *i = (struct instruction *)ptr;
err_set_loc(s->path, s->line);
operand op = {0};
if (!get_op(s, &op)) {
err_at("incorrect operands for %s", i->mnem);
return;
}
// Find possible addressing modes
disambiguate(&op);
int i_am = i->am & op.am;
if (!i_am) {
err_at("incorrect addressing mode for %s", i->mnem);
return;
}
// Get opcode index from the addressing mode
int am_i = am_to_opcode_i(i_am);
unsigned char opc = i->opcode[am_i];
switch (i_am) {
unsigned char lo, hi;
case AM_ABS:
case AM_ABSII:
case AM_ABSX:
case AM_ABSY:
case AM_ABSI:
if (op.is_imm && op.imm > WORD_MAX) {
err("address too large");
break;
}
if (op.is_imm) {
lo = op.imm & 0xFF;
hi = (op.imm >> 8) & 0xFF;
prog_add_byte(p, hi);
prog_add_byte(p, lo);
} else {
// Absolute relocation
prog_add_reloc(p, op.sym, true, false, s->path, s->line);
prog_add_byte(p, 0);
prog_add_byte(p, 0);
}
prog_add_byte(p, opc);
break;
case AM_IMPL:
case AM_STK:
case AM_ACC:
prog_add_byte(p, opc);
break;
case AM_IMM:
case AM_PCR:
case AM_ZP:
case AM_ZPII:
case AM_ZPX:
case AM_ZPY:
case AM_ZPI:
case AM_ZPIIY:
if (op.is_imm && op.imm > BYTE_MAX) {
char *what;
if (i_am == AM_IMM) {
what = "immediate";
} else {
what = "pc offset";
}
// Zero-page address cannot be too large, since AM_ZP... is removed
// in `disambiguate()` in case immediate is greater than BYTE_MAX
err_at("%s too large", what);
break;
}
if (op.is_imm) {
prog_add_byte(p, (unsigned char)op.imm);
} else {
if (i_am == AM_PCR) {
// PC-relative relocation
prog_add_reloc(p, op.sym, false, true, s->path, s->line);
} else {
// Zero-page relocation
prog_add_reloc(p, op.sym, false, false, s->path, s->line);
}
prog_add_byte(p, 0);
}
prog_add_byte(p, opc);
break;
}
}