-
Notifications
You must be signed in to change notification settings - Fork 0
/
asm.c
481 lines (395 loc) · 8.89 KB
/
asm.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#define CODE_ADDR_BITS 6
#define CODE_ROM_SIZE (1 << CODE_ADDR_BITS)
#define CODE_ADDR_MASK (CODE_ROM_SIZE - 1)
#define ALU_ADD 0
#define ALU_XOR 1
#define ALU_AND 2
#define ALU_OR 3
#define ALU_NOPX 4
#define ALU_NOPY 5
#define ALU_MAX 6
struct label {
struct label *next;
char *name;
uint8_t addr;
};
struct code_rom {
uint16_t code[CODE_ROM_SIZE];
struct label *labels;
const char *fn;
unsigned int line;
uint8_t addr;
uint8_t ip;
};
/* Easy string tokeniser */
static int easy_explode(char *str, char split,
char **toks, int max_toks)
{
char *tmp;
int tok;
int state;
for(tmp=str,state=tok=0; *tmp && tok <= max_toks; tmp++) {
if ( state == 0 ) {
if ( *tmp == split && (tok < max_toks)) {
toks[tok++] = NULL;
}else if ( !isspace(*tmp) ) {
state = 1;
toks[tok++] = tmp;
}
}else if ( state == 1 ) {
if ( tok < max_toks ) {
if ( *tmp == split || isspace(*tmp) ) {
*tmp = '\0';
state = 0;
}
}else if ( *tmp == '\n' )
*tmp = '\0';
}
}
return tok;
}
static int label_is_valid(char *ptr)
{
return 1;
}
static struct label *label_lookup(struct code_rom *rom, const char *label)
{
struct label *l;
for(l = rom->labels; l; l = l->next)
if ( !strcmp(l->name, label) )
return l;
return NULL;
}
static int add_label(struct code_rom *rom, char *ptr)
{
struct label *l;
if ( !label_is_valid(ptr) ) {
fprintf(stderr, "%s:%u: invalid label name: %s\n",
rom->fn, rom->line, ptr);
return 0;
}
if ( label_lookup(rom, ptr) ) {
fprintf(stderr, "%s:%u: duplicate label: %s\n",
rom->fn, rom->line, ptr);
return 0;
}
l = calloc(1, sizeof(*l));
if ( NULL == l ) {
fprintf(stderr, "%s:%u: %s\n",
rom->fn, rom->line, strerror(errno));
return 0;
}
l->name = strdup(ptr);
if ( NULL == l->name ) {
fprintf(stderr, "%s:%u: %s\n",
rom->fn, rom->line, strerror(errno));
free(l);
return 0;
}
l->addr = rom->addr;
l->next = rom->labels;
rom->labels = l;
return 1;
}
static int emit_insn(struct code_rom *rom, uint16_t insn)
{
if ( rom->addr >= CODE_ROM_SIZE ) {
return 0;
}
rom->code[rom->addr++] = insn;
return 1;
}
static const char * const cpu_alu_name(uint8_t op)
{
static const char * const a[] = {"add", "xor", "and",
"or", "nopx", "nopy"};
if ( op >= ALU_MAX )
abort();
return a[op];
}
static int reg_from_name(struct code_rom *rom, const char *name, uint8_t *num)
{
static const char * const r[] = {"x0", "x1", "y0", "y1"};
unsigned int i;
for(i = 0; i < sizeof(r)/sizeof(*r); i++) {
if ( !strcmp(r[i], name) ) {
*num = i;
return 1;
}
}
fprintf(stderr, "%s:%u: unknown register: %s\n",
rom->fn, rom->line, name);
return 0;
}
static int nreg_from_name(struct code_rom *rom, const char r,
const char *name, uint8_t *num)
{
if ( name[0] != r )
goto barf;
switch(name[1]) {
case '0':
*num = 0;
break;
case '1':
*num = 1;
break;
default:
goto barf;
}
if ( name[2] == '\0' )
return 1;
barf:
fprintf(stderr, "%s:%u: bad %c register, %s\n",
rom->fn, rom->line, r, name);
return 0;
}
static int xreg_from_name(struct code_rom *rom, const char *name, uint8_t *num)
{
return nreg_from_name(rom, 'x', name, num);
}
static int yreg_from_name(struct code_rom *rom, const char *name, uint8_t *num)
{
return nreg_from_name(rom, 'y', name, num);
}
static int imm_from_str(struct code_rom *rom, const char *s, uint8_t *c)
{
unsigned long int ret;
char *end = NULL;
if ( *s != '$' )
goto err;
s++;
ret = strtoul(s, &end, 0);
if ( end == s || *end != '\0' || ret & ~0xff )
goto err;
*c = ret;
return 1;
err:
fprintf(stderr, "%s:%u: bad address or integer literal: %s\n",
rom->fn, rom->line, s);
return 0;
}
static int op_ldi(struct code_rom *rom, char *operands)
{
char *tok[2] = {NULL, NULL};
uint8_t reg;
uint8_t imm;
int n;
n = easy_explode(operands, ',', tok, 2);
if ( n != 2 ) {
fprintf(stderr, "%s:%u: ldi: wrong number of arguments\n",
rom->fn, rom->line);
return 0;
}
if ( !reg_from_name(rom, tok[0], ®) )
return 0;
if ( !imm_from_str(rom, tok[1], &imm) )
return 0;
if ( !emit_insn(rom, (1 << 4) | (1 << reg)) )
return 0;
if ( !emit_insn(rom, imm) )
return 0;
return 1;
}
static int alu_op(struct code_rom *rom, uint8_t op, char *operands)
{
char *tok[3] = {NULL, NULL, NULL};
uint8_t reg, x, y;
int n;
n = easy_explode(operands, ',', tok, 3);
if ( n != 3 ) {
fprintf(stderr, "%s:%u: %s: wrong number of arguments\n",
rom->fn, rom->line, cpu_alu_name(op));
return 0;
}
if ( !xreg_from_name(rom, tok[0], &x) )
return 0;
if ( !yreg_from_name(rom, tok[1], &y) )
return 0;
if ( !reg_from_name(rom, tok[2], ®) )
return 0;
return emit_insn(rom, (1 << 7) | (x << 6) | (y << 5) | reg);
}
static int op_add(struct code_rom *rom, char *operands)
{
return alu_op(rom, ALU_ADD, operands);
}
static int op_xor(struct code_rom *rom, char *operands)
{
return alu_op(rom, ALU_XOR, operands);
}
static int op_and(struct code_rom *rom, char *operands)
{
return alu_op(rom, ALU_AND, operands);
}
static int op_or(struct code_rom *rom, char *operands)
{
return alu_op(rom, ALU_OR, operands);
}
static int op_nopx(struct code_rom *rom, char *operands)
{
return alu_op(rom, ALU_NOPX, operands);
}
static int op_nopy(struct code_rom *rom, char *operands)
{
return alu_op(rom, ALU_NOPY, operands);
}
static int op_halt(struct code_rom *rom, char *operands)
{
return emit_insn(rom, 0);
}
static int cbranch_op(struct code_rom *rom, uint8_t invert,
uint8_t zero, const char *label)
{
struct label *l;
uint16_t insn;
invert = !!invert;
zero = !!zero;
l = label_lookup(rom, label);
if ( NULL == l ) {
fprintf(stderr, "%s:%u: label not found: %s\n",
rom->fn, rom->line, label);
return 0;
}
insn = (1 << 8) | (invert << 7) | (zero << 6) | l->addr;
return emit_insn(rom, insn);
}
static int op_jnz(struct code_rom *rom, char *operands)
{
return cbranch_op(rom, 1, 1, operands);
}
static int op_jz(struct code_rom *rom, char *operands)
{
return cbranch_op(rom, 0, 1, operands);
}
static int op_jnc(struct code_rom *rom, char *operands)
{
return cbranch_op(rom, 1, 0, operands);
}
static int op_jc(struct code_rom *rom, char *operands)
{
return cbranch_op(rom, 0, 0, operands);
}
static int dispatch_insn(struct code_rom *rom, char *insn, char *operands)
{
static const struct {
const char *name;
int(*dispatch)(struct code_rom *rom, char *operands);
}tbl[] = {
{"ldi", op_ldi},
{"add", op_add},
{"xor", op_xor},
{"and", op_and},
{"or", op_or},
{"nopx", op_nopx},
{"nopy", op_nopy},
{"halt", op_halt},
{"jnz", op_jnz},
{"jz", op_jz},
{"jnc", op_jnc},
{"jc", op_jc},
};
unsigned int i;
for(i = 0; i < sizeof(tbl)/sizeof(*tbl); i++) {
if ( !strcmp(tbl[i].name, insn) )
return (tbl[i].dispatch)(rom, operands);
}
fprintf(stderr, "%s:%u: unknown instruction: %s\n",
rom->fn, rom->line, insn);
return 0;
}
static int add_insn(struct code_rom *rom, char *ptr)
{
char *tok[2] = {NULL, NULL};
int n;
n = easy_explode(ptr, 0, tok, 2);
if ( n < 1 )
return 0;
return dispatch_insn(rom, tok[0], tok[1]);
}
static int assemble(struct code_rom *rom, FILE *f)
{
char buf[1024];
char *ptr, *end;
for(rom->line = 1; fgets(buf, sizeof(buf), f); rom->line++ ) {
end = strchr(buf, '\r');
if ( NULL == end )
end = strchr(buf, '\n');
if ( NULL == end ) {
fprintf(stderr, "%s:%u: Line too long\n",
rom->fn, rom->line);
return 0;
}
*end = '\0';
/* strip trailing whitespace */
for(end = end - 1; isspace(*end); end--)
*end= '\0';
/* strip leading whitespace */
for(ptr = buf; isspace(*ptr); ptr++)
/* nothing */;
if ( *ptr == '\0' || *ptr == ';' ) {
/* empty line or comment */
continue;
}else if ( *end == ':' ) {
*end = '\0';
if ( !add_label(rom, ptr) )
return 0;
}else{
if ( !add_insn(rom, ptr) )
return 0;
}
}
return 1;
}
static int write_rom(struct code_rom *rom, FILE *f)
{
size_t sz;
sz = fwrite(rom->code, sizeof(*rom->code), rom->addr, f);
if ( sz != rom->addr || ferror(f) ) {
fprintf(stderr, "%s: %s\n",
rom->fn, strerror(errno));
return 0;
}
printf("%lu insns successfuly written\n", (unsigned long)sz);
return 1;
}
int main(int argc, char **argv)
{
struct code_rom rom;
FILE *in, *out;
memset(&rom, 0, sizeof(rom));
if ( argc != 3 ) {
fprintf(stderr, "%s: Usage: %s [in] [out]\n", argv[0], argv[0]);
return EXIT_FAILURE;
}
if (strcmp(argv[1], "-")) {
rom.fn = argv[1];
in = fopen(argv[1], "r");
}else{
in = stdin;
rom.fn = "(stdin)";
}
if ( NULL == in ) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], rom.fn, strerror(errno));
return EXIT_FAILURE;
}
if ( !assemble(&rom, in) )
return EXIT_FAILURE;
if (strcmp(argv[2], "-")) {
rom.fn = argv[2];
out = fopen(argv[2], "w");
}else{
out = stdout;
rom.fn = "(stdout)";
}
if ( !write_rom(&rom, out) )
return EXIT_FAILURE;
fclose(out);
return EXIT_SUCCESS;
}