Prefixed helpers in uxn.c

This commit is contained in:
neauoire 2022-01-02 11:04:13 -08:00
parent 4ac632f227
commit ecc2654165
1 changed files with 52 additions and 52 deletions

104
src/uxn.c
View File

@ -21,15 +21,15 @@ WITH REGARD TO THIS SOFTWARE.
/* clang-format off */ /* clang-format off */
/* Utilities */ /* Utilities */
static void (*push)(Stack *s, Uint16 a); static void (*uxn_push)(Stack *s, Uint16 a);
static Uint16 (*pop8)(Stack *s); static Uint16 (*uxn_pop8)(Stack *s);
static Uint16 (*pop)(Stack *s); static Uint16 (*uxn_pop)(Stack *s);
static void (*poke)(Uint8 *m, Uint16 a, Uint16 b); static void (*uxn_poke)(Uint8 *m, Uint16 a, Uint16 b);
static Uint16 (*peek)(Uint8 *m, Uint16 a); static Uint16 (*uxn_peek)(Uint8 *m, Uint16 a);
static void (*devw)(Device *d, Uint8 a, Uint16 b); static void (*uxn_devw)(Device *d, Uint8 a, Uint16 b);
static Uint16 (*devr)(Device *d, Uint8 a); static Uint16 (*uxn_devr)(Device *d, Uint8 a);
static void (*warp)(Uxn *u, Uint16 a); static void (*uxn_warp)(Uxn *u, Uint16 a);
static void (*pull)(Uxn *u); static void (*uxn_pull)(Uxn *u);
/* byte mode */ /* byte mode */
static void push8(Stack *s, Uint16 a) { if(s->ptr == 0xff) { s->error = 2; return; } s->dat[s->ptr++] = a; } static void push8(Stack *s, Uint16 a) { if(s->ptr == 0xff) { s->error = 2; return; } s->dat[s->ptr++] = a; }
static Uint16 pop8k(Stack *s) { if(s->kptr == 0) { s->error = 1; return 0; } return s->dat[--s->kptr]; } static Uint16 pop8k(Stack *s) { if(s->kptr == 0) { s->error = 1; return 0; } return s->dat[--s->kptr]; }
@ -42,7 +42,7 @@ static void warp8(Uxn *u, Uint16 a){ u->ram.ptr += (Sint8)a; }
static void pull8(Uxn *u){ push8(u->src, peek8(u->ram.dat, u->ram.ptr++)); } static void pull8(Uxn *u){ push8(u->src, peek8(u->ram.dat, u->ram.ptr++)); }
/* short mode */ /* short mode */
static void push16(Stack *s, Uint16 a) { push8(s, a >> 8); push8(s, a); } static void push16(Stack *s, Uint16 a) { push8(s, a >> 8); push8(s, a); }
static Uint16 pop16(Stack *s) { Uint8 a = pop8(s), b = pop8(s); return a + (b << 8); } static Uint16 pop16(Stack *s) { Uint8 a = uxn_pop8(s), b = uxn_pop8(s); return a + (b << 8); }
void poke16(Uint8 *m, Uint16 a, Uint16 b) { poke8(m, a, b >> 8); poke8(m, a + 1, b); } void poke16(Uint8 *m, Uint16 a, Uint16 b) { poke8(m, a, b >> 8); poke8(m, a + 1, b); }
Uint16 peek16(Uint8 *m, Uint16 a) { return (peek8(m, a) << 8) + peek8(m, a + 1); } Uint16 peek16(Uint8 *m, Uint16 a) { return (peek8(m, a) << 8) + peek8(m, a + 1); }
static void devw16(Device *d, Uint8 a, Uint16 b) { devw8(d, a, b >> 8); devw8(d, a + 1, b); } static void devw16(Device *d, Uint8 a, Uint16 b) { devw8(d, a, b >> 8); devw8(d, a + 1, b); }
@ -72,60 +72,60 @@ uxn_eval(Uxn *u, Uint16 vec)
} }
/* Keep Mode */ /* Keep Mode */
if(instr & MODE_KEEP) { if(instr & MODE_KEEP) {
pop8 = pop8k; uxn_pop8 = pop8k;
u->src->kptr = u->src->ptr; u->src->kptr = u->src->ptr;
} else { } else {
pop8 = pop8d; uxn_pop8 = pop8d;
} }
/* Short Mode */ /* Short Mode */
if(instr & MODE_SHORT) { if(instr & MODE_SHORT) {
push = push16; pop = pop16; uxn_push = push16; uxn_pop = pop16;
poke = poke16; peek = peek16; uxn_poke = poke16; uxn_peek = peek16;
devw = devw16; devr = devr16; uxn_devw = devw16; uxn_devr = devr16;
warp = warp16; pull = pull16; uxn_warp = warp16; uxn_pull = pull16;
} else { } else {
push = push8; pop = pop8; uxn_push = push8; uxn_pop = uxn_pop8;
poke = poke8; peek = peek8; uxn_poke = poke8; uxn_peek = peek8;
devw = devw8; devr = devr8; uxn_devw = devw8; uxn_devr = devr8;
warp = warp8; pull = pull8; uxn_warp = warp8; uxn_pull = pull8;
} }
switch(instr & 0x1f){ switch(instr & 0x1f){
/* Stack */ /* Stack */
case 0x00: /* LIT */ pull(u); break; case 0x00: /* LIT */ uxn_pull(u); break;
case 0x01: /* INC */ a = pop(u->src); push(u->src, a + 1); break; case 0x01: /* INC */ a = uxn_pop(u->src); uxn_push(u->src, a + 1); break;
case 0x02: /* POP */ pop(u->src); break; case 0x02: /* POP */ uxn_pop(u->src); break;
case 0x03: /* DUP */ a = pop(u->src); push(u->src, a); push(u->src, a); break; case 0x03: /* DUP */ a = uxn_pop(u->src); uxn_push(u->src, a); uxn_push(u->src, a); break;
case 0x04: /* NIP */ a = pop(u->src); pop(u->src); push(u->src, a); break; case 0x04: /* NIP */ a = uxn_pop(u->src); uxn_pop(u->src); uxn_push(u->src, a); break;
case 0x05: /* SWP */ a = pop(u->src), b = pop(u->src); push(u->src, a); push(u->src, b); break; case 0x05: /* SWP */ a = uxn_pop(u->src), b = uxn_pop(u->src); uxn_push(u->src, a); uxn_push(u->src, b); break;
case 0x06: /* OVR */ a = pop(u->src), b = pop(u->src); push(u->src, b); push(u->src, a); push(u->src, b); break; case 0x06: /* OVR */ a = uxn_pop(u->src), b = uxn_pop(u->src); uxn_push(u->src, b); uxn_push(u->src, a); uxn_push(u->src, b); break;
case 0x07: /* ROT */ a = pop(u->src), b = pop(u->src), c = pop(u->src); push(u->src, b); push(u->src, a); push(u->src, c); break; case 0x07: /* ROT */ a = uxn_pop(u->src), b = uxn_pop(u->src), c = uxn_pop(u->src); uxn_push(u->src, b); uxn_push(u->src, a); uxn_push(u->src, c); break;
/* Logic */ /* Logic */
case 0x08: /* EQU */ a = pop(u->src), b = pop(u->src); push8(u->src, b == a); break; case 0x08: /* EQU */ a = uxn_pop(u->src), b = uxn_pop(u->src); push8(u->src, b == a); break;
case 0x09: /* NEQ */ a = pop(u->src), b = pop(u->src); push8(u->src, b != a); break; case 0x09: /* NEQ */ a = uxn_pop(u->src), b = uxn_pop(u->src); push8(u->src, b != a); break;
case 0x0a: /* GTH */ a = pop(u->src), b = pop(u->src); push8(u->src, b > a); break; case 0x0a: /* GTH */ a = uxn_pop(u->src), b = uxn_pop(u->src); push8(u->src, b > a); break;
case 0x0b: /* LTH */ a = pop(u->src), b = pop(u->src); push8(u->src, b < a); break; case 0x0b: /* LTH */ a = uxn_pop(u->src), b = uxn_pop(u->src); push8(u->src, b < a); break;
case 0x0c: /* JMP */ a = pop(u->src); warp(u, a); break; case 0x0c: /* JMP */ a = uxn_pop(u->src); uxn_warp(u, a); break;
case 0x0d: /* JCN */ a = pop(u->src); if(pop8(u->src)) warp(u, a); break; case 0x0d: /* JCN */ a = uxn_pop(u->src); if(uxn_pop8(u->src)) uxn_warp(u, a); break;
case 0x0e: /* JSR */ a = pop(u->src); push16(u->dst, u->ram.ptr); warp(u, a); break; case 0x0e: /* JSR */ a = uxn_pop(u->src); push16(u->dst, u->ram.ptr); uxn_warp(u, a); break;
case 0x0f: /* STH */ a = pop(u->src); push(u->dst, a); break; case 0x0f: /* STH */ a = uxn_pop(u->src); uxn_push(u->dst, a); break;
/* Memory */ /* Memory */
case 0x10: /* LDZ */ a = pop8(u->src); push(u->src, peek(u->ram.dat, a)); break; case 0x10: /* LDZ */ a = uxn_pop8(u->src); uxn_push(u->src, uxn_peek(u->ram.dat, a)); break;
case 0x11: /* STZ */ a = pop8(u->src); b = pop(u->src); poke(u->ram.dat, a, b); break; case 0x11: /* STZ */ a = uxn_pop8(u->src); b = uxn_pop(u->src); uxn_poke(u->ram.dat, a, b); break;
case 0x12: /* LDR */ a = pop8(u->src); push(u->src, peek(u->ram.dat, u->ram.ptr + (Sint8)a)); break; case 0x12: /* LDR */ a = uxn_pop8(u->src); uxn_push(u->src, uxn_peek(u->ram.dat, u->ram.ptr + (Sint8)a)); break;
case 0x13: /* STR */ a = pop8(u->src); b = pop(u->src); poke(u->ram.dat, u->ram.ptr + (Sint8)a, b); break; case 0x13: /* STR */ a = uxn_pop8(u->src); b = uxn_pop(u->src); uxn_poke(u->ram.dat, u->ram.ptr + (Sint8)a, b); break;
case 0x14: /* LDA */ a = pop16(u->src); push(u->src, peek(u->ram.dat, a)); break; case 0x14: /* LDA */ a = pop16(u->src); uxn_push(u->src, uxn_peek(u->ram.dat, a)); break;
case 0x15: /* STA */ a = pop16(u->src); b = pop(u->src); poke(u->ram.dat, a, b); break; case 0x15: /* STA */ a = pop16(u->src); b = uxn_pop(u->src); uxn_poke(u->ram.dat, a, b); break;
case 0x16: /* DEI */ a = pop8(u->src); push(u->src, devr(&u->dev[a >> 4], a)); break; case 0x16: /* DEI */ a = uxn_pop8(u->src); uxn_push(u->src, uxn_devr(&u->dev[a >> 4], a)); break;
case 0x17: /* DEO */ a = pop8(u->src); b = pop(u->src); devw(&u->dev[a >> 4], a, b); break; case 0x17: /* DEO */ a = uxn_pop8(u->src); b = uxn_pop(u->src); uxn_devw(&u->dev[a >> 4], a, b); break;
/* Arithmetic */ /* Arithmetic */
case 0x18: /* ADD */ a = pop(u->src), b = pop(u->src); push(u->src, b + a); break; case 0x18: /* ADD */ a = uxn_pop(u->src), b = uxn_pop(u->src); uxn_push(u->src, b + a); break;
case 0x19: /* SUB */ a = pop(u->src), b = pop(u->src); push(u->src, b - a); break; case 0x19: /* SUB */ a = uxn_pop(u->src), b = uxn_pop(u->src); uxn_push(u->src, b - a); break;
case 0x1a: /* MUL */ a = pop(u->src), b = pop(u->src); push(u->src, (Uint32)b * a); break; case 0x1a: /* MUL */ a = uxn_pop(u->src), b = uxn_pop(u->src); uxn_push(u->src, (Uint32)b * a); break;
case 0x1b: /* DIV */ a = pop(u->src), b = pop(u->src); if(a == 0) { u->src->error = 3; a = 1; } push(u->src, b / a); break; case 0x1b: /* DIV */ a = uxn_pop(u->src), b = uxn_pop(u->src); if(a == 0) { u->src->error = 3; a = 1; } uxn_push(u->src, b / a); break;
case 0x1c: /* AND */ a = pop(u->src), b = pop(u->src); push(u->src, b & a); break; case 0x1c: /* AND */ a = uxn_pop(u->src), b = uxn_pop(u->src); uxn_push(u->src, b & a); break;
case 0x1d: /* ORA */ a = pop(u->src), b = pop(u->src); push(u->src, b | a); break; case 0x1d: /* ORA */ a = uxn_pop(u->src), b = uxn_pop(u->src); uxn_push(u->src, b | a); break;
case 0x1e: /* EOR */ a = pop(u->src), b = pop(u->src); push(u->src, b ^ a); break; case 0x1e: /* EOR */ a = uxn_pop(u->src), b = uxn_pop(u->src); uxn_push(u->src, b ^ a); break;
case 0x1f: /* SFT */ a = pop8(u->src), b = pop(u->src); push(u->src, b >> (a & 0x0f) << ((a & 0xf0) >> 4)); break; case 0x1f: /* SFT */ a = uxn_pop8(u->src), b = uxn_pop(u->src); uxn_push(u->src, b >> (a & 0x0f) << ((a & 0xf0) >> 4)); break;
} }
if(u->wst.error) return uxn_halt(u, u->wst.error, "Working-stack", instr); if(u->wst.error) return uxn_halt(u, u->wst.error, "Working-stack", instr);
if(u->rst.error) return uxn_halt(u, u->rst.error, "Return-stack", instr); if(u->rst.error) return uxn_halt(u, u->rst.error, "Return-stack", instr);