0
0
Fork 0
mirror of https://git.sr.ht/~rabbits/uxn synced 2024-11-23 14:25:10 +00:00

Reworked pop8 and pop16 so they do not need offsets

This commit is contained in:
Andrew Alderwick 2021-05-12 20:36:18 +01:00
parent 9164b7f9fe
commit 76a38e3db9
2 changed files with 69 additions and 64 deletions

131
src/uxn.c
View file

@ -17,15 +17,15 @@ WITH REGARD TO THIS SOFTWARE.
/* clang-format off */
void push8(Stack *s, Uint8 a) { if (s->ptr == 0xff) { s->error = 2; return; } s->dat[s->ptr++] = a; }
Uint8 pop8_keep(Stack *s, Uint8 a) { if (s->ptr < a + 1) s->error = 1; return s->dat[s->ptr - a - 1]; }
Uint8 pop8_nokeep(Stack *s, Uint8 a) { if (s->ptr == 0) { s->error = 1; return 0; } return s->dat[--s->ptr]; (void) a; }
static Uint8 (*pop8)(Stack *s, Uint8 a);
Uint8 pop8_keep(Stack *s) { if (s->kptr == 0) { s->error = 1; return 0; } return s->dat[--s->kptr]; }
Uint8 pop8_nokeep(Stack *s) { if (s->ptr == 0) { s->error = 1; return 0; } return s->dat[--s->ptr]; }
static Uint8 (*pop8)(Stack *s);
void mempoke8(Uint8 *m, Uint16 a, Uint8 b) { m[a] = b; }
Uint8 mempeek8(Uint8 *m, Uint16 a) { return m[a]; }
void devpoke8(Device *d, Uint8 a, Uint8 b) { d->dat[a & 0xf] = b; d->talk(d, a & 0x0f, 1); }
Uint8 devpeek8(Device *d, Uint8 a) { d->talk(d, a & 0x0f, 0); return d->dat[a & 0xf]; }
void push16(Stack *s, Uint16 a) { push8(s, a >> 8); push8(s, a); }
Uint16 pop16(Stack *s, Uint8 a) { return pop8(s, a * 2) + (pop8(s, a * 2 + 1) << 8); }
Uint16 pop16(Stack *s) { return pop8(s) + (pop8(s) << 8); }
void mempoke16(Uint8 *m, Uint16 a, Uint16 b) { mempoke8(m, a, b >> 8); mempoke8(m, a + 1, b); }
Uint16 mempeek16(Uint8 *m, Uint16 a) { return (mempeek8(m, a) << 8) + mempeek8(m, a + 1); }
void devpoke16(Device *d, Uint8 a, Uint16 b) { devpoke8(d, a, b >> 8); devpoke8(d, a + 1, b); }
@ -34,72 +34,72 @@ Uint16 devpeek16(Device *d, Uint16 a) { return (devpeek8(d, a) << 8) + devpeek8(
void op_brk(Uxn *u) { u->ram.ptr = 0; }
void op_nop(Uxn *u) { (void)u; }
void op_lit(Uxn *u) { push8(u->src, mempeek8(u->ram.dat, u->ram.ptr++)); }
void op_pop(Uxn *u) { pop8(u->src, 0); }
void op_dup(Uxn *u) { Uint8 a = pop8(u->src, 0); push8(u->src, a); push8(u->src, a); }
void op_swp(Uxn *u) { Uint8 b = pop8(u->src, 0), a = pop8(u->src, 1); push8(u->src, b); push8(u->src, a); }
void op_ovr(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b); push8(u->src, a); push8(u->src, b); }
void op_rot(Uxn *u) { Uint8 c = pop8(u->src, 0), b = pop8(u->src, 1), a = pop8(u->src, 2); push8(u->src, b); push8(u->src, c); push8(u->src, a); }
void op_pop(Uxn *u) { pop8(u->src); }
void op_dup(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, a); push8(u->src, a); }
void op_swp(Uxn *u) { Uint8 b = pop8(u->src), a = pop8(u->src); push8(u->src, b); push8(u->src, a); }
void op_ovr(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b); push8(u->src, a); push8(u->src, b); }
void op_rot(Uxn *u) { Uint8 c = pop8(u->src), b = pop8(u->src), a = pop8(u->src); push8(u->src, b); push8(u->src, c); push8(u->src, a); }
/* Logic */
void op_equ(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b == a); }
void op_neq(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b != a); }
void op_gth(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b > a); }
void op_lth(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b < a); }
void op_jmp(Uxn *u) { Uint8 a = pop8(u->src, 0); u->ram.ptr += (Sint8)a; }
void op_jnz(Uxn *u) { Uint8 a = pop8(u->src, 0); if (pop8(u->src, 1)) u->ram.ptr += (Sint8)a; }
void op_jsr(Uxn *u) { Uint8 a = pop8(u->src, 0); push16(u->dst, u->ram.ptr); u->ram.ptr += (Sint8)a; }
void op_sth(Uxn *u) { Uint8 a = pop8(u->src, 0); push8(u->dst, a); }
void op_equ(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b == a); }
void op_neq(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b != a); }
void op_gth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b > a); }
void op_lth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b < a); }
void op_jmp(Uxn *u) { Uint8 a = pop8(u->src); u->ram.ptr += (Sint8)a; }
void op_jnz(Uxn *u) { Uint8 a = pop8(u->src); if (pop8(u->src)) u->ram.ptr += (Sint8)a; }
void op_jsr(Uxn *u) { Uint8 a = pop8(u->src); push16(u->dst, u->ram.ptr); u->ram.ptr += (Sint8)a; }
void op_sth(Uxn *u) { Uint8 a = pop8(u->src); push8(u->dst, a); }
/* Memory */
void op_pek(Uxn *u) { Uint8 a = pop8(u->src, 0); push8(u->src, mempeek8(u->ram.dat, a)); }
void op_pok(Uxn *u) { Uint8 a = pop8(u->src, 0); Uint8 b = pop8(u->src, 1); mempoke8(u->ram.dat, a, b); }
void op_ldr(Uxn *u) { Uint8 a = pop8(u->src, 0); push8(u->src, mempeek8(u->ram.dat, u->ram.ptr + (Sint8)a)); }
void op_str(Uxn *u) { Uint8 a = pop8(u->src, 0); Uint8 b = pop8(u->src, 1); mempoke8(u->ram.dat, u->ram.ptr + (Sint8)a, b); }
void op_lda(Uxn *u) { Uint16 a = pop16(u->src, 0); push8(u->src, mempeek8(u->ram.dat, a)); }
void op_sta(Uxn *u) { Uint16 a = pop16(u->src, 0); Uint8 b = pop8(u->src, 0); mempoke8(u->ram.dat, a, b); }
void op_dei(Uxn *u) { Uint8 a = pop8(u->src, 0); push8(u->src, devpeek8(&u->dev[a >> 4], a)); }
void op_deo(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); devpoke8(&u->dev[a >> 4], a, b); }
void op_pek(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, mempeek8(u->ram.dat, a)); }
void op_pok(Uxn *u) { Uint8 a = pop8(u->src); Uint8 b = pop8(u->src); mempoke8(u->ram.dat, a, b); }
void op_ldr(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, mempeek8(u->ram.dat, u->ram.ptr + (Sint8)a)); }
void op_str(Uxn *u) { Uint8 a = pop8(u->src); Uint8 b = pop8(u->src); mempoke8(u->ram.dat, u->ram.ptr + (Sint8)a, b); }
void op_lda(Uxn *u) { Uint16 a = pop16(u->src); push8(u->src, mempeek8(u->ram.dat, a)); }
void op_sta(Uxn *u) { Uint16 a = pop16(u->src); Uint8 b = pop8(u->src); mempoke8(u->ram.dat, a, b); }
void op_dei(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, devpeek8(&u->dev[a >> 4], a)); }
void op_deo(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); devpoke8(&u->dev[a >> 4], a, b); }
/* Arithmetic */
void op_add(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b + a); }
void op_sub(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b - a); }
void op_mul(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b * a); }
void op_div(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b / a); }
void op_and(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b & a); }
void op_ora(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b | a); }
void op_eor(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b ^ a); }
void op_sft(Uxn *u) { Uint8 a = pop8(u->src, 0), b = pop8(u->src, 1); push8(u->src, b >> (a & 0x07) << ((a & 0x70) >> 4)); }
void op_add(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b + a); }
void op_sub(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b - a); }
void op_mul(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b * a); }
void op_div(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b / a); }
void op_and(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b & a); }
void op_ora(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b | a); }
void op_eor(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b ^ a); }
void op_sft(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b >> (a & 0x07) << ((a & 0x70) >> 4)); }
/* Stack */
void op_lit16(Uxn *u) { push16(u->src, mempeek16(u->ram.dat, u->ram.ptr++)); u->ram.ptr++; }
void op_pop16(Uxn *u) { pop16(u->src, 0); }
void op_dup16(Uxn *u) { Uint16 a = pop16(u->src, 0); push16(u->src, a); push16(u->src, a); }
void op_swp16(Uxn *u) { Uint16 b = pop16(u->src, 0), a = pop16(u->src, 1); push16(u->src, b); push16(u->src, a); }
void op_ovr16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push16(u->src, b); push16(u->src, a); push16(u->src, b); }
void op_rot16(Uxn *u) { Uint16 c = pop16(u->src, 0), b = pop16(u->src, 1), a = pop16(u->src, 2); push16(u->src, b); push16(u->src, c); push16(u->src, a); }
void op_pop16(Uxn *u) { pop16(u->src); }
void op_dup16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->src, a); push16(u->src, a); }
void op_swp16(Uxn *u) { Uint16 b = pop16(u->src), a = pop16(u->src); push16(u->src, b); push16(u->src, a); }
void op_ovr16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b); push16(u->src, a); push16(u->src, b); }
void op_rot16(Uxn *u) { Uint16 c = pop16(u->src), b = pop16(u->src), a = pop16(u->src); push16(u->src, b); push16(u->src, c); push16(u->src, a); }
/* Logic(16-bits) */
void op_equ16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push8(u->src, b == a); }
void op_neq16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push8(u->src, b != a); }
void op_gth16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push8(u->src, b > a); }
void op_lth16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push8(u->src, b < a); }
void op_jmp16(Uxn *u) { u->ram.ptr = pop16(u->src, 0); }
void op_jnz16(Uxn *u) { Uint16 a = pop16(u->src, 0); if (pop8(u->src, 0)) u->ram.ptr = a; }
void op_jsr16(Uxn *u) { push16(u->dst, u->ram.ptr); u->ram.ptr = pop16(u->src, 0); }
void op_sth16(Uxn *u) { Uint16 a = pop16(u->src, 0); push16(u->dst, a); }
void op_equ16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b == a); }
void op_neq16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b != a); }
void op_gth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b > a); }
void op_lth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b < a); }
void op_jmp16(Uxn *u) { u->ram.ptr = pop16(u->src); }
void op_jnz16(Uxn *u) { Uint16 a = pop16(u->src); if (pop8(u->src)) u->ram.ptr = a; }
void op_jsr16(Uxn *u) { push16(u->dst, u->ram.ptr); u->ram.ptr = pop16(u->src); }
void op_sth16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->dst, a); }
/* Memory(16-bits) */
void op_pek16(Uxn *u) { Uint8 a = pop8(u->src, 0); push16(u->src, mempeek16(u->ram.dat, a)); }
void op_pok16(Uxn *u) { Uint8 a = pop8(u->src, 0); Uint16 b = pop16(u->src, 0); mempoke16(u->ram.dat, a, b); }
void op_ldr16(Uxn *u) { Uint8 a = pop8(u->src, 0); push16(u->src, mempeek16(u->ram.dat, u->ram.ptr + (Sint8)a)); }
void op_str16(Uxn *u) { Uint8 a = pop8(u->src, 0); Uint16 b = pop16(u->src, 0); mempoke16(u->ram.dat, u->ram.ptr + (Sint8)a, b); }
void op_lda16(Uxn *u) { Uint16 a = pop16(u->src, 0); push16(u->src, mempeek16(u->ram.dat, a)); }
void op_sta16(Uxn *u) { Uint16 a = pop16(u->src, 0); Uint16 b = pop16(u->src, 1); mempoke16(u->ram.dat, a, b); }
void op_dei16(Uxn *u) { Uint8 a = pop8(u->src, 0); push16(u->src, devpeek16(&u->dev[a >> 4], a)); }
void op_deo16(Uxn *u) { Uint8 a = pop8(u->src, 0); Uint16 b = pop16(u->src, 0); devpoke16(&u->dev[a >> 4], a, b); }
void op_pek16(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, mempeek16(u->ram.dat, a)); }
void op_pok16(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); mempoke16(u->ram.dat, a, b); }
void op_ldr16(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, mempeek16(u->ram.dat, u->ram.ptr + (Sint8)a)); }
void op_str16(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); mempoke16(u->ram.dat, u->ram.ptr + (Sint8)a, b); }
void op_lda16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->src, mempeek16(u->ram.dat, a)); }
void op_sta16(Uxn *u) { Uint16 a = pop16(u->src); Uint16 b = pop16(u->src); mempoke16(u->ram.dat, a, b); }
void op_dei16(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, devpeek16(&u->dev[a >> 4], a)); }
void op_deo16(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); devpoke16(&u->dev[a >> 4], a, b); }
/* Arithmetic(16-bits) */
void op_add16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push16(u->src, b + a); }
void op_sub16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push16(u->src, b - a); }
void op_mul16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push16(u->src, b * a); }
void op_div16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push16(u->src, b / a); }
void op_and16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push16(u->src, b & a); }
void op_ora16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push16(u->src, b | a); }
void op_eor16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push16(u->src, b ^ a); }
void op_sft16(Uxn *u) { Uint16 a = pop16(u->src, 0), b = pop16(u->src, 1); push16(u->src, b >> (a & 0x000f) << ((a & 0x00f0) >> 4)); }
void op_add16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b + a); }
void op_sub16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b - a); }
void op_mul16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b * a); }
void op_div16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b / a); }
void op_and16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b & a); }
void op_ora16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b | a); }
void op_eor16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b ^ a); }
void op_sft16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b >> (a & 0x000f) << ((a & 0x00f0) >> 4)); }
void (*ops[])(Uxn *u) = {
op_brk, op_lit, op_nop, op_pop, op_dup, op_swp, op_ovr, op_rot,
@ -131,7 +131,12 @@ opcuxn(Uxn *u, Uint8 instr)
Uint8 op = instr & 0x3f, freturn = instr & 0x40, fkeep = instr & 0x80;
u->src = freturn ? &u->rst : &u->wst;
u->dst = freturn ? &u->wst : &u->rst;
pop8 = fkeep ? pop8_keep : pop8_nokeep;
if(fkeep) {
pop8 = pop8_keep;
u->src->kptr = u->src->ptr;
} else {
pop8 = pop8_nokeep;
}
(*ops[op])(u);
}

View file

@ -19,7 +19,7 @@ typedef signed short Sint16;
#define PAGE_PROGRAM 0x0100
typedef struct {
Uint8 ptr, error;
Uint8 ptr, kptr, error;
Uint8 dat[256];
} Stack;