forked from swetland/dcpu16
-
Notifications
You must be signed in to change notification settings - Fork 9
/
assembler.c
378 lines (348 loc) · 8.08 KB
/
assembler.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
/*
* Copyright (c) 2012, Brian Swetland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/* A DCPU-16 Assembler */
/* DCPU-16 Spec is Copyright 2012 Mojang */
/* http://0x10c.com/doc/dcpu-16.txt */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
typedef uint16_t u16;
typedef uint32_t u32;
extern u16 *disassemble(u16 *pc, char *out);
static u16 image[65536] = { 0, };
static u16 PC = 0;
static FILE *fin;
static const char *filename = "";
static int linenumber = 0;
static char linebuffer[128] = { 0, };
static char *lineptr = linebuffer;
static int token;
static char tstring[128];
static u16 tnumber;
void die(const char *fmt, ...) {
va_list ap;
fprintf(stderr,"%s:%d: ", filename, linenumber);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr,"\n");
if (linebuffer[0])
fprintf(stderr,"%s:%d: >> %s <<\n", filename, linenumber, linebuffer);
exit(1);
}
struct fixup {
struct fixup *next;
struct label *label;
u16 pc;
};
struct label {
struct label *next;
u16 pc;
u16 defined;
char name[1];
};
struct label *labels = 0;
struct fixup *fixups = 0;
struct label *mklabel(const char *name, u16 pc, u16 def) {
struct label *l;
for (l = labels; l; l = l->next) {
if (!strcasecmp(name, l->name)) {
if (def) {
if (l->defined)
die("cannot redefine label: %s", name);
l->defined = def;
l->pc = pc;
}
return l;
}
}
l = malloc(sizeof(*l) + strlen(name));
l->defined = def;
l->pc = pc;
strcpy(l->name, name);
l->next = labels;
labels = l;
return l;
}
void use_label(const char *name, u16 pc) {
struct label *l = mklabel(name, 0, 0);
if (l->defined) {
image[pc] = l->pc;
} else {
struct fixup *f = malloc(sizeof(*f));
f->next = fixups;
f->pc = pc;
f->label = l;
fixups = f;
}
}
void set_label(const char *name, u16 pc) {
mklabel(name, pc, 1);
}
void resolve_fixups(void) {
struct fixup *f;
for (f = fixups; f; f = f->next) {
if (f->label->defined) {
image[f->pc] = f->label->pc;
} else {
die("undefined reference to '%s' at 0x%04x", f->label->name, f->pc);
}
}
}
enum tokens {
tA, tB, tC, tX, tY, tZ, tI, tJ,
tXXX, tSET, tADD, tSUB, tMUL, tDIV, tMOD, tSHL,
tSHR, tAND, tBOR, tXOR, tIFE, tIFN, tIFG, tIFB,
tJSR,
tPOP, tPEEK, tPUSH, tSP, tPC, tO,
tWORD,
tCOMMA, tOBRACK, tCBRACK, tCOLON, tPLUS,
tSTRING, tNUMBER, tEOF,
};
static const char *tnames[] = {
"A", "B", "C", "X", "Y", "Z", "I", "J",
"XXX", "SET", "ADD", "SUB", "MUL", "DIV", "MOD", "SHL",
"SHR", "AND", "BOR", "XOR", "IFE", "IFN", "IFG", "IFB",
"JSR",
"POP", "PEEK", "PUSH", "SP", "PC", "O",
"WORD",
",", "[", "]", ":", "+",
"<STRING>", "<NUMBER>", "<EOF>",
};
#define LASTKEYWORD tWORD
int _next(void) {
char c;
nextline:
if (!*lineptr) {
if (feof(fin)) return tEOF;
if (fgets(linebuffer, 128, fin) == 0) return tEOF;
lineptr = linebuffer;
linenumber++;
}
while (*lineptr <= ' ') {
if (*lineptr == 0) goto nextline;
lineptr++;
}
switch ((c = *lineptr++)) {
case ',': return tCOMMA;
case '+': return tPLUS;
case '[': return tOBRACK;
case ']': return tCBRACK;
case ':': return tCOLON;
case '/': case ';': *lineptr = 0; goto nextline;
default:
if (isdigit(c) || ((c == '-') && isdigit(*lineptr))) {
tnumber = strtoul(lineptr-1, &lineptr, 0);
return tNUMBER;
}
if (isalpha(c)) {
int n;
char *x = tstring;
lineptr--;
while (isalnum(*lineptr))
*x++ = tolower(*lineptr++);
*x = 0;
for (n = 0; n <= LASTKEYWORD; n++)
if (!strcasecmp(tnames[n], tstring))
return n;
return tSTRING;
}
die("illegal character '%c'", c);
return tEOF;
}
}
int next(void) {
token = _next();
//fprintf(stderr,"%3d %s\n", token, tnames[token]);
return token;
}
void expect(int t) {
if (next() != t)
die("expecting %s, found %s", tnames[t], tnames[token]);
}
void assemble_imm_or_label(void) {
next();
if (token == tNUMBER) {
image[PC++] = tnumber;
} else if (token == tSTRING) {
image[PC] = 0;
use_label(tstring, PC++);
} else {
die("expected number or label");
}
}
int assemble_operand(void) {
u16 n;
next();
switch (token) {
case tA: case tB: case tC: case tX:
case tY: case tZ: case tI: case tJ:
return token & 7;
case tPOP: return 0x18;
case tPEEK: return 0x19;
case tPUSH: return 0x1a;
case tSP: return 0x1b;
case tPC: return 0x1c;
case tO: return 0x1d;
case tNUMBER:
if (tnumber < 0x20)
return tnumber + 0x20;
image[PC++] = tnumber;
return 0x1f;
case tSTRING:
image[PC] = 0;
use_label(tstring, PC++);
return 0x1f;
default:
if (token != tOBRACK)
die("expected [");
}
next();
switch (token) {
case tA: case tB: case tC: case tX:
case tY: case tZ: case tI: case tJ:
n = token & 7;
expect(tCBRACK);
return n;
case tSTRING:
use_label(tstring, PC);
case tNUMBER:
image[PC++] = tnumber;
next();
if (token == tCBRACK) {
return 0x1e;
} else if ((token == tCOMMA) || (token == tPLUS)) {
next();
if ((token >= tA) && (token <= tJ)) {
n = 0x10 | (token & 7);
} else {
die("invalid register");
}
expect(tCBRACK);
return n;
} else {
die("invalid operand");
}
default:
die("invalid operand");
}
return 0;
}
void assemble_binop(int op) {
int pc = PC++;
int a, b;
a = assemble_operand();
expect(tCOMMA);
b = assemble_operand();
image[pc] = op | (a << 4) | (b << 10);
}
void assemble(const char *fn) {
u16 pc, n;
fin = fopen(fn, "r");
filename = fn;
linenumber = 0;
if (!fin) die("cannot read file");
for (;;) {
next();
switch (token) {
case tEOF:
goto done;
case tCOLON:
expect(tSTRING);
set_label(tstring, PC);
continue;
case tWORD:
assemble_imm_or_label();
continue;
case tSET: case tADD: case tSUB: case tMUL:
case tDIV: case tMOD: case tSHL: case tSHR:
case tAND: case tBOR: case tXOR: case tIFE:
case tIFN: case tIFG: case tIFB:
assemble_binop(token - tXXX);
continue;
case tJSR:
pc = PC++;
n = assemble_operand();
image[pc] = (n << 10) | 0x0010;
continue;
default:
die("unexpected: %s", tnames[token]);
}
}
done:
fclose(fin);
}
void emit(const char *fn) {
FILE *fp;
u16 *pc = image;
u16 *end = image + PC;
u16 *dis = pc;
filename = fn;
linenumber = 0;
fp = fopen(fn, "w");
if (!fp) die("cannot write file");
while (pc < end) {
if (pc == dis) {
char out[128];
dis = disassemble(pc, out);
fprintf(fp, "%04x\t%04x:\t%s\n", *pc, (unsigned)(pc-image), out);
} else {
fprintf(fp, "%04x\n", *pc);
}
pc++;
}
fclose(fp);
}
int main(int argc, char **argv) {
const char *outfn = "out.hex";
while (argc > 1) {
argc--;
argv++;
if (argv[0][0] == '-') {
if (!strcmp(argv[0],"-o")) {
if (argc > 1) {
outfn = argv[1];
argc--;
argv++;
continue;
}
}
die("unknown option: %s", argv[0]);
}
assemble(argv[0]);
}
if (PC != 0) {
linebuffer[0] = 0;
resolve_fixups();
emit(outfn);
}
return 0;
}