uxn/src/uxnasm.c

385 lines
9.3 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.
*/
2021-04-20 21:30:26 +00:00
#define TRIM 0x0100
2021-03-28 18:06:36 +00:00
2021-01-29 20:14:37 +00:00
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-01-29 20:14:37 +00:00
2021-03-14 20:30:17 +00:00
typedef struct {
2021-06-18 00:41:55 +00:00
char name[64], items[128][64];
2021-03-14 21:26:17 +00:00
Uint8 len, refs;
2021-03-14 20:30:17 +00:00
} Macro;
2021-03-14 04:51:43 +00:00
typedef struct {
2021-04-20 17:31:50 +00:00
char name[64];
2021-04-20 04:00:14 +00:00
Uint8 refs;
Uint16 addr;
2021-01-31 05:31:49 +00:00
} Label;
2021-03-13 22:55:29 +00:00
typedef struct {
2021-04-23 14:42:07 +00:00
Uint8 data[256 * 256], mlen;
Uint16 ptr, length, llen;
Label labels[512];
2021-03-14 20:30:17 +00:00
Macro macros[256];
2021-03-13 22:55:29 +00:00
} Program;
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-04-19 16:51:52 +00:00
"BRK", "LIT", "NOP", "POP", "DUP", "SWP", "OVR", "ROT",
2021-05-11 18:12:07 +00:00
"EQU", "NEQ", "GTH", "LTH", "JMP", "JCN", "JSR", "STH",
2021-05-11 18:14:52 +00:00
"LDZ", "STZ", "LDR", "STR", "LDA", "STA", "DEI", "DEO",
2021-03-21 17:24:44 +00:00
"ADD", "SUB", "MUL", "DIV", "AND", "ORA", "EOR", "SFT"
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 */
2021-06-08 15:09:03 +00:00
int sihx(char *s) { int i = 0; char c; while((c = s[i++])) if(!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f')) return 0; return i > 1; } /* string is hexadecimal */
2021-04-22 21:29:48 +00:00
int ssin(char *s, char *ss) { int a = 0, b = 0; while(s[a]) { if(s[a] == ss[b]) { if(!ss[b + 1]) return a - b; b++; } else b = 0; a++; } return -1; } /* string substring index */
2021-04-20 18:32:26 +00:00
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'); return n; } /* string to num */
2021-04-22 21:29:48 +00:00
int slen(char *s) { int i = 0; while(s[i] && s[++i]) ; return i; } /* string length */
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-04-22 21:29:48 +00:00
char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* string cat */
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-04-19 16:51:52 +00:00
if(lit) pushbyte(0x01, 0);
2021-02-04 20:22:08 +00:00
p.data[p.ptr++] = b;
p.length = p.ptr;
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
{
2021-04-19 16:51:52 +00:00
if(lit) pushbyte(0x21, 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
pushword(char *s)
{
int i = 0;
char c;
2021-03-13 22:55:29 +00:00
while((c = s[i++])) pushbyte(c, 0);
}
2021-03-14 20:30:17 +00:00
Macro *
findmacro(char *name)
{
int i;
for(i = 0; i < p.mlen; ++i)
if(scmp(p.macros[i].name, name, 64))
return &p.macros[i];
return NULL;
}
2021-01-31 05:31:49 +00:00
Label *
2021-04-20 04:00:14 +00:00
findlabel(char *name)
2021-01-31 05:31:49 +00:00
{
2021-04-20 04:00:14 +00:00
int i;
2021-03-13 22:55:29 +00:00
for(i = 0; i < p.llen; ++i)
if(scmp(p.labels[i].name, name, 64))
return &p.labels[i];
2021-01-31 05:31:49 +00:00
return NULL;
}
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-03-27 18:04:05 +00:00
if(s[3 + m] == '2')
i |= (1 << 5); /* mode: short */
else if(s[3 + m] == 'r')
i |= (1 << 6); /* mode: return */
2021-05-12 17:42:24 +00:00
else if(s[3 + m] == 'k')
i |= (1 << 7); /* mode: keep */
2021-03-27 18:04:05 +00:00
else
return 0; /* failed to match */
2021-02-06 18:39:13 +00:00
m++;
}
return i;
}
return 0;
}
2021-03-11 23:47:28 +00:00
char *
sublabel(char *src, char *scope, char *name)
{
2021-04-25 00:12:25 +00:00
return scat(scat(scpy(scope, src, 64), "/"), name);
2021-03-11 23:47:28 +00:00
}
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-03-14 20:30:17 +00:00
int
makemacro(char *name, FILE *f)
{
Macro *m;
char word[64];
if(findmacro(name))
return error("Macro duplicate", name);
if(sihx(name) && slen(name) % 2 == 0)
return error("Macro name is hex number", name);
2021-06-08 15:09:03 +00:00
if(findopcode(name) || !slen(name))
2021-03-14 20:30:17 +00:00
return error("Macro name is invalid", name);
m = &p.macros[p.mlen++];
scpy(name, m->name, 64);
while(fscanf(f, "%s", word)) {
if(word[0] == '{') continue;
if(word[0] == '}') break;
2021-04-20 17:31:50 +00:00
if(m->len > 64)
2021-04-15 17:19:59 +00:00
return error("Macro too large", name);
2021-04-20 17:31:50 +00:00
if(slen(word) >= 64)
2021-04-15 17:19:59 +00:00
return error("Word too long", name);
2021-03-14 20:30:17 +00:00
scpy(word, m->items[m->len++], 64);
}
2021-06-18 03:53:27 +00:00
printf("New macro #%d: %s, %d items\n", p.mlen, m->name, m->len);
2021-03-14 20:30:17 +00:00
return 1;
}
2021-02-23 06:15:02 +00:00
int
2021-03-14 01:34:08 +00:00
makelabel(char *name, Uint16 addr)
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-06-08 15:09:03 +00:00
if(findopcode(name) || !slen(name))
2021-02-13 16:38:23 +00:00
return error("Label name is invalid", name);
2021-03-13 22:55:29 +00:00
l = &p.labels[p.llen++];
2021-02-04 21:49:03 +00:00
l->addr = addr;
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-06-18 03:53:27 +00:00
printf("New label #%d: %s, at 0x%04x\n", p.llen, l->name, l->addr);
2021-02-04 21:49:03 +00:00
return 1;
}
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-03-14 20:30:17 +00:00
int
walktoken(char *w)
{
Macro *m;
if(findopcode(w) || scmp(w, "BRK", 4))
return 1;
switch(w[0]) {
2021-04-20 17:31:50 +00:00
case '[': return 0;
case ']': return 0;
case '\'': return 1;
2021-04-20 04:33:52 +00:00
case '.': return 2; /* zero-page: LIT addr-lb */
2021-04-20 17:31:50 +00:00
case ',': return 2; /* relative: LIT addr-rel */
2021-04-20 20:05:34 +00:00
case ':': return 2; /* absolute: addr-hb addr-lb */
2021-04-20 17:31:50 +00:00
case ';': return 3; /* absolute: LIT addr-hb addr-lb */
case '$': return shex(w + 1);
case '#': return slen(w + 1) == 4 ? 3 : 2;
case '"': return slen(w + 1);
2021-03-14 20:30:17 +00:00
}
2021-03-14 20:47:09 +00:00
if((m = findmacro(w))) {
int i, res = 0;
for(i = 0; i < m->len; ++i)
2021-04-25 00:12:25 +00:00
res += walktoken(m->items[i]);
2021-03-14 20:47:09 +00:00
return res;
}
2021-03-14 20:30:17 +00:00
return error("Unknown label in first pass", w);
}
int
parsetoken(char *w)
{
Label *l;
Macro *m;
2021-04-20 04:33:52 +00:00
if(w[0] == '.' && (l = findlabel(w + 1))) { /* zero-page */
if(l->addr > 0xff)
return error("Address is not in zero page", w);
2021-04-20 04:33:52 +00:00
pushbyte(l->addr, 1);
2021-04-19 16:29:39 +00:00
return ++l->refs;
2021-04-25 04:18:15 +00:00
} else if(w[0] == ',' && (l = findlabel(w + 1))) { /* relative */
2021-04-20 04:33:52 +00:00
int off = l->addr - p.ptr - 3;
if(off < -126 || off > 126)
return error("Address is too far", w);
pushbyte((Sint8)off, 1);
return ++l->refs;
2021-04-25 04:18:15 +00:00
} else if(w[0] == ':' && (l = findlabel(w + 1))) { /* raw */
2021-04-20 20:05:34 +00:00
pushshort(l->addr, 0);
return ++l->refs;
2021-04-20 04:33:52 +00:00
} else if(w[0] == ';' && (l = findlabel(w + 1))) { /* absolute */
2021-04-20 04:00:14 +00:00
pushshort(l->addr, 1);
2021-04-19 16:29:39 +00:00
return ++l->refs;
2021-04-25 04:18:15 +00:00
} else if(findopcode(w) || scmp(w, "BRK", 4)) { /* opcode */
2021-04-20 17:31:50 +00:00
pushbyte(findopcode(w), 0);
2021-03-14 20:30:17 +00:00
return 1;
2021-04-25 04:18:15 +00:00
} else if(w[0] == '"') { /* string */
pushword(w + 1);
return 1;
2021-04-25 04:18:15 +00:00
} else if(w[0] == '\'') { /* char */
pushbyte((Uint8)w[1], 0);
return 1;
2021-04-25 04:18:15 +00:00
} else if(w[0] == '#') { /* immediate */
2021-04-13 04:16:31 +00:00
if(slen(w + 1) == 1)
pushbyte((Uint8)w[1], 1);
if(sihx(w + 1) && slen(w + 1) == 2)
2021-03-14 20:30:17 +00:00
pushbyte(shex(w + 1), 1);
2021-04-13 04:16:31 +00:00
else if(sihx(w + 1) && slen(w + 1) == 4)
2021-03-14 20:30:17 +00:00
pushshort(shex(w + 1), 1);
else
2021-06-18 00:41:55 +00:00
return error("Invalid hexadecimal literal", w);
2021-03-14 20:30:17 +00:00
return 1;
2021-04-25 04:18:15 +00:00
} else if(sihx(w)) { /* raw */
2021-04-20 17:31:50 +00:00
if(slen(w) == 2)
pushbyte(shex(w), 0);
else if(slen(w) == 4)
pushshort(shex(w), 0);
2021-04-25 04:18:15 +00:00
else
2021-06-08 15:20:00 +00:00
return error("Invalid hexadecimal value", w);
2021-04-20 17:31:50 +00:00
return 1;
2021-04-25 00:12:25 +00:00
} else if((m = findmacro(w))) {
int i;
for(i = 0; i < m->len; ++i)
if(!parsetoken(m->items[i]))
2021-06-18 00:41:55 +00:00
return error("Invalid macro", m->name);
2021-04-25 00:12:25 +00:00
return ++m->refs;
2021-03-14 20:30:17 +00:00
}
2021-06-18 00:41:55 +00:00
return error("Invalid token", w);
2021-03-14 20:30:17 +00:00
}
2021-02-04 05:53:56 +00:00
int
2021-01-29 20:14:37 +00:00
pass1(FILE *f)
{
2021-04-20 17:31:50 +00:00
int ccmnt = 0;
2021-02-04 23:23:04 +00:00
Uint16 addr = 0;
2021-03-11 23:47:28 +00:00
char w[64], scope[64], subw[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;
2021-06-08 14:37:12 +00:00
if(w[0] == '|') {
if(!sihx(w + 1))
return error("Invalid padding", w);
2021-04-20 17:31:50 +00:00
addr = shex(w + 1);
2021-06-08 14:37:12 +00:00
} else if(w[0] == '%') {
2021-03-14 20:30:17 +00:00
if(!makemacro(w + 1, f))
2021-06-08 14:37:12 +00:00
return error("Invalid macro", w);
} else if(w[0] == '@') {
2021-03-14 01:34:08 +00:00
if(!makelabel(w + 1, addr))
2021-06-08 14:37:12 +00:00
return error("Invalid label", w);
2021-03-11 23:47:28 +00:00
scpy(w + 1, scope, 64);
2021-04-20 17:31:50 +00:00
} else if(w[0] == '&') {
if(!makelabel(sublabel(subw, scope, w + 1), addr))
2021-06-08 15:20:00 +00:00
return error("Invalid sublabel", w);
2021-04-20 17:31:50 +00:00
} else if(sihx(w))
addr += slen(w) / 2;
2021-04-20 04:33:52 +00:00
else
2021-03-14 20:30:17 +00:00
addr += walktoken(w);
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)
{
2021-04-25 00:12:25 +00:00
int ccmnt = 0, cmacr = 0;
2021-03-11 23:47:28 +00:00
char w[64], scope[64], subw[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-03-14 20:30:17 +00:00
if(w[0] == '%') continue;
2021-04-20 00:23:30 +00:00
if(w[0] == '&') continue;
2021-04-20 17:31:50 +00:00
if(w[0] == '[') continue;
if(w[0] == ']') continue;
2021-03-13 22:55:29 +00:00
if(skipblock(w, &ccmnt, '(', ')')) continue;
2021-04-25 00:12:25 +00:00
if(skipblock(w, &cmacr, '{', '}')) continue;
2021-03-14 20:30:17 +00:00
if(w[0] == '|') {
if(p.length && shex(w + 1) < p.ptr)
return error("Memory Overwrite", w);
2021-03-14 20:30:17 +00:00
p.ptr = shex(w + 1);
continue;
2021-04-20 17:31:50 +00:00
} else if(w[0] == '$') {
2021-04-20 04:33:52 +00:00
p.ptr += shex(w + 1);
continue;
2021-04-20 17:31:50 +00:00
} else if(w[0] == '@') {
2021-03-11 23:47:28 +00:00
scpy(w + 1, scope, 64);
continue;
}
2021-04-20 04:00:14 +00:00
if(w[1] == '&')
2021-03-14 20:30:17 +00:00
scpy(sublabel(subw, scope, w + 2), w + 1, 64);
2021-04-20 17:31:50 +00:00
if(!parsetoken(w))
2021-03-14 20:30:17 +00:00
return error("Unknown label in second pass", w);
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
2021-03-14 01:34:08 +00:00
cleanup(char *filename)
2021-03-01 17:16:40 +00:00
{
int i;
printf("Assembled %s(%d bytes), %d labels, %d macros.\n\n", filename, (p.length - TRIM), p.llen, p.mlen);
2021-03-13 22:55:29 +00:00
for(i = 0; i < p.llen; ++i)
2021-04-23 02:57:47 +00:00
if(p.labels[i].name[0] >= 'A' && p.labels[i].name[0] <= 'Z')
continue; /* Ignore capitalized labels(devices) */
2021-05-04 17:57:57 +00:00
else if(!p.labels[i].refs)
2021-03-13 22:55:29 +00:00
printf("--- Unused label: %s\n", p.labels[i].name);
2021-03-14 21:26:17 +00:00
for(i = 0; i < p.mlen; ++i)
if(!p.macros[i].refs)
printf("--- Unused macro: %s\n", p.macros[i].name);
2021-03-01 17:16:40 +00:00
}
2021-01-29 20:14:37 +00:00
int
main(int argc, char *argv[])
{
FILE *f;
2021-04-19 16:29:39 +00:00
if(argc < 3)
return !error("Input", "Missing");
if(!(f = fopen(argv[1], "r")))
return !error("Open", "Failed");
if(!pass1(f) || !pass2(f))
return !error("Assembly", "Failed");
fwrite(p.data + TRIM, p.length - TRIM, 1, fopen(argv[2], "wb"));
2021-01-29 21:59:16 +00:00
fclose(f);
2021-03-14 01:34:08 +00:00
cleanup(argv[2]);
2021-01-29 20:14:37 +00:00
return 0;
}