diff --git a/assembler.c b/assembler.c index f0a9c25..216c0d8 100644 --- a/assembler.c +++ b/assembler.c @@ -45,7 +45,7 @@ Program p; /* clang-format off */ char ops[][4] = { - "BRK", "NOP", "LIT", "LDR", "STR", "JMP", "JSR", "RTN", + "BRK", "NOP", "LIT", "LDR", "STR", "JMP", "JSR", "---", "EQU", "NEQ", "GTH", "LTH", "AND", "XOR", "ROL", "ROR", "POP", "DUP", "SWP", "OVR", "ROT", "CLN", "WSR", "RSW", "ADD", "SUB", "MUL", "DIV", "---", "---", "---", "---" @@ -155,7 +155,7 @@ findopcode(char *s) continue; while(s[3 + m]) { if(s[3 + m] == '2') i |= (1 << 5); /* mode: short */ - if(s[3 + m] == 'S') i |= (1 << 6); /* mode: signed */ + if(s[3 + m] == 'r') i |= (1 << 6); /* mode: return */ if(s[3 + m] == '?') i |= (1 << 7); /* mode: conditional */ m++; } diff --git a/build.sh b/build.sh index 539d023..9bc2dea 100755 --- a/build.sh +++ b/build.sh @@ -20,5 +20,5 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr # cc uxn.c emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator # run -./bin/assembler projects/software/nasu.usm bin/boot.rom +./bin/assembler projects/software/left.usm bin/boot.rom ./bin/emulator bin/boot.rom diff --git a/projects/examples/dev.console.usm b/projects/examples/dev.console.usm index e07d4da..89c3b90 100644 --- a/projects/examples/dev.console.usm +++ b/projects/examples/dev.console.usm @@ -1,5 +1,7 @@ ( hello world ) +%RTN { JMP2r } + @RESET ,text1 ,print-label JSR2 diff --git a/projects/examples/dev.mouse.usm b/projects/examples/dev.mouse.usm index b72ebb9..51d0f25 100644 --- a/projects/examples/dev.mouse.usm +++ b/projects/examples/dev.mouse.usm @@ -1,5 +1,7 @@ ( Mouse ) +%RTN { JMP2r } + ;label { x 2 y 2 color 1 addr 2 } ;cat { x 2 y 2 } ;pointer { x 2 y 2 } diff --git a/projects/examples/gui.editor.usm b/projects/examples/gui.editor.usm index 8b90c28..c6c17e0 100644 --- a/projects/examples/gui.editor.usm +++ b/projects/examples/gui.editor.usm @@ -1,5 +1,7 @@ ( app/hex ) +%RTN { JMP2r } + ;label { x 2 y 2 color 1 addr 2 } ;pixel { x 1 y 1 } ;window { x1 2 y1 2 x2 2 y2 2 } @@ -7,7 +9,6 @@ ;editor { x1 2 y1 2 addr 2 } ;rect { x1 2 y1 2 x2 2 y2 2 } ;color { byte 1 } -;addr { short 2 } |0100 @RESET diff --git a/projects/examples/gui.hover.usm b/projects/examples/gui.hover.usm index 657f6ed..94de739 100644 --- a/projects/examples/gui.hover.usm +++ b/projects/examples/gui.hover.usm @@ -1,5 +1,7 @@ ( draw routines ) +%RTN { JMP2r } + ;color { byte 1 } ;pointer { x 2 y 2 sprite 2 } ;rect { x1 2 y1 2 x2 2 y2 2 } diff --git a/projects/examples/gui.label.usm b/projects/examples/gui.label.usm index af27c33..8043a1d 100644 --- a/projects/examples/gui.label.usm +++ b/projects/examples/gui.label.usm @@ -1,5 +1,7 @@ ( draw routines ) +%RTN { JMP2r } + ;label { x 2 y 2 color 1 addr 2 } ;rect { x1 2 y1 2 x2 2 y2 2 } ;center { x 2 y 2 } diff --git a/projects/examples/gui.picture.usm b/projects/examples/gui.picture.usm index fc0de35..839906b 100644 --- a/projects/examples/gui.picture.usm +++ b/projects/examples/gui.picture.usm @@ -1,5 +1,7 @@ ( gui/picture ) +%RTN { JMP2r } + ;pict { x 2 y 2 width 2 height 2 color 1 addr 2 } |0100 @RESET diff --git a/projects/examples/gui.shapes.usm b/projects/examples/gui.shapes.usm index b7ba5c6..d361fb9 100644 --- a/projects/examples/gui.shapes.usm +++ b/projects/examples/gui.shapes.usm @@ -1,5 +1,7 @@ ( draw routines ) +%RTN { JMP2r } + ;label { x 2 y 2 color 1 addr 2 } ;pict { x 2 y 2 width 2 height 2 color 1 addr 2 } ;rect { x1 2 y1 2 x2 2 y2 2 } diff --git a/projects/software/left.usm b/projects/software/left.usm index 6d10d28..d96ab3d 100644 --- a/projects/software/left.usm +++ b/projects/software/left.usm @@ -15,6 +15,9 @@ - Real scrolling distance ) +%RTN { JMP2r } +%RTN? { JMP2r? } + ;lock { byte 1 } ;k { byte 1 } ;l { byte 1 } diff --git a/projects/software/nasu.usm b/projects/software/nasu.usm index e5dd187..e9e6348 100644 --- a/projects/software/nasu.usm +++ b/projects/software/nasu.usm @@ -6,6 +6,9 @@ 1 2 3 - Select brush ) +%RTN { JMP2r } +%RTN? { JMP2r? } + ;bankview { x 2 y 2 mode 1 addr 2 } ;tileview { x 2 y 2 addr 2 } ;rect { x1 2 y1 2 x2 2 y2 2 } diff --git a/uxn.c b/uxn.c index 14c8242..6975ff1 100644 --- a/uxn.c +++ b/uxn.c @@ -33,63 +33,63 @@ Uint16 peek16(Stack *s, Uint8 a) { return peek8(s, a * 2) + (peek8(s, a * 2 + 1) void op_brk(Uxn *u) { setflag(&u->status, FLAG_HALT, 1); } void op_lit(Uxn *u) { u->literal += 1; } void op_nop(Uxn *u) { (void)u; } -void op_jmp(Uxn *u) { Uint8 a = pop8(&u->wst); u->ram.ptr += (Sint8)a; } -void op_jsr(Uxn *u) { Uint8 a = pop8(&u->wst); push16(&u->rst, u->ram.ptr); u->ram.ptr += (Sint8)a; } -void op_rtn(Uxn *u) { u->ram.ptr = pop16(&u->rst); } -void op_ldr(Uxn *u) { Uint16 a = pop16(&u->wst); push8(&u->wst, mempeek8(u, a)); } -void op_str(Uxn *u) { Uint16 a = pop16(&u->wst); Uint8 b = pop8(&u->wst); mempoke8(u, a, b); } +void op_jmp(Uxn *u) { Uint8 a = pop8(u->src); u->ram.ptr += (Sint8)a; } +void op_jsr(Uxn *u) { Uint8 a = pop8(u->src); push16(u->dst, u->ram.ptr); u->ram.ptr += (Sint8)a; } +void op_rtn(Uxn *u) { u->ram.ptr = pop16(u->dst); } +void op_ldr(Uxn *u) { Uint16 a = pop16(u->src); push8(u->src, mempeek8(u, a)); } +void op_str(Uxn *u) { Uint16 a = pop16(u->src); Uint8 b = pop8(u->src); mempoke8(u, a, b); } /* Logic */ -void op_and(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b & a); } -void op_xor(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b | a); } -void op_rol(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b << (a % 8)); } -void op_ror(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b >> (a % 8)); } +void op_and(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b & a); } +void op_xor(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b | a); } +void op_rol(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b << (a % 8)); } +void op_ror(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b >> (a % 8)); } /* Stack */ -void op_pop(Uxn *u) { pop8(&u->wst); } -void op_dup(Uxn *u) { push8(&u->wst, peek8(&u->wst, 0)); } -void op_swp(Uxn *u) { Uint8 b = pop8(&u->wst), a = pop8(&u->wst); push8(&u->wst, b); push8(&u->wst, a); } -void op_ovr(Uxn *u) { push8(&u->wst, peek8(&u->wst, 1)); } -void op_rot(Uxn *u) { Uint8 c = pop8(&u->wst), b = pop8(&u->wst), a = pop8(&u->wst); push8(&u->wst, b); push8(&u->wst, c); push8(&u->wst, a); } -void op_cln(Uxn *u) { push8(&u->wst, peek8(&u->rst, 0)); } -void op_wsr(Uxn *u) { Uint8 a = pop8(&u->wst); push8(&u->rst, a); } -void op_rsw(Uxn *u) { Uint8 a = pop8(&u->rst); push8(&u->wst, a); } +void op_pop(Uxn *u) { pop8(u->src); } +void op_dup(Uxn *u) { push8(u->src, peek8(u->src, 0)); } +void op_swp(Uxn *u) { Uint8 b = pop8(u->src), a = pop8(u->src); push8(u->src, b); push8(u->src, a); } +void op_ovr(Uxn *u) { push8(u->src, peek8(u->src, 1)); } +void op_rot(Uxn *u) { Uint8 c = pop8(u->src), b = pop8(u->src), a = pop8(u->src); push8(u->src, b); push8(u->src, c); push8(u->src, a); } +void op_cln(Uxn *u) { push8(u->src, peek8(u->dst, 0)); } +void op_wsr(Uxn *u) { Uint8 a = pop8(u->src); push8(u->dst, a); } +void op_rsw(Uxn *u) { Uint8 a = pop8(u->dst); push8(u->src, a); } /* Arithmetic */ -void op_add(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b + (Sint8)a : b + a); } -void op_sub(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b - (Sint8)a : b - a); } -void op_mul(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b * (Sint8)a : b * a); } -void op_div(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b / (Sint8)a : b / a); } -void op_equ(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b == (Sint8)a : b == a); } -void op_neq(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b != (Sint8)a : b != a); } -void op_gth(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b > (Sint8)a : b > a); } -void op_lth(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b < (Sint8)a : b < a); } +void op_add(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b + a); } +void op_sub(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b - a); } +void op_mul(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b * a); } +void op_div(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b / a); } +void op_equ(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b == a); } +void op_neq(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b != a); } +void op_gth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b > a); } +void op_lth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b < a); } /* --- */ void op_lit16(Uxn *u) { u->literal += 2; } -void op_nop16(Uxn *u) { printf("%04x\n", pop16(&u->wst)); } -void op_jmp16(Uxn *u) { u->ram.ptr = pop16(&u->wst); } -void op_jsr16(Uxn *u) { push16(&u->rst, u->ram.ptr); u->ram.ptr = pop16(&u->wst); } -void op_ldr16(Uxn *u) { Uint16 a = pop16(&u->wst); push16(&u->wst, mempeek16(u, a)); } -void op_str16(Uxn *u) { Uint16 a = pop16(&u->wst); Uint16 b = pop16(&u->wst); mempoke16(u, a, b); } -void op_and16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b & a); } -void op_xor16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b ^ a); } -void op_rol16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b << (a % 16)); } -void op_ror16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b >> (a % 16)); } +void op_nop16(Uxn *u) { printf("%04x\n", pop16(u->src)); } +void op_jmp16(Uxn *u) { u->ram.ptr = pop16(u->src); } +void op_jsr16(Uxn *u) { push16(u->dst, u->ram.ptr); u->ram.ptr = pop16(u->src); } +void op_ldr16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->src, mempeek16(u, a)); } +void op_str16(Uxn *u) { Uint16 a = pop16(u->src); Uint16 b = pop16(u->src); mempoke16(u, a, b); } +void op_and16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b & a); } +void op_xor16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b ^ a); } +void op_rol16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b << (a % 16)); } +void op_ror16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b >> (a % 16)); } /* Stack(16-bits) */ -void op_pop16(Uxn *u) { pop16(&u->wst); } -void op_dup16(Uxn *u) { push16(&u->wst, peek16(&u->wst, 0)); } -void op_swp16(Uxn *u) { Uint16 b = pop16(&u->wst), a = pop16(&u->wst); push16(&u->wst, b); push16(&u->wst, a); } -void op_ovr16(Uxn *u) { push16(&u->wst, peek16(&u->wst, 1)); } -void op_rot16(Uxn *u) { Uint16 c = pop16(&u->wst), b = pop16(&u->wst), a = pop16(&u->wst); push16(&u->wst, b); push16(&u->wst, c); push16(&u->wst, a); } -void op_cln16(Uxn *u) { push16(&u->wst, peek16(&u->rst, 0)); } -void op_wsr16(Uxn *u) { Uint16 a = pop16(&u->wst); push16(&u->rst, a); } -void op_rsw16(Uxn *u) { Uint16 a = pop16(&u->rst); push16(&u->wst, a); } +void op_pop16(Uxn *u) { pop16(u->src); } +void op_dup16(Uxn *u) { push16(u->src, peek16(u->src, 0)); } +void op_swp16(Uxn *u) { Uint16 b = pop16(u->src), a = pop16(u->src); push16(u->src, b); push16(u->src, a); } +void op_ovr16(Uxn *u) { push16(u->src, peek16(u->src, 1)); } +void op_rot16(Uxn *u) { Uint16 c = pop16(u->src), b = pop16(u->src), a = pop16(u->src); push16(u->src, b); push16(u->src, c); push16(u->src, a); } +void op_cln16(Uxn *u) { push16(u->src, peek16(u->dst, 0)); } +void op_wsr16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->dst, a); } +void op_rsw16(Uxn *u) { Uint16 a = pop16(u->dst); push16(u->src, a); } /* Arithmetic(16-bits) */ -void op_add16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b + (Sint16)a : b + a); } -void op_sub16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b - (Sint16)a : b - a); } -void op_mul16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b * (Sint16)a : b * a); } -void op_div16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b / (Sint16)a : b / a); } -void op_equ16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b == (Sint16)a : b == a); } -void op_neq16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b != (Sint16)a : b != a); } -void op_gth16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b > (Sint16)a : b > a); } -void op_lth16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b < (Sint16)a : b < a); } +void op_add16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b + a); } +void op_sub16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b - a); } +void op_mul16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b * a); } +void op_div16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b / a); } +void op_equ16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b == a); } +void op_neq16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b != a); } +void op_gth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b > a); } +void op_lth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b < a); } void (*ops[])(Uxn *u) = { op_brk, op_nop, op_lit, op_ldr, op_str, op_jmp, op_jsr, op_rtn, @@ -139,25 +139,32 @@ lituxn(Uxn *u, Uint8 instr) int opcuxn(Uxn *u, Uint8 instr) { - Uint8 op = instr & 0x1f, fcond; + Uint8 op = instr & 0x1f, fcond, freturn; setflag(&u->status, FLAG_SHORT, (instr >> 5) & 1); - setflag(&u->status, FLAG_SIGN, (instr >> 6) & 1); + setflag(&u->status, FLAG_RETURN, (instr >> 6) & 1); setflag(&u->status, FLAG_COND, (instr >> 7) & 1); fcond = getflag(&u->status, FLAG_COND); + freturn = getflag(&u->status, FLAG_RETURN); + u->src = freturn ? &u->rst : &u->wst; + u->dst = freturn ? &u->wst : &u->rst; if(getflag(&u->status, FLAG_SHORT)) op += 32; - if(u->wst.ptr < opr[op][0] || (fcond && u->wst.ptr < 1)) + if(u->src->ptr < opr[op][0] || (fcond && u->src->ptr < 1)) return haltuxn(u, "Working-stack underflow", op); - if(u->wst.ptr + opr[op][1] - opr[op][0] >= 255) + if(u->src->ptr + opr[op][1] - opr[op][0] >= 255) return haltuxn(u, "Working-stack overflow", instr); - if(u->rst.ptr < opr[op][2]) + if(u->dst->ptr < opr[op][2]) return haltuxn(u, "Return-stack underflow", op); - if(u->rst.ptr + opr[op][3] - opr[op][2] >= 255) + if(u->dst->ptr + opr[op][3] - opr[op][2] >= 255) return haltuxn(u, "Return-stack overflow", instr); if(!fcond || (fcond && pop8(&u->wst))) (*ops[op])(u); - else - u->wst.ptr -= opr[op][0] - opr[op][1]; + else { + if(freturn) + u->src->ptr -= opr[op][2] - opr[op][3]; + else + u->src->ptr -= opr[op][0] - opr[op][1]; + } return 1; } diff --git a/uxn.h b/uxn.h index 549528c..6b1d35a 100644 --- a/uxn.h +++ b/uxn.h @@ -18,7 +18,7 @@ typedef signed short Sint16; #define FLAG_HALT 0x01 #define FLAG_SHORT 0x02 -#define FLAG_SIGN 0x04 +#define FLAG_RETURN 0x04 #define FLAG_COND 0x08 typedef struct { @@ -39,7 +39,7 @@ typedef struct Device { typedef struct { Uint8 literal, status, devices; Uint16 counter, vreset, vframe, verror; - Stack wst, rst; + Stack wst, rst, *src, *dst; Memory ram; Device dev[16]; } Uxn;