uxn/assembler.c

402 lines
10 KiB
C
Raw Normal View History

2021-01-29 20:14:37 +00:00
#include <stdio.h>
/*
Copyright (c) 2021 Devine Lu Linvega
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/
typedef unsigned char Uint8;
2021-02-13 00:18:52 +00:00
typedef signed char Sint8;
2021-02-04 20:22:08 +00:00
typedef unsigned short Uint16;
2021-02-13 00:18:52 +00:00
typedef signed short Sint16;
2021-01-29 20:14:37 +00:00
2021-01-29 21:59:16 +00:00
typedef struct {
2021-03-01 17:16:40 +00:00
Uint8 data[256 * 256];
Uint16 ptr;
2021-01-29 21:59:16 +00:00
} Program;
2021-02-23 06:15:02 +00:00
typedef struct {
2021-03-01 17:16:40 +00:00
Uint8 len, length[16], size, refs;
2021-02-23 19:10:49 +00:00
char name[64], params[16][64];
2021-02-23 06:15:02 +00:00
} Macro;
2021-01-31 05:31:49 +00:00
typedef struct {
2021-03-01 17:16:40 +00:00
Uint8 len, offset, refs;
2021-02-04 20:22:08 +00:00
Uint16 addr;
char name[64];
2021-02-23 19:10:49 +00:00
Macro *macro;
2021-01-31 05:31:49 +00:00
} Label;
2021-02-23 06:15:02 +00:00
int macroslen;
Macro macros[256];
2021-01-31 05:31:49 +00:00
int labelslen;
Label labels[256];
2021-02-23 06:15:02 +00:00
2021-02-07 18:21:41 +00:00
Program p;
2021-01-30 22:25:48 +00:00
2021-02-02 04:21:27 +00:00
/* clang-format off */
2021-02-06 18:39:13 +00:00
char ops[][4] = {
2021-03-02 18:14:55 +00:00
"BRK", "NOP", "LIT", "JMP", "JSR", "RTS", "LDR", "STR",
"---", "---", "---", "---", "AND", "XOR", "ROL", "ROR",
2021-02-27 04:00:01 +00:00
"POP", "DUP", "SWP", "OVR", "ROT", "---", "WSR", "RSW",
2021-02-07 18:21:41 +00:00
"ADD", "SUB", "MUL", "DIV", "EQU", "NEQ", "GTH", "LTH"
2021-02-04 20:22:08 +00:00
};
2021-02-02 04:21:27 +00:00
2021-02-23 19:10:49 +00:00
int scin(char *s, char c) { int i = 0; while(s[i]) if(s[i++] == c) return i - 1; return -1; } /* string char index */
int scmp(char *a, char *b, int len) { int i = 0; while(a[i] == b[i] && i < len) if(!a[i++]) return 1; return 0; } /* string compare */
int slen(char *s) { int i = 0; while(s[i] && s[++i]) ; return i; } /* string length */
int sihx(char *s) { int i = 0; char c; while((c = s[i++])) if(!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F')) return 0; return 1; } /* string is hexadecimal */
int shex(char *s) { int n = 0, i = 0; char c; while((c = s[i++])) if(c >= '0' && c <= '9') n = n * 16 + (c - '0'); else if(c >= 'A' && c <= 'F') n = n * 16 + 10 + (c - 'A'); else if(c >= 'a' && c <= 'f') n = n * 16 + 10 + (c - 'a'); return n; } /* string to num */
2021-02-07 18:21:41 +00:00
char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } /* string copy */
2021-01-29 21:59:16 +00:00
#pragma mark - Helpers
2021-02-07 18:21:41 +00:00
/* clang-format on */
2021-01-30 22:25:48 +00:00
2021-02-04 20:22:08 +00:00
#pragma mark - I/O
2021-02-02 04:21:27 +00:00
void
2021-02-04 20:22:08 +00:00
pushbyte(Uint8 b, int lit)
2021-02-02 04:21:27 +00:00
{
2021-02-10 04:35:31 +00:00
if(lit)
pushbyte(0x02, 0);
2021-02-04 20:22:08 +00:00
p.data[p.ptr++] = b;
2021-02-02 04:21:27 +00:00
}
void
2021-02-04 20:22:08 +00:00
pushshort(Uint16 s, int lit)
2021-02-02 04:21:27 +00:00
{
if(lit)
pushbyte(0x22, 0);
2021-02-04 20:22:08 +00:00
pushbyte((s >> 8) & 0xff, 0);
pushbyte(s & 0xff, 0);
2021-02-02 04:21:27 +00:00
}
void
pushtext(char *s, int lit)
{
int i = 0;
char c;
if(lit)
pushbyte(0x22, 0);
while((c = s[i++]))
pushbyte(c, 0);
}
2021-02-23 06:15:02 +00:00
Macro *
findmacro(char *s)
{
int i;
for(i = 0; i < macroslen; ++i)
2021-02-23 19:10:49 +00:00
if(scmp(macros[i].name, s, 64))
2021-02-23 06:15:02 +00:00
return &macros[i];
return NULL;
}
2021-01-31 05:31:49 +00:00
Label *
2021-02-23 20:04:59 +00:00
findlabel(char *s)
2021-01-31 05:31:49 +00:00
{
2021-02-23 20:04:59 +00:00
int i, rng = scin(s, '.');
char name[64];
scpy(s, name, rng > 0 ? rng + 1 : 64);
2021-01-31 05:31:49 +00:00
for(i = 0; i < labelslen; ++i)
2021-02-23 20:04:59 +00:00
if(scmp(labels[i].name, name, 64))
2021-01-31 05:31:49 +00:00
return &labels[i];
return NULL;
}
2021-02-23 20:04:59 +00:00
Uint16
findlabeladdr(char *s)
2021-02-23 19:10:49 +00:00
{
2021-02-23 20:04:59 +00:00
int i, o = 0;
char *param;
Label *l = findlabel(s);
if(scin(s, '.') < 1)
return l->addr;
param = s + scin(s, '.') + 1;
2021-02-23 19:10:49 +00:00
for(i = 0; i < l->macro->len; ++i) {
2021-02-23 20:04:59 +00:00
if(scmp(l->macro->params[i], param, 64))
return l->addr + o;
2021-02-23 19:10:49 +00:00
o += l->macro->length[i];
}
2021-03-08 18:55:57 +00:00
printf("!!! Warning %s.%s[%s]\n", l->name, param, l->macro->name);
2021-02-23 20:04:59 +00:00
return 0;
2021-02-23 19:10:49 +00:00
}
2021-02-23 20:04:59 +00:00
Uint8
findlabellen(char *s)
2021-02-23 19:10:49 +00:00
{
2021-02-23 20:04:59 +00:00
int i;
char *param;
Label *l = findlabel(s);
if(scin(s, '.') < 1)
return l->len;
param = s + scin(s, '.') + 1;
for(i = 0; i < l->macro->len; ++i)
if(scmp(l->macro->params[i], param, 64))
return l->macro->length[i];
2021-03-08 18:55:57 +00:00
printf("!!! Warning %s.%s[%s]\n", l->name, param, l->macro->name);
2021-02-23 20:04:59 +00:00
return 0;
2021-02-23 19:10:49 +00:00
}
2021-02-06 18:39:13 +00:00
Uint8
2021-02-13 21:21:05 +00:00
findopcode(char *s)
2021-02-06 18:39:13 +00:00
{
int i;
2021-02-07 18:21:41 +00:00
for(i = 0; i < 0x20; ++i) {
2021-02-06 18:39:13 +00:00
int m = 0;
2021-02-07 18:21:41 +00:00
char *o = ops[i];
if(o[0] != s[0] || o[1] != s[1] || o[2] != s[2])
continue;
2021-02-06 18:39:13 +00:00
while(s[3 + m]) {
2021-02-13 16:38:23 +00:00
if(s[3 + m] == '2') i |= (1 << 5); /* mode: short */
if(s[3 + m] == 'S') i |= (1 << 6); /* mode: signed */
2021-02-08 04:49:00 +00:00
if(s[3 + m] == '?') i |= (1 << 7); /* mode: conditional */
2021-02-06 18:39:13 +00:00
m++;
}
return i;
}
return 0;
}
2021-02-04 20:22:08 +00:00
#pragma mark - Parser
2021-01-31 05:31:49 +00:00
2021-01-30 22:25:48 +00:00
int
2021-02-04 20:22:08 +00:00
error(char *name, char *id)
2021-01-30 22:25:48 +00:00
{
2021-02-06 04:18:30 +00:00
printf("Error: %s[%s]\n", name, id);
2021-02-04 21:49:03 +00:00
return 0;
}
2021-02-23 06:15:02 +00:00
int
makemacro(char *name, FILE *f)
{
Uint8 mode = 0;
Macro *m;
char wv[64];
if(findmacro(name))
return error("Macro duplicate", name);
2021-03-11 20:19:59 +00:00
if(sihx(name) && slen(name) % 2 == 0)
2021-02-23 06:15:02 +00:00
return error("Macro name is hex number", name);
if(findopcode(name))
return error("Macro name is invalid", name);
m = &macros[macroslen++];
2021-02-23 19:10:49 +00:00
scpy(name, m->name, 64);
2021-02-23 06:15:02 +00:00
while(fscanf(f, "%s", wv)) {
if(wv[0] == '{')
continue;
if(wv[0] == '}')
break;
if(mode == 0)
2021-02-23 19:10:49 +00:00
scpy(wv, m->params[m->len], 64);
2021-02-23 06:15:02 +00:00
else {
2021-02-23 19:10:49 +00:00
m->length[m->len] = shex(wv);
m->size += m->length[m->len];
2021-02-23 06:15:02 +00:00
m->len++;
}
mode = !mode;
}
2021-02-23 19:10:49 +00:00
printf("New macro: %s[%d:%d]\n", name, m->len, m->size);
2021-02-23 06:15:02 +00:00
return 1;
}
2021-02-04 21:49:03 +00:00
int
2021-02-23 19:10:49 +00:00
makelabel(char *name, Uint16 addr, Uint8 len, Macro *m)
2021-02-04 21:49:03 +00:00
{
Label *l;
2021-02-07 18:21:41 +00:00
if(findlabel(name))
return error("Label duplicate", name);
2021-03-11 20:19:59 +00:00
if(sihx(name) && slen(name) % 2 == 0)
2021-02-13 00:18:52 +00:00
return error("Label name is hex number", name);
2021-02-13 21:21:05 +00:00
if(findopcode(name))
2021-02-13 16:38:23 +00:00
return error("Label name is invalid", name);
2021-02-04 21:49:03 +00:00
l = &labels[labelslen++];
l->addr = addr;
2021-02-15 01:00:17 +00:00
l->len = len;
2021-03-01 17:16:40 +00:00
l->refs = 0;
2021-02-07 18:21:41 +00:00
scpy(name, l->name, 64);
2021-02-23 19:10:49 +00:00
if(m)
l->macro = m;
2021-03-03 05:47:03 +00:00
printf("New label: %s, at 0x%04x[%d]\n", l->name, l->addr, l->len);
2021-02-04 21:49:03 +00:00
return 1;
}
2021-02-05 18:51:45 +00:00
int
makeconst(char *id, FILE *f)
{
char wv[64];
fscanf(f, "%s", wv);
2021-02-23 19:10:49 +00:00
return makelabel(id, shex(wv), 1, 0);
2021-02-05 18:51:45 +00:00
}
2021-02-11 00:41:16 +00:00
int
makevariable(char *id, Uint16 *addr, FILE *f)
{
char wv[64];
2021-02-26 18:16:35 +00:00
Uint16 origin;
Uint8 len;
2021-02-23 19:10:49 +00:00
Macro *m = NULL;
2021-02-11 00:41:16 +00:00
fscanf(f, "%s", wv);
origin = *addr;
2021-03-02 18:14:55 +00:00
if(sihx(wv))
len = shex(wv);
else if((m = findmacro(wv))) {
2021-02-23 19:10:49 +00:00
len = m->size;
2021-03-01 17:16:40 +00:00
m->refs++;
} else
2021-03-02 18:14:55 +00:00
return error("Invalid macro", wv);
2021-02-23 19:10:49 +00:00
*addr += len;
return makelabel(id, origin, len, m);
2021-02-11 00:41:16 +00:00
}
int
2021-02-23 06:15:02 +00:00
skipblock(char *w, int *cap, char a, char b)
2021-02-15 22:04:58 +00:00
{
2021-02-23 06:15:02 +00:00
if(w[0] == b) {
2021-02-15 22:04:58 +00:00
*cap = 0;
return 1;
}
2021-02-23 06:15:02 +00:00
if(w[0] == a) *cap = 1;
2021-02-15 22:04:58 +00:00
if(*cap) return 1;
return 0;
}
2021-02-04 05:53:56 +00:00
int
2021-01-29 20:14:37 +00:00
pass1(FILE *f)
{
int ccmnt = 0, cbits = 0;
2021-02-04 23:23:04 +00:00
Uint16 addr = 0;
2021-02-04 20:22:08 +00:00
char w[64];
2021-02-23 19:10:49 +00:00
printf("Pass 1\n");
2021-02-04 20:22:08 +00:00
while(fscanf(f, "%s", w) == 1) {
2021-02-23 06:15:02 +00:00
if(skipblock(w, &ccmnt, '(', ')')) continue;
if(skipblock(w, &cbits, '[', ']')) {
if(w[0] == '[' || w[0] == ']')
continue;
2021-03-01 17:38:54 +00:00
if(sihx(w))
addr += slen(w) == 4 ? 2 : 1;
else
2021-03-05 20:02:01 +00:00
addr += slen(w);
} else if(w[0] == '@') {
2021-02-23 19:10:49 +00:00
if(!makelabel(w + 1, addr, 0, NULL))
2021-02-11 00:41:16 +00:00
return error("Pass1 failed", w);
} else if(w[0] == ';') {
if(!makevariable(w + 1, &addr, f))
return error("Pass1 failed", w);
2021-02-23 06:15:02 +00:00
} else if(w[0] == '&') {
if(!makemacro(w + 1, f))
return error("Pass1 failed", w);
2021-02-11 00:41:16 +00:00
} else if(w[0] == ':') {
2021-02-05 18:51:45 +00:00
if(!makeconst(w + 1, f))
return error("Pass1 failed", w);
2021-02-23 19:10:49 +00:00
} else if(findopcode(w) || scmp(w, "BRK", 4))
2021-02-04 20:22:08 +00:00
addr += 1;
2021-02-07 18:21:41 +00:00
else {
switch(w[0]) {
2021-02-28 19:17:32 +00:00
case '|':
if(shex(w + 1) < addr)
return error("Memory Overlap", w);
addr = shex(w + 1);
break;
case '=': addr += 4; break; /* STR helper (lit addr-hb addr-lb str) */
case '~': addr += 4; break; /* LDR helper (lit addr-hb addr-lb ldr) */
2021-03-11 22:00:32 +00:00
case '$': addr += 4; break; /* JSR helper (lit addr-hb addr-lb jsr) */
case '/': addr += 4; break; /* JMP helper (lit addr-hb addr-lb jmp) */
case ',': addr += 3; break;
2021-03-11 20:19:59 +00:00
case '.': addr += 2; break;
case '^': addr += 2; break; /* Relative jump: lit addr-offset */
case '+': /* signed positive */
case '-': /* signed negative */
case '#': addr += (slen(w + 1) == 2 ? 2 : 3); break;
2021-02-13 21:21:05 +00:00
default: return error("Unknown label in first pass", w);
2021-02-07 18:21:41 +00:00
}
}
2021-01-30 22:25:48 +00:00
}
rewind(f);
2021-02-04 20:22:08 +00:00
return 1;
2021-01-30 22:25:48 +00:00
}
2021-02-04 20:22:08 +00:00
int
2021-01-30 22:25:48 +00:00
pass2(FILE *f)
{
int ccmnt = 0, cbits = 0, cmacro = 0;
2021-02-04 20:22:08 +00:00
char w[64];
2021-02-23 19:10:49 +00:00
printf("Pass 2\n");
2021-02-04 20:22:08 +00:00
while(fscanf(f, "%s", w) == 1) {
2021-02-01 19:58:47 +00:00
Uint8 op = 0;
2021-01-31 05:31:49 +00:00
Label *l;
2021-02-05 18:51:45 +00:00
if(w[0] == '@') continue;
2021-02-23 06:15:02 +00:00
if(w[0] == '&') continue;
if(skipblock(w, &ccmnt, '(', ')')) continue;
if(skipblock(w, &cmacro, '{', '}')) continue;
2021-02-13 00:18:52 +00:00
/* clang-format off */
if(skipblock(w, &cbits, '[', ']')) {
if(w[0] == '[' || w[0] == ']') { continue; }
2021-03-01 17:38:54 +00:00
if(slen(w) == 4 && sihx(w)) pushshort(shex(w), 0);
else if(slen(w) == 2 && sihx(w)) pushbyte(shex(w), 0);
else pushtext(w, 0);
}
2021-02-15 22:04:58 +00:00
else if(w[0] == '|') p.ptr = shex(w + 1);
2021-02-23 19:10:49 +00:00
else if((op = findopcode(w)) || scmp(w, "BRK", 4)) pushbyte(op, 0);
2021-03-11 20:19:59 +00:00
else if(w[0] == '^' && (l = findlabel(w + 1))) {
int off = l->addr - p.ptr - 3;
if(off < -126 || off > 126){ printf("Address %s is too far(%d).\n", w, off); return 0; }
pushbyte((Sint8)(l->addr - p.ptr - 3), 1); l->refs++;
}
2021-02-13 00:18:52 +00:00
else if(w[0] == ':') fscanf(f, "%s", w);
else if(w[0] == ';') fscanf(f, "%s", w);
2021-03-11 20:19:59 +00:00
else if(w[0] == '.' && (l = findlabel(w + 1))) { pushshort(findlabeladdr(w + 1), 0); l->refs++; }
else if(w[0] == ',' && (l = findlabel(w + 1))) { pushshort(findlabeladdr(w + 1), 1); l->refs++; }
2021-03-11 22:00:32 +00:00
else if(w[0] == '=' && (l = findlabel(w + 1)) && l->len){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode(findlabellen(w + 1) == 2 ? "STR2" : "STR"), 0); l->refs++;}
else if(w[0] == '~' && (l = findlabel(w + 1)) && l->len){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode(findlabellen(w + 1) == 2 ? "LDR2" : "LDR"), 0); l->refs++;}
else if(w[0] == '/' && (l = findlabel(w + 1))){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode("JMP2"), 0); l->refs++;}
else if(w[0] == '$' && (l = findlabel(w + 1))){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode("JSR2"), 0); l->refs++;}
2021-02-14 18:22:42 +00:00
else if(w[0] == '#' && sihx(w + 1) && slen(w + 1) == 2) pushbyte(shex(w + 1), 1);
else if(w[0] == '#' && sihx(w + 1) && slen(w + 1) == 4) pushshort(shex(w + 1), 1);
2021-02-13 00:18:52 +00:00
else if(w[0] == '+' && sihx(w + 1) && slen(w + 1) == 2) pushbyte((Sint8)shex(w + 1), 1);
else if(w[0] == '+' && sihx(w + 1) && slen(w + 1) == 4) pushshort((Sint16)shex(w + 1), 1);
else if(w[0] == '-' && sihx(w + 1) && slen(w + 1) == 2) pushbyte((Sint8)(shex(w + 1) * -1), 1);
else if(w[0] == '-' && sihx(w + 1) && slen(w + 1) == 4) pushshort((Sint16)(shex(w + 1) * -1), 1);
2021-02-28 19:17:32 +00:00
else return error("Unknown label in second pass", w);
2021-02-13 00:18:52 +00:00
/* clang-format on */
2021-01-29 20:14:37 +00:00
}
2021-02-04 20:22:08 +00:00
return 1;
2021-01-29 20:14:37 +00:00
}
2021-03-01 17:16:40 +00:00
void
cleanup(void)
{
int i;
for(i = 0; i < labelslen; ++i)
if(!labels[i].refs)
printf("--- Unused label: %s\n", labels[i].name);
for(i = 0; i < macroslen; ++i)
if(!macros[i].refs)
printf("--- Unused macro: %s\n", macros[i].name);
}
2021-01-29 20:14:37 +00:00
int
main(int argc, char *argv[])
{
FILE *f;
if(argc < 3)
2021-02-04 20:22:08 +00:00
return error("Input", "Missing");
2021-01-29 20:14:37 +00:00
if(!(f = fopen(argv[1], "r")))
2021-02-04 20:22:08 +00:00
return error("Open", "Failed");
if(!pass1(f) || !pass2(f))
return error("Assembly", "Failed");
2021-01-29 21:59:16 +00:00
fwrite(p.data, sizeof(p.data), 1, fopen(argv[2], "wb"));
fclose(f);
2021-02-08 20:16:39 +00:00
printf("Assembled %s.\n\n", argv[2]);
2021-03-01 17:16:40 +00:00
cleanup();
2021-01-29 20:14:37 +00:00
return 0;
}