Allow the system device to handle errors
This commit is contained in:
parent
aed26e5bd0
commit
c704491d79
|
@ -15,12 +15,9 @@ WITH REGARD TO THIS SOFTWARE.
|
|||
*/
|
||||
|
||||
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"};
|
||||
"underflow",
|
||||
"overflow",
|
||||
"division by zero"};
|
||||
|
||||
static void
|
||||
system_print(Stack *s, char *name)
|
||||
|
@ -42,10 +39,17 @@ system_inspect(Uxn *u)
|
|||
}
|
||||
|
||||
int
|
||||
uxn_halt(Uxn *u, Uint8 error, Uint16 addr)
|
||||
uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr)
|
||||
{
|
||||
system_inspect(u);
|
||||
fprintf(stderr, "Halted: %s#%04x, at 0x%04x\n", errors[error], u->ram[addr], addr);
|
||||
Stack *origin = (instr & 0x40) ? u->wst : u->rst;
|
||||
Uint8 *d = &u->dev[0x00];
|
||||
origin->err = err;
|
||||
if(GETVEC(d))
|
||||
uxn_eval(u, GETVEC(d));
|
||||
else {
|
||||
system_inspect(u);
|
||||
fprintf(stderr, "%s %s, at 0x%04x.\n", (instr & 0x40) ? "Return-stack" : "Working-stack", errors[err - 1], addr);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
12
src/uxn.c
12
src/uxn.c
|
@ -16,8 +16,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 = 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 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 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]; }
|
||||
|
@ -82,7 +82,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 = 4; goto err; } PUSH(src, b / a) break;
|
||||
case 0x1b: /* DIV */ POP(a) POP(b) if(a == 0) { errcode = 3; 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;
|
||||
|
@ -90,12 +90,8 @@ uxn_eval(Uxn *u, Uint16 pc)
|
|||
}
|
||||
}
|
||||
return 1;
|
||||
|
||||
err:
|
||||
/* set 1 in errcode if it involved the return stack instead of the working stack */
|
||||
/* (stack overflow & ( opcode was STH / JSR )) ^ Return Mode */
|
||||
errcode |= ((errcode >> 1 & ((instr & 0x1e) == 0x0e)) ^ instr >> 6) & 1;
|
||||
return uxn_halt(u, errcode, pc - 1);
|
||||
return uxn_halt(u, instr, errcode, pc - 1);
|
||||
}
|
||||
|
||||
/* clang-format on */
|
||||
|
|
|
@ -26,7 +26,7 @@ typedef unsigned int Uint32;
|
|||
/* clang-format on */
|
||||
|
||||
typedef struct {
|
||||
Uint8 dat[255], ptr;
|
||||
Uint8 dat[254], err, ptr;
|
||||
} Stack;
|
||||
|
||||
typedef struct Uxn {
|
||||
|
@ -38,4 +38,4 @@ typedef struct Uxn {
|
|||
|
||||
int uxn_boot(Uxn *u, Uint8 *ram);
|
||||
int uxn_eval(Uxn *u, Uint16 pc);
|
||||
int uxn_halt(Uxn *u, Uint8 err, Uint16 addr);
|
||||
int uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr);
|
||||
|
|
Loading…
Reference in New Issue