commit 7f275772ef387c51a6d8a09d21bfcf2082490f3a
parent 5ac6ff4313f3a0845d3def14e63d21362f8b9aef
Author: Devine Lu Linvega <aliceffekt@gmail.com>
Date: Sun, 1 Jan 2023 16:21:05 -0800
Added HALT macro
Diffstat:
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/src/uxn.c b/src/uxn.c
@@ -17,11 +17,12 @@ WITH REGARD TO THIS SOFTWARE.
pc: program counter. sp: ptr to src stack ptr. kptr: "keep" mode copy of src stack ptr.
x,y: macro in params. d: macro in device. j,k,dev: macro temp variables. o: macro out param. */
-#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 HALT(c) { return uxn_halt(u, instr, (c), pc - 1); }
+#define PUSH8(s, x) { if(s->ptr == 0xff) HALT(2) s->dat[s->ptr++] = (x); }
+#define PUSH16(s, x) { if((j = s->ptr) >= 0xfe) HALT(2) 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)) HALT(1) o = (Uint16)src->dat[--j]; *sp = j; }
+#define POP16(o) { if((j = *sp) <= 1) HALT(1) 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]; }
@@ -33,7 +34,7 @@ WITH REGARD TO THIS SOFTWARE.
int
uxn_eval(Uxn *u, Uint16 pc)
{
- unsigned int a, b, c, j, k, bs, instr, errcode;
+ unsigned int a, b, c, j, k, bs, instr;
Uint8 kptr, *sp;
Stack *src, *dst;
if(!pc || u->dev[0x0f]) return 0;
@@ -43,7 +44,7 @@ uxn_eval(Uxn *u, Uint16 pc)
else { src = u->wst; dst = u->rst; }
/* Keep Mode */
if(instr & 0x80) { kptr = src->ptr; sp = &kptr; }
- else { sp = &src->ptr; }
+ else sp = &src->ptr;
/* Short Mode */
bs = instr & 0x20 ? 1 : 0;
switch(instr & 0x1f) {
@@ -74,7 +75,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) HALT(3) 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;
@@ -82,8 +83,6 @@ uxn_eval(Uxn *u, Uint16 pc)
}
}
return 1;
-err:
- return uxn_halt(u, instr, errcode, pc - 1);
}
/* clang-format on */