uxn11/src/uxnasm.c

548 lines
14 KiB
C
Raw Normal View History

2022-07-13 00:27:52 -04:00
#include <stdio.h>
/*
2023-01-07 15:02:57 -05:00
Copyright (c) 2021-2023 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.
*/
#define TRIM 0x0100
#define LENGTH 0x10000
typedef unsigned char Uint8;
typedef signed char Sint8;
typedef unsigned short Uint16;
typedef struct {
char name[0x40], items[0x40][0x40];
Uint8 len;
} Macro;
typedef struct {
char name[0x40];
Uint16 addr, refs;
} Label;
typedef struct {
char name[0x40], rune;
Uint16 addr;
} Reference;
typedef struct {
Uint8 data[LENGTH];
2023-08-01 23:32:42 -04:00
Uint8 lambda_stack[0x100], lambda_ptr, lambda_count;
char scope[0x40], lambda[0x10], *location, *entry;
2022-07-13 00:27:52 -04:00
unsigned int ptr, length;
2023-08-01 23:32:42 -04:00
Uint16 label_len, macro_len, refs_len;
2022-07-13 00:27:52 -04:00
Label labels[0x400];
Macro macros[0x100];
Reference refs[0x1000];
2022-07-13 00:27:52 -04:00
} Program;
Program p;
/* clang-format off */
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"
};
2024-02-25 20:21:30 -05:00
static char *runes = "|$@&,_.-;=!?#\"%~";
2022-07-13 00:27:52 -04:00
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; } /* string compare */
static 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 */
static 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 */
static int slen(char *s) { int i = 0; while(s[i]) i++; return i; } /* string length */
static int spos(char *s, char c) { Uint8 i = 0, j; while((j = s[i++])) if(j == c) return i; return -1; } /* character position */
static 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 */
static char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* string cat */
/* clang-format on */
static int parse(char *w, FILE *f);
static int
error_top(const char *name, const char *msg)
2022-07-13 00:27:52 -04:00
{
fprintf(stderr, "%s: %s\n", name, msg);
return 0;
}
static int
error_asm(const char *name, const char *msg)
{
fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, msg, p.scope, p.location, 123);
return 0;
}
static int
error_res(const char *name, const char *msg)
{
fprintf(stderr, "%s: %s.\n", name, msg);
return 0;
}
static char *
setlocation(char *name)
{
p.location = name;
return name;
}
2022-07-13 00:27:52 -04:00
static char *
sublabel(char *src, char *scope, char *name)
{
2023-01-21 21:25:15 -05:00
if(slen(scope) + slen(name) >= 0x3f) {
error_asm("Sublabel length too long", name);
2023-01-21 21:25:15 -05:00
return NULL;
}
2022-07-13 00:27:52 -04:00
return scat(scat(scpy(scope, src, 0x40), "/"), name);
}
static Macro *
findmacro(char *name)
{
int i;
2023-08-01 23:32:42 -04:00
for(i = 0; i < p.macro_len; i++)
2022-07-13 00:27:52 -04:00
if(scmp(p.macros[i].name, name, 0x40))
return &p.macros[i];
return NULL;
}
static Label *
findlabel(char *name)
{
int i;
2023-08-01 23:32:42 -04:00
for(i = 0; i < p.label_len; i++)
2022-07-13 00:27:52 -04:00
if(scmp(p.labels[i].name, name, 0x40))
return &p.labels[i];
return NULL;
}
static Uint8
findopcode(char *s)
{
int i;
for(i = 0; i < 0x20; i++) {
int m = 0;
if(!scmp(ops[i], s, 3))
continue;
if(!i) i |= (1 << 7); /* force keep for LIT */
while(s[3 + m]) {
if(s[3 + m] == '2')
i |= (1 << 5); /* mode: short */
else if(s[3 + m] == 'r')
i |= (1 << 6); /* mode: return */
else if(s[3 + m] == 'k')
i |= (1 << 7); /* mode: keep */
else
return 0; /* failed to match */
m++;
}
return i;
}
return 0;
}
static int
makemacro(char *name, FILE *f)
{
Macro *m;
char word[0x40];
if(findmacro(name))
return error_asm("Macro duplicate", name);
2022-07-13 00:27:52 -04:00
if(sihx(name) && slen(name) % 2 == 0)
return error_asm("Macro name is hex number", name);
2022-07-13 00:27:52 -04:00
if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
return error_asm("Macro name is invalid", name);
2023-08-01 23:32:42 -04:00
if(p.macro_len == 0x100)
return error_asm("Macros limit exceeded", name);
2023-08-01 23:32:42 -04:00
m = &p.macros[p.macro_len++];
2022-07-13 00:27:52 -04:00
scpy(name, m->name, 0x40);
while(fscanf(f, "%63s", word) == 1) {
if(word[0] == '{') continue;
if(word[0] == '}') break;
if(word[0] == '%')
return error_asm("Macro error", name);
2022-07-13 00:27:52 -04:00
if(m->len >= 0x40)
return error_asm("Macro size exceeded", name);
2022-07-13 00:27:52 -04:00
scpy(word, m->items[m->len++], 0x40);
}
return 1;
}
2024-02-25 20:21:30 -05:00
static int
isrune(char c)
{
char cc, *r = runes;
while((cc = *r++))
if(c == cc) return 1;
return 0;
}
2022-07-13 00:27:52 -04:00
static int
makelabel(char *name)
{
Label *l;
if(findlabel(name))
return error_asm("Label duplicate", name);
2022-07-13 00:27:52 -04:00
if(sihx(name) && (slen(name) == 2 || slen(name) == 4))
return error_asm("Label name is hex number", name);
2022-07-13 00:27:52 -04:00
if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
return error_asm("Label name is invalid", name);
2024-02-25 20:21:30 -05:00
if(isrune(name[0]))
return error_asm("Label name is runic", name);
2023-08-01 23:32:42 -04:00
if(p.label_len == 0x400)
return error_asm("Labels limit exceeded", name);
2023-08-01 23:32:42 -04:00
l = &p.labels[p.label_len++];
2022-07-13 00:27:52 -04:00
l->addr = p.ptr;
l->refs = 0;
scpy(name, l->name, 0x40);
return 1;
}
2023-08-08 19:47:29 -04:00
static char *
makelambda(int id)
{
scpy("lambda", p.lambda, 0x07);
p.lambda[6] = '0' + (id >> 0x4);
p.lambda[7] = '0' + (id & 0xf);
return p.lambda;
}
2022-07-13 00:27:52 -04:00
static int
makereference(char *scope, char *label, char rune, Uint16 addr)
2022-07-13 00:27:52 -04:00
{
char subw[0x40], parent[0x40];
Reference *r;
if(p.refs_len >= 0x1000)
return error_asm("References limit exceeded", label);
2023-08-01 23:32:42 -04:00
r = &p.refs[p.refs_len++];
2023-08-08 19:47:29 -04:00
if(label[0] == '{') {
p.lambda_stack[p.lambda_ptr++] = p.lambda_count;
scpy(makelambda(p.lambda_count++), r->name, 0x40);
} else if(label[0] == '&' || label[0] == '/') {
if(!sublabel(subw, scope, label + 1))
return error_asm("Invalid sublabel", label);
2023-01-21 21:25:15 -05:00
scpy(subw, r->name, 0x40);
} else {
int pos = spos(label, '/');
2022-07-13 00:27:52 -04:00
if(pos > 0) {
Label *l;
if((l = findlabel(scpy(label, parent, pos))))
2022-07-13 00:27:52 -04:00
l->refs++;
}
scpy(label, r->name, 0x40);
2022-07-13 00:27:52 -04:00
}
r->rune = rune;
2022-07-13 00:27:52 -04:00
r->addr = addr;
return 1;
}
static int
writebyte(Uint8 b)
{
if(p.ptr < TRIM)
return error_asm("Writing in zero-page", "");
2022-07-13 00:27:52 -04:00
else if(p.ptr > 0xffff)
return error_asm("Writing after the end of RAM", "");
2022-07-13 00:27:52 -04:00
else if(p.ptr < p.length)
return error_asm("Memory overwrite", "");
2022-07-13 00:27:52 -04:00
p.data[p.ptr++] = b;
p.length = p.ptr;
return 1;
}
static int
writeopcode(char *w)
{
2023-01-02 20:33:57 -05:00
return writebyte(findopcode(w));
2022-07-13 00:27:52 -04:00
}
static int
writeshort(Uint16 s, int lit)
{
if(lit)
if(!writebyte(findopcode("LIT2"))) return 0;
return writebyte(s >> 8) && writebyte(s & 0xff);
}
static int
writelitbyte(Uint8 b)
{
2023-01-02 20:33:57 -05:00
return writebyte(findopcode("LIT")) && writebyte(b);
2022-07-13 00:27:52 -04:00
}
static int
doinclude(char *filename)
2022-07-13 00:27:52 -04:00
{
FILE *f;
char w[0x40];
if(!(f = fopen(setlocation(filename), "r")))
return error_asm("Include missing", filename);
2022-07-13 00:27:52 -04:00
while(fscanf(f, "%63s", w) == 1)
if(!parse(w, f))
return error_asm("Unknown token", w);
2022-07-13 00:27:52 -04:00
fclose(f);
return 1;
}
static int
parse(char *w, FILE *f)
{
int i;
char word[0x40], subw[0x40], c;
2023-04-10 13:16:17 -04:00
Label *l;
2022-07-13 00:27:52 -04:00
Macro *m;
if(slen(w) >= 63)
return error_asm("Invalid token", w);
2022-07-13 00:27:52 -04:00
switch(w[0]) {
case '(': /* comment */
if(slen(w) != 1) fprintf(stderr, "-- Malformed comment: %s\n", w);
i = 1; /* track nested comment depth */
while(fscanf(f, "%63s", word) == 1) {
if(slen(word) != 1)
continue;
else if(word[0] == '(')
i++;
else if(word[0] == ')' && --i < 1)
break;
}
break;
case '~': /* include */
if(!doinclude(w + 1))
return error_asm("Invalid include", w);
2022-07-13 00:27:52 -04:00
break;
case '%': /* macro */
if(!makemacro(w + 1, f))
return error_asm("Invalid macro", w);
2022-07-13 00:27:52 -04:00
break;
case '|': /* pad-absolute */
2023-04-10 13:16:17 -04:00
if(sihx(w + 1))
p.ptr = shex(w + 1);
else if(w[1] == '&') {
if(!sublabel(subw, p.scope, w + 2) || !(l = findlabel(subw)))
return error_asm("Invalid sublabel", w);
2023-04-10 13:16:17 -04:00
p.ptr = l->addr;
} else {
if(!(l = findlabel(w + 1)))
return error_asm("Invalid label", w);
2023-04-10 13:16:17 -04:00
p.ptr = l->addr;
}
2022-07-13 00:27:52 -04:00
break;
case '$': /* pad-relative */
2023-04-10 13:16:17 -04:00
if(sihx(w + 1))
p.ptr += shex(w + 1);
else if(w[1] == '&') {
if(!sublabel(subw, p.scope, w + 2) || !(l = findlabel(subw)))
return error_asm("Invalid sublabel", w);
2023-04-10 13:16:17 -04:00
p.ptr += l->addr;
} else {
if(!(l = findlabel(w + 1)))
return error_asm("Invalid label", w);
2023-04-10 13:16:17 -04:00
p.ptr += l->addr;
}
2022-07-13 00:27:52 -04:00
break;
case '@': /* label */
if(!makelabel(w + 1))
return error_asm("Invalid label", w);
i = 0;
while(w[i + 1] != '/' && i < 0x3e && (p.scope[i] = w[i + 1]))
i++;
p.scope[i] = '\0';
2022-07-13 00:27:52 -04:00
break;
case '&': /* sublabel */
2023-01-21 21:25:15 -05:00
if(!sublabel(subw, p.scope, w + 1) || !makelabel(subw))
return error_asm("Invalid sublabel", w);
2022-07-13 00:27:52 -04:00
break;
case '#': /* literals hex */
2023-01-02 20:33:57 -05:00
if(sihx(w + 1) && slen(w) == 3)
return writelitbyte(shex(w + 1));
else if(sihx(w + 1) && slen(w) == 5)
return writeshort(shex(w + 1), 1);
else
return error_asm("Invalid hex literal", w);
2022-07-13 00:27:52 -04:00
break;
2022-12-09 22:54:04 -05:00
case '_': /* raw byte relative */
2024-03-06 11:58:47 -05:00
return makereference(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff);
2022-07-13 00:27:52 -04:00
case ',': /* literal byte relative */
2024-03-06 11:58:47 -05:00
return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writelitbyte(0xff);
2022-12-09 22:54:04 -05:00
case '-': /* raw byte absolute */
2024-03-06 11:58:47 -05:00
return makereference(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff);
2022-12-09 22:54:04 -05:00
case '.': /* literal byte zero-page */
2024-03-06 11:58:47 -05:00
return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writelitbyte(0xff);
case ':': fprintf(stderr, "Deprecated rune %s, use =%s\n", w, w + 1);
case '=': /* raw short absolute */
2024-03-06 11:58:47 -05:00
return makereference(p.scope, w + 1, w[0], p.ptr) && writeshort(0xffff, 0);
2022-12-09 22:54:04 -05:00
case ';': /* literal short absolute */
2024-03-06 11:58:47 -05:00
return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writeshort(0xffff, 1);
2023-01-12 12:45:19 -05:00
case '?': /* JCI */
2024-03-06 11:58:47 -05:00
return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x20) && writeshort(0xffff, 0);
2023-01-12 12:45:19 -05:00
case '!': /* JMI */
2024-03-06 11:58:47 -05:00
return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x40) && writeshort(0xffff, 0);
2022-07-13 00:27:52 -04:00
case '"': /* raw string */
i = 0;
while((c = w[++i]))
if(!writebyte(c)) return 0;
break;
2023-08-01 23:32:42 -04:00
case '}': /* lambda end */
if(!makelabel(makelambda(p.lambda_stack[--p.lambda_ptr])))
return error_asm("Invalid label", w);
2023-08-08 19:47:29 -04:00
break;
2022-07-13 00:27:52 -04:00
case '[':
case ']':
if(slen(w) == 1) break; /* else fallthrough */
default:
/* opcode */
2023-01-02 20:33:57 -05:00
if(findopcode(w) || scmp(w, "BRK", 4))
return writeopcode(w);
2022-07-13 00:27:52 -04:00
/* raw byte */
2023-01-02 20:33:57 -05:00
else if(sihx(w) && slen(w) == 2)
return writebyte(shex(w));
2022-07-13 00:27:52 -04:00
/* raw short */
2023-01-02 20:33:57 -05:00
else if(sihx(w) && slen(w) == 4)
return writeshort(shex(w), 0);
2022-07-13 00:27:52 -04:00
/* macro */
else if((m = findmacro(w))) {
for(i = 0; i < m->len; i++)
if(!parse(m->items[i], f))
return 0;
return 1;
} else
2024-03-06 11:58:47 -05:00
return makereference(p.scope, w, ' ', p.ptr + 1) && writebyte(0x60) && writeshort(0xffff, 0);
2022-07-13 00:27:52 -04:00
}
return 1;
}
static int
resolve(void)
{
Label *l;
int i;
2023-01-12 12:34:45 -05:00
Uint16 a;
2023-08-01 23:32:42 -04:00
for(i = 0; i < p.refs_len; i++) {
2022-07-13 00:27:52 -04:00
Reference *r = &p.refs[i];
switch(r->rune) {
2022-12-09 22:54:04 -05:00
case '_':
2023-01-12 12:34:45 -05:00
case ',':
2022-07-13 00:27:52 -04:00
if(!(l = findlabel(r->name)))
return error_res("Unknown relative reference", r->name);
2022-12-09 22:54:04 -05:00
p.data[r->addr] = (Sint8)(l->addr - r->addr - 2);
if((Sint8)p.data[r->addr] != (l->addr - r->addr - 2))
return error_asm("Relative reference is too far", r->name);
2022-07-13 00:27:52 -04:00
l->refs++;
break;
2022-12-09 22:54:04 -05:00
case '-':
case '.':
if(!(l = findlabel(r->name)))
return error_res("Unknown zero-page reference", r->name);
2023-01-12 12:34:45 -05:00
p.data[r->addr] = l->addr & 0xff;
2022-07-13 00:27:52 -04:00
l->refs++;
break;
case ':':
2022-12-09 22:54:04 -05:00
case '=':
2023-01-12 12:34:45 -05:00
case ';':
2022-07-13 00:27:52 -04:00
if(!(l = findlabel(r->name)))
return error_res("Unknown absolute reference", r->name);
2022-12-09 22:54:04 -05:00
p.data[r->addr] = l->addr >> 0x8;
2022-07-13 00:27:52 -04:00
p.data[r->addr + 1] = l->addr & 0xff;
l->refs++;
break;
2023-01-12 12:34:45 -05:00
case '?':
case '!':
default:
2022-12-09 22:54:04 -05:00
if(!(l = findlabel(r->name)))
return error_res("Unknown absolute reference", r->name);
2023-01-12 12:34:45 -05:00
a = l->addr - r->addr - 2;
p.data[r->addr] = a >> 0x8;
p.data[r->addr + 1] = a & 0xff;
2022-12-09 22:54:04 -05:00
l->refs++;
break;
2022-07-13 00:27:52 -04:00
}
}
return 1;
}
static int
assemble(FILE *f)
{
char w[0x40];
2023-01-21 21:25:15 -05:00
p.ptr = 0x100;
2022-07-13 00:27:52 -04:00
scpy("on-reset", p.scope, 0x40);
2023-01-07 15:02:57 -05:00
while(fscanf(f, "%62s", w) == 1)
if(slen(w) > 0x3d || !parse(w, f))
return error_asm("Invalid token", w);
2022-07-13 00:27:52 -04:00
return resolve();
}
static void
review(char *filename)
{
int i;
2023-08-01 23:32:42 -04:00
for(i = 0; i < p.label_len; i++)
2022-07-13 00:27:52 -04:00
if(p.labels[i].name[0] >= 'A' && p.labels[i].name[0] <= 'Z')
continue; /* Ignore capitalized labels(devices) */
else if(!p.labels[i].refs)
fprintf(stderr, "-- Unused label: %s\n", p.labels[i].name);
fprintf(stderr,
"Assembled %s in %d bytes(%.2f%% used), %d labels, %d macros.\n",
filename,
p.length - TRIM,
(p.length - TRIM) / 652.80,
2023-08-01 23:32:42 -04:00
p.label_len,
p.macro_len);
2022-07-13 00:27:52 -04:00
}
2022-12-09 22:54:04 -05:00
static void
writesym(char *filename)
{
2023-01-07 15:02:57 -05:00
int i;
char symdst[0x60];
FILE *fp;
if(slen(filename) > 0x60 - 5)
return;
fp = fopen(scat(scpy(filename, symdst, slen(filename) + 1), ".sym"), "w");
2022-12-09 22:54:04 -05:00
if(fp != NULL) {
2023-08-01 23:32:42 -04:00
for(i = 0; i < p.label_len; i++) {
2023-01-12 12:34:45 -05:00
Uint8 hb = p.labels[i].addr >> 8, lb = p.labels[i].addr & 0xff;
fwrite(&hb, 1, 1, fp);
fwrite(&lb, 1, 1, fp);
2022-12-09 22:54:04 -05:00
fwrite(p.labels[i].name, slen(p.labels[i].name) + 1, 1, fp);
}
}
fclose(fp);
}
2022-07-13 00:27:52 -04:00
int
main(int argc, char *argv[])
{
FILE *src, *dst;
2023-08-08 19:47:29 -04:00
if(argc == 1)
return error_top("usage", "uxnasm [-v] input.tal output.rom");
2023-08-08 19:47:29 -04:00
if(argv[1][0] == '-' && argv[1][1] == 'v')
return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 6 Mar 2024.\n");
if(!(src = fopen(setlocation(argv[1]), "r")))
return !error_top("Invalid input", argv[1]);
p.entry = argv[1];
2022-07-13 00:27:52 -04:00
if(!assemble(src))
return !error_top("Assembly", "Failed to assemble rom.");
2024-02-25 20:36:33 -05:00
if(!(dst = fopen(argv[2], "wb")))
return !error_top("Invalid Output", argv[2]);
2022-07-13 00:27:52 -04:00
if(p.length <= TRIM)
return !error_top("Assembly", "Output rom is empty.");
2022-07-13 00:27:52 -04:00
fwrite(p.data + TRIM, p.length - TRIM, 1, dst);
2023-10-31 13:59:42 -04:00
if(!scmp(argv[2], "-", 2)) {
review(argv[2]);
writesym(argv[2]);
}
2022-07-13 00:27:52 -04:00
return 0;
}