diff --git a/README.md b/README.md index 3d534e5..284d4e5 100644 --- a/README.md +++ b/README.md @@ -62,11 +62,11 @@ BRK ## TODOs -- short variable - Implement signed flag to operators. - On-screen debugger. - Auto-advance ldr? - Getting rid of IOR/IOW would be nice.. +- Sending from the wst to the rst, balance mode/flag? ## Refs diff --git a/uxn.c b/uxn.c index 0ea421a..80890c5 100644 --- a/uxn.c +++ b/uxn.c @@ -19,87 +19,68 @@ WITH REGARD TO THIS SOFTWARE. void setflag(Uint8 *st, char flag, int b) { if(b) *st |= flag; else *st &= (~flag); } int getflag(Uint8 *st, char flag) { return *st & flag; } -Uint8 mempeek8(Uxn *u, Uint16 s) { return u->ram.dat[s]; } -Uint16 mempeek16(Uxn *u, Uint16 s) { return (u->ram.dat[s] << 8) + (u->ram.dat[s + 1] & 0xff); } - -void wspush8(Uxn *u, Uint8 b) { u->wst.dat[u->wst.ptr++] = b; } -Uint8 wspop8(Uxn *u) { return u->wst.dat[--u->wst.ptr]; } -Uint8 wspeek8(Uxn *u, Uint8 o) { return u->wst.dat[u->wst.ptr - o]; } -void wspush16(Uxn *u, Uint16 s) { wspush8(u,s >> 8); wspush8(u,s & 0xff); } -Uint16 wspop16(Uxn *u) { return wspop8(u) + (wspop8(u) << 8); } -Uint16 wspeek16(Uxn *u, Uint8 o) { return (u->wst.dat[u->wst.ptr - o] << 8) + u->wst.dat[u->wst.ptr - o + 1]; } - - -Uint8 rspop8(Uxn *u){ - return u->rst.dat[--u->rst.ptr]; -} - -void rspush16(Uxn *u, Uint16 a) { - - - u->rst.dat[u->rst.ptr++] = (a >> 8) & 0xff; - u->rst.dat[u->rst.ptr++] = a & 0xff; - -} -Uint16 rspop16(Uxn *u) { - return rspop8(u) + (rspop8(u) << 8); -} - - +Uint8 mempeek8(Memory *m, Uint16 a) { return m->dat[a]; } +Uint16 mempeek16(Memory *m, Uint16 a) { return (mempeek8(m, a) << 8) + mempeek8(m, a + 1); } +void push8(St8 *s, Uint8 a) { s->dat[s->ptr++] = a; } +Uint8 pop8(St8 *s) { return s->dat[--s->ptr]; } +Uint8 peek8(St8 *s, Uint8 a) { return s->dat[s->ptr - a]; } +void push16(St8 *s, Uint16 a) { push8(s, a >> 8); push8(s, a); } +Uint16 pop16(St8 *s) { return pop8(s) + (pop8(s) << 8); } +Uint16 peek16(St8 *s, Uint8 a) { return (peek8(s, a) << 8) + peek8(s, a + 1); } /* I/O */ void op_brk(Uxn *u) { setflag(&u->status,FLAG_HALT, 1); } void op_li1(Uxn *u) { u->literal += 1; } void op_lix(Uxn *u) { u->literal += u->ram.dat[u->ram.ptr++]; } void op_nop(Uxn *u) { printf("NOP"); (void)u; } -void op_ior(Uxn *u) { Device *dev = &u->dev[mempeek8(u, u->devr)]; if(dev) wspush8(u, dev->read(dev, wspop8(u))); } -void op_iow(Uxn *u) { Uint8 a = wspop8(u); Device *dev = &u->dev[mempeek8(u, u->devw)]; if(dev) dev->write(dev, a); } -void op_ldr(Uxn *u) { Uint16 a = wspop16(u); wspush8(u, u->ram.dat[a]); } -void op_str(Uxn *u) { Uint16 a = wspop16(u); Uint8 b = wspop8(u); u->ram.dat[a] = b; } +void op_ior(Uxn *u) { Device *dev = &u->dev[mempeek8(&u->ram, u->devr)]; if(dev) push8(&u->wst, dev->read(dev, pop8(&u->wst))); } +void op_iow(Uxn *u) { Uint8 a = pop8(&u->wst); Device *dev = &u->dev[mempeek8(&u->ram, u->devw)]; if(dev) dev->write(dev, a); } +void op_ldr(Uxn *u) { Uint16 a = pop16(&u->wst); push8(&u->wst, u->ram.dat[a]); } +void op_str(Uxn *u) { Uint16 a = pop16(&u->wst); Uint8 b = pop8(&u->wst); u->ram.dat[a] = b; } /* Logic */ -void op_jmp(Uxn *u) { u->ram.ptr = wspop16(u); } -void op_jsr(Uxn *u) { rspush16(u, u->ram.ptr); u->ram.ptr = wspop16(u); } -void op_rts(Uxn *u) { u->ram.ptr = rspop16(u); } +void op_jmp(Uxn *u) { u->ram.ptr = pop16(&u->wst); } +void op_jsr(Uxn *u) { push16(&u->rst, u->ram.ptr); u->ram.ptr = pop16(&u->wst); } +void op_rts(Uxn *u) { u->ram.ptr = pop16(&u->rst); } /* Stack */ -void op_pop(Uxn *u) { wspop8(u); } -void op_dup(Uxn *u) { wspush8(u, wspeek8(u, 1)); } -void op_swp(Uxn *u) { Uint8 b = wspop8(u), a = wspop8(u); wspush8(u, b); wspush8(u, a); } -void op_ovr(Uxn *u) { Uint8 a = wspeek8(u,2); wspush8(u, a); } -void op_rot(Uxn *u) { Uint8 c = wspop8(u), b = wspop8(u), a = wspop8(u); wspush8(u, b); wspush8(u, c); wspush8(u, a); } -void op_and(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b & a); } -void op_ora(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b | a); } -void op_rol(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b << a); } +void op_pop(Uxn *u) { pop8(&u->wst); } +void op_dup(Uxn *u) { push8(&u->wst, peek8(&u->wst, 1)); } +void op_swp(Uxn *u) { Uint8 b = pop8(&u->wst), a = pop8(&u->wst); push8(&u->wst, b); push8(&u->wst, a); } +void op_ovr(Uxn *u) { Uint8 a = peek8(&u->wst,2); push8(&u->wst, a); } +void op_rot(Uxn *u) { Uint8 c = pop8(&u->wst), b = pop8(&u->wst), a = pop8(&u->wst); push8(&u->wst, b); push8(&u->wst, c); push8(&u->wst, a); } +void op_and(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b & a); } +void op_ora(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b | a); } +void op_rol(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b << a); } /* Arithmetic */ -void op_add(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b + a); } -void op_sub(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b - a); } -void op_mul(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b * a); } -void op_div(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b / a); } -void op_equ(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b == a); } -void op_neq(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b != a); } -void op_gth(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b > a); } -void op_lth(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b < a); } +void op_add(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b + a); } +void op_sub(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b - a); } +void op_mul(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b * a); } +void op_div(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b / a); } +void op_equ(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b == a); } +void op_neq(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b != a); } +void op_gth(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b > a); } +void op_lth(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b < a); } /* --- */ -void op_ior16(Uxn *u) { Uint8 a = wspop8(u); Device *dev = &u->dev[mempeek8(u, u->devr)]; if(dev) wspush16(u, (dev->read(dev, a) << 8) + dev->read(dev, a + 1)); } -void op_iow16(Uxn *u) { Uint8 a = wspop8(u); Uint8 b = wspop8(u); Device *dev = &u->dev[mempeek8(u, u->devw)]; if(dev) { dev->write(dev, b); dev->write(dev, a); } } -void op_ldr16(Uxn *u) { Uint16 a = wspop16(u); wspush16(u, (u->ram.dat[a] << 8) + u->ram.dat[a + 1]); } -void op_str16(Uxn *u) { Uint16 a = wspop16(u); Uint16 b = wspop16(u); u->ram.dat[a] = b >> 8; u->ram.dat[a + 1] = b & 0xff; } +void op_ior16(Uxn *u) { Uint8 a = pop8(&u->wst); Device *dev = &u->dev[mempeek8(&u->ram, u->devr)]; if(dev) push16(&u->wst, (dev->read(dev, a) << 8) + dev->read(dev, a + 1)); } +void op_iow16(Uxn *u) { Uint8 a = pop8(&u->wst); Uint8 b = pop8(&u->wst); Device *dev = &u->dev[mempeek8(&u->ram, u->devw)]; if(dev) { dev->write(dev, b); dev->write(dev, a); } } +void op_ldr16(Uxn *u) { Uint16 a = pop16(&u->wst); push16(&u->wst, (u->ram.dat[a] << 8) + u->ram.dat[a + 1]); } +void op_str16(Uxn *u) { Uint16 a = pop16(&u->wst); Uint16 b = pop16(&u->wst); u->ram.dat[a] = b >> 8; u->ram.dat[a + 1] = b & 0xff; } /* Stack(16-bits) */ -void op_pop16(Uxn *u) { wspop16(u); } -void op_dup16(Uxn *u) { wspush16(u, wspeek16(u, 2)); } -void op_swp16(Uxn *u) { Uint16 b = wspop16(u), a = wspop16(u); wspush16(u, b); wspush16(u, a); } -void op_ovr16(Uxn *u) { Uint16 a = wspeek16(u, 4); wspush16(u, a); } -void op_rot16(Uxn *u) { Uint16 c = wspop16(u), b = wspop16(u), a = wspop16(u); wspush16(u, b); wspush16(u, c); wspush16(u, a); } -void op_and16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b & a); } -void op_ora16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b | a); } -void op_rol16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b << a); } +void op_pop16(Uxn *u) { pop16(&u->wst); } +void op_dup16(Uxn *u) { push16(&u->wst, peek16(&u->wst, 2)); } +void op_swp16(Uxn *u) { Uint16 b = pop16(&u->wst), a = pop16(&u->wst); push16(&u->wst, b); push16(&u->wst, a); } +void op_ovr16(Uxn *u) { Uint16 a = peek16(&u->wst, 4); push16(&u->wst, a); } +void op_rot16(Uxn *u) { Uint16 c = pop16(&u->wst), b = pop16(&u->wst), a = pop16(&u->wst); push16(&u->wst, b); push16(&u->wst, c); push16(&u->wst, a); } +void op_and16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b & a); } +void op_ora16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b | a); } +void op_rol16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b << a); } /* Arithmetic(16-bits) */ -void op_add16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b + a); } -void op_sub16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b - a); } -void op_mul16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b * a); } -void op_div16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b / a); } -void op_equ16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush8(u, b == a); } -void op_neq16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush8(u, b != a); } -void op_gth16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush8(u, b > a); } -void op_lth16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush8(u, b < a); } +void op_add16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b + a); } +void op_sub16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b - a); } +void op_mul16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b * a); } +void op_div16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b / a); } +void op_equ16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, b == a); } +void op_neq16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, b != a); } +void op_gth16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, b > a); } +void op_lth16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, b < a); } void (*ops[])(Uxn *u) = { op_brk, op_nop, op_li1, op_lix, op_ior, op_iow, op_ldr, op_str, @@ -139,7 +120,7 @@ lituxn(Uxn *u, Uint8 instr) { if(u->wst.ptr >= 255) return haltuxn(u, "Stack overflow", instr); - wspush8(u, instr); + push8(&u->wst, instr); u->literal--; return 1; } @@ -157,7 +138,7 @@ opcuxn(Uxn *u, Uint8 instr) return haltuxn(u, "Stack underflow", op); if(u->wst.ptr + opr[op][1] - opr[op][0] >= 255) return haltuxn(u, "Stack overflow", instr); - if(!getflag(&u->status, FLAG_COND) || (getflag(&u->status, FLAG_COND) && wspop8(u))) + if(!getflag(&u->status, FLAG_COND) || (getflag(&u->status, FLAG_COND) && pop8(&u->wst))) (*ops[op])(u); return 1; } @@ -205,9 +186,9 @@ loaduxn(Uxn *u, char *filepath) fread(u->ram.dat, sizeof(u->ram.dat), 1, f); u->devr = 0xfff8; u->devw = 0xfff9; - u->vreset = mempeek16(u, 0xfffa); - u->vframe = mempeek16(u, 0xfffc); - u->verror = mempeek16(u, 0xfffe); + u->vreset = mempeek16(&u->ram, 0xfffa); + u->vframe = mempeek16(&u->ram, 0xfffc); + u->verror = mempeek16(&u->ram, 0xfffe); printf("Uxn loaded[%s] vrst:%04x vfrm:%04x verr:%04x.\n", filepath, u->vreset,