uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

commit f3426fa886dc8b199d07e3fdf549e030a2fe3614
parent 6ac543f312ae222ca445b90db32a1d0f8c88f8ad
Author: neauoire <aliceffekt@gmail.com>
Date:   Thu,  4 Feb 2021 13:49:03 -0800

Improved assembler

Diffstat:
Mexamples/core.usm | 10++++++++--
Muxn.c | 39+++++++++++++++++++++++----------------
Muxnasm.c | 57+++++++++++++++++++++++++++++++--------------------------
3 files changed, 62 insertions(+), 44 deletions(-)

diff --git a/examples/core.usm b/examples/core.usm @@ -1,3 +1,9 @@ ( comment ) -,abcd ,ef STR -\ No newline at end of file +;var1 +;var2 +;var3 + +,ef ,var2 STR + +:label1 +\ No newline at end of file diff --git a/uxn.c b/uxn.c @@ -56,7 +56,7 @@ getflag(char flag) } void -echo(Stack *s, Uint8 len, char *name) +echos(Stack *s, Uint8 len, char *name) { int i; printf("%s\n", name); @@ -68,21 +68,32 @@ echo(Stack *s, Uint8 len, char *name) printf("\n\n"); } +void +echom(Memory *m, Uint8 len, char *name) +{ + int i; + printf("%s\n", name); + for(i = 0; i < len; ++i) { + if(i % 16 == 0) + printf("\n"); + printf("%02x ", m->dat[i]); + } + printf("\n\n"); +} + #pragma mark - Operations /* clang-format off */ -void wspush(Uint8 v) { cpu.wst.dat[cpu.wst.ptr++] = v; } +void wspush(Uint8 b) { cpu.wst.dat[cpu.wst.ptr++] = b; } Uint8 wspop(void) { return cpu.wst.dat[--cpu.wst.ptr]; } -Uint16 wspop16(void) { - - Uint8 a = cpu.wst.dat[--cpu.wst.ptr]; - Uint8 b = cpu.wst.dat[--cpu.wst.ptr]; - return a + (b << 8); +void wspush16(Uint16 s) { + printf("0x%04x\n", s); } +Uint16 wspop16(void) { return wspop() + (wspop() << 8); } Uint8 wspeek(void) { return cpu.wst.dat[cpu.wst.ptr - 1]; } -void rspush(Uint8 v) { cpu.rst.dat[cpu.rst.ptr++] = v; } +void rspush(Uint8 b) { cpu.rst.dat[cpu.rst.ptr++] = b; } Uint8 rspop(void) { return cpu.rst.dat[--cpu.rst.ptr]; } void op_brk() { setflag(FLAG_HALT, 1); } @@ -109,13 +120,8 @@ void op_add() { wspush(wspop() + wspop()); } void op_sub() { wspush(wspop() - wspop()); } void op_mul() { wspush(wspop() * wspop()); } void op_div() { wspush(wspop() / wspop()); } -void op_ldr() { } -void op_str() { - - Uint8 b = wspop(); - Uint16 addr = wspop16(); - printf("store: %02x @ %04x\n", b, addr); -} +void op_ldr() { wspush16(wspop16()); } +void op_str() { cpu.ram.dat[wspop16()] = wspop(); } void (*ops[])(void) = { op_brk, op_rts, op_lit, op_drp, op_dup, op_swp, op_ovr, op_rot, @@ -215,6 +221,7 @@ main(int argc, char *argv[]) load(f); run(); /* print result */ - echo(&cpu.wst, 0x40, "stack"); + echos(&cpu.wst, 0x40, "stack"); + echom(&cpu.ram, 0x40, "ram"); return 0; } diff --git a/uxnasm.c b/uxnasm.c @@ -148,27 +148,6 @@ pushshort(Uint16 s, int lit) pushbyte(s & 0xff, 0); } -#pragma mark - Parser - -Uint8 -findop(char *s) -{ - int i; - for(i = 0; i < 32; ++i) - if(scmp(opcodes[i], s)) - return i; - return 0; -} - -void -makelabel(char *id, Uint8 addr) -{ - Label *l = &labels[labelslen++]; - scpy(suca(id), l->name, 64); - l->addr = addr; - printf("New label: %s[0x%02x]\n", l->name, l->addr); -} - Label * findlabel(char *s) { @@ -184,19 +163,45 @@ findlabel(char *s) int error(char *name, char *id) { - printf("Error: %s - %s\n", name, id); + printf("Error: %s(%s)\n", name, id); + return 0; +} + +Uint8 +findop(char *s) +{ + int i; + for(i = 0; i < 32; ++i) + if(scmp(opcodes[i], s)) + return i; return 0; } int +makelabel(char *id, Uint8 addr) +{ + Label *l; + if(findlabel(id)) + return error("Label duplicate", id); + l = &labels[labelslen++]; + scpy(id, l->name, 64); + l->addr = addr; + printf("New label: %s[0x%02x]\n", l->name, l->addr); + return 1; +} + +int pass1(FILE *f) { int skip = 0, addr = 0, vars = 0; char w[64]; while(fscanf(f, "%s", w) == 1) { if(iscomment(w, &skip)) continue; - if(w[0] == ':') makelabel(w + 1, addr); - if(w[0] == ';') makelabel(w + 1, vars++); + suca(w); + if(w[0] == ':' && !makelabel(w + 1, addr)) + return error("Pass1 failed", w); + if(w[0] == ';' && !makelabel(w + 1, vars++)) + return error("Pass1 failed", w); /* move addr ptr */ if(findop(w) || scmp(w, "BRK")) addr += 1; @@ -213,7 +218,7 @@ pass1(FILE *f) else if(ismarker(w)) addr += 0; else - return error("Unknown label(pass1)", w); + return error("Unknown label", w); } rewind(f); return 1; @@ -242,7 +247,7 @@ pass2(FILE *f) else if(sihx(w + 1) && slen(w + 1) == 4) pushshort(shex(w + 1), w[0] == ','); else - return error("Unknown label(pass2)", w); + return error("Unknown label", w); } return 1; }