0
0
Fork 0
mirror of https://git.sr.ht/~rabbits/uxn synced 2024-11-01 03:52:39 +00:00
uxn/src/uxnasm.c

446 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.
*/
2021-04-20 21:30:26 +00:00
#define TRIM 0x0100
#define LENGTH 0x10000
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 {
char name[64], items[64][64];
Uint8 len;
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];
Uint16 addr, refs;
2021-01-31 05:31:49 +00:00
} Label;
2021-11-27 19:33:22 +00:00
typedef struct {
char name[64], rune;
Uint16 addr;
} Reference;
2021-03-13 22:55:29 +00:00
typedef struct {
2021-10-26 15:59:58 +00:00
Uint8 data[LENGTH];
unsigned int ptr, length;
Uint16 llen, mlen, rlen;
2021-11-27 22:07:25 +00:00
Label labels[512];
Macro macros[256];
Reference refs[2048];
2021-11-27 19:33:22 +00:00
char scope[64];
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-11-27 21:55:33 +00:00
static int litlast = 0;
2021-01-30 22:25:48 +00:00
2021-02-02 04:21:27 +00:00
/* clang-format off */
2021-06-28 21:42:36 +00:00
static char ops[][4] = {
"LIT", "INC", "POP", "DUP", "NIP", "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-08-29 18:43:00 +00: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 */
2021-06-28 21:42:36 +00:00
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 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-06-28 21:42:36 +00:00
static 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
2021-02-07 18:21:41 +00:00
/* clang-format on */
2021-01-30 22:25:48 +00:00
2021-11-30 00:19:47 +00:00
static int parse(char *w, FILE *f);
2021-11-27 19:33:22 +00:00
static int
error(const char *name, const char *msg)
{
fprintf(stderr, "%s: %s\n", name, msg);
return 0;
}
2021-02-04 20:22:08 +00:00
2021-11-27 22:44:28 +00:00
static char *
sublabel(char *src, char *scope, char *name)
{
return scat(scat(scpy(scope, src, 64), "/"), name);
}
2021-06-28 21:42:36 +00:00
static Macro *
2021-03-14 20:30:17 +00:00
findmacro(char *name)
{
int i;
for(i = 0; i < p.mlen; i++)
2021-03-14 20:30:17 +00:00
if(scmp(p.macros[i].name, name, 64))
return &p.macros[i];
return NULL;
}
2021-06-28 21:42:36 +00:00
static 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;
for(i = 0; i < p.llen; i++)
2021-03-13 22:55:29 +00:00
if(scmp(p.labels[i].name, name, 64))
return &p.labels[i];
2021-01-31 05:31:49 +00:00
return NULL;
}
2021-06-28 21:42:36 +00:00
static Uint8
2021-02-13 21:21:05 +00:00
findopcode(char *s)
2021-02-06 18:39:13 +00:00
{
int i;
for(i = 0; i < 0x20; i++) {
2021-02-06 18:39:13 +00:00
int m = 0;
2021-08-29 18:43:00 +00:00
if(!scmp(ops[i], s, 3))
2021-02-07 18:21:41 +00:00
continue;
2021-10-29 23:28:33 +00:00
if(!i) i |= (1 << 7); /* force keep for LIT */
2021-02-06 18:39:13 +00:00
while(s[3 + m]) {
2021-08-30 17:19:33 +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 */
m++;
2021-02-06 18:39:13 +00:00
}
return i;
}
return 0;
}
2021-06-28 21:42:36 +00:00
static int
2021-03-14 20:30:17 +00:00
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-11-17 23:14:05 +00:00
if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
2021-03-14 20:30:17 +00:00
return error("Macro name is invalid", name);
2021-11-27 22:07:25 +00:00
if(p.mlen == 256)
2021-11-29 16:48:12 +00:00
return error("Macros limit exceeded", name);
2021-03-14 20:30:17 +00:00
m = &p.macros[p.mlen++];
scpy(name, m->name, 64);
while(fscanf(f, "%63s", word) == 1) {
2021-03-14 20:30:17 +00:00
if(word[0] == '{') continue;
if(word[0] == '}') break;
2021-11-29 16:48:12 +00:00
if(word[0] == '%')
return error("Macro error", name);
if(m->len >= 64)
return error("Macro size exceeded", name);
2021-03-14 20:30:17 +00:00
scpy(word, m->items[m->len++], 64);
}
return 1;
}
2021-06-28 21:42:36 +00:00
static int
makelabel(char *name)
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);
if(sihx(name) && (slen(name) == 2 || slen(name) == 4))
2021-02-13 00:18:52 +00:00
return error("Label name is hex number", name);
if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
2021-02-13 16:38:23 +00:00
return error("Label name is invalid", name);
2021-11-27 22:07:25 +00:00
if(p.llen == 512)
2021-11-29 16:48:12 +00:00
return error("Labels limit exceeded", name);
2021-03-13 22:55:29 +00:00
l = &p.labels[p.llen++];
2021-11-27 19:33:22 +00:00
l->addr = p.ptr;
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-04 21:49:03 +00:00
return 1;
}
2021-11-27 22:44:28 +00:00
static int
makereference(char *scope, char *label, Uint16 addr)
{
char subw[64];
Reference *r;
if(p.rlen == 2048)
2021-11-29 16:48:12 +00:00
return error("References limit exceeded", label);
2021-11-27 22:44:28 +00:00
r = &p.refs[p.rlen++];
if(label[1] == '&')
scpy(sublabel(subw, scope, label + 2), r->name, 64);
else
scpy(label + 1, r->name, 64);
r->rune = label[0];
r->addr = addr;
return 1;
}
static int
writebyte(Uint8 b)
2021-02-15 22:04:58 +00:00
{
if(p.ptr < TRIM) {
2021-11-29 16:48:12 +00:00
fprintf(stderr, "-- Writing in zero-page: %02x\n", b);
return 0;
2022-02-19 01:02:47 +00:00
} else if(p.ptr > 0xffff) {
fprintf(stderr, "-- Writing after the end of RAM: %02x\n", b);
return 0;
2022-02-19 01:02:47 +00:00
} else if(p.ptr < p.length) {
fprintf(stderr, "-- Memory overwrite: %04x -> %04x\n", p.length, p.ptr);
return 0;
}
2021-11-27 19:33:22 +00:00
p.data[p.ptr++] = b;
p.length = p.ptr;
2021-11-27 21:55:33 +00:00
litlast = 0;
return 1;
2021-02-15 22:04:58 +00:00
}
static int
2021-11-27 19:33:22 +00:00
writeshort(Uint16 s, int lit)
2021-03-14 20:30:17 +00:00
{
if(lit)
if(!writebyte(findopcode("LIT2"))) return 0;
return writebyte(s >> 8) && writebyte(s & 0xff);
2021-03-14 20:30:17 +00:00
}
static int
2021-11-27 21:55:33 +00:00
writelitbyte(Uint8 b)
{
if(litlast) { /* combine literals */
Uint8 hb = p.data[p.ptr - 1];
p.ptr -= 2;
p.length = p.ptr;
return writeshort((hb << 8) + b, 1);
2021-11-27 21:55:33 +00:00
}
if(!writebyte(findopcode("LIT"))) return 0;
if(!writebyte(b)) return 0;
2021-11-27 21:55:33 +00:00
litlast = 1;
return 1;
2021-11-27 21:55:33 +00:00
}
2021-11-27 22:07:25 +00:00
static int
doinclude(const char *filename)
{
FILE *f;
char w[64];
if(!(f = fopen(filename, "r")))
2021-11-29 16:48:12 +00:00
return error("Include missing", filename);
2021-11-27 22:07:25 +00:00
while(fscanf(f, "%63s", w) == 1)
2021-11-30 00:19:47 +00:00
if(!parse(w, f))
2021-11-27 22:07:25 +00:00
return error("Unknown token", w);
fclose(f);
return 1;
}
2021-06-28 21:42:36 +00:00
static int
2021-11-30 00:19:47 +00:00
parse(char *w, FILE *f)
2021-01-29 20:14:37 +00:00
{
2021-12-29 17:33:23 +00:00
int i;
2021-11-27 21:55:33 +00:00
char word[64], subw[64], c;
Macro *m;
2021-11-27 19:33:22 +00:00
if(slen(w) >= 63)
return error("Invalid token", w);
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) {
2021-12-29 17:11:03 +00:00
if(slen(word) != 1)
continue;
else if(word[0] == '(')
i++;
else if(word[0] == ')' && --i < 1)
break;
}
2021-11-27 19:33:22 +00:00
break;
case '~': /* include */
if(!doinclude(w + 1))
return error("Invalid include", w);
break;
case '%': /* macro */
if(!makemacro(w + 1, f))
return error("Invalid macro", w);
break;
case '|': /* pad-absolute */
if(!sihx(w + 1))
return error("Invalid padding", w);
p.ptr = shex(w + 1);
litlast = 0;
2021-11-27 19:33:22 +00:00
break;
case '$': /* pad-relative */
if(!sihx(w + 1))
return error("Invalid padding", w);
p.ptr += shex(w + 1);
litlast = 0;
2021-11-27 19:33:22 +00:00
break;
case '@': /* label */
if(!makelabel(w + 1))
return error("Invalid label", w);
scpy(w + 1, p.scope, 64);
2021-11-27 22:58:37 +00:00
litlast = 0;
2021-11-27 19:33:22 +00:00
break;
case '&': /* sublabel */
if(!makelabel(sublabel(subw, p.scope, w + 1)))
return error("Invalid sublabel", w);
2021-11-27 22:58:37 +00:00
litlast = 0;
2021-11-27 19:33:22 +00:00
break;
case '#': /* literals hex */
if(!sihx(w + 1) || (slen(w) != 3 && slen(w) != 5))
return error("Invalid hex literal", w);
if(slen(w) == 3) {
if(!writelitbyte(shex(w + 1))) return 0;
2022-02-19 01:02:47 +00:00
} else if(slen(w) == 5) {
if(!writeshort(shex(w + 1), 1)) return 0;
}
2021-11-27 19:33:22 +00:00
break;
case '.': /* literal byte zero-page */
2021-11-27 22:44:28 +00:00
makereference(p.scope, w, p.ptr - litlast);
if(!writelitbyte(0xff)) return 0;
2021-11-27 21:55:33 +00:00
break;
2021-11-27 19:33:22 +00:00
case ',': /* literal byte relative */
2021-11-27 22:44:28 +00:00
makereference(p.scope, w, p.ptr - litlast);
if(!writelitbyte(0xff)) return 0;
2021-11-27 19:33:22 +00:00
break;
case ';': /* literal short absolute */
2021-11-27 22:44:28 +00:00
makereference(p.scope, w, p.ptr);
if(!writeshort(0xffff, 1)) return 0;
2021-11-27 19:33:22 +00:00
break;
case ':': /* raw short absolute */
2021-11-27 22:44:28 +00:00
makereference(p.scope, w, p.ptr);
if(!writeshort(0xffff, 0)) return 0;
2021-11-27 19:33:22 +00:00
break;
case '\'': /* raw char */
if(!writebyte((Uint8)w[1])) return 0;
2021-11-27 19:33:22 +00:00
break;
case '"': /* raw string */
i = 0;
while((c = w[++i]))
if(!writebyte(c)) return 0;
2021-11-27 19:33:22 +00:00
break;
case '[': break; /* ignored */
case ']': break; /* ignored */
default:
/* opcode */
if(findopcode(w) || scmp(w, "BRK", 4)) {
if(!writebyte(findopcode(w))) return 0;
}
2021-11-27 19:33:22 +00:00
/* raw byte */
else if(sihx(w) && slen(w) == 2) {
if(!writebyte(shex(w))) return 0;
}
2021-11-27 19:33:22 +00:00
/* raw short */
else if(sihx(w) && slen(w) == 4) {
if(!writeshort(shex(w), 0)) return 0;
}
2021-11-27 19:33:22 +00:00
/* macro */
else if((m = findmacro(w))) {
for(i = 0; i < m->len; i++)
2021-11-30 00:19:47 +00:00
if(!parse(m->items[i], f))
2021-11-27 19:33:22 +00:00
return 0;
return 1;
} else
return error("Unknown token", w);
2021-01-30 22:25:48 +00:00
}
2021-02-04 20:22:08 +00:00
return 1;
2021-01-30 22:25:48 +00:00
}
2021-06-28 21:42:36 +00:00
static int
2021-11-27 19:33:22 +00:00
resolve(void)
2021-01-30 22:25:48 +00:00
{
2021-11-27 19:33:22 +00:00
Label *l;
int i;
for(i = 0; i < p.rlen; i++) {
2021-11-27 19:33:22 +00:00
Reference *r = &p.refs[i];
switch(r->rune) {
case '.':
if(!(l = findlabel(r->name)))
return error("Unknown zero-page reference", r->name);
p.data[r->addr + 1] = l->addr & 0xff;
l->refs++;
break;
case ',':
if(!(l = findlabel(r->name)))
return error("Unknown relative reference", r->name);
p.data[r->addr + 1] = (Sint8)(l->addr - r->addr - 3);
if((Sint8)p.data[r->addr + 1] != (l->addr - r->addr - 3))
return error("Relative reference is too far", r->name);
2021-11-27 19:33:22 +00:00
l->refs++;
break;
case ';':
if(!(l = findlabel(r->name)))
return error("Unknown absolute reference", r->name);
p.data[r->addr + 1] = l->addr >> 0x8;
p.data[r->addr + 2] = l->addr & 0xff;
l->refs++;
break;
case ':':
if(!(l = findlabel(r->name)))
return error("Unknown absolute reference", r->name);
p.data[r->addr + 0] = l->addr >> 0x8;
p.data[r->addr + 1] = l->addr & 0xff;
l->refs++;
break;
default:
return error("Unknown reference", r->name);
2021-03-11 23:47:28 +00:00
}
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-11-27 19:33:22 +00:00
static int
assemble(FILE *f)
{
char w[64];
scpy("on-reset", p.scope, 64);
while(fscanf(f, "%63s", w) == 1)
2021-11-30 00:19:47 +00:00
if(!parse(w, f))
2021-11-27 19:33:22 +00:00
return error("Unknown token", w);
2021-11-30 18:27:35 +00:00
return resolve();
2021-11-27 19:33:22 +00:00
}
2021-11-27 22:07:25 +00:00
static void
review(char *filename)
{
int i;
for(i = 0; i < p.llen; i++)
2021-11-27 22:07:25 +00: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)
2021-11-29 16:48:12 +00:00
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,
2022-02-18 23:01:46 +00:00
(p.length - TRIM) / 652.80,
2021-11-29 16:48:12 +00:00
p.llen,
p.mlen);
2021-11-27 22:07:25 +00:00
}
2021-01-29 20:14:37 +00:00
int
main(int argc, char *argv[])
{
2021-10-29 16:29:23 +00:00
FILE *src, *dst;
2021-04-19 16:29:39 +00:00
if(argc < 3)
2021-07-25 00:09:46 +00:00
return !error("usage", "input.tal output.rom");
2021-10-29 16:29:23 +00:00
if(!(src = fopen(argv[1], "r")))
2021-11-29 16:48:12 +00:00
return !error("Invalid input", argv[1]);
2021-11-27 19:33:22 +00:00
if(!assemble(src))
2021-07-25 00:09:46 +00:00
return !error("Assembly", "Failed to assemble rom.");
2021-10-29 16:29:23 +00:00
if(!(dst = fopen(argv[2], "wb")))
return !error("Invalid Output", argv[2]);
fwrite(p.data + TRIM, p.length - TRIM, 1, dst);
2021-11-27 19:33:22 +00:00
review(argv[2]);
2021-01-29 20:14:37 +00:00
return 0;
}