diff --git a/src/devices/system.c b/src/devices/system.c index a899d16..aba1e13 100644 --- a/src/devices/system.c +++ b/src/devices/system.c @@ -19,11 +19,6 @@ char *boot_rom; Uint8 dei_masks[0x100], deo_masks[0x100]; Uint16 dev_vers[0x10], dei_mask[0x10], deo_mask[0x10]; -static const char *errors[] = { - "underflow", - "overflow", - "division by zero"}; - static int system_load(Uxn *u, char *filename) { @@ -164,24 +159,3 @@ system_deo(Uxn *u, Uint8 *d, Uint8 port) break; } } - -/* Errors */ - -int -emu_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr) -{ - Uint8 *d = &u->dev[0]; - Uint16 handler = PEEK2(d); - if(handler) { - u->wst.ptr = 4; - u->wst.dat[0] = addr >> 0x8; - u->wst.dat[1] = addr & 0xff; - u->wst.dat[2] = instr; - u->wst.dat[3] = err; - return uxn_eval(u, handler); - } else { - system_inspect(u); - fprintf(stderr, "%s %s, by %02x at 0x%04x.\n", (instr & 0x40) ? "Return-stack" : "Working-stack", errors[err - 1], instr, addr); - } - return 0; -} diff --git a/src/uxn.c b/src/uxn.c index 363dbf1..cc5b585 100644 --- a/src/uxn.c +++ b/src/uxn.c @@ -17,7 +17,7 @@ WITH REGARD TO THIS SOFTWARE. [ L2 ][ N2 ][ T2 ] < */ -#define T *ptr +#define T *(ptr) #define N *(ptr - 1) #define L *(ptr - 2) #define X *(ptr - 3) @@ -31,21 +31,20 @@ WITH REGARD TO THIS SOFTWARE. #define N2_(v) { r = (v); L = r; X = r >> 8; } #define L2_(v) { r = (v); Y = r; Z = r >> 8; } -#define HALT(c) { return emu_halt(u, ins, c, pc - 1); } #define FLIP { s = ins & 0x40 ? &u->wst : &u->rst; } -#define SET(x, y) { r = s->ptr; if(x > r) HALT(1) r += y; if(ins & 0x80) r += x; if(r > 254) HALT(2) ptr = s->dat + r - 1; s->ptr = r; } -#define SHIFT(y) { r = s->ptr + y; if(r > 254) HALT(2) ptr = s->dat + r - 1; s->ptr = r; } +#define SHIFT(y) { r = s->ptr + (y); ptr = s->dat + r - 1; s->ptr = r; } +#define SET(x, y) { SHIFT((ins & 0x80) ? x + y : y) } int uxn_eval(Uxn *u, Uint16 pc) { int t, n, l, r; - Uint8 *ram = u->ram; + Uint8 *ram = u->ram, *rr; if(!pc || u->dev[0x0f]) return 0; for(;;) { int ins = ram[pc++]; Stack *s = ins & 0x40 ? &u->rst : &u->wst; - Uint8 *ptr = s->dat + s->ptr - 1, *rr; + Uint8 *ptr = s->dat + s->ptr - 1; switch(ins & 0x1f ? ins & 0x3f : ins << 4) { /* IMM */ case 0x000: /* BRK */ return 1; @@ -107,8 +106,8 @@ uxn_eval(Uxn *u, Uint16 pc) case 0x39: /* SUB2 */ t=T2;n=N2; SET(4,-2) T2_(n - t) break; case 0x1a: /* MUL */ t=T;n=N; SET(2,-1) T = n * t; break; case 0x3a: /* MUL2 */ t=T2;n=N2; SET(4,-2) T2_(n * t) break; - case 0x1b: /* DIV */ t=T;n=N; SET(2,-1) if(!t) HALT(3) T = n / t; break; - case 0x3b: /* DIV2 */ t=T2;n=N2; SET(4,-2) if(!t) HALT(3) T2_(n / t) break; + case 0x1b: /* DIV */ t=T;n=N; SET(2,-1) T = t ? n / t : 0; break; + case 0x3b: /* DIV2 */ t=T2;n=N2; SET(4,-2) T2_(t ? n / t : 0) break; case 0x1c: /* AND */ t=T;n=N; SET(2,-1) T = n & t; break; case 0x3c: /* AND2 */ t=T2;n=N2; SET(4,-2) T2_(n & t) break; case 0x1d: /* ORA */ t=T;n=N; SET(2,-1) T = n | t; break; diff --git a/src/uxn.h b/src/uxn.h index 4415950..5fc5b5d 100644 --- a/src/uxn.h +++ b/src/uxn.h @@ -39,7 +39,6 @@ typedef struct Uxn { extern Uint8 emu_dei(Uxn *u, Uint8 addr); extern void emu_deo(Uxn *u, Uint8 addr); -extern int emu_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr); extern Uint8 dei_masks[0x100], deo_masks[0x100]; extern Uint16 dev_vers[0x10], dei_mask[0x10], deo_mask[0x10]; diff --git a/src/uxn11.c b/src/uxn11.c index 2159476..fc24258 100644 --- a/src/uxn11.c +++ b/src/uxn11.c @@ -270,7 +270,7 @@ main(int argc, char **argv) system_connect(0xf, LINK_VERSION, LINK_DEIMASK, LINK_DEOMASK); /* Read flags */ if(argv[i][0] == '-' && argv[i][1] == 'v') - return system_version("Uxn11 - Graphical Varvara Emulator", "29 Aug 2023"); + return system_version("Uxn11 - Graphical Varvara Emulator", "2 Sep 2023"); if(!emu_init()) return system_error("Init", "Failed to initialize varvara."); if(!system_init(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++])) diff --git a/src/uxncli.c b/src/uxncli.c index eb45754..e909139 100644 --- a/src/uxncli.c +++ b/src/uxncli.c @@ -76,7 +76,7 @@ main(int argc, char **argv) system_connect(0xf, LINK_VERSION, LINK_DEIMASK, LINK_DEOMASK); /* Read flags */ if(argv[i][0] == '-' && argv[i][1] == 'v') - return system_version("Uxncli - Console Varvara Emulator", "29 Aug 2023"); + return system_version("Uxncli - Console Varvara Emulator", "2 Sep 2023"); if(!system_init(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++])) return system_error("Init", "Failed to initialize uxn."); /* Game Loop */