Rename u->ram.dat to u->ram, u->ram.ptr to pc.

This commit is contained in:
Andrew Alderwick 2022-01-04 01:36:21 +00:00
parent 205f63cc43
commit 7e3353ea72
4 changed files with 26 additions and 33 deletions

View File

@ -14,7 +14,7 @@ WITH REGARD TO THIS SOFTWARE.
/* clang-format off */
/* a,b,c: general use. bs: byte/short bool. src, dst: stack ptrs, swapped in return mode.
sp: ptr to src stack ptr. kptr: "keep" mode copy of src stack ptr.
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); }
@ -23,25 +23,24 @@ WITH REGARD TO THIS SOFTWARE.
#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.dat[(x)] = (y) >> 8; u->ram.dat[(x) + 1] = (y); } else { u->ram.dat[(x)] = y; } }
#define PEEK16(o, x) { o = (u->ram.dat[(x)] << 8) + u->ram.dat[(x) + 1]; }
#define PEEK(o, x) { if(bs) { PEEK16(o, x) } else { o = u->ram.dat[(x)]; } }
#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]; }
#define PEEK(o, x) { if(bs) { PEEK16(o, x) } else { o = u->ram[(x)]; } }
#define DEVR(o, d, x) { dev = (d); o = dev->dei(dev, (x) & 0x0f); if(bs) { o = (o << 8) + dev->dei(dev, ((x) + 1) & 0x0f); } }
#define DEVW8(x, y) { dev->dat[(x) & 0xf] = y; dev->deo(dev, (x) & 0x0f); }
#define DEVW(d, x, y) { dev = (d); if(bs) { DEVW8((x), (y) >> 8); DEVW8((x) + 1, (y)); } else { DEVW8((x), (y)) } }
#define WARP(x) { if(bs) u->ram.ptr = (x); else u->ram.ptr += (Sint8)(x); }
#define WARP(x) { if(bs) pc = (x); else pc += (Sint8)(x); }
int
uxn_eval(Uxn *u, Uint16 vec)
uxn_eval(Uxn *u, Uint16 pc)
{
unsigned int a, b, c, j, k, bs, instr, errcode;
Uint8 kptr, *sp;
Stack *src, *dst;
Device *dev;
if(!vec || u->dev[0].dat[0xf]) return 0;
u->ram.ptr = vec;
if(!pc || u->dev[0].dat[0xf]) return 0;
if(u->wst.ptr > 0xf8) u->wst.ptr = 0xf8;
while((instr = u->ram.dat[u->ram.ptr++])) {
while((instr = u->ram[pc++])) {
/* Return Mode */
if(instr & 0x40) {
src = &u->rst; dst = &u->wst;
@ -59,8 +58,8 @@ uxn_eval(Uxn *u, Uint16 vec)
bs = instr & 0x20 ? 1 : 0;
switch(instr & 0x1f) {
/* Stack */
case 0x00: /* LIT */ if(bs) { PEEK16(a, u->ram.ptr) PUSH16(src, a) u->ram.ptr += 2; }
else { a = u->ram.dat[u->ram.ptr++]; PUSH8(src, a) } break;
case 0x00: /* LIT */ if(bs) { PEEK16(a, pc) PUSH16(src, a) pc += 2; }
else { a = u->ram[pc]; PUSH8(src, a) ++pc; } break;
case 0x01: /* INC */ POP(a) PUSH(src, a + 1) break;
case 0x02: /* POP */ POP(a) break;
case 0x03: /* DUP */ POP(a) PUSH(src, a) PUSH(src, a) break;
@ -75,13 +74,13 @@ uxn_eval(Uxn *u, Uint16 vec)
case 0x0b: /* LTH */ POP(a) POP(b) PUSH8(src, b < a) break;
case 0x0c: /* JMP */ POP(a) WARP(a) break;
case 0x0d: /* JCN */ POP(a) POP8(b) if(b) WARP(a) break;
case 0x0e: /* JSR */ POP(a) PUSH16(dst, u->ram.ptr) WARP(a) break;
case 0x0e: /* JSR */ POP(a) PUSH16(dst, pc) WARP(a) break;
case 0x0f: /* STH */ POP(a) PUSH(dst, a) break;
/* Memory */
case 0x10: /* LDZ */ POP8(a) PEEK(b, a) PUSH(src, b) break;
case 0x11: /* STZ */ POP8(a) POP(b) POKE(a, b) break;
case 0x12: /* LDR */ POP8(a) PEEK(b, u->ram.ptr + (Sint8)a) PUSH(src, b) break;
case 0x13: /* STR */ POP8(a) POP(b) c = u->ram.ptr + (Sint8)a; POKE(c, b) break;
case 0x12: /* LDR */ POP8(a) PEEK(b, pc + (Sint8)a) PUSH(src, b) break;
case 0x13: /* STR */ POP8(a) POP(b) c = pc + (Sint8)a; POKE(c, b) break;
case 0x14: /* LDA */ POP16(a) PEEK(b, a) PUSH(src, b) break;
case 0x15: /* STA */ POP16(a) POP(b) POKE(a, b) break;
case 0x16: /* DEI */ POP8(a) DEVR(b, &u->dev[a >> 4], a) PUSH(src, b) break;
@ -100,7 +99,7 @@ uxn_eval(Uxn *u, Uint16 vec)
return 1;
err:
return uxn_halt(u, errcode, "Stack", instr);
return uxn_halt(u, errcode, "Stack", pc - 1);
}
/* clang-format on */
@ -112,7 +111,7 @@ uxn_boot(Uxn *u, Uint8 *memory)
char *cptr = (char *)u;
for(i = 0; i < sizeof(*u); ++i)
cptr[i] = 0x00;
u->ram.dat = memory;
u->ram = memory;
return 1;
}
@ -122,7 +121,7 @@ uxn_port(Uxn *u, Uint8 id, Uint8 (*deifn)(Device *d, Uint8 port), void (*deofn)(
Device *d = &u->dev[id];
d->addr = id * 0x10;
d->u = u;
d->mem = u->ram.dat;
d->mem = u->ram;
d->dei = deifn;
d->deo = deofn;
return d;

View File

@ -29,11 +29,6 @@ typedef struct {
Uint8 dat[256];
} Stack;
typedef struct {
Uint16 ptr;
Uint8 *dat;
} Memory;
typedef struct Device {
struct Uxn *u;
Uint8 addr, dat[16], *mem;
@ -44,11 +39,11 @@ typedef struct Device {
typedef struct Uxn {
Stack wst, rst;
Memory ram;
Uint8 *ram;
Device dev[16];
} Uxn;
int uxn_boot(Uxn *c, Uint8 *memory);
int uxn_eval(Uxn *u, Uint16 vec);
int uxn_halt(Uxn *u, Uint8 error, char *name, int id);
int uxn_eval(Uxn *u, Uint16 pc);
int uxn_halt(Uxn *u, Uint8 error, char *name, Uint16 addr);
Device *uxn_port(Uxn *u, Uint8 id, Uint8 (*deifn)(Device *, Uint8), void (*deofn)(Device *, Uint8));

View File

@ -118,9 +118,9 @@ nil_deo(Device *d, Uint8 port)
static const char *errors[] = {"underflow", "overflow", "division by zero"};
int
uxn_halt(Uxn *u, Uint8 error, char *name, int id)
uxn_halt(Uxn *u, Uint8 error, char *name, Uint16 addr)
{
fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], id, u->ram.ptr);
fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], u->ram[addr], addr);
return 0;
}
@ -138,7 +138,6 @@ run(Uxn *u)
Device *d = devconsole;
while((!u->dev[0].dat[0xf]) && (read(0, &devconsole->dat[0x2], 1) > 0)) {
DEVPEEK16(vec, 0);
if(!vec) vec = u->ram.ptr; /* continue after last BRK */
uxn_eval(u, vec);
}
}
@ -149,7 +148,7 @@ load(Uxn *u, char *filepath)
FILE *f;
int r;
if(!(f = fopen(filepath, "rb"))) return 0;
r = fread(u->ram.dat + PAGE_PROGRAM, 1, 0xffff - PAGE_PROGRAM, f);
r = fread(u->ram + PAGE_PROGRAM, 1, 0xffff - PAGE_PROGRAM, f);
fclose(f);
if(r < 1) return 0;
fprintf(stderr, "Loaded %s\n", filepath);

View File

@ -125,7 +125,7 @@ static void
redraw(Uxn *u)
{
if(devsystem->dat[0xe])
screen_debug(&uxn_screen, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram.dat);
screen_debug(&uxn_screen, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram);
screen_redraw(&uxn_screen, uxn_screen.pixels);
if(SDL_UpdateTexture(gTexture, &gRect, uxn_screen.pixels, uxn_screen.width * sizeof(Uint32)) != 0)
error("SDL_UpdateTexture", SDL_GetError());
@ -276,7 +276,7 @@ load(Uxn *u, char *rom)
SDL_RWops *f;
int r;
if(!(f = SDL_RWFromFile(rom, "rb"))) return 0;
r = f->read(f, u->ram.dat + PAGE_PROGRAM, 1, 0xffff - PAGE_PROGRAM);
r = f->read(f, u->ram + PAGE_PROGRAM, 1, 0xffff - PAGE_PROGRAM);
f->close(f);
if(r < 1) return 0;
fprintf(stderr, "Loaded %s\n", rom);
@ -413,9 +413,9 @@ do_shortcut(Uxn *u, SDL_Event *event)
static const char *errors[] = {"underflow", "overflow", "division by zero"};
int
uxn_halt(Uxn *u, Uint8 error, char *name, int id)
uxn_halt(Uxn *u, Uint8 error, char *name, Uint16 addr)
{
fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], id, u->ram.ptr);
fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], u->ram[addr], addr);
return 0;
}