-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm.c
More file actions
309 lines (249 loc) · 7.44 KB
/
asm.c
File metadata and controls
309 lines (249 loc) · 7.44 KB
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
#include "asm.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
void sym_clear(struct symtab *syms) {
size_t i;
for (i = 0; i < syms->num_symbols; ++i)
free(syms->symbols[i].id);
syms->num_symbols = 0;
}
static sym_def_t *sym_lookup(struct symtab *syms, const char *name) {
size_t i;
for (i = 0; i < syms->num_symbols; ++i) {
if (strcmp(syms->symbols[i].id, name) == 0)
return &syms->symbols[i];
}
return NULL;
}
/* prints the instructions at start_ofs, up to len bytes */
void print_instr(uint8_t *mem, uint16_t len, uint16_t start_ofs) {
uint16_t end = len + start_ofs;
uint16_t pc = start_ofs;
while (pc < end) {
uint16_t instr_pc = pc;
uint8_t code = mem[pc++];
opc_descr_t *opcode = instr_descr(code);
char instr[64] = {0};
char prefix[64] = {0};
size_t num_bytes = 1;
const char *fmt = "";
sprintf(prefix, ".%04X ", instr_pc);
if (opcode->cfun) {
switch (opcode->addr_m) {
/* 3 byte instructions */
case ADR_ABX: fmt = "%s $%04X,X";
case ADR_ABY: if (!*fmt) fmt = "%s $%04X,Y";
case ADR_ABS: if (!*fmt) fmt = "%s $%04X";
case ADR_IND: if (!*fmt) fmt = "%s ($%04X)";
case ADR_REL: if (!*fmt) fmt = "%s $%04X,PC";
num_bytes = 3;
uint8_t hi = mem[pc++];
uint16_t val = (uint16_t)hi << 8 | mem[pc++];
sprintf(instr, fmt, opcode->name, val);
break;
/* 2 byte instructions */
case ADR_IMM: fmt = "%s #$%02X";
case ADR_ZPX: if (!*fmt) fmt = "%s $%02X,X";
case ADR_ZPY: if (!*fmt) fmt = "%s $%02X,Y";
case ADR_ZP: if (!*fmt) fmt = "%s $%02X";
case ADR_IZX: if (!*fmt) fmt = "%s ($%02X,X)";
case ADR_IZY: if (!*fmt) fmt = "%s ($%02X),Y";
val = mem[pc++];
num_bytes = 2;
sprintf(instr, fmt, opcode->name, val);
break;
default:
strcpy(instr, opcode->name);
}
}
while (num_bytes--) {
sprintf(prefix + strlen(prefix), "%02X ", mem[instr_pc++]);
}
printf("%-16s %s\n", prefix, instr);
}
}
static int isnotnl(int c) {return (c != '\n');}
static int ischars(int c) {return (!isspace(c) && c != ';' && c != '\n');}
static void eat_while(const char **p, int (*pred)(int)) {
const char *pos = *p;
while (*pos && (*p = pos, pred(*pos++)));
}
static int parse_value(const char *text, uint8_t mode,
uint16_t *res, struct symtab *symbols) {
/* if text == NULL, there is no value */
if (mode != ADR_IMP && !text)
return -1;
long val;
char *end = 0;
switch (mode) {
case ADR_IMP:
return 0;
case ADR_ABS: /* 12342, $123F, LABEL */
if (text[0] == '$')
val = strtol(text + 1, &end, 16);
else
val = strtol(text, &end, 10);
if (end != (text[0] == '$' ? text + 1 : text)) {
*res = val;
return 2;
}
/* maybe it's a label, let's try it */
if (text[0] == '$')
return -1;
while (*end) {
if (!isalpha(*end))
return -1;
end++;
}
/* note: we've ruined 'end' here */
sym_def_t *symbol = sym_lookup(symbols, text);
if (!symbol) {
fprintf(stderr,
"error: symbol '%s' not defined\n", text);
return -1;
}
*res = symbol->address;
return 2;
case ADR_IMM: /* #255, #$FA */
if (text[0] != '#')
return -1;
if (text[1] == '$')
val = strtol(text + 2, &end, 16);
else
val = strtol(text + 1, &end, 10);
if (end == (text[1] == '$' ? text + 2 : text + 1))
return -1;
if (val > 0xFF)
return -1;
*res = val;
return 1;
/* TODO: the rest of the modes */
}
return -1;
}
static void parse_line(const char *grps[], size_t num_grps,
struct cpu_state *cpu, struct symtab *sym) {
/* 1: 3 label instr/keyword param*/
/* 2: 2 label instr/keyword*/
/* 3: 2 instr/keyword param */
/* 4: 1 instr/keyword */
/* 5: 1 label */
uint16_t modes = 0;
size_t instr_start = 0;
modes = instr_modes(grps[0]);
if (modes == 0) {
/* first group isn't a valid instruction, maybe it's a keyword */
if (strcmp(grps[0], "org") == 0) {
assert(num_grps == 2);
uint16_t val;
if (parse_value(grps[1], ADR_ABS, &val, sym) != -1) {
printf("jumped to $0x%04X\n", val);
cpu->pc = val;
}
else {
fprintf(stderr,
"error: origin takes an absolute address - not %s\n", grps[1]);
return;
}
}
else {
assert(sym->num_symbols < 100 && "can't define any more symbols");
uint8_t flags = 0;
char sym_name[64] = {0};
strncpy(sym_name, grps[0], sizeof sym_name - 1);
sym_name[sizeof sym_name - 1] = '\0';
size_t len = strlen(sym_name);
if (len > 0 && sym_name[len - 1] == ':') {
sym_name[len - 1] = '\0';
flags |= SYM_GLOBAL;
}
if (sym_lookup(sym, sym_name)) {
fprintf(stderr,
"error: symbol '%s' already defined\n", sym_name);
return;
}
printf("defining '%s' as $%04X\n", grps[0], cpu->pc);
sym_def_t *symdef = &sym->symbols[sym->num_symbols++];
symdef->id = strdup(sym_name);
symdef->address = cpu->pc;
symdef->flags = flags;
if (num_grps > 1) {
instr_start = 1;
modes = instr_modes(grps[1]);
grps++; /* grps will now point to the instruction */
}
}
}
if (modes) {
/* there's a valid instruction in there */
/* TODO: all these instr_modes and instr_named calls are doing more work
* than necessary. */
size_t i;
for (i = 0; i < ADR_MAX; ++i) {
uint16_t val;
int bytes;
const char *value = (num_grps - instr_start != 1 ?
grps[1] : NULL);
if (modes & (1 << i) &&
(bytes = parse_value(value, i, &val, sym)) != -1) {
printf("%s with %s means mode %zu\n",
grps[0], grps[1], i);
uint8_t op = instr_named(grps[0], i);
assert(op);
cpu->mem[cpu->pc++] = op;
switch (bytes) {
case 2: cpu->mem[cpu->pc++] = val >> 8;
case 1: cpu->mem[cpu->pc++] = val & 0xFF;
case 0: break;
default: assert(!"some strange amount of bytes");
}
break;
}
}
if (i == ADR_MAX) {
fprintf(stderr,
"error: the value given for '%s', %s, doesn't really work for me\n",
grps[0],
grps[1]);
return;
}
}
}
void parse_asm(const char *text, struct cpu_state *cpu, struct symtab *sym) {
const char *pos = text;
char value[64] = {0};
char *val_ptr = value;
const char *val_grps[8];
size_t num_grps = 0;
while (*pos) {
eat_while(&pos, isspace);
const char *chars_start = pos;
eat_while(&pos, ischars);
size_t diff = pos - chars_start;
if (diff) {
if (val_ptr + diff >= value + sizeof value ||
num_grps >= sizeof val_grps / sizeof val_grps[0]) {
fprintf(stderr, "error: we ran out of space");
abort();
}
strncpy(val_ptr, chars_start, diff);
assert(num_grps < 8);
val_grps[num_grps++] = val_ptr;
val_ptr += diff;
*val_ptr++ = '\0';
}
if (*pos == ';') {
eat_while(&pos, isnotnl);
}
if (*pos == '\n') {
if (num_grps > 0)
parse_line(val_grps, num_grps, cpu, sym);
num_grps = 0;
val_ptr = value;
pos++;
}
}
}