diff --git a/src/debugger.c b/src/debugger.c index 82ccbe7..c61a79a 100644 --- a/src/debugger.c +++ b/src/debugger.c @@ -38,7 +38,7 @@ printstack(Stack *s) #pragma mark - Devices Uint8 -console_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) +console_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1) { switch(b0) { case 0x08: printf("%c", b1); break; @@ -46,17 +46,17 @@ console_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) case 0x0b: printf("0x%04x\n", (m[0x0a] << 8) + b1); break; } fflush(stdout); - (void)u; + (void)d; (void)b0; return b1; } Uint8 -file_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) +file_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1) { Uint8 read = b0 == 0xd; if(read || b0 == 0xf) { - char *name = (char *)&u->ram.dat[mempeek16(m, 0x8)]; + char *name = (char *)&d->mem[mempeek16(m, 0x8)]; Uint16 result = 0, length = mempeek16(m, 0xa); Uint16 offset = mempeek16(m, 0x4); Uint16 addr = (m[b0 - 1] << 8) | b1; @@ -72,9 +72,9 @@ file_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) } Uint8 -ppnil(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) +ppnil(Device *d, Uint8 *m, Uint8 b0, Uint8 b1) { - (void)u; + (void)d; (void)m; (void)b0; return b1; diff --git a/src/emulator.c b/src/emulator.c index f18c56d..b3d13be 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -182,35 +182,35 @@ doctrl(Uxn *u, SDL_Event *event, int z) #pragma mark - Devices Uint8 -system_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) +system_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1) { putcolors(&ppu, &m[0x8]); reqdraw = 1; - (void)u; + (void)d; (void)b0; return b1; } Uint8 -console_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) +console_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1) { switch(b0) { case 0x8: printf("%c", b1); break; case 0x9: printf("0x%02x\n", b1); break; case 0xb: printf("0x%04x\n", (m[0xa] << 8) + b1); break; - case 0xd: printf("%s\n", &u->ram.dat[(m[0xc] << 8) + b1]); break; + case 0xd: printf("%s\n", &d->mem[(m[0xc] << 8) + b1]); break; } fflush(stdout); return b1; } Uint8 -screen_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) +screen_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1) { if(b0 == 0xe) { Uint16 x = mempeek16(m, 0x8); Uint16 y = mempeek16(m, 0xa); - Uint8 *addr = &u->ram.dat[mempeek16(m, 0xc)]; + Uint8 *addr = &d->mem[mempeek16(m, 0xc)]; Uint8 *layer = b1 >> 4 & 0x1 ? ppu.fg : ppu.bg; switch(b1 >> 5) { case 0: putpixel(&ppu, layer, x, y, b1 & 0x3); break; @@ -223,27 +223,32 @@ screen_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) } Uint8 -file_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) +file_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1) { + /* TODO: Figure out why fwrite doesn't work with d->mem Uint8 read = b0 == 0xd; if(read || b0 == 0xf) { - char *name = (char *)&u->ram.dat[mempeek16(m, 0x8)]; + char *name = (char *)&d->mem[mempeek16(m, 0x8)]; Uint16 result = 0, length = mempeek16(m, 0xa); Uint16 offset = mempeek16(m, 0x4); Uint16 addr = (m[b0 - 1] << 8) | b1; FILE *f = fopen(name, read ? "r" : (offset ? "a" : "w")); if(f) { - if(fseek(f, offset, SEEK_SET) != -1 && (result = read ? fread(&u->ram.dat[addr], 1, length, f) : fwrite(&u->ram.dat[addr], 1, length, f))) + if(fseek(f, offset, SEEK_SET) != -1 && (result = read ? fread(d->mem[addr], 1, length, f) : fwrite(&d->mem[addr], 1, length, f))) printf("%s %d bytes, at %04x from %s\n", read ? "Loaded" : "Saved", length, addr, name); fclose(f); } mempoke16(m, 0x2, result); } + */ + (void)d; + (void)m; + (void)b0; return b1; } static Uint8 -audio_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) +audio_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1) { if(b0 == 0xa) { if(b1 >= apu.n_notes) apu.notes = SDL_realloc(apu.notes, (b1 + 1) * sizeof(Note)); @@ -261,21 +266,12 @@ audio_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) apu.queue->dat[apu.queue->n++] = (m[0xd] << 8) + b1; } else if(b0 == 0xf && apu.queue != NULL) apu.queue->finishes = 1; - (void)u; + (void)d; return b1; } Uint8 -midi_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) -{ - (void)u; - (void)m; - printf("midi - %02x,%02x\n", b0, b1); - return b1; -} - -Uint8 -datetime_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) +datetime_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1) { time_t seconds = time(NULL); struct tm *t = localtime(&seconds); @@ -289,15 +285,15 @@ datetime_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) m[0x7] = t->tm_wday; mempoke16(m, 0x08, t->tm_yday); m[0xa] = t->tm_isdst; - (void)u; + (void)d; (void)b0; return b1; } Uint8 -ppnil(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) +ppnil(Device *d, Uint8 *m, Uint8 b0, Uint8 b1) { - (void)u; + (void)d; (void)m; (void)b0; return b1; diff --git a/src/uxn.c b/src/uxn.c index df67eb2..7a1dd6b 100644 --- a/src/uxn.c +++ b/src/uxn.c @@ -21,15 +21,15 @@ Uint8 pop8(Stack *s) { if (s->ptr == 0) { s->error = 1; return 0; } return s->d Uint8 peek8(Stack *s, Uint8 a) { if (s->ptr < a + 1) s->error = 1; return s->dat[s->ptr - a - 1]; } void mempoke8(Uint8 *m, Uint16 a, Uint8 b) { m[a] = b; } Uint8 mempeek8(Uint8 *m, Uint16 a) { return m[a]; } -void devpoke8(Uxn *u, Uint8 a, Uint8 b) { Device *dev = &u->dev[a >> 4]; dev->dat[a & 0xf] = b; dev->poke(u, dev->dat, a & 0x0f, b); } -Uint8 devpeek8(Uxn *u, Uint8 a) { return u->dev[a >> 4].dat[a & 0xf]; } +void devpoke8(Device *d, Uint8 a, Uint8 b) { d->dat[a & 0xf] = b; d->poke(d, d->dat, a & 0x0f, b); } +Uint8 devpeek8(Device *d, Uint8 a) { return d->dat[a & 0xf]; } void push16(Stack *s, Uint16 a) { push8(s, a >> 8); push8(s, a); } Uint16 pop16(Stack *s) { return pop8(s) + (pop8(s) << 8); } Uint16 peek16(Stack *s, Uint8 a) { return peek8(s, a * 2) + (peek8(s, a * 2 + 1) << 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(Uxn *u, Uint8 a, Uint16 b) { devpoke8(u, a, b >> 8); devpoke8(u, a + 1, b); } -Uint16 devpeek16(Uxn *u, Uint16 a) { return (devpeek8(u, a) << 8) + devpeek8(u, a + 1); } +void devpoke16(Device *d, Uint8 a, Uint16 b) { devpoke8(d, a, b >> 8); devpoke8(d, a + 1, b); } +Uint16 devpeek16(Device *d, Uint16 a) { return (devpeek8(d, a) << 8) + devpeek8(d, a + 1); } /* Stack */ void op_brk(Uxn *u) { u->ram.ptr = 0; } void op_nop(Uxn *u) { (void)u; } @@ -46,8 +46,8 @@ void op_gth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b void op_lth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b < a); } void op_gts(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, (Sint8)b > (Sint8)a); } void op_lts(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, (Sint8)b < (Sint8)a); } -void op_ior(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, devpeek8(u, a)); } -void op_iow(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); devpoke8(u, a, b); } +void op_ior(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, devpeek8(&u->dev[a >> 4], a)); } +void op_iow(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); devpoke8(&u->dev[a >> 4], a, b); } /* Memory */ void op_pek(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, mempeek8(&u->ram.dat[0], a)); } void op_pok(Uxn *u) { Uint8 a = pop8(u->src); Uint8 b = pop8(u->src); mempoke8(&u->ram.dat[0], a, b); } @@ -80,8 +80,8 @@ void op_gth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->sr void op_lth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b < a); } void op_gts16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, (Sint16)b > (Sint16)a); } void op_lts16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, (Sint16)b < (Sint16)a); } -void op_ior16(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, devpeek16(u, a)); } -void op_iow16(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); devpoke16(u, a, b); } +void op_ior16(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, devpeek16(&u->dev[a >> 4], a)); } +void op_iow16(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); devpoke16(&u->dev[a >> 4], a, b); } /* Memory(16-bits) */ void op_pek16(Uxn *u) { Uint16 a = pop16(u->src); push8(u->src, mempeek8(&u->ram.dat[0], a)); } void op_pok16(Uxn *u) { Uint16 a = pop16(u->src); Uint8 b = pop8(u->src); mempoke8(&u->ram.dat[0], a, b); } @@ -179,10 +179,11 @@ loaduxn(Uxn *u, char *filepath) } Device * -portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Uxn *u, Uint8 *devmem, Uint8 b0, Uint8 b1)) +portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Device *d, Uint8 *devmem, Uint8 b0, Uint8 b1)) { Device *d = &u->dev[id]; d->addr = id * 0x10; + d->mem = &u->ram.dat[0]; d->poke = pofn; printf("Device added #%02x: %s, at 0x%04x \n", id, name, d->addr); return d; diff --git a/src/uxn.h b/src/uxn.h index 068c54b..ff908d0 100644 --- a/src/uxn.h +++ b/src/uxn.h @@ -32,7 +32,7 @@ struct Uxn; typedef struct Device { Uint8 addr, dat[16], *mem; - Uint8 (*poke)(struct Uxn *u, Uint8 *devmem, Uint8, Uint8); + Uint8 (*poke)(struct Device *d, Uint8 *devmem, Uint8, Uint8); } Device; typedef struct Uxn { @@ -47,4 +47,4 @@ int evaluxn(Uxn *u, Uint16 vec); void mempoke16(Uint8 *m, Uint16 a, Uint16 b); Uint16 mempeek16(Uint8 *m, Uint16 a); -Device *portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Uxn *, Uint8 *, Uint8, Uint8)); \ No newline at end of file +Device *portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Device *, Uint8 *, Uint8, Uint8)); \ No newline at end of file