From 6b207ed328a9a208d6f1e991c9f292ca1d6d49e0 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 28 Feb 2024 08:53:34 -0800 Subject: [PATCH 01/31] Improved stack printing --- makefile | 8 ++++---- src/devices/system.c | 16 ++++++---------- src/uxn11.c | 2 +- src/uxncli.c | 2 +- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/makefile b/makefile index 6d59e68..a0b9b06 100644 --- a/makefile +++ b/makefile @@ -12,11 +12,11 @@ all: dest bin/uxnasm bin/uxncli bin/uxn11 dest: @ mkdir -p bin run: all bin/uxnasm bin/uxncli bin/uxn11 - @ ./bin/uxn11 + @ bin/uxn11 test: bin/uxnasm bin/uxncli bin/uxn11 - @ ./bin/uxnasm -v && ./bin/uxncli -v && ./bin/uxn11 -v - @ ./bin/uxnasm etc/opctest.tal bin/opctest.rom - @ ./bin/uxncli bin/opctest.rom + @ bin/uxnasm -v && ./bin/uxncli -v && ./bin/uxn11 -v + @ bin/uxnasm etc/opctest.tal bin/opctest.rom + @ bin/uxncli bin/opctest.rom install: all bin/uxnasm bin/uxncli bin/uxn11 @ cp bin/uxn11 bin/uxnasm bin/uxncli ~/bin/ uninstall: diff --git a/src/devices/system.c b/src/devices/system.c index 81521ca..3912382 100644 --- a/src/devices/system.c +++ b/src/devices/system.c @@ -47,14 +47,10 @@ static void system_print(Stack *s, char *name) { Uint8 i; - fprintf(stderr, "%s ", name); - for(i = 0; i < 9; i++) { - Uint8 pos = s->ptr - 4 + i; - fprintf(stderr, !pos ? "[%02x]" : i == 4 ? "<%02x>" : - " %02x ", - s->dat[pos]); - } - fprintf(stderr, "\n"); + fprintf(stderr, "%s ", name, s->ptr); + for(i = s->ptr - 7; i != s->ptr + 1; i++) + fprintf(stderr, "%02x%c", s->dat[i], i == 0 ? '|' : ' '); + fprintf(stderr, "< \n"); } int @@ -68,8 +64,8 @@ system_error(char *msg, const char *err) void system_inspect(Uxn *u) { - system_print(&u->wst, "wst"); - system_print(&u->rst, "rst"); + system_print(&u->wst, "WST"); + system_print(&u->rst, "RST"); } int diff --git a/src/uxn11.c b/src/uxn11.c index b52a95f..b5a2d28 100644 --- a/src/uxn11.c +++ b/src/uxn11.c @@ -267,7 +267,7 @@ main(int argc, char **argv) int i = 1; char *rom; if(i != argc && argv[i][0] == '-' && argv[i][1] == 'v') { - fprintf(stdout, "Uxn11 - Varvara Emulator, 22 Feb 2023.\n"); + fprintf(stdout, "Uxn11 - Varvara Emulator, 28 Feb 2023.\n"); i++; } rom = i == argc ? "boot.rom" : argv[i++]; diff --git a/src/uxncli.c b/src/uxncli.c index 950209a..b7f8062 100644 --- a/src/uxncli.c +++ b/src/uxncli.c @@ -71,7 +71,7 @@ main(int argc, char **argv) return system_error("usage", "uxncli [-v] file.rom [args..]"); /* Read flags */ if(argv[i][0] == '-' && argv[i][1] == 'v') - return system_version("Uxncli - Console Varvara Emulator", "15 Feb 2024"); + return system_version("Uxncli - Console Varvara Emulator", "28 Feb 2024"); if(!system_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++])) return system_error("Init", "Failed to initialize uxn."); /* Game Loop */ From 6e7360ee71772a15457c93f0f17e31e6eedd340b Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 28 Feb 2024 09:04:21 -0800 Subject: [PATCH 02/31] Improved stack printing --- src/devices/system.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/devices/system.c b/src/devices/system.c index 3912382..dfde335 100644 --- a/src/devices/system.c +++ b/src/devices/system.c @@ -44,10 +44,9 @@ system_load(Uxn *u, char *filename) } static void -system_print(Stack *s, char *name) +system_print(Stack *s) { Uint8 i; - fprintf(stderr, "%s ", name, s->ptr); for(i = s->ptr - 7; i != s->ptr + 1; i++) fprintf(stderr, "%02x%c", s->dat[i], i == 0 ? '|' : ' '); fprintf(stderr, "< \n"); @@ -64,8 +63,8 @@ system_error(char *msg, const char *err) void system_inspect(Uxn *u) { - system_print(&u->wst, "WST"); - system_print(&u->rst, "RST"); + fprintf(stderr, "WST "), system_print(&u->wst); + fprintf(stderr, "RST "), system_print(&u->rst); } int From c95c615bbc17145f6e47c817006d3defe26feddb Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Mon, 4 Mar 2024 09:17:55 -0800 Subject: [PATCH 03/31] Fixed issue with debug port --- src/devices/system.c | 2 +- src/uxn11.c | 2 +- src/uxncli.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/devices/system.c b/src/devices/system.c index dfde335..0f96fdb 100644 --- a/src/devices/system.c +++ b/src/devices/system.c @@ -47,7 +47,7 @@ static void system_print(Stack *s) { Uint8 i; - for(i = s->ptr - 7; i != s->ptr + 1; i++) + for(i = s->ptr - 7; i != (Uint8)(s->ptr + 1); i++) fprintf(stderr, "%02x%c", s->dat[i], i == 0 ? '|' : ' '); fprintf(stderr, "< \n"); } diff --git a/src/uxn11.c b/src/uxn11.c index b5a2d28..c806331 100644 --- a/src/uxn11.c +++ b/src/uxn11.c @@ -267,7 +267,7 @@ main(int argc, char **argv) int i = 1; char *rom; if(i != argc && argv[i][0] == '-' && argv[i][1] == 'v') { - fprintf(stdout, "Uxn11 - Varvara Emulator, 28 Feb 2023.\n"); + fprintf(stdout, "Uxn11 - Varvara Emulator, 4 Mar 2023.\n"); i++; } rom = i == argc ? "boot.rom" : argv[i++]; diff --git a/src/uxncli.c b/src/uxncli.c index b7f8062..76e625b 100644 --- a/src/uxncli.c +++ b/src/uxncli.c @@ -71,7 +71,7 @@ main(int argc, char **argv) return system_error("usage", "uxncli [-v] file.rom [args..]"); /* Read flags */ if(argv[i][0] == '-' && argv[i][1] == 'v') - return system_version("Uxncli - Console Varvara Emulator", "28 Feb 2024"); + return system_version("Uxncli - Console Varvara Emulator", "4 Mar 2024"); if(!system_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++])) return system_error("Init", "Failed to initialize uxn."); /* Game Loop */ From aaab3e3e34e8e64bf3318dfbf737381ca0ebf52e Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 6 Mar 2024 08:50:46 -0800 Subject: [PATCH 04/31] (uxnasm) Stop after hitting refcount limit --- src/uxnasm.c | 152 ++++++++++++++++++++++++++++----------------------- 1 file changed, 83 insertions(+), 69 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index d1dcf09..91f66b9 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -36,12 +36,12 @@ typedef struct { typedef struct { Uint8 data[LENGTH]; Uint8 lambda_stack[0x100], lambda_ptr, lambda_count; - char scope[0x40], lambda[0x10]; + char scope[0x40], lambda[0x10], *location, *entry; unsigned int ptr, length; Uint16 label_len, macro_len, refs_len; Label labels[0x400]; Macro macros[0x100]; - Reference refs[0x800]; + Reference refs[0x1000]; } Program; Program p; @@ -70,17 +70,38 @@ static char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); whi static int parse(char *w, FILE *f); static int -error(const char *name, const char *msg) +error_top(const char *name, const char *msg) { 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; +} + static char * sublabel(char *src, char *scope, char *name) { if(slen(scope) + slen(name) >= 0x3f) { - error("Sublabel length too long", name); + error_asm("Sublabel length too long", name); return NULL; } return scat(scat(scpy(scope, src, 0x40), "/"), name); @@ -137,22 +158,22 @@ makemacro(char *name, FILE *f) Macro *m; char word[0x40]; if(findmacro(name)) - return error("Macro duplicate", name); + return error_asm("Macro duplicate", name); if(sihx(name) && slen(name) % 2 == 0) - return error("Macro name is hex number", name); + return error_asm("Macro name is hex number", name); if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name)) - return error("Macro name is invalid", name); + return error_asm("Macro name is invalid", name); if(p.macro_len == 0x100) - return error("Macros limit exceeded", name); + return error_asm("Macros limit exceeded", name); m = &p.macros[p.macro_len++]; scpy(name, m->name, 0x40); while(fscanf(f, "%63s", word) == 1) { if(word[0] == '{') continue; if(word[0] == '}') break; if(word[0] == '%') - return error("Macro error", name); + return error_asm("Macro error", name); if(m->len >= 0x40) - return error("Macro size exceeded", name); + return error_asm("Macro size exceeded", name); scpy(word, m->items[m->len++], 0x40); } return 1; @@ -172,15 +193,15 @@ makelabel(char *name) { Label *l; if(findlabel(name)) - return error("Label duplicate", name); + return error_asm("Label duplicate", name); if(sihx(name) && (slen(name) == 2 || slen(name) == 4)) - return error("Label name is hex number", name); + return error_asm("Label name is hex number", name); if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name)) - return error("Label name is invalid", name); + return error_asm("Label name is invalid", name); if(isrune(name[0])) - return error("Label name is runic", name); + return error_asm("Label name is runic", name); if(p.label_len == 0x400) - return error("Labels limit exceeded", name); + return error_asm("Labels limit exceeded", name); l = &p.labels[p.label_len++]; l->addr = p.ptr; l->refs = 0; @@ -202,15 +223,15 @@ makereference(char *scope, char *label, char rune, Uint16 addr) { char subw[0x40], parent[0x40]; Reference *r; - if(p.refs_len >= 0x800) - return error("References limit exceeded", label); + if(p.refs_len >= 0x1000) + return error_asm("References limit exceeded", label); r = &p.refs[p.refs_len++]; 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("Invalid sublabel", label); + return error_asm("Invalid sublabel", label); scpy(subw, r->name, 0x40); } else { int pos = spos(label, '/'); @@ -230,11 +251,11 @@ static int writebyte(Uint8 b) { if(p.ptr < TRIM) - return error("Writing in zero-page", ""); + return error_asm("Writing in zero-page", ""); else if(p.ptr > 0xffff) - return error("Writing after the end of RAM", ""); + return error_asm("Writing after the end of RAM", ""); else if(p.ptr < p.length) - return error("Memory overwrite", ""); + return error_asm("Memory overwrite", ""); p.data[p.ptr++] = b; p.length = p.ptr; return 1; @@ -261,15 +282,15 @@ writelitbyte(Uint8 b) } static int -doinclude(const char *filename) +doinclude(char *filename) { FILE *f; char w[0x40]; - if(!(f = fopen(filename, "r"))) - return error("Include missing", filename); + if(!(f = fopen(setlocation(filename), "r"))) + return error_asm("Include missing", filename); while(fscanf(f, "%63s", w) == 1) if(!parse(w, f)) - return error("Unknown token", w); + return error_asm("Unknown token", w); fclose(f); return 1; } @@ -282,7 +303,7 @@ parse(char *w, FILE *f) Label *l; Macro *m; if(slen(w) >= 63) - return error("Invalid token", w); + return error_asm("Invalid token", w); switch(w[0]) { case '(': /* comment */ if(slen(w) != 1) fprintf(stderr, "-- Malformed comment: %s\n", w); @@ -298,22 +319,22 @@ parse(char *w, FILE *f) break; case '~': /* include */ if(!doinclude(w + 1)) - return error("Invalid include", w); + return error_asm("Invalid include", w); break; case '%': /* macro */ if(!makemacro(w + 1, f)) - return error("Invalid macro", w); + return error_asm("Invalid macro", w); break; case '|': /* pad-absolute */ 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("Invalid sublabel", w); + return error_asm("Invalid sublabel", w); p.ptr = l->addr; } else { if(!(l = findlabel(w + 1))) - return error("Invalid label", w); + return error_asm("Invalid label", w); p.ptr = l->addr; } break; @@ -322,17 +343,17 @@ parse(char *w, FILE *f) p.ptr += shex(w + 1); else if(w[1] == '&') { if(!sublabel(subw, p.scope, w + 2) || !(l = findlabel(subw))) - return error("Invalid sublabel", w); + return error_asm("Invalid sublabel", w); p.ptr += l->addr; } else { if(!(l = findlabel(w + 1))) - return error("Invalid label", w); + return error_asm("Invalid label", w); p.ptr += l->addr; } break; case '@': /* label */ if(!makelabel(w + 1)) - return error("Invalid label", w); + return error_asm("Invalid label", w); i = 0; while(w[i + 1] != '/' && i < 0x3e && (p.scope[i] = w[i + 1])) i++; @@ -340,7 +361,7 @@ parse(char *w, FILE *f) break; case '&': /* sublabel */ if(!sublabel(subw, p.scope, w + 1) || !makelabel(subw)) - return error("Invalid sublabel", w); + return error_asm("Invalid sublabel", w); break; case '#': /* literals hex */ if(sihx(w + 1) && slen(w) == 3) @@ -348,32 +369,25 @@ parse(char *w, FILE *f) else if(sihx(w + 1) && slen(w) == 5) return writeshort(shex(w + 1), 1); else - return error("Invalid hex literal", w); + return error_asm("Invalid hex literal", w); break; case '_': /* raw byte relative */ - makereference(p.scope, w + 1, w[0], p.ptr); - return writebyte(0xff); + return writebyte(0xff) && makereference(p.scope, w + 1, w[0], p.ptr); case ',': /* literal byte relative */ - makereference(p.scope, w + 1, w[0], p.ptr + 1); - return writelitbyte(0xff); + return writelitbyte(0xff) && makereference(p.scope, w + 1, w[0], p.ptr + 1); case '-': /* raw byte absolute */ - makereference(p.scope, w + 1, w[0], p.ptr); - return writebyte(0xff); + return writebyte(0xff) && makereference(p.scope, w + 1, w[0], p.ptr); case '.': /* literal byte zero-page */ - makereference(p.scope, w + 1, w[0], p.ptr + 1); - return writelitbyte(0xff); + return writelitbyte(0xff) && makereference(p.scope, w + 1, w[0], p.ptr + 1); + case ':': fprintf(stderr, "Deprecated rune %s, use =%s\n", w, w + 1); case '=': /* raw short absolute */ - makereference(p.scope, w + 1, w[0], p.ptr); - return writeshort(0xffff, 0); + return writeshort(0xffff, 0) && makereference(p.scope, w + 1, w[0], p.ptr); case ';': /* literal short absolute */ - makereference(p.scope, w + 1, w[0], p.ptr + 1); - return writeshort(0xffff, 1); + return writeshort(0xffff, 1) && makereference(p.scope, w + 1, w[0], p.ptr + 1); case '?': /* JCI */ - makereference(p.scope, w + 1, w[0], p.ptr + 1); - return writebyte(0x20) && writeshort(0xffff, 0); + return writebyte(0x20) && writeshort(0xffff, 0) && makereference(p.scope, w + 1, w[0], p.ptr + 1); case '!': /* JMI */ - makereference(p.scope, w + 1, w[0], p.ptr + 1); - return writebyte(0x40) && writeshort(0xffff, 0); + return writebyte(0x40) && writeshort(0xffff, 0) && makereference(p.scope, w + 1, w[0], p.ptr + 1); case '"': /* raw string */ i = 0; while((c = w[++i])) @@ -381,7 +395,7 @@ parse(char *w, FILE *f) break; case '}': /* lambda end */ if(!makelabel(makelambda(p.lambda_stack[--p.lambda_ptr]))) - return error("Invalid label", w); + return error_asm("Invalid label", w); break; case '[': case ']': @@ -402,10 +416,8 @@ parse(char *w, FILE *f) if(!parse(m->items[i], f)) return 0; return 1; - } else { - makereference(p.scope, w, ' ', p.ptr + 1); - return writebyte(0x60) && writeshort(0xffff, 0); - } + } else + return writebyte(0x60) && writeshort(0xffff, 0) && makereference(p.scope, w, ' ', p.ptr + 1); } return 1; } @@ -422,23 +434,24 @@ resolve(void) case '_': case ',': if(!(l = findlabel(r->name))) - return error("Unknown relative reference", r->name); + return error_res("Unknown relative reference", r->name); p.data[r->addr] = (Sint8)(l->addr - r->addr - 2); if((Sint8)p.data[r->addr] != (l->addr - r->addr - 2)) - return error("Relative reference is too far", r->name); + return error_asm("Relative reference is too far", r->name); l->refs++; break; case '-': case '.': if(!(l = findlabel(r->name))) - return error("Unknown zero-page reference", r->name); + return error_res("Unknown zero-page reference", r->name); p.data[r->addr] = l->addr & 0xff; l->refs++; break; + case ':': case '=': case ';': if(!(l = findlabel(r->name))) - return error("Unknown absolute reference", r->name); + return error_res("Unknown absolute reference", r->name); p.data[r->addr] = l->addr >> 0x8; p.data[r->addr + 1] = l->addr & 0xff; l->refs++; @@ -447,7 +460,7 @@ resolve(void) case '!': default: if(!(l = findlabel(r->name))) - return error("Unknown absolute reference", r->name); + return error_res("Unknown absolute reference", r->name); a = l->addr - r->addr - 2; p.data[r->addr] = a >> 0x8; p.data[r->addr + 1] = a & 0xff; @@ -466,7 +479,7 @@ assemble(FILE *f) scpy("on-reset", p.scope, 0x40); while(fscanf(f, "%62s", w) == 1) if(slen(w) > 0x3d || !parse(w, f)) - return error("Invalid token", w); + return error_asm("Invalid token", w); return resolve(); } @@ -513,17 +526,18 @@ main(int argc, char *argv[]) { FILE *src, *dst; if(argc == 1) - return error("usage", "uxnasm [-v] input.tal output.rom"); + return error_top("usage", "uxnasm [-v] input.tal output.rom"); if(argv[1][0] == '-' && argv[1][1] == 'v') - return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 25 Feb 2024.\n"); - if(!(src = fopen(argv[1], "r"))) - return !error("Invalid input", argv[1]); + 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]; if(!assemble(src)) - return !error("Assembly", "Failed to assemble rom."); + return !error_top("Assembly", "Failed to assemble rom."); if(!(dst = fopen(argv[2], "wb"))) - return !error("Invalid Output", argv[2]); + return !error_top("Invalid Output", argv[2]); if(p.length <= TRIM) - return !error("Assembly", "Output rom is empty."); + return !error_top("Assembly", "Output rom is empty."); fwrite(p.data + TRIM, p.length - TRIM, 1, dst); if(!scmp(argv[2], "-", 2)) { review(argv[2]); From 8fb5a11731217934de5fd1c38abf2216fe0c0e82 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 6 Mar 2024 08:58:47 -0800 Subject: [PATCH 05/31] Fixed assembly issue --- src/uxnasm.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 91f66b9..cffe1e4 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -372,22 +372,22 @@ parse(char *w, FILE *f) return error_asm("Invalid hex literal", w); break; case '_': /* raw byte relative */ - return writebyte(0xff) && makereference(p.scope, w + 1, w[0], p.ptr); + return makereference(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff); case ',': /* literal byte relative */ - return writelitbyte(0xff) && makereference(p.scope, w + 1, w[0], p.ptr + 1); + return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writelitbyte(0xff); case '-': /* raw byte absolute */ - return writebyte(0xff) && makereference(p.scope, w + 1, w[0], p.ptr); + return makereference(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff); case '.': /* literal byte zero-page */ - return writelitbyte(0xff) && makereference(p.scope, w + 1, w[0], p.ptr + 1); + 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 */ - return writeshort(0xffff, 0) && makereference(p.scope, w + 1, w[0], p.ptr); + return makereference(p.scope, w + 1, w[0], p.ptr) && writeshort(0xffff, 0); case ';': /* literal short absolute */ - return writeshort(0xffff, 1) && makereference(p.scope, w + 1, w[0], p.ptr + 1); + return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writeshort(0xffff, 1); case '?': /* JCI */ - return writebyte(0x20) && writeshort(0xffff, 0) && makereference(p.scope, w + 1, w[0], p.ptr + 1); + return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x20) && writeshort(0xffff, 0); case '!': /* JMI */ - return writebyte(0x40) && writeshort(0xffff, 0) && makereference(p.scope, w + 1, w[0], p.ptr + 1); + return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x40) && writeshort(0xffff, 0); case '"': /* raw string */ i = 0; while((c = w[++i])) @@ -417,7 +417,7 @@ parse(char *w, FILE *f) return 0; return 1; } else - return writebyte(0x60) && writeshort(0xffff, 0) && makereference(p.scope, w, ' ', p.ptr + 1); + return makereference(p.scope, w, ' ', p.ptr + 1) && writebyte(0x60) && writeshort(0xffff, 0); } return 1; } From 45fd270e9dfbe60097bf1804a7dae7c48568887a Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 7 Mar 2024 11:02:18 -0800 Subject: [PATCH 06/31] Fixed issue with mouse where scroll-x would trigger click events --- src/uxn11.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/uxn11.c b/src/uxn11.c index c806331..779b8ff 100644 --- a/src/uxn11.c +++ b/src/uxn11.c @@ -171,6 +171,10 @@ emu_event(Uxn *u) mouse_scroll(u, &u->dev[0x90], 0, 1); else if(e->button == 5) mouse_scroll(u, &u->dev[0x90], 0, -1); + else if(e->button == 6) + mouse_scroll(u, &u->dev[0x90], 1, 0); + else if(e->button == 7) + mouse_scroll(u, &u->dev[0x90], -1, 0); else mouse_down(u, &u->dev[0x90], 0x1 << (e->button - 1)); } break; @@ -267,7 +271,7 @@ main(int argc, char **argv) int i = 1; char *rom; if(i != argc && argv[i][0] == '-' && argv[i][1] == 'v') { - fprintf(stdout, "Uxn11 - Varvara Emulator, 4 Mar 2023.\n"); + fprintf(stdout, "Uxn11 - Varvara Emulator, 7 Mar 2023.\n"); i++; } rom = i == argc ? "boot.rom" : argv[i++]; From 52f9f9d4a74e11b33590e2bada13b59c2e5576f2 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 7 Mar 2024 21:01:12 -0800 Subject: [PATCH 07/31] (uxnasm) Print assembly result to stdout --- src/devices/mouse.c | 18 ++++++------------ src/uxnasm.c | 21 +++++++-------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/devices/mouse.c b/src/devices/mouse.c index 4b6df1a..e7f1002 100644 --- a/src/devices/mouse.c +++ b/src/devices/mouse.c @@ -29,23 +29,17 @@ mouse_up(Uxn *u, Uint8 *d, Uint8 mask) void mouse_pos(Uxn *u, Uint8 *d, Uint16 x, Uint16 y) { - *(d + 2) = x >> 8; - *(d + 3) = x; - *(d + 4) = y >> 8; - *(d + 5) = y; + *(d + 2) = x >> 8, *(d + 3) = x; + *(d + 4) = y >> 8, *(d + 5) = y; uxn_eval(u, PEEK2(d)); } void mouse_scroll(Uxn *u, Uint8 *d, Uint16 x, Uint16 y) { - *(d + 0xa) = x >> 8; - *(d + 0xb) = x; - *(d + 0xc) = -y >> 8; - *(d + 0xd) = -y; + *(d + 0xa) = x >> 8, *(d + 0xb) = x; + *(d + 0xc) = -y >> 8, *(d + 0xd) = -y; uxn_eval(u, PEEK2(d)); - *(d + 0xa) = 0; - *(d + 0xb) = 0; - *(d + 0xc) = 0; - *(d + 0xd) = 0; + *(d + 0xa) = 0, *(d + 0xb) = 0; + *(d + 0xc) = 0, *(d + 0xd) = 0; } diff --git a/src/uxnasm.c b/src/uxnasm.c index cffe1e4..2cafd80 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -83,13 +83,6 @@ error_asm(const char *name, const char *msg) 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) { @@ -434,7 +427,7 @@ resolve(void) case '_': case ',': if(!(l = findlabel(r->name))) - return error_res("Unknown relative reference", r->name); + return error_top("Unknown relative reference", r->name); 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); @@ -443,7 +436,7 @@ resolve(void) case '-': case '.': if(!(l = findlabel(r->name))) - return error_res("Unknown zero-page reference", r->name); + return error_top("Unknown zero-page reference", r->name); p.data[r->addr] = l->addr & 0xff; l->refs++; break; @@ -451,7 +444,7 @@ resolve(void) case '=': case ';': if(!(l = findlabel(r->name))) - return error_res("Unknown absolute reference", r->name); + return error_top("Unknown absolute reference", r->name); p.data[r->addr] = l->addr >> 0x8; p.data[r->addr + 1] = l->addr & 0xff; l->refs++; @@ -460,7 +453,7 @@ resolve(void) case '!': default: if(!(l = findlabel(r->name))) - return error_res("Unknown absolute reference", r->name); + return error_top("Unknown absolute reference", r->name); a = l->addr - r->addr - 2; p.data[r->addr] = a >> 0x8; p.data[r->addr + 1] = a & 0xff; @@ -491,8 +484,8 @@ review(char *filename) 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, + fprintf(stdout, "-- Unused label: %s\n", p.labels[i].name); + fprintf(stdout, "Assembled %s in %d bytes(%.2f%% used), %d labels, %d macros.\n", filename, p.length - TRIM, @@ -528,7 +521,7 @@ main(int argc, char *argv[]) if(argc == 1) return error_top("usage", "uxnasm [-v] input.tal output.rom"); if(argv[1][0] == '-' && argv[1][1] == 'v') - return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 6 Mar 2024.\n"); + return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 7 Mar 2024.\n"); if(!(src = fopen(setlocation(argv[1]), "r"))) return !error_top("Invalid input", argv[1]); p.entry = argv[1]; From 4a97a20a0c5e210e84226fd8d69f1dcf757f2eb0 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Mon, 18 Mar 2024 10:37:44 -0700 Subject: [PATCH 08/31] Added extra System/Expansion ports --- etc/mmu.tal | 56 ++++++++++++++++++++++++++++++++++++++++++++ src/devices/system.c | 23 ++++++++++++++---- src/uxn11.c | 2 +- src/uxncli.c | 2 +- 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 etc/mmu.tal diff --git a/etc/mmu.tal b/etc/mmu.tal new file mode 100644 index 0000000..7d2a85e --- /dev/null +++ b/etc/mmu.tal @@ -0,0 +1,56 @@ +|00 @System &vector $2 &expansion $2 &wst $1 &rst $1 &metadata $2 &r $2 &g $2 &b $2 &debug $1 &state $1 +|100 + +@on-reset ( -> ) + ;buf + #2018 DEO + ;buf + #0a18 DEO + ( | copy left ) + ;buf #0006 ADD2 ;mmu-cpyl/a STA2 + ;buf #0003 ADD2 ;mmu-cpyl/b STA2 + ;mmu-cpyl .System/expansion DEO2 + ;buf + #2018 DEO + ;res1 + #0a18 DEO + ( | copy right ) + ;buf #0003 ADD2 ;mmu-cpyr/a STA2 + ;buf #0009 ADD2 ;mmu-cpyr/b STA2 + ;mmu-cpyr .System/expansion DEO2 + ;buf + #2018 DEO + ;res2 + #0a18 DEO + ( | copy left ) + ;buf #0009 ADD2 ;mmu-cpyl/a STA2 + ;buf #0006 ADD2 ;mmu-cpyl/b STA2 + ;mmu-cpyl .System/expansion DEO2 + ;buf + #2018 DEO + ;res3 + #0a18 DEO + ( | memset ) + ;buf #0003 ADD2 ;mmu-fill/a STA2 + ;mmu-fill .System/expansion DEO2 + ;buf + #2018 DEO + ;res4 + #0a18 DEO + BRK + +@ ( str* -- ) + LDAk #18 DEO + INC2 LDAk ? + POP2 JMP2r + +@buf [ "......[hello]..... $1 ] +@res1 [ "...[hello]o]...... $1 ] +@res2 [ "...[hello[hello].. $1 ] +@res3 [ "...[he[hello]lo].. $1 ] +@res4 [ "...-------------.. $1 ] + +@mmu-fill [ 00 000d 0000 &a $2 2d ] +@mmu-cpyl [ 01 0007 0000 &a $2 0000 &b $2 ] +@mmu-cpyr [ 02 0007 0000 &a $2 0000 &b $2 ] + diff --git a/src/devices/system.c b/src/devices/system.c index 0f96fdb..5987eb5 100644 --- a/src/devices/system.c +++ b/src/devices/system.c @@ -116,14 +116,29 @@ system_deo(Uxn *u, Uint8 *d, Uint8 port) case 0x3: ram = u->ram; addr = PEEK2(d + 2); - if(ram[addr] == 0x1) { + if(ram[addr] == 0x0) { + Uint8 value = ram[addr + 7]; Uint16 i, length = PEEK2(ram + addr + 1); - Uint16 a_page = PEEK2(ram + addr + 1 + 2), a_addr = PEEK2(ram + addr + 1 + 4); - Uint16 b_page = PEEK2(ram + addr + 1 + 6), b_addr = PEEK2(ram + addr + 1 + 8); + Uint16 dst_page = PEEK2(ram + addr + 3), dst_addr = PEEK2(ram + addr + 5); + int dst = (dst_page % RAM_PAGES) * 0x10000; + for(i = 0; i < length; i++) + ram[dst + (Uint16)(dst_addr + i)] = value; + } else if(ram[addr] == 0x1) { + Uint16 i, length = PEEK2(ram + addr + 1); + Uint16 a_page = PEEK2(ram + addr + 3), a_addr = PEEK2(ram + addr + 5); + Uint16 b_page = PEEK2(ram + addr + 7), b_addr = PEEK2(ram + addr + 9); int src = (a_page % RAM_PAGES) * 0x10000, dst = (b_page % RAM_PAGES) * 0x10000; for(i = 0; i < length; i++) ram[dst + (Uint16)(b_addr + i)] = ram[src + (Uint16)(a_addr + i)]; - } + } else if(ram[addr] == 0x2) { + Uint16 i, length = PEEK2(ram + addr + 1); + Uint16 a_page = PEEK2(ram + addr + 3), a_addr = PEEK2(ram + addr + 5); + Uint16 b_page = PEEK2(ram + addr + 7), b_addr = PEEK2(ram + addr + 9); + int src = (a_page % RAM_PAGES) * 0x10000, dst = (b_page % RAM_PAGES) * 0x10000; + for(i = length - 1; i != 0xffff; i--) + ram[dst + (Uint16)(b_addr + i)] = ram[src + (Uint16)(a_addr + i)]; + } else + fprintf(stderr, "Unknown Expansion Command 0x%02x\n", ram[addr]); break; case 0x4: u->wst.ptr = d[4]; diff --git a/src/uxn11.c b/src/uxn11.c index 779b8ff..c795ca0 100644 --- a/src/uxn11.c +++ b/src/uxn11.c @@ -271,7 +271,7 @@ main(int argc, char **argv) int i = 1; char *rom; if(i != argc && argv[i][0] == '-' && argv[i][1] == 'v') { - fprintf(stdout, "Uxn11 - Varvara Emulator, 7 Mar 2023.\n"); + fprintf(stdout, "Uxn11 - Varvara Emulator, 18 Mar 2024.\n"); i++; } rom = i == argc ? "boot.rom" : argv[i++]; diff --git a/src/uxncli.c b/src/uxncli.c index 76e625b..f7f9fc2 100644 --- a/src/uxncli.c +++ b/src/uxncli.c @@ -71,7 +71,7 @@ main(int argc, char **argv) return system_error("usage", "uxncli [-v] file.rom [args..]"); /* Read flags */ if(argv[i][0] == '-' && argv[i][1] == 'v') - return system_version("Uxncli - Console Varvara Emulator", "4 Mar 2024"); + return system_version("Uxncli - Console Varvara Emulator", "18 Mar 2024"); if(!system_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++])) return system_error("Init", "Failed to initialize uxn."); /* Game Loop */ From b7660a192018340ea843d4384854699a2da38d15 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Tue, 19 Mar 2024 09:08:26 -0700 Subject: [PATCH 09/31] Exit after returning code --- src/uxn11.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uxn11.c b/src/uxn11.c index c795ca0..a5ec9f8 100644 --- a/src/uxn11.c +++ b/src/uxn11.c @@ -102,7 +102,6 @@ emu_end(Uxn *u) XDestroyImage(ximage); XDestroyWindow(display, window); XCloseDisplay(display); - exit(0); return u->dev[0x0f] & 0x7f; } @@ -145,6 +144,7 @@ emu_event(Uxn *u) } break; case ClientMessage: emu_end(u); + exit(0); break; case KeyPress: { KeySym sym; @@ -271,7 +271,7 @@ main(int argc, char **argv) int i = 1; char *rom; if(i != argc && argv[i][0] == '-' && argv[i][1] == 'v') { - fprintf(stdout, "Uxn11 - Varvara Emulator, 18 Mar 2024.\n"); + fprintf(stdout, "Uxn11 - Varvara Emulator, 19 Mar 2024.\n"); i++; } rom = i == argc ? "boot.rom" : argv[i++]; From 0061d91c7b5520682dafbed5fd12d107a9579e53 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 21 Mar 2024 17:22:05 -0700 Subject: [PATCH 10/31] Close nicely --- src/devices/console.c | 7 +++---- src/uxn11.c | 8 ++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/devices/console.c b/src/devices/console.c index 75021c2..21804db 100644 --- a/src/devices/console.c +++ b/src/devices/console.c @@ -127,6 +127,7 @@ start_fork_pty(Uint8 *d) static void start_fork_pipe(Uint8 *d) { + pid_t pid; if(child_mode & 0x01) { /* parent writes to child's stdin */ if(pipe(to_child_fd) == -1) { @@ -135,7 +136,6 @@ start_fork_pipe(Uint8 *d) return; } } - if(child_mode & 0x06) { /* parent reads from child's stdout and/or stderr */ if(pipe(from_child_fd) == -1) { @@ -144,8 +144,7 @@ start_fork_pipe(Uint8 *d) return; } } - - pid_t pid = fork(); + pid = fork(); if(pid < 0) { /* failure */ d[0x6] = 0xff; fprintf(stderr, "fork failure\n"); @@ -180,9 +179,9 @@ start_fork_pipe(Uint8 *d) static void kill_child(Uint8 *d, int options) { + int wstatus; if(child_pid) { kill(child_pid, 9); - int wstatus; if(waitpid(child_pid, &wstatus, options)) { d[0x6] = 1; d[0x7] = WEXITSTATUS(wstatus); diff --git a/src/uxn11.c b/src/uxn11.c index a5ec9f8..4ccb8b7 100644 --- a/src/uxn11.c +++ b/src/uxn11.c @@ -102,6 +102,7 @@ emu_end(Uxn *u) XDestroyImage(ximage); XDestroyWindow(display, window); XCloseDisplay(display); + exit(0); return u->dev[0x0f] & 0x7f; } @@ -143,8 +144,7 @@ emu_event(Uxn *u) XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, PAD, PAD, w, h); } break; case ClientMessage: - emu_end(u); - exit(0); + u->dev[0x0f] = 0x80; break; case KeyPress: { KeySym sym; @@ -225,7 +225,7 @@ display_init(void) } static int -emu_run(Uxn *u, char *rom) +emu_run(Uxn *u) { int i = 1, n; char expirations[8]; @@ -287,7 +287,7 @@ main(int argc, char **argv) u.dev[0x17] = argc - i; if(uxn_eval(&u, PAGE_PROGRAM)) { console_listen(&u, i, argc, argv); - emu_run(&u, boot_rom); + emu_run(&u); } return emu_end(&u); } From b302645412df19a7894296f6ff07d41be0aa4d0a Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Fri, 22 Mar 2024 10:43:02 -0700 Subject: [PATCH 11/31] Removed old device masks --- src/devices/file.h | 2 -- src/devices/mouse.h | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/devices/file.h b/src/devices/file.h index b29b482..5b06409 100644 --- a/src/devices/file.h +++ b/src/devices/file.h @@ -10,8 +10,6 @@ WITH REGARD TO THIS SOFTWARE. */ #define FILE_VERSION 1 -#define FILE_DEIMASK 0x0000 -#define FILE_DEOMASK 0xa260 #define POLYFILEY 2 #define DEV_FILE0 0xa diff --git a/src/devices/mouse.h b/src/devices/mouse.h index e848e7b..3af21f0 100644 --- a/src/devices/mouse.h +++ b/src/devices/mouse.h @@ -10,8 +10,6 @@ WITH REGARD TO THIS SOFTWARE. */ #define MOUSE_VERSION 1 -#define MOUSE_DEIMASK 0x0000 -#define MOUSE_DEOMASK 0x0000 void mouse_down(Uxn *u, Uint8 *d, Uint8 mask); void mouse_up(Uxn *u, Uint8 *d, Uint8 mask); From 5fdd442ec3b1fdff66a0637325e258c57fd18e90 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 28 Mar 2024 17:52:50 -0700 Subject: [PATCH 12/31] (uxnasm) Updated --- src/uxnasm.c | 678 +++++++++++++++++++++------------------------------ 1 file changed, 276 insertions(+), 402 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 2cafd80..cc68e13 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -1,7 +1,7 @@ #include /* -Copyright (c) 2021-2023 Devine Lu Linvega, Andrew Alderwick +Copyright (c) 2021-2024 Devine Lu Linvega, Andrew Alderwick Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -11,43 +11,25 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ -#define TRIM 0x0100 -#define LENGTH 0x10000 +/* clang-format off */ + +#define PAGE 0x0100 typedef unsigned char Uint8; typedef signed char Sint8; typedef unsigned short Uint16; +typedef struct { char *name, rune, *content; Uint16 addr, refs; } Item; +typedef struct { int line; char *path; } Context; -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]; - Uint8 lambda_stack[0x100], lambda_ptr, lambda_count; - char scope[0x40], lambda[0x10], *location, *entry; - unsigned int ptr, length; - Uint16 label_len, macro_len, refs_len; - Label labels[0x400]; - Macro macros[0x100]; - Reference refs[0x1000]; -} Program; - -Program p; - -/* clang-format off */ +static int ptr, length; +static char token[0x40], scope[0x40], lambda[0x05]; +static char dict[0x4000], *dictnext = dict; +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]; +static char *runes = "|$@&,_.-;=!?#\"%~"; +static char *hexad = "0123456789abcdef"; static char ops[][4] = { "LIT", "INC", "POP", "NIP", "SWP", "ROT", "DUP", "OVR", "EQU", "NEQ", "GTH", "LTH", "JMP", "JCN", "JSR", "STH", @@ -55,68 +37,47 @@ static char ops[][4] = { "ADD", "SUB", "MUL", "DIV", "AND", "ORA", "EOR", "SFT" }; -static char *runes = "|$@&,_.-;=!?#\"%~"; +static int find(char *s, char t) { int i = 0; char c; while((c = *s++)) { if(c == t) return i; i++; } return -1; } /* chr in str */ +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; } /* str to hex */ +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; } /* str compare */ +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; } /* str copy */ +static char *save(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s++) && *s); *dictnext++ = c; return o; } /* save to dict */ +static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), save(b, 0); return res; } /* join two str */ -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 */ +#define ishex(x) (shex(x) >= 0) +#define isopc(x) (findopcode(x) || scmp(x, "BRK", 4)) +#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(name, msg) !fprintf(stderr, "%s: %s\n", name, msg) +#define error_asm(name) !fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, token, scope, ctx->path, ctx->line) /* clang-format on */ -static int parse(char *w, FILE *f); - -static int -error_top(const char *name, const char *msg) -{ - 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 parse(char *w, FILE *f, Context *ctx); static char * -setlocation(char *name) +push(char *s, char c) { - p.location = name; - return name; -} - -static char * -sublabel(char *src, char *scope, char *name) -{ - if(slen(scope) + slen(name) >= 0x3f) { - error_asm("Sublabel length too long", name); - return NULL; + 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; } - return scat(scat(scpy(scope, src, 0x40), "/"), name); + return save(s, c); } -static Macro * -findmacro(char *name) +static Item * +finditem(char *name, Item *list, int len) { int i; - for(i = 0; i < p.macro_len; i++) - if(scmp(p.macros[i].name, name, 0x40)) - return &p.macros[i]; - return NULL; -} - -static Label * -findlabel(char *name) -{ - int i; - for(i = 0; i < p.label_len; i++) - if(scmp(p.labels[i].name, name, 0x40)) - return &p.labels[i]; + if(name[0] == '&') + name = join(scope, '/', name + 1); + for(i = 0; i < len; i++) + if(scmp(list[i].name, name, 0x40)) + return &list[i]; return NULL; } @@ -125,19 +86,18 @@ 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 */ + 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); else - return 0; /* failed to match */ + return 0; m++; } return i; @@ -146,395 +106,309 @@ findopcode(char *s) } static int -makemacro(char *name, FILE *f) +walkcomment(FILE *f, Context *ctx) { - Macro *m; - char word[0x40]; - if(findmacro(name)) - return error_asm("Macro duplicate", name); - if(sihx(name) && slen(name) % 2 == 0) - return error_asm("Macro name is hex number", name); - if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name)) - return error_asm("Macro name is invalid", name); - if(p.macro_len == 0x100) - return error_asm("Macros limit exceeded", name); - m = &p.macros[p.macro_len++]; - 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); - if(m->len >= 0x40) - return error_asm("Macro size exceeded", name); - scpy(word, m->items[m->len++], 0x40); + 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; + } + return 0; +} + +static int +walkmacro(Item *m, Context *ctx) +{ + char c, *contentptr = m->content, *cptr = token; + while((c = *contentptr++)) { + if(c < 0x21) { + *cptr++ = 0x00; + if(token[0] && !parse(token, NULL, ctx)) return 0; + cptr = token; + } else + *cptr++ = c; } return 1; } static int -isrune(char c) +walkfile(FILE *f, Context *ctx) { - char cc, *r = runes; - while((cc = *r++)) - if(c == cc) return 1; - return 0; -} - -static int -makelabel(char *name) -{ - Label *l; - if(findlabel(name)) - return error_asm("Label duplicate", name); - if(sihx(name) && (slen(name) == 2 || slen(name) == 4)) - return error_asm("Label name is hex number", name); - if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name)) - return error_asm("Label name is invalid", name); - if(isrune(name[0])) - return error_asm("Label name is runic", name); - if(p.label_len == 0x400) - return error_asm("Labels limit exceeded", name); - l = &p.labels[p.label_len++]; - l->addr = p.ptr; - l->refs = 0; - scpy(name, l->name, 0x40); + 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"); + } return 1; } 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; + lambda[0] = (char)0xce; + lambda[1] = (char)0xbb; + lambda[2] = hexad[id >> 0x4]; + lambda[3] = hexad[id & 0xf]; + return lambda; } static int -makereference(char *scope, char *label, char rune, Uint16 addr) +makemacro(char *name, FILE *f, Context *ctx) { - char subw[0x40], parent[0x40]; - Reference *r; - if(p.refs_len >= 0x1000) - return error_asm("References limit exceeded", label); - r = &p.refs[p.refs_len++]; - 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); - scpy(subw, r->name, 0x40); - } else { - int pos = spos(label, '/'); - if(pos > 0) { - Label *l; - if((l = findlabel(scpy(label, parent, pos)))) - l->refs++; - } - scpy(label, r->name, 0x40); + 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 = ¯os[macro_len++]; + m->name = push(name, 0); + m->content = dictnext; + while(f && fread(&c, 1, 1, f) && c != '{') + if(c == 0xa) ctx->line += 1; + while(f && fread(&c, 1, 1, f) && c != '}') { + if(c == 0xa) ctx->line += 1; + if(c == '%') return 0; + if(c == '(') + walkcomment(f, ctx); + 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"); + if(isinvalid(name)) return error_asm("Label is invalid"); + if(findlabel(name)) return error_asm("Label is duplicate"); + l = &labels[labels_len++]; + l->name = push(name, 0); + l->addr = ptr; + l->refs = 0; + if(setscope) { + int i = 0; + while(name[i] != '/' && i < 0x3e && (scope[i] = name[i])) + i++; + scope[i] = '\0'; + } + return 1; +} + +static int +makeref(char *label, char rune, Uint16 addr) +{ + Item *r; + if(refs_len >= 0x1000) return error_top("References limit exceeded", label); + r = &refs[refs_len++]; + if(label[0] == '{') { + lambda_stack[lambda_ptr++] = lambda_len; + r->name = push(makelambda(lambda_len++), 0); + } else if(label[0] == '&' || label[0] == '/') { + r->name = join(scope, '/', label + 1); + } else + r->name = push(label, 0); r->rune = rune; r->addr = addr; return 1; } static int -writebyte(Uint8 b) +writepad(char *w) { - if(p.ptr < TRIM) - return error_asm("Writing in zero-page", ""); - else if(p.ptr > 0xffff) - return error_asm("Writing after the end of RAM", ""); - else if(p.ptr < p.length) - return error_asm("Memory overwrite", ""); - p.data[p.ptr++] = b; - p.length = p.ptr; + 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; + } + return 0; +} + +static int +writebyte(Uint8 b, Context *ctx) +{ + if(ptr < PAGE) + return error_asm("Writing in zero-page"); + 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; } static int -writeopcode(char *w) +writehex(char *w, Context *ctx) { - return writebyte(findopcode(w)); + if(*w == '#') + writebyte(findopcode("LIT") | !!(++w)[2] << 5, ctx); + if(!w[2]) + return writebyte(shex(w), ctx); + else if(!w[4]) + return writeshort(shex(w)); + else + return 0; } static int -writeshort(Uint16 s, int lit) +writestring(char *w, Context *ctx) { - if(lit) - if(!writebyte(findopcode("LIT2"))) return 0; - return writebyte(s >> 8) && writebyte(s & 0xff); + char c; + while((c = *(w++))) + if(!writebyte(c, ctx)) return 0; + return 1; } static int -writelitbyte(Uint8 b) -{ - return writebyte(findopcode("LIT")) && writebyte(b); -} - -static int -doinclude(char *filename) +assemble(char *filename) { FILE *f; - char w[0x40]; - if(!(f = fopen(setlocation(filename), "r"))) - return error_asm("Include missing", filename); - while(fscanf(f, "%63s", w) == 1) - if(!parse(w, f)) - return error_asm("Unknown token", w); + int res = 0; + Context ctx; + ctx.line = 0; + ctx.path = push(filename, 0); + if(!(f = fopen(filename, "r"))) + return error_top("Invalid source", filename); + res = walkfile(f, &ctx); fclose(f); - return 1; + return res; } static int -parse(char *w, FILE *f) +parse(char *w, FILE *f, Context *ctx) { - int i; - char word[0x40], subw[0x40], c; - Label *l; - Macro *m; - if(slen(w) >= 63) - return error_asm("Invalid token", w); + Item *m; 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); - break; - case '%': /* macro */ - if(!makemacro(w + 1, f)) - return error_asm("Invalid macro", w); - break; - case '|': /* pad-absolute */ - 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); - p.ptr = l->addr; - } else { - if(!(l = findlabel(w + 1))) - return error_asm("Invalid label", w); - p.ptr = l->addr; - } - break; - case '$': /* pad-relative */ - 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); - p.ptr += l->addr; - } else { - if(!(l = findlabel(w + 1))) - return error_asm("Invalid label", w); - p.ptr += l->addr; - } - 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'; - break; - case '&': /* sublabel */ - if(!sublabel(subw, p.scope, w + 1) || !makelabel(subw)) - return error_asm("Invalid sublabel", w); - break; - case '#': /* literals hex */ - 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); - break; - case '_': /* raw byte relative */ - return makereference(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff); - case ',': /* literal byte relative */ - return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writelitbyte(0xff); - case '-': /* raw byte absolute */ - return makereference(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff); - case '.': /* literal byte zero-page */ - 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 */ - return makereference(p.scope, w + 1, w[0], p.ptr) && writeshort(0xffff, 0); - case ';': /* literal short absolute */ - return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writeshort(0xffff, 1); - case '?': /* JCI */ - return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x20) && writeshort(0xffff, 0); - case '!': /* JMI */ - return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x40) && writeshort(0xffff, 0); - case '"': /* raw string */ - i = 0; - while((c = w[++i])) - if(!writebyte(c)) return 0; - break; - case '}': /* lambda end */ - if(!makelabel(makelambda(p.lambda_stack[--p.lambda_ptr]))) - return error_asm("Invalid label", w); - break; + case '(': return !walkcomment(f, ctx) ? error_asm("Invalid comment") : 1; + case '%': return !makemacro(w + 1, f, ctx) ? error_asm("Invalid macro") : 1; + case '@': return !makelabel(w + 1, 1, ctx) ? error_asm("Invalid label") : 1; + case '&': return !makelabel(w, 0, ctx) ? error_asm("Invalid sublabel") : 1; + case '}': return !makelabel(makelambda(lambda_stack[--lambda_ptr]), 0, ctx) ? error_asm("Invalid label") : 1; + case '#': return !ishex(w + 1) || !writehex(w, ctx) ? error_asm("Invalid hexadecimal") : 1; + case '_': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx); + case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx); + case '-': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx); + case '.': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx); + case ':': fprintf(stderr, "Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */ + case '=': return makeref(w + 1, w[0], ptr) && writeshort(0xffff); + case ';': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff); + case '?': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x20, ctx) && writeshort(0xffff); + case '!': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x40, ctx) && writeshort(0xffff); + case '"': return !writestring(w + 1, ctx) ? error_asm("Invalid string") : 1; + case '~': return !assemble(w + 1) ? error_asm("Invalid include") : 1; + case '$': + case '|': return !writepad(w) ? error_asm("Invalid padding") : 1; case '[': - case ']': - if(slen(w) == 1) break; /* else fallthrough */ - default: - /* opcode */ - if(findopcode(w) || scmp(w, "BRK", 4)) - return writeopcode(w); - /* raw byte */ - else if(sihx(w) && slen(w) == 2) - return writebyte(shex(w)); - /* raw short */ - else if(sihx(w) && slen(w) == 4) - return writeshort(shex(w), 0); - /* macro */ - else if((m = findmacro(w))) { - for(i = 0; i < m->len; i++) - if(!parse(m->items[i], f)) - return 0; - return 1; - } else - return makereference(p.scope, w, ' ', p.ptr + 1) && writebyte(0x60) && writeshort(0xffff, 0); + case ']': return 1; } - return 1; + if(ishex(w)) return writehex(w, ctx); + if(isopc(w)) return writebyte(findopcode(w), ctx); + if((m = findmacro(w))) return walkmacro(m, ctx); + return makeref(w, ' ', ptr + 1) && writebyte(0x60, ctx) && writeshort(0xffff); } static int resolve(void) { - Label *l; - int i; - Uint16 a; - for(i = 0; i < p.refs_len; i++) { - Reference *r = &p.refs[i]; + int i, rel; + if(!length) return error_top("Assembly", "Output rom is empty."); + for(i = 0; i < refs_len; i++) { + Item *r = &refs[i], *l = findlabel(r->name); + Uint8 *rom = data + r->addr; + if(!l) return 0; switch(r->rune) { case '_': case ',': - if(!(l = findlabel(r->name))) - return error_top("Unknown relative reference", r->name); - 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); - l->refs++; + *rom = rel = l->addr - r->addr - 2; + if((Sint8)data[r->addr] != rel) + return error_top("Relative reference is too far", r->name); break; case '-': case '.': - if(!(l = findlabel(r->name))) - return error_top("Unknown zero-page reference", r->name); - p.data[r->addr] = l->addr & 0xff; - l->refs++; + *rom = l->addr; break; case ':': case '=': case ';': - if(!(l = findlabel(r->name))) - return error_top("Unknown absolute reference", r->name); - p.data[r->addr] = l->addr >> 0x8; - p.data[r->addr + 1] = l->addr & 0xff; - l->refs++; + *rom++ = l->addr >> 8, *rom = l->addr; break; case '?': case '!': default: - if(!(l = findlabel(r->name))) - return error_top("Unknown absolute reference", r->name); - a = l->addr - r->addr - 2; - p.data[r->addr] = a >> 0x8; - p.data[r->addr + 1] = a & 0xff; - l->refs++; + rel = l->addr - r->addr - 2; + *rom++ = rel >> 8, *rom = rel; break; } + l->refs++; } return 1; } static int -assemble(FILE *f) -{ - char w[0x40]; - p.ptr = 0x100; - scpy("on-reset", p.scope, 0x40); - while(fscanf(f, "%62s", w) == 1) - if(slen(w) > 0x3d || !parse(w, f)) - return error_asm("Invalid token", w); - return resolve(); -} - -static void -review(char *filename) +build(char *rompath) { int i; - for(i = 0; i < p.label_len; i++) - 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(stdout, "-- Unused label: %s\n", p.labels[i].name); + FILE *dst, *dstsym; + char *sympath = join(rompath, '.', "sym"); + /* rom */ + if(!(dst = fopen(rompath, "wb"))) + return !error_top("Invalid output file", rompath); + for(i = 0; i < labels_len; i++) + if(labels[i].name[0] - 'A' > 25 && !labels[i].refs) + fprintf(stdout, "-- Unused label: %s\n", labels[i].name); + fwrite(data + PAGE, length - PAGE, 1, dst); fprintf(stdout, "Assembled %s in %d bytes(%.2f%% used), %d labels, %d macros.\n", - filename, - p.length - TRIM, - (p.length - TRIM) / 652.80, - p.label_len, - p.macro_len); -} - -static void -writesym(char *filename) -{ - int i; - char symdst[0x60]; - FILE *fp; - if(slen(filename) > 0x60 - 5) - return; - fp = fopen(scat(scpy(filename, symdst, slen(filename) + 1), ".sym"), "w"); - if(fp != NULL) { - for(i = 0; i < p.label_len; i++) { - Uint8 hb = p.labels[i].addr >> 8, lb = p.labels[i].addr & 0xff; - fwrite(&hb, 1, 1, fp); - fwrite(&lb, 1, 1, fp); - fwrite(p.labels[i].name, slen(p.labels[i].name) + 1, 1, fp); - } + rompath, + length - PAGE, + (length - PAGE) / 652.80, + labels_len, + macro_len); + /* sym */ + if(!(dstsym = fopen(sympath, "w"))) + return !error_top("Invalid symbols file", sympath); + 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); } - fclose(fp); + fclose(dst), fclose(dstsym); + return 1; } int main(int argc, char *argv[]) { - FILE *src, *dst; - if(argc == 1) - return error_top("usage", "uxnasm [-v] input.tal output.rom"); - if(argv[1][0] == '-' && argv[1][1] == 'v') - return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 7 Mar 2024.\n"); - if(!(src = fopen(setlocation(argv[1]), "r"))) - return !error_top("Invalid input", argv[1]); - p.entry = argv[1]; - if(!assemble(src)) - return !error_top("Assembly", "Failed to assemble rom."); - if(!(dst = fopen(argv[2], "wb"))) - return !error_top("Invalid Output", argv[2]); - if(p.length <= TRIM) - return !error_top("Assembly", "Output rom is empty."); - fwrite(p.data + TRIM, p.length - TRIM, 1, dst); - if(!scmp(argv[2], "-", 2)) { - review(argv[2]); - writesym(argv[2]); - } + ptr = PAGE; + scpy("on-reset", scope, 0x40); + if(argc == 1) return error_top("usage", "uxnasm [-v] input.tal output.rom"); + if(scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 28 Mar 2024.\n"); + if(!assemble(argv[1]) || !length) return !error_top("Assembly", "Failed to assemble rom."); + if(!resolve()) return !error_top("Assembly", "Failed to resolve symbols."); + if(!build(argv[2])) return !error_top("Assembly", "Failed to build rom."); return 0; -} +} \ No newline at end of file From e46c9b5d61a42222ea945a584c9cff533c94babc Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 28 Mar 2024 19:56:40 -0700 Subject: [PATCH 13/31] Added archive function to makefile --- makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/makefile b/makefile index a0b9b06..5f0991f 100644 --- a/makefile +++ b/makefile @@ -5,7 +5,7 @@ EMU_src=${CLI_src} src/devices/screen.c src/devices/controller.c src/devices/mou RELEASE_flags=-DNDEBUG -O2 -g0 -s DEBUG_flags=-std=c89 -D_POSIX_C_SOURCE=199309L -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined -.PHONY: all debug dest run test install uninstall format clean +.PHONY: all debug dest run test install uninstall format clean archive all: dest bin/uxnasm bin/uxncli bin/uxn11 @@ -23,6 +23,8 @@ uninstall: @ rm -f ~/bin/uxn11 ~/bin/uxnasm ~/bin/uxncli format: @ clang-format -i src/uxnasm.c src/uxncli.c src/uxn11.c src/devices/* +archive: + @ cp src/uxnasm.c ../oscean/etc/uxnasm.c.txt clean: @ rm -fr bin From 3662a6ed15c35fb21c616d41d5bbd7694c643147 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 28 Mar 2024 20:24:24 -0700 Subject: [PATCH 14/31] (uxnasm) Optimization --- src/uxnasm.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index cc68e13..24949db 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -40,7 +40,7 @@ static char ops[][4] = { static int find(char *s, char t) { int i = 0; char c; while((c = *s++)) { if(c == t) return i; i++; } return -1; } /* chr in str */ 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; } /* str to hex */ 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; } /* str compare */ -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; } /* str copy */ +static char *copy(char *src, char *dst, char c) { while(*src && *src != c) *dst++ = *src++; *dst++ = 0; return dst; } /* str copy */ static char *save(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s++) && *s); *dictnext++ = c; return o; } /* save to dict */ static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), save(b, 0); return res; } /* join two str */ @@ -200,12 +200,7 @@ makelabel(char *name, int setscope, Context *ctx) l->name = push(name, 0); l->addr = ptr; l->refs = 0; - if(setscope) { - int i = 0; - while(name[i] != '/' && i < 0x3e && (scope[i] = name[i])) - i++; - scope[i] = '\0'; - } + if(setscope) copy(name, scope, '/'); return 1; } @@ -404,7 +399,7 @@ int main(int argc, char *argv[]) { ptr = PAGE; - scpy("on-reset", scope, 0x40); + copy("on-reset", scope, 0); if(argc == 1) return error_top("usage", "uxnasm [-v] input.tal output.rom"); if(scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 28 Mar 2024.\n"); if(!assemble(argv[1]) || !length) return !error_top("Assembly", "Failed to assemble rom."); From 28b72a459a33f12d6ea281317c85794eae66eff8 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 28 Mar 2024 20:32:58 -0700 Subject: [PATCH 15/31] (uxnasm) Larger dict buffer --- src/uxnasm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 24949db..795f5a9 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -23,7 +23,7 @@ typedef struct { int line; char *path; } Context; static int ptr, length; static char token[0x40], scope[0x40], lambda[0x05]; -static char dict[0x4000], *dictnext = dict; +static char dict[0x8000], *dictnext = dict; 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]; From da54634b74999d689b112caea306489404342fe4 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Fri, 29 Mar 2024 10:25:51 -0700 Subject: [PATCH 16/31] (uxnasm) More explicit resolve errors --- src/uxnasm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 795f5a9..780b629 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -97,7 +97,7 @@ findopcode(char *s) else if(s[m] == 'k') i |= (1 << 7); else - return 0; + return error_top("Unknown opcode mode", s); m++; } return i; @@ -177,7 +177,7 @@ makemacro(char *name, FILE *f, Context *ctx) if(c == 0xa) ctx->line += 1; while(f && fread(&c, 1, 1, f) && c != '}') { if(c == 0xa) ctx->line += 1; - if(c == '%') return 0; + if(c == '%') return error_top("Nested macro", name); if(c == '(') walkcomment(f, ctx); else @@ -331,7 +331,7 @@ resolve(void) for(i = 0; i < refs_len; i++) { Item *r = &refs[i], *l = findlabel(r->name); Uint8 *rom = data + r->addr; - if(!l) return 0; + if(!l) return error_top("Unknown label", r->name); switch(r->rune) { case '_': case ',': @@ -401,7 +401,7 @@ main(int argc, char *argv[]) ptr = PAGE; copy("on-reset", scope, 0); if(argc == 1) return error_top("usage", "uxnasm [-v] input.tal output.rom"); - if(scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 28 Mar 2024.\n"); + if(scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 29 Mar 2024.\n"); if(!assemble(argv[1]) || !length) return !error_top("Assembly", "Failed to assemble rom."); if(!resolve()) return !error_top("Assembly", "Failed to resolve symbols."); if(!build(argv[2])) return !error_top("Assembly", "Failed to build rom."); From 3fda89b6093dd508c08c652c0bc60d42934c3ba4 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Fri, 29 Mar 2024 10:42:58 -0700 Subject: [PATCH 17/31] (uxnasm) Catch missized #hex --- src/uxnasm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 780b629..d69c313 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -258,9 +258,9 @@ writehex(char *w, Context *ctx) { if(*w == '#') writebyte(findopcode("LIT") | !!(++w)[2] << 5, ctx); - if(!w[2]) + if(w[1] && !w[2]) return writebyte(shex(w), ctx); - else if(!w[4]) + else if(w[3] && !w[4]) return writeshort(shex(w)); else return 0; From 618dc4e84304bec070eae44aa39182fc0a554423 Mon Sep 17 00:00:00 2001 From: Lobo Torres Date: Fri, 29 Mar 2024 15:35:08 -0300 Subject: [PATCH 18/31] (uxnasm) Explicit error for empty rom --- src/uxnasm.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index d69c313..7af82ec 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -400,10 +400,11 @@ main(int argc, char *argv[]) { ptr = PAGE; copy("on-reset", scope, 0); - if(argc == 1) return error_top("usage", "uxnasm [-v] input.tal output.rom"); - if(scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 29 Mar 2024.\n"); - if(!assemble(argv[1]) || !length) return !error_top("Assembly", "Failed to assemble rom."); + if(argc == 2 && scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 29 Mar 2024.\n"); + if(argc != 3) return error_top("usage", "uxnasm [-v] input.tal output.rom"); + if(!assemble(argv[1])) return !error_top("Assembly", "Failed to assemble rom."); + if(!length) return !error_top("Assembly", "Output rom is empty."); if(!resolve()) return !error_top("Assembly", "Failed to resolve symbols."); if(!build(argv[2])) return !error_top("Assembly", "Failed to build rom."); return 0; -} \ No newline at end of file +} From 4f3ced955228832d7b73a0dcc681187ea85d7596 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Sat, 30 Mar 2024 19:53:04 -0700 Subject: [PATCH 19/31] (uxnasm) Improved errors --- src/uxnasm.c | 65 ++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 7af82ec..024f64a 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -97,7 +97,7 @@ findopcode(char *s) else if(s[m] == 'k') i |= (1 << 7); else - return error_top("Unknown opcode mode", s); + return 0; m++; } return i; @@ -115,7 +115,7 @@ walkcomment(FILE *f, Context *ctx) if(c == '(') depth++; if(c == ')' && --depth < 1) return 1; } - return 0; + return error_asm("Comment incomplete"); } static int @@ -177,9 +177,9 @@ makemacro(char *name, FILE *f, Context *ctx) if(c == 0xa) ctx->line += 1; while(f && fread(&c, 1, 1, f) && c != '}') { if(c == 0xa) ctx->line += 1; - if(c == '%') return error_top("Nested macro", name); - if(c == '(') - walkcomment(f, ctx); + if(c == '%') return error_top("Macro nested", name); + if(c == '(' && !walkcomment(f, ctx)) + return 0; else *dictnext++ = c; } @@ -194,8 +194,8 @@ makelabel(char *name, int setscope, Context *ctx) if(name[0] == '&') name = join(scope, '/', name + 1); if(labels_len >= 0x400) return error_asm("Labels limit exceeded"); - if(isinvalid(name)) return error_asm("Label is invalid"); - if(findlabel(name)) return error_asm("Label is duplicate"); + if(isinvalid(name)) return error_asm("Label invalid"); + if(findlabel(name)) return error_asm("Label duplicate"); l = &labels[labels_len++]; l->name = push(name, 0); l->addr = ptr; @@ -223,7 +223,7 @@ makeref(char *label, char rune, Uint16 addr) } static int -writepad(char *w) +writepad(char *w, Context *ctx) { Item *l; int rel = w[0] == '$' ? ptr : 0; @@ -235,14 +235,14 @@ writepad(char *w) ptr = l->addr + rel; return 1; } - return 0; + return error_asm("Padding invalid"); } static int writebyte(Uint8 b, Context *ctx) { if(ptr < PAGE) - return error_asm("Writing in zero-page"); + return error_asm("Writing zero-page"); else if(ptr >= 0x10000) return error_asm("Writing outside memory"); else if(ptr < length) @@ -263,7 +263,7 @@ writehex(char *w, Context *ctx) else if(w[3] && !w[4]) return writeshort(shex(w)); else - return 0; + return error_asm("Hexadecimal invalid"); } static int @@ -271,7 +271,7 @@ writestring(char *w, Context *ctx) { char c; while((c = *(w++))) - if(!writebyte(c, ctx)) return 0; + if(!writebyte(c, ctx)) return error_asm("String invalid"); return 1; } @@ -284,7 +284,7 @@ assemble(char *filename) ctx.line = 0; ctx.path = push(filename, 0); if(!(f = fopen(filename, "r"))) - return error_top("Invalid source", filename); + return error_top("Source missing", filename); res = walkfile(f, &ctx); fclose(f); return res; @@ -295,12 +295,12 @@ parse(char *w, FILE *f, Context *ctx) { Item *m; switch(w[0]) { - case '(': return !walkcomment(f, ctx) ? error_asm("Invalid comment") : 1; - case '%': return !makemacro(w + 1, f, ctx) ? error_asm("Invalid macro") : 1; - case '@': return !makelabel(w + 1, 1, ctx) ? error_asm("Invalid label") : 1; - case '&': return !makelabel(w, 0, ctx) ? error_asm("Invalid sublabel") : 1; - case '}': return !makelabel(makelambda(lambda_stack[--lambda_ptr]), 0, ctx) ? error_asm("Invalid label") : 1; - case '#': return !ishex(w + 1) || !writehex(w, ctx) ? error_asm("Invalid hexadecimal") : 1; + 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 ishex(w + 1) && writehex(w, ctx); case '_': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx); case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx); case '-': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx); @@ -310,10 +310,10 @@ parse(char *w, FILE *f, Context *ctx) case ';': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff); case '?': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x20, ctx) && writeshort(0xffff); case '!': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x40, ctx) && writeshort(0xffff); - case '"': return !writestring(w + 1, ctx) ? error_asm("Invalid string") : 1; - case '~': return !assemble(w + 1) ? error_asm("Invalid include") : 1; + case '"': return writestring(w + 1, ctx); + case '~': return !assemble(w + 1) ? error_asm("Include missing") : 1; case '$': - case '|': return !writepad(w) ? error_asm("Invalid padding") : 1; + case '|': return writepad(w, ctx); case '[': case ']': return 1; } @@ -324,20 +324,20 @@ parse(char *w, FILE *f, Context *ctx) } static int -resolve(void) +resolve(char *filename) { int i, rel; - if(!length) return error_top("Assembly", "Output rom is empty."); + if(!length) return error_top("Output empty", filename); for(i = 0; i < refs_len; i++) { Item *r = &refs[i], *l = findlabel(r->name); Uint8 *rom = data + r->addr; - if(!l) return error_top("Unknown label", r->name); + if(!l) return error_top("Label unknown", r->name); switch(r->rune) { case '_': case ',': *rom = rel = l->addr - r->addr - 2; if((Sint8)data[r->addr] != rel) - return error_top("Relative reference is too far", r->name); + return error_top("Relative reference too far", r->name); break; case '-': case '.': @@ -368,7 +368,7 @@ build(char *rompath) char *sympath = join(rompath, '.', "sym"); /* rom */ if(!(dst = fopen(rompath, "wb"))) - return !error_top("Invalid output file", rompath); + return !error_top("Output file invalid", rompath); for(i = 0; i < labels_len; i++) if(labels[i].name[0] - 'A' > 25 && !labels[i].refs) fprintf(stdout, "-- Unused label: %s\n", labels[i].name); @@ -382,7 +382,7 @@ build(char *rompath) macro_len); /* sym */ if(!(dstsym = fopen(sympath, "w"))) - return !error_top("Invalid symbols file", sympath); + return !error_top("Symbols file invalid", sympath); 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; @@ -400,11 +400,10 @@ main(int argc, char *argv[]) { ptr = PAGE; copy("on-reset", scope, 0); - if(argc == 2 && scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 29 Mar 2024.\n"); + if(argc == 2 && scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 30 Mar 2024.\n"); if(argc != 3) return error_top("usage", "uxnasm [-v] input.tal output.rom"); - if(!assemble(argv[1])) return !error_top("Assembly", "Failed to assemble rom."); - if(!length) return !error_top("Assembly", "Output rom is empty."); - if(!resolve()) return !error_top("Assembly", "Failed to resolve symbols."); - if(!build(argv[2])) return !error_top("Assembly", "Failed to build rom."); + if(!assemble(argv[1])) return 1; + if(!resolve(argv[2])) return 1; + if(!build(argv[2])) return 1; return 0; } From 6bc1232bad3c1b398a3f0b53b30a561d05b6493d Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Tue, 2 Apr 2024 08:38:01 -0700 Subject: [PATCH 20/31] (uxnasm) Start line number at 1 --- src/uxnasm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 024f64a..df8442f 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -281,7 +281,7 @@ assemble(char *filename) FILE *f; int res = 0; Context ctx; - ctx.line = 0; + ctx.line = 1; ctx.path = push(filename, 0); if(!(f = fopen(filename, "r"))) return error_top("Source missing", filename); From f4df46b5b74990e24ef935c5c224109deef2a640 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Tue, 2 Apr 2024 12:03:24 -0700 Subject: [PATCH 21/31] Allow lambdas in macros --- src/uxnasm.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index df8442f..0efd08e 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -165,6 +165,7 @@ makelambda(int id) static int makemacro(char *name, FILE *f, Context *ctx) { + int depth = 0; char c; Item *m; if(macro_len >= 0x100) return error_asm("Macros limit exceeded"); @@ -175,9 +176,11 @@ makemacro(char *name, FILE *f, Context *ctx) m->content = dictnext; while(f && fread(&c, 1, 1, f) && c != '{') if(c == 0xa) ctx->line += 1; - while(f && fread(&c, 1, 1, f) && c != '}') { + while(f && fread(&c, 1, 1, f)) { if(c == 0xa) ctx->line += 1; if(c == '%') return error_top("Macro nested", name); + if(c == '{') depth++; + if(c == '}' && --depth) break; if(c == '(' && !walkcomment(f, ctx)) return 0; else @@ -400,7 +403,7 @@ main(int argc, char *argv[]) { ptr = PAGE; copy("on-reset", scope, 0); - if(argc == 2 && scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 30 Mar 2024.\n"); + if(argc == 2 && scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 2 Apr 2024.\n"); if(argc != 3) return error_top("usage", "uxnasm [-v] input.tal output.rom"); if(!assemble(argv[1])) return 1; if(!resolve(argv[2])) return 1; From 10bb9334903aaaea543c65e3502a57d37c7f17ef Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Tue, 2 Apr 2024 15:12:00 -0700 Subject: [PATCH 22/31] (uxnasm) Output to stdout --- etc/hello.tal | 6 ++++++ src/uxnasm.c | 12 ++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 etc/hello.tal diff --git a/etc/hello.tal b/etc/hello.tal new file mode 100644 index 0000000..260f119 --- /dev/null +++ b/etc/hello.tal @@ -0,0 +1,6 @@ +%square { DUP MUL ?{ } } + +|100 + #12 square +BRK + diff --git a/src/uxnasm.c b/src/uxnasm.c index 0efd08e..654778c 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -50,8 +50,8 @@ static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), #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(name, msg) !fprintf(stderr, "%s: %s\n", name, msg) -#define error_asm(name) !fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, token, scope, ctx->path, ctx->line) +#define error_top(name, msg) !printf("%s: %s\n", name, msg) +#define error_asm(name) !printf("%s: %s in @%s, %s:%d.\n", name, token, scope, ctx->path, ctx->line) /* clang-format on */ @@ -308,7 +308,7 @@ parse(char *w, FILE *f, Context *ctx) case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx); case '-': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx); case '.': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx); - case ':': fprintf(stderr, "Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */ + case ':': printf("Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */ case '=': return makeref(w + 1, w[0], ptr) && writeshort(0xffff); case ';': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff); case '?': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x20, ctx) && writeshort(0xffff); @@ -374,9 +374,9 @@ build(char *rompath) return !error_top("Output file invalid", rompath); for(i = 0; i < labels_len; i++) if(labels[i].name[0] - 'A' > 25 && !labels[i].refs) - fprintf(stdout, "-- Unused label: %s\n", labels[i].name); + printf("-- Unused label: %s\n", labels[i].name); fwrite(data + PAGE, length - PAGE, 1, dst); - fprintf(stdout, + printf( "Assembled %s in %d bytes(%.2f%% used), %d labels, %d macros.\n", rompath, length - PAGE, @@ -403,7 +403,7 @@ main(int argc, char *argv[]) { ptr = PAGE; copy("on-reset", scope, 0); - if(argc == 2 && scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 2 Apr 2024.\n"); + if(argc == 2 && scmp(argv[1], "-v", 2)) return !printf("Uxnasm - Uxntal Assembler, 2 Apr 2024.\n"); if(argc != 3) return error_top("usage", "uxnasm [-v] input.tal output.rom"); if(!assemble(argv[1])) return 1; if(!resolve(argv[2])) return 1; From 364172ff5b0b2be295917b111346ee885a1c46ab Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 3 Apr 2024 09:07:51 -0700 Subject: [PATCH 23/31] (uxnasm) Fixed issue with long hex values --- src/uxnasm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 654778c..d6ef190 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -303,7 +303,7 @@ parse(char *w, FILE *f, Context *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 ishex(w + 1) && writehex(w, ctx); + case '#': return writehex(w, ctx); case '_': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx); case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx); case '-': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx); @@ -403,7 +403,7 @@ main(int argc, char *argv[]) { ptr = PAGE; copy("on-reset", scope, 0); - if(argc == 2 && scmp(argv[1], "-v", 2)) return !printf("Uxnasm - Uxntal Assembler, 2 Apr 2024.\n"); + if(argc == 2 && scmp(argv[1], "-v", 2)) return !printf("Uxnasm - Uxntal Assembler, 3 Apr 2024.\n"); if(argc != 3) return error_top("usage", "uxnasm [-v] input.tal output.rom"); if(!assemble(argv[1])) return 1; if(!resolve(argv[2])) return 1; From 8e8b179a25b1b5bca888e1828a9dba9581c18f2f Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 3 Apr 2024 19:19:26 -0700 Subject: [PATCH 24/31] (uxnasm) Fixed issue with skipped last token in include --- src/uxnasm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index d6ef190..3eebac1 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -149,7 +149,8 @@ walkfile(FILE *f, Context *ctx) else return error_asm("Token too long"); } - return 1; + *cptr++ = 0; + return parse(token, f, ctx); } static char * From 0df50c45ac039e0715eef0001e52d219ffc2137a Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 3 Apr 2024 19:37:06 -0700 Subject: [PATCH 25/31] (uxnasm) Report unknown mode --- src/uxnasm.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 3eebac1..27ee1bd 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -45,7 +45,7 @@ static char *save(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), save(b, 0); return res; } /* join two str */ #define ishex(x) (shex(x) >= 0) -#define isopc(x) (findopcode(x) || scmp(x, "BRK", 4)) +#define isopc(x) (findopcode(x, ctx) || scmp(x, "BRK", 4)) #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) @@ -82,7 +82,7 @@ finditem(char *name, Item *list, int len) } static Uint8 -findopcode(char *s) +findopcode(char *s, Context *ctx) { int i; for(i = 0; i < 0x20; i++) { @@ -97,7 +97,7 @@ findopcode(char *s) else if(s[m] == 'k') i |= (1 << 7); else - return 0; + return error_asm("Opcode mode unknown"); m++; } return i; @@ -261,7 +261,7 @@ static int writehex(char *w, Context *ctx) { if(*w == '#') - writebyte(findopcode("LIT") | !!(++w)[2] << 5, ctx); + writebyte(findopcode("LIT", ctx) | !!(++w)[2] << 5, ctx); if(w[1] && !w[2]) return writebyte(shex(w), ctx); else if(w[3] && !w[4]) @@ -299,6 +299,7 @@ parse(char *w, FILE *f, Context *ctx) { Item *m; switch(w[0]) { + case 0x0: return 1; case '(': return walkcomment(f, ctx); case '%': return makemacro(w + 1, f, ctx); case '@': return makelabel(w + 1, 1, ctx); @@ -306,12 +307,12 @@ parse(char *w, FILE *f, Context *ctx) case '}': return makelabel(makelambda(lambda_stack[--lambda_ptr]), 0, ctx); case '#': return writehex(w, ctx); case '_': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx); - case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx); + case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT", ctx), ctx) && writebyte(0xff, ctx); case '-': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx); - case '.': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx); + case '.': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT", ctx), ctx) && writebyte(0xff, ctx); case ':': printf("Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */ case '=': return makeref(w + 1, w[0], ptr) && writeshort(0xffff); - case ';': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff); + case ';': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2", ctx), ctx) && writeshort(0xffff); case '?': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x20, ctx) && writeshort(0xffff); case '!': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x40, ctx) && writeshort(0xffff); case '"': return writestring(w + 1, ctx); @@ -322,7 +323,7 @@ parse(char *w, FILE *f, Context *ctx) case ']': return 1; } if(ishex(w)) return writehex(w, ctx); - if(isopc(w)) return writebyte(findopcode(w), ctx); + if(isopc(w)) return writebyte(findopcode(w, ctx), ctx); if((m = findmacro(w))) return walkmacro(m, ctx); return makeref(w, ' ', ptr + 1) && writebyte(0x60, ctx) && writeshort(0xffff); } From c298c6fab4a98a9e3e0381d35ecf9fbd952adf72 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 3 Apr 2024 19:46:26 -0700 Subject: [PATCH 26/31] (uxnasm) No context for findopcode --- src/uxnasm.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 27ee1bd..bfb9dce 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -45,7 +45,7 @@ static char *save(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), save(b, 0); return res; } /* join two str */ #define ishex(x) (shex(x) >= 0) -#define isopc(x) (findopcode(x, ctx) || scmp(x, "BRK", 4)) +#define isopc(x) (findopcode(x) || scmp(x, "BRK", 4)) #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) @@ -82,7 +82,7 @@ finditem(char *name, Item *list, int len) } static Uint8 -findopcode(char *s, Context *ctx) +findopcode(char *s) { int i; for(i = 0; i < 0x20; i++) { @@ -97,7 +97,7 @@ findopcode(char *s, Context *ctx) else if(s[m] == 'k') i |= (1 << 7); else - return error_asm("Opcode mode unknown"); + return 0; m++; } return i; @@ -261,7 +261,7 @@ static int writehex(char *w, Context *ctx) { if(*w == '#') - writebyte(findopcode("LIT", ctx) | !!(++w)[2] << 5, ctx); + writebyte(findopcode("LIT") | !!(++w)[2] << 5, ctx); if(w[1] && !w[2]) return writebyte(shex(w), ctx); else if(w[3] && !w[4]) @@ -307,12 +307,12 @@ parse(char *w, FILE *f, Context *ctx) case '}': return makelabel(makelambda(lambda_stack[--lambda_ptr]), 0, ctx); case '#': return writehex(w, ctx); case '_': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx); - case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT", ctx), ctx) && writebyte(0xff, ctx); + case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx); case '-': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx); - case '.': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT", ctx), ctx) && writebyte(0xff, ctx); + case '.': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx); case ':': printf("Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */ case '=': return makeref(w + 1, w[0], ptr) && writeshort(0xffff); - case ';': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2", ctx), ctx) && writeshort(0xffff); + case ';': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff); case '?': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x20, ctx) && writeshort(0xffff); case '!': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x40, ctx) && writeshort(0xffff); case '"': return writestring(w + 1, ctx); @@ -323,7 +323,7 @@ parse(char *w, FILE *f, Context *ctx) case ']': return 1; } if(ishex(w)) return writehex(w, ctx); - if(isopc(w)) return writebyte(findopcode(w, ctx), ctx); + if(isopc(w)) return writebyte(findopcode(w), ctx); if((m = findmacro(w))) return walkmacro(m, ctx); return makeref(w, ' ', ptr + 1) && writebyte(0x60, ctx) && writeshort(0xffff); } From 1e665423042a07af06fe1e169726acc50187624f Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 3 Apr 2024 20:20:40 -0700 Subject: [PATCH 27/31] (uxnasm) References print file and line number --- src/uxnasm.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index bfb9dce..edc57fb 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -18,8 +18,8 @@ WITH REGARD TO THIS SOFTWARE. typedef unsigned char Uint8; typedef signed char Sint8; typedef unsigned short Uint16; -typedef struct { char *name, rune, *content; Uint16 addr, refs; } Item; typedef struct { int line; char *path; } Context; +typedef struct { char *name, rune, *data; Uint16 addr, refs, line; } Item; static int ptr, length; static char token[0x40], scope[0x40], lambda[0x05]; @@ -37,12 +37,12 @@ static char ops[][4] = { "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; } /* chr in str */ -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; } /* str to hex */ -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; } /* str compare */ -static char *copy(char *src, char *dst, char c) { while(*src && *src != c) *dst++ = *src++; *dst++ = 0; return dst; } /* str copy */ -static char *save(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s++) && *s); *dictnext++ = c; return o; } /* save to dict */ -static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), save(b, 0); return res; } /* join two str */ +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; } #define ishex(x) (shex(x) >= 0) #define isopc(x) (findopcode(x) || scmp(x, "BRK", 4)) @@ -50,8 +50,9 @@ static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), #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(name, msg) !printf("%s: %s\n", name, msg) -#define error_asm(name) !printf("%s: %s in @%s, %s:%d.\n", name, token, scope, ctx->path, ctx->line) +#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) /* clang-format on */ @@ -121,8 +122,8 @@ walkcomment(FILE *f, Context *ctx) static int walkmacro(Item *m, Context *ctx) { - char c, *contentptr = m->content, *cptr = token; - while((c = *contentptr++)) { + char c, *dataptr = m->data, *cptr = token; + while((c = *dataptr++)) { if(c < 0x21) { *cptr++ = 0x00; if(token[0] && !parse(token, NULL, ctx)) return 0; @@ -174,7 +175,7 @@ makemacro(char *name, FILE *f, Context *ctx) if(findmacro(name)) return error_asm("Macro is duplicate"); m = ¯os[macro_len++]; m->name = push(name, 0); - m->content = dictnext; + m->data = dictnext; while(f && fread(&c, 1, 1, f) && c != '{') if(c == 0xa) ctx->line += 1; while(f && fread(&c, 1, 1, f)) { @@ -209,7 +210,7 @@ makelabel(char *name, int setscope, Context *ctx) } static int -makeref(char *label, char rune, Uint16 addr) +makeref(char *label, char rune, Uint16 addr, Context *ctx) { Item *r; if(refs_len >= 0x1000) return error_top("References limit exceeded", label); @@ -223,6 +224,8 @@ makeref(char *label, char rune, Uint16 addr) r->name = push(label, 0); r->rune = rune; r->addr = addr; + r->line = ctx->line; + r->data = ctx->path; return 1; } @@ -306,15 +309,15 @@ parse(char *w, FILE *f, Context *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) && writebyte(0xff, ctx); - case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx); - case '-': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx); - case '.': return makeref(w + 1, w[0], ptr + 1) && 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); + 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 ':': printf("Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */ - case '=': return makeref(w + 1, w[0], ptr) && writeshort(0xffff); - case ';': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff); - case '?': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x20, ctx) && writeshort(0xffff); - case '!': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x40, ctx) && writeshort(0xffff); + 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); case '"': return writestring(w + 1, ctx); case '~': return !assemble(w + 1) ? error_asm("Include missing") : 1; case '$': @@ -325,7 +328,7 @@ parse(char *w, FILE *f, Context *ctx) if(ishex(w)) return writehex(w, ctx); if(isopc(w)) return writebyte(findopcode(w), ctx); if((m = findmacro(w))) return walkmacro(m, ctx); - return makeref(w, ' ', ptr + 1) && writebyte(0x60, ctx) && writeshort(0xffff); + return makeref(w, ' ', ptr + 1, ctx) && writebyte(0x60, ctx) && writeshort(0xffff); } static int @@ -336,13 +339,13 @@ resolve(char *filename) for(i = 0; i < refs_len; i++) { Item *r = &refs[i], *l = findlabel(r->name); Uint8 *rom = data + r->addr; - if(!l) return error_top("Label unknown", r->name); + if(!l) return error_ref("Label unknown"); switch(r->rune) { case '_': case ',': *rom = rel = l->addr - r->addr - 2; if((Sint8)data[r->addr] != rel) - return error_top("Relative reference too far", r->name); + return error_ref("Relative reference too far"); break; case '-': case '.': From 04740acf950a77f4b344892014ddff6e79c9a970 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 3 Apr 2024 20:34:58 -0700 Subject: [PATCH 28/31] (uxnasm) Report the correct line number in references --- src/uxnasm.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index edc57fb..31ce2d7 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -112,7 +112,7 @@ walkcomment(FILE *f, Context *ctx) char c; int depth = 1; while(f && fread(&c, 1, 1, f)) { - if(c == 0xa) ctx->line += 1; + if(c == 0xa) ctx->line++; if(c == '(') depth++; if(c == ')' && --depth < 1) return 1; } @@ -139,11 +139,10 @@ walkfile(FILE *f, Context *ctx) { 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; + if(token[0] && !parse(token, f, ctx)) return 0; + if(c == 0xa) ctx->line++; cptr = token; } else if(cptr - token < 0x3f) *cptr++ = c; @@ -177,9 +176,9 @@ makemacro(char *name, FILE *f, Context *ctx) m->name = push(name, 0); m->data = dictnext; while(f && fread(&c, 1, 1, f) && c != '{') - if(c == 0xa) ctx->line += 1; + if(c == 0xa) ctx->line++; while(f && fread(&c, 1, 1, f)) { - if(c == 0xa) ctx->line += 1; + if(c == 0xa) ctx->line++; if(c == '%') return error_top("Macro nested", name); if(c == '{') depth++; if(c == '}' && --depth) break; From 183554dd69669bd8fa65845dd3a67b85bf4cd9b4 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 3 Apr 2024 20:57:26 -0700 Subject: [PATCH 29/31] (uxnasm) Only find hex glyph once --- src/uxnasm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 31ce2d7..2bdfccb 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -38,7 +38,7 @@ static char ops[][4] = { }; 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 shex(char *s) { int d, n = 0; char c; while((c = *s++)) { d = find(hexad, c); if(d < 0) return d; n = n << 4, n |= d; } 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; } From 9dfcb0ecf04fd20ff4026f2ff582a44ad126943f Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 3 Apr 2024 21:07:56 -0700 Subject: [PATCH 30/31] (uxnasm) Print token during nested macro error --- src/uxnasm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 2bdfccb..c036c97 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -179,7 +179,7 @@ makemacro(char *name, FILE *f, Context *ctx) if(c == 0xa) ctx->line++; while(f && fread(&c, 1, 1, f)) { if(c == 0xa) ctx->line++; - if(c == '%') return error_top("Macro nested", name); + if(c == '%') return error_asm("Macro nested"); if(c == '{') depth++; if(c == '}' && --depth) break; if(c == '(' && !walkcomment(f, ctx)) @@ -212,7 +212,7 @@ static int makeref(char *label, char rune, Uint16 addr, Context *ctx) { Item *r; - if(refs_len >= 0x1000) return error_top("References limit exceeded", label); + if(refs_len >= 0x1000) return error_asm("References limit exceeded"); r = &refs[refs_len++]; if(label[0] == '{') { lambda_stack[lambda_ptr++] = lambda_len; From f590734916f5846e7be543760f11fa1ba7994b95 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Sat, 13 Apr 2024 19:31:26 -0700 Subject: [PATCH 31/31] Fix non-ASCII bytes being detected as whitespace --- src/uxnasm.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index c036c97..762b9a9 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -122,7 +122,8 @@ walkcomment(FILE *f, Context *ctx) static int walkmacro(Item *m, Context *ctx) { - char c, *dataptr = m->data, *cptr = token; + unsigned char c; + char *dataptr = m->data, *cptr = token; while((c = *dataptr++)) { if(c < 0x21) { *cptr++ = 0x00; @@ -137,7 +138,8 @@ walkmacro(Item *m, Context *ctx) static int walkfile(FILE *f, Context *ctx) { - char c, *cptr = token; + unsigned char c; + char *cptr = token; while(f && fread(&c, 1, 1, f)) { if(c < 0x21) { *cptr++ = 0x00; @@ -407,7 +409,7 @@ main(int argc, char *argv[]) { ptr = PAGE; copy("on-reset", scope, 0); - if(argc == 2 && scmp(argv[1], "-v", 2)) return !printf("Uxnasm - Uxntal Assembler, 3 Apr 2024.\n"); + if(argc == 2 && scmp(argv[1], "-v", 2)) return !printf("Uxnasm - Uxntal Assembler, 13 Apr 2024.\n"); if(argc != 3) return error_top("usage", "uxnasm [-v] input.tal output.rom"); if(!assemble(argv[1])) return 1; if(!resolve(argv[2])) return 1;