uxn

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

commit c77799dac2d10a89f5d85931c4b0aa21162c2603
parent c37831749f669b1736f93c2fa6dceb4f6a65e3de
Author: Andrew Alderwick <andrew@alderwick.co.uk>
Date:   Wed,  5 Jan 2022 13:06:22 +0000

Just pass an error code to uxn_halt.

Diffstat:
Msrc/devices/system.c | 12+++++++++---
Msrc/uxn.c | 14+++++++-------
Msrc/uxn.h | 2+-
3 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/src/devices/system.c b/src/devices/system.c @@ -13,11 +13,17 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ -static const char *errors[] = {"underflow", "overflow", "division by zero"}; +static const char *errors[] = { + "Working-stack underflow", + "Return-stack underflow", + "Working-stack overflow", + "Return-stack overflow", + "Working-stack division by zero", + "Return-stack division by zero"}; int -uxn_halt(Uxn *u, Uint8 error, char *name, Uint16 addr) +uxn_halt(Uxn *u, Uint8 error, Uint16 addr) { - fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], u->ram[addr], addr); + fprintf(stderr, "Halted: %s#%04x, at 0x%04x\n", errors[error], u->ram[addr], addr); return 0; } diff --git a/src/uxn.c b/src/uxn.c @@ -20,8 +20,8 @@ WITH REGARD TO THIS SOFTWARE. #define PUSH8(s, x) { if(s->ptr == 0xff) { errcode = 2; goto err; } s->dat[s->ptr++] = (x); } #define PUSH16(s, x) { if((j = s->ptr) >= 0xfe) { errcode = 2; goto err; } k = (x); s->dat[j] = k >> 8; s->dat[j + 1] = k; s->ptr = j + 2; } #define PUSH(s, x) { if(bs) { PUSH16(s, (x)) } else { PUSH8(s, (x)) } } -#define POP8(o) { if(!(j = *sp)) { errcode = 1; goto err; } o = (Uint16)src->dat[--j]; *sp = j; } -#define POP16(o) { if((j = *sp) <= 1) { errcode = 1; goto err; } o = src->dat[j - 1]; o += src->dat[j - 2] << 8; *sp = j - 2; } +#define POP8(o) { if(!(j = *sp)) { errcode = 0; goto err; } o = (Uint16)src->dat[--j]; *sp = j; } +#define POP16(o) { if((j = *sp) <= 1) { errcode = 0; goto err; } o = src->dat[j - 1]; o += src->dat[j - 2] << 8; *sp = j - 2; } #define POP(o) { if(bs) { POP16(o) } else { POP8(o) } } #define POKE(x, y) { if(bs) { u->ram[(x)] = (y) >> 8; u->ram[(x) + 1] = (y); } else { u->ram[(x)] = y; } } #define PEEK16(o, x) { o = (u->ram[(x)] << 8) + u->ram[(x) + 1]; } @@ -89,7 +89,7 @@ uxn_eval(Uxn *u, Uint16 pc) case 0x18: /* ADD */ POP(a) POP(b) PUSH(src, b + a) break; case 0x19: /* SUB */ POP(a) POP(b) PUSH(src, b - a) break; case 0x1a: /* MUL */ POP(a) POP(b) PUSH(src, (Uint32)b * a) break; - case 0x1b: /* DIV */ POP(a) POP(b) if(a == 0) { errcode = 3; goto err; } PUSH(src, b / a) break; + case 0x1b: /* DIV */ POP(a) POP(b) if(a == 0) { errcode = 4; goto err; } PUSH(src, b / a) break; case 0x1c: /* AND */ POP(a) POP(b) PUSH(src, b & a) break; case 0x1d: /* ORA */ POP(a) POP(b) PUSH(src, b | a) break; case 0x1e: /* EOR */ POP(a) POP(b) PUSH(src, b ^ a) break; @@ -99,10 +99,10 @@ uxn_eval(Uxn *u, Uint16 pc) return 1; err: - if ((instr & 0x40 ? 1 : 0) ^ (errcode == 2 && ((instr & 0x1f) == 0x0e || (instr & 0x1f) == 0x0f))) - return uxn_halt(u, errcode, "Return-stack", pc - 1); - else - return uxn_halt(u, errcode, "Working-stack", pc - 1); + /* set 1 in errcode if it involved the return stack instead of the working stack */ + /* (stack overflow & ( instruction opcode was STH or JSR )) ^ Return Mode */ + errcode |= ((errcode >> 1 & instr >> 1 & instr >> 2 & instr >> 3 & ~instr >> 4) ^ instr >> 6) & 1; + return uxn_halt(u, errcode, pc - 1); } /* clang-format on */ diff --git a/src/uxn.h b/src/uxn.h @@ -45,5 +45,5 @@ typedef struct Uxn { int uxn_boot(Uxn *c, Uint8 *memory); int uxn_eval(Uxn *u, Uint16 pc); -int uxn_halt(Uxn *u, Uint8 error, char *name, Uint16 addr); +int uxn_halt(Uxn *u, Uint8 error, Uint16 addr); Device *uxn_port(Uxn *u, Uint8 id, Uint8 (*deifn)(Device *, Uint8), void (*deofn)(Device *, Uint8));