uxn11/src/uxnasm.c

418 lines
11 KiB
C
Raw Normal View History

2022-07-13 00:27:52 -04:00
#include <stdio.h>
/*
2024-03-28 20:52:50 -04:00
Copyright (c) 2021-2024 Devine Lu Linvega, Andrew Alderwick
2022-07-13 00:27:52 -04:00
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.
*/
2024-03-28 20:52:50 -04:00
/* clang-format off */
#define PAGE 0x0100
2022-07-13 00:27:52 -04:00
typedef unsigned char Uint8;
typedef signed char Sint8;
typedef unsigned short Uint16;
2024-03-28 20:52:50 -04:00
typedef struct { int line; char *path; } Context;
typedef struct { char *name, rune, *data; Uint16 addr, refs, line; } Item;
2022-07-13 00:27:52 -04:00
2024-03-28 20:52:50 -04:00
static int ptr, length;
static char token[0x40], scope[0x40], lambda[0x05];
2024-03-28 23:32:58 -04:00
static char dict[0x8000], *dictnext = dict;
2024-03-28 20:52:50 -04:00
static Uint8 data[0x10000], lambda_stack[0x100], lambda_ptr, lambda_len;
static Uint16 labels_len, refs_len, macro_len;
static Item labels[0x400], refs[0x1000], macros[0x100];
2022-07-13 00:27:52 -04:00
2024-03-28 20:52:50 -04:00
static char *runes = "|$@&,_.-;=!?#\"%~";
static char *hexad = "0123456789abcdef";
2022-07-13 00:27:52 -04:00
static char ops[][4] = {
"LIT", "INC", "POP", "NIP", "SWP", "ROT", "DUP", "OVR",
"EQU", "NEQ", "GTH", "LTH", "JMP", "JCN", "JSR", "STH",
"LDZ", "STZ", "LDR", "STR", "LDA", "STA", "DEI", "DEO",
"ADD", "SUB", "MUL", "DIV", "AND", "ORA", "EOR", "SFT"
};
static int find(char *s, char t) { int i = 0; char c; while((c = *s++)) { if(c == t) return i; i++; } return -1; }
static int shex(char *s) { int n = 0; char c; while((c = *s++)) { if(find(hexad, c) < 0) return -1; n = n << 4, n |= find(hexad, c); } return n; }
static int scmp(char *a, char *b, int len) { int i = 0; while(a[i] == b[i]) if(!a[i] || ++i >= len) return 1; return 0; }
static char *copy(char *src, char *dst, char c) { while(*src && *src != c) *dst++ = *src++; *dst++ = 0; return dst; }
static char *save(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s++) && *s); *dictnext++ = c; return o; }
static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), save(b, 0); return res; }
2024-03-28 20:52:50 -04:00
#define ishex(x) (shex(x) >= 0)
2024-04-03 22:46:26 -04:00
#define isopc(x) (findopcode(x) || scmp(x, "BRK", 4))
2024-03-28 20:52:50 -04:00
#define isinvalid(x) (!x[0] || ishex(x) || isopc(x) || find(runes, x[0]) >= 0)
#define writeshort(x) (writebyte(x >> 8, ctx) && writebyte(x & 0xff, ctx))
#define findlabel(x) finditem(x, labels, labels_len)
#define findmacro(x) finditem(x, macros, macro_len)
#define error_top(id, msg) !printf("%s: %s\n", id, msg)
#define error_asm(id) !printf("%s: %s in @%s, %s:%d.\n", id, token, scope, ctx->path, ctx->line)
#define error_ref(id) !printf("%s: %s, %s:%d\n", id, r->name, r->data, r->line)
2022-07-13 00:27:52 -04:00
/* clang-format on */
2024-03-28 20:52:50 -04:00
static int parse(char *w, FILE *f, Context *ctx);
2022-07-13 00:27:52 -04:00
static char *
2024-03-28 20:52:50 -04:00
push(char *s, char c)
2022-07-13 00:27:52 -04:00
{
2024-03-28 20:52:50 -04:00
char *d = dict;
for(d = dict; d < dictnext; d++) {
char *ss = s, *dd = d, a, b;
while((a = *dd++) == (b = *ss++))
if(!a && !b) return d;
2023-01-21 21:25:15 -05:00
}
2024-03-28 20:52:50 -04:00
return save(s, c);
2022-07-13 00:27:52 -04:00
}
2024-03-28 20:52:50 -04:00
static Item *
finditem(char *name, Item *list, int len)
2022-07-13 00:27:52 -04:00
{
int i;
2024-03-28 20:52:50 -04:00
if(name[0] == '&')
name = join(scope, '/', name + 1);
for(i = 0; i < len; i++)
if(scmp(list[i].name, name, 0x40))
return &list[i];
2022-07-13 00:27:52 -04:00
return NULL;
}
static Uint8
2024-04-03 22:46:26 -04:00
findopcode(char *s)
2022-07-13 00:27:52 -04:00
{
int i;
for(i = 0; i < 0x20; i++) {
2024-03-28 20:52:50 -04:00
int m = 3;
if(!scmp(ops[i], s, 3)) continue;
if(!i) i |= (1 << 7);
while(s[m]) {
if(s[m] == '2')
i |= (1 << 5);
else if(s[m] == 'r')
i |= (1 << 6);
else if(s[m] == 'k')
i |= (1 << 7);
2022-07-13 00:27:52 -04:00
else
2024-04-03 22:46:26 -04:00
return 0;
2022-07-13 00:27:52 -04:00
m++;
}
return i;
}
return 0;
}
static int
2024-03-28 20:52:50 -04:00
walkcomment(FILE *f, Context *ctx)
2022-07-13 00:27:52 -04:00
{
2024-03-28 20:52:50 -04:00
char c;
int depth = 1;
while(f && fread(&c, 1, 1, f)) {
if(c == 0xa) ctx->line += 1;
if(c == '(') depth++;
if(c == ')' && --depth < 1) return 1;
2022-07-13 00:27:52 -04:00
}
2024-03-30 22:53:04 -04:00
return error_asm("Comment incomplete");
2022-07-13 00:27:52 -04:00
}
2024-02-25 20:21:30 -05:00
static int
2024-03-28 20:52:50 -04:00
walkmacro(Item *m, Context *ctx)
2024-02-25 20:21:30 -05:00
{
char c, *dataptr = m->data, *cptr = token;
while((c = *dataptr++)) {
2024-03-28 20:52:50 -04:00
if(c < 0x21) {
*cptr++ = 0x00;
if(token[0] && !parse(token, NULL, ctx)) return 0;
cptr = token;
} else
*cptr++ = c;
}
return 1;
2024-02-25 20:21:30 -05:00
}
2022-07-13 00:27:52 -04:00
static int
2024-03-28 20:52:50 -04:00
walkfile(FILE *f, Context *ctx)
2022-07-13 00:27:52 -04:00
{
2024-03-28 20:52:50 -04:00
char c, *cptr = token;
while(f && fread(&c, 1, 1, f)) {
if(c == 0xa) ctx->line += 1;
if(c < 0x21) {
*cptr++ = 0x00;
if(token[0] && !parse(token, f, ctx))
return 0;
cptr = token;
} else if(cptr - token < 0x3f)
*cptr++ = c;
else
return error_asm("Token too long");
}
*cptr++ = 0;
return parse(token, f, ctx);
2022-07-13 00:27:52 -04:00
}
2023-08-08 19:47:29 -04:00
static char *
makelambda(int id)
{
2024-03-28 20:52:50 -04:00
lambda[0] = (char)0xce;
lambda[1] = (char)0xbb;
lambda[2] = hexad[id >> 0x4];
lambda[3] = hexad[id & 0xf];
return lambda;
2023-08-08 19:47:29 -04:00
}
2022-07-13 00:27:52 -04:00
static int
2024-03-28 20:52:50 -04:00
makemacro(char *name, FILE *f, Context *ctx)
2022-07-13 00:27:52 -04:00
{
2024-04-02 15:03:24 -04:00
int depth = 0;
2024-03-28 20:52:50 -04:00
char c;
Item *m;
if(macro_len >= 0x100) return error_asm("Macros limit exceeded");
if(isinvalid(name)) return error_asm("Macro is invalid");
if(findmacro(name)) return error_asm("Macro is duplicate");
m = &macros[macro_len++];
m->name = push(name, 0);
m->data = dictnext;
2024-03-28 20:52:50 -04:00
while(f && fread(&c, 1, 1, f) && c != '{')
if(c == 0xa) ctx->line += 1;
2024-04-02 15:03:24 -04:00
while(f && fread(&c, 1, 1, f)) {
2024-03-28 20:52:50 -04:00
if(c == 0xa) ctx->line += 1;
2024-03-30 22:53:04 -04:00
if(c == '%') return error_top("Macro nested", name);
2024-04-02 15:03:24 -04:00
if(c == '{') depth++;
if(c == '}' && --depth) break;
2024-03-30 22:53:04 -04:00
if(c == '(' && !walkcomment(f, ctx))
return 0;
2024-03-28 20:52:50 -04:00
else
*dictnext++ = c;
}
*dictnext++ = 0;
return 1;
}
static int
makelabel(char *name, int setscope, Context *ctx)
{
Item *l;
if(name[0] == '&')
name = join(scope, '/', name + 1);
if(labels_len >= 0x400) return error_asm("Labels limit exceeded");
2024-03-30 22:53:04 -04:00
if(isinvalid(name)) return error_asm("Label invalid");
if(findlabel(name)) return error_asm("Label duplicate");
2024-03-28 20:52:50 -04:00
l = &labels[labels_len++];
l->name = push(name, 0);
l->addr = ptr;
l->refs = 0;
2024-03-28 23:24:24 -04:00
if(setscope) copy(name, scope, '/');
2024-03-28 20:52:50 -04:00
return 1;
}
static int
makeref(char *label, char rune, Uint16 addr, Context *ctx)
2024-03-28 20:52:50 -04:00
{
Item *r;
if(refs_len >= 0x1000) return error_top("References limit exceeded", label);
r = &refs[refs_len++];
2023-08-08 19:47:29 -04:00
if(label[0] == '{') {
2024-03-28 20:52:50 -04:00
lambda_stack[lambda_ptr++] = lambda_len;
r->name = push(makelambda(lambda_len++), 0);
} else if(label[0] == '&' || label[0] == '/') {
2024-03-28 20:52:50 -04:00
r->name = join(scope, '/', label + 1);
} else
r->name = push(label, 0);
r->rune = rune;
2022-07-13 00:27:52 -04:00
r->addr = addr;
r->line = ctx->line;
r->data = ctx->path;
2022-07-13 00:27:52 -04:00
return 1;
}
static int
2024-03-30 22:53:04 -04:00
writepad(char *w, Context *ctx)
2022-07-13 00:27:52 -04:00
{
2024-03-28 20:52:50 -04:00
Item *l;
int rel = w[0] == '$' ? ptr : 0;
if(ishex(w + 1)) {
ptr = shex(w + 1) + rel;
return 1;
}
if((l = findlabel(w + 1))) {
ptr = l->addr + rel;
return 1;
}
2024-03-30 22:53:04 -04:00
return error_asm("Padding invalid");
2022-07-13 00:27:52 -04:00
}
static int
2024-03-28 20:52:50 -04:00
writebyte(Uint8 b, Context *ctx)
2022-07-13 00:27:52 -04:00
{
2024-03-28 20:52:50 -04:00
if(ptr < PAGE)
2024-03-30 22:53:04 -04:00
return error_asm("Writing zero-page");
2024-03-28 20:52:50 -04:00
else if(ptr >= 0x10000)
return error_asm("Writing outside memory");
else if(ptr < length)
return error_asm("Writing rewind");
data[ptr++] = b;
if(b)
length = ptr;
return 1;
2022-07-13 00:27:52 -04:00
}
static int
2024-03-28 20:52:50 -04:00
writehex(char *w, Context *ctx)
2022-07-13 00:27:52 -04:00
{
2024-03-28 20:52:50 -04:00
if(*w == '#')
2024-04-03 22:46:26 -04:00
writebyte(findopcode("LIT") | !!(++w)[2] << 5, ctx);
2024-03-29 13:42:58 -04:00
if(w[1] && !w[2])
2024-03-28 20:52:50 -04:00
return writebyte(shex(w), ctx);
2024-03-29 13:42:58 -04:00
else if(w[3] && !w[4])
2024-03-28 20:52:50 -04:00
return writeshort(shex(w));
else
2024-03-30 22:53:04 -04:00
return error_asm("Hexadecimal invalid");
2022-07-13 00:27:52 -04:00
}
static int
2024-03-28 20:52:50 -04:00
writestring(char *w, Context *ctx)
2022-07-13 00:27:52 -04:00
{
2024-03-28 20:52:50 -04:00
char c;
while((c = *(w++)))
2024-03-30 22:53:04 -04:00
if(!writebyte(c, ctx)) return error_asm("String invalid");
2024-03-28 20:52:50 -04:00
return 1;
2022-07-13 00:27:52 -04:00
}
static int
2024-03-28 20:52:50 -04:00
assemble(char *filename)
2022-07-13 00:27:52 -04:00
{
FILE *f;
2024-03-28 20:52:50 -04:00
int res = 0;
Context ctx;
2024-04-02 11:38:01 -04:00
ctx.line = 1;
2024-03-28 20:52:50 -04:00
ctx.path = push(filename, 0);
if(!(f = fopen(filename, "r")))
2024-03-30 22:53:04 -04:00
return error_top("Source missing", filename);
2024-03-28 20:52:50 -04:00
res = walkfile(f, &ctx);
2022-07-13 00:27:52 -04:00
fclose(f);
2024-03-28 20:52:50 -04:00
return res;
2022-07-13 00:27:52 -04:00
}
static int
2024-03-28 20:52:50 -04:00
parse(char *w, FILE *f, Context *ctx)
2022-07-13 00:27:52 -04:00
{
2024-03-28 20:52:50 -04:00
Item *m;
2022-07-13 00:27:52 -04:00
switch(w[0]) {
2024-04-03 22:37:06 -04:00
case 0x0: return 1;
2024-03-30 22:53:04 -04:00
case '(': return walkcomment(f, ctx);
case '%': return makemacro(w + 1, f, ctx);
case '@': return makelabel(w + 1, 1, ctx);
case '&': return makelabel(w, 0, ctx);
case '}': return makelabel(makelambda(lambda_stack[--lambda_ptr]), 0, ctx);
case '#': return writehex(w, ctx);
case '_': return makeref(w + 1, w[0], ptr, ctx) && writebyte(0xff, ctx);
case ',': return makeref(w + 1, w[0], ptr + 1, ctx) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
case '-': return makeref(w + 1, w[0], ptr, ctx) && writebyte(0xff, ctx);
case '.': return makeref(w + 1, w[0], ptr + 1, ctx) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
2024-04-02 18:12:00 -04:00
case ':': printf("Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */
case '=': return makeref(w + 1, w[0], ptr, ctx) && writeshort(0xffff);
case ';': return makeref(w + 1, w[0], ptr + 1, ctx) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff);
case '?': return makeref(w + 1, w[0], ptr + 1, ctx) && writebyte(0x20, ctx) && writeshort(0xffff);
case '!': return makeref(w + 1, w[0], ptr + 1, ctx) && writebyte(0x40, ctx) && writeshort(0xffff);
2024-03-30 22:53:04 -04:00
case '"': return writestring(w + 1, ctx);
case '~': return !assemble(w + 1) ? error_asm("Include missing") : 1;
2024-03-28 20:52:50 -04:00
case '$':
2024-03-30 22:53:04 -04:00
case '|': return writepad(w, ctx);
2022-07-13 00:27:52 -04:00
case '[':
2024-03-28 20:52:50 -04:00
case ']': return 1;
2022-07-13 00:27:52 -04:00
}
2024-03-28 20:52:50 -04:00
if(ishex(w)) return writehex(w, ctx);
2024-04-03 22:46:26 -04:00
if(isopc(w)) return writebyte(findopcode(w), ctx);
2024-03-28 20:52:50 -04:00
if((m = findmacro(w))) return walkmacro(m, ctx);
return makeref(w, ' ', ptr + 1, ctx) && writebyte(0x60, ctx) && writeshort(0xffff);
2022-07-13 00:27:52 -04:00
}
static int
2024-03-30 22:53:04 -04:00
resolve(char *filename)
2022-07-13 00:27:52 -04:00
{
2024-03-28 20:52:50 -04:00
int i, rel;
2024-03-30 22:53:04 -04:00
if(!length) return error_top("Output empty", filename);
2024-03-28 20:52:50 -04:00
for(i = 0; i < refs_len; i++) {
Item *r = &refs[i], *l = findlabel(r->name);
Uint8 *rom = data + r->addr;
if(!l) return error_ref("Label unknown");
2022-07-13 00:27:52 -04:00
switch(r->rune) {
2022-12-09 22:54:04 -05:00
case '_':
2023-01-12 12:34:45 -05:00
case ',':
2024-03-28 20:52:50 -04:00
*rom = rel = l->addr - r->addr - 2;
if((Sint8)data[r->addr] != rel)
return error_ref("Relative reference too far");
2022-07-13 00:27:52 -04:00
break;
2022-12-09 22:54:04 -05:00
case '-':
case '.':
2024-03-28 20:52:50 -04:00
*rom = l->addr;
2022-07-13 00:27:52 -04:00
break;
case ':':
2022-12-09 22:54:04 -05:00
case '=':
2023-01-12 12:34:45 -05:00
case ';':
2024-03-28 20:52:50 -04:00
*rom++ = l->addr >> 8, *rom = l->addr;
2022-07-13 00:27:52 -04:00
break;
2023-01-12 12:34:45 -05:00
case '?':
case '!':
default:
2024-03-28 20:52:50 -04:00
rel = l->addr - r->addr - 2;
*rom++ = rel >> 8, *rom = rel;
2022-12-09 22:54:04 -05:00
break;
2022-07-13 00:27:52 -04:00
}
2024-03-28 20:52:50 -04:00
l->refs++;
2022-07-13 00:27:52 -04:00
}
return 1;
}
static int
2024-03-28 20:52:50 -04:00
build(char *rompath)
2022-07-13 00:27:52 -04:00
{
int i;
2024-03-28 20:52:50 -04:00
FILE *dst, *dstsym;
char *sympath = join(rompath, '.', "sym");
/* rom */
if(!(dst = fopen(rompath, "wb")))
2024-03-30 22:53:04 -04:00
return !error_top("Output file invalid", rompath);
2024-03-28 20:52:50 -04:00
for(i = 0; i < labels_len; i++)
if(labels[i].name[0] - 'A' > 25 && !labels[i].refs)
2024-04-02 18:12:00 -04:00
printf("-- Unused label: %s\n", labels[i].name);
2024-03-28 20:52:50 -04:00
fwrite(data + PAGE, length - PAGE, 1, dst);
2024-04-02 18:12:00 -04:00
printf(
2022-07-13 00:27:52 -04:00
"Assembled %s in %d bytes(%.2f%% used), %d labels, %d macros.\n",
2024-03-28 20:52:50 -04:00
rompath,
length - PAGE,
(length - PAGE) / 652.80,
labels_len,
macro_len);
/* sym */
if(!(dstsym = fopen(sympath, "w")))
2024-03-30 22:53:04 -04:00
return !error_top("Symbols file invalid", sympath);
2024-03-28 20:52:50 -04:00
for(i = 0; i < labels_len; i++) {
Uint8 hb = labels[i].addr >> 8, lb = labels[i].addr;
char c, d = 0, *name = labels[i].name;
fwrite(&hb, 1, 1, dstsym);
fwrite(&lb, 1, 1, dstsym);
while((c = *name++)) fwrite(&c, 1, 1, dstsym);
fwrite(&d, 1, 1, dstsym);
2022-12-09 22:54:04 -05:00
}
2024-03-28 20:52:50 -04:00
fclose(dst), fclose(dstsym);
return 1;
2022-12-09 22:54:04 -05:00
}
2022-07-13 00:27:52 -04:00
int
main(int argc, char *argv[])
{
2024-03-28 20:52:50 -04:00
ptr = PAGE;
2024-03-28 23:24:24 -04:00
copy("on-reset", scope, 0);
if(argc == 2 && scmp(argv[1], "-v", 2)) return !printf("Uxnasm - Uxntal Assembler, 3 Apr 2024.\n");
2024-03-29 14:35:08 -04:00
if(argc != 3) return error_top("usage", "uxnasm [-v] input.tal output.rom");
2024-03-30 22:53:04 -04:00
if(!assemble(argv[1])) return 1;
if(!resolve(argv[2])) return 1;
if(!build(argv[2])) return 1;
2022-07-13 00:27:52 -04:00
return 0;
2024-03-29 14:35:08 -04:00
}