uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

commit 6a1bf9215d9ee27eb4da3926fc250689208ebdcf
parent c9e35a5ad803adfb8eda34346ab7c5daacbeaf07
Author: neauoire <aliceffekt@gmail.com>
Date:   Tue, 20 Apr 2021 20:38:15 -0700

Removed genpoke macros

Diffstat:
Msrc/debugger.c | 8++++----
Msrc/emulator.c | 43+++++++++++++++++++++----------------------
Msrc/ppu.c | 36+++++++++++++++++++-----------------
Msrc/ppu.h | 10+++++-----
Msrc/uxn.c | 28++++++++++++++--------------
Msrc/uxn.h | 13+++----------
6 files changed, 66 insertions(+), 72 deletions(-)

diff --git a/src/debugger.c b/src/debugger.c @@ -56,9 +56,9 @@ file_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) { Uint8 read = b0 == 0xd; if(read || b0 == 0xf) { - char *name = (char *)&u->ram.dat[genpeek16(m, 0x8)]; - Uint16 result = 0, length = genpeek16(m, 0xa); - Uint16 offset = genpeek16(m, 0x4); + char *name = (char *)&u->ram.dat[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) { @@ -66,7 +66,7 @@ file_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) printf("%s %d bytes, at %04x from %s\n", read ? "Loaded" : "Saved", length, addr, name); fclose(f); } - genpoke16(m, 0x2, result); + mempoke16(m, 0x2, result); } return b1; } diff --git a/src/emulator.c b/src/emulator.c @@ -49,7 +49,7 @@ audio_callback(void *u, Uint8 *stream, int len) void redraw(Uint32 *dst, Uxn *u) { - drawppu(&ppu); + draw(&ppu); if(debug) drawdebugger(&ppu, u->wst.dat, u->wst.ptr); SDL_UpdateTexture(gTexture, NULL, dst, ppu.width * sizeof(Uint32)); @@ -130,8 +130,8 @@ domouse(SDL_Event *event) Uint8 flag = 0x00; Uint16 x = clamp(event->motion.x / zoom - ppu.pad, 0, ppu.hor * 8 - 1); Uint16 y = clamp(event->motion.y / zoom - ppu.pad, 0, ppu.ver * 8 - 1); - genpoke16(devmouse->dat, 0x2, x); - genpoke16(devmouse->dat, 0x4, y); + mempoke16(devmouse->dat, 0x2, x); + mempoke16(devmouse->dat, 0x4, y); devmouse->dat[7] = 0x00; switch(event->button.button) { case SDL_BUTTON_LEFT: flag = 0x01; break; @@ -184,8 +184,7 @@ doctrl(Uxn *u, SDL_Event *event, int z) Uint8 system_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) { - getcolors(&ppu, &m[0x8]); - printf("%02x%02x %02x%02x %02x%02x\n", m[0x8], m[0x9], m[0xa], m[0xb], m[0xc], m[0xd]); + putcolors(&ppu, &m[0x8]); reqdraw = 1; (void)u; (void)b0; @@ -209,9 +208,9 @@ Uint8 screen_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) { if(b0 == 0xe) { - Uint16 x = genpeek16(m, 0x8); - Uint16 y = genpeek16(m, 0xa); - Uint8 *addr = &u->ram.dat[genpeek16(m, 0xc)]; + Uint16 x = mempeek16(m, 0x8); + Uint16 y = mempeek16(m, 0xa); + Uint8 *addr = &u->ram.dat[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; @@ -228,9 +227,9 @@ file_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) { Uint8 read = b0 == 0xd; if(read || b0 == 0xf) { - char *name = (char *)&u->ram.dat[genpeek16(m, 0x8)]; - Uint16 result = 0, length = genpeek16(m, 0xa); - Uint16 offset = genpeek16(m, 0x4); + char *name = (char *)&u->ram.dat[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) { @@ -238,7 +237,7 @@ file_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) printf("%s %d bytes, at %04x from %s\n", read ? "Loaded" : "Saved", length, addr, name); fclose(f); } - genpoke16(m, 0x2, result); + mempoke16(m, 0x2, result); } return b1; } @@ -249,16 +248,16 @@ audio_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) if(b0 == 0xa) { if(b1 >= apu.n_notes) apu.notes = SDL_realloc(apu.notes, (b1 + 1) * sizeof(Note)); while(b1 >= apu.n_notes) SDL_zero(apu.notes[apu.n_notes++]); - apu_play_note(&apu.notes[b1], genpeek16(m, 0x0), genpeek16(m, 0x2), m[0x8], m[0x9] & 0x7f, m[0x9] > 0x7f); + apu_play_note(&apu.notes[b1], mempeek16(m, 0x0), mempeek16(m, 0x2), m[0x8], m[0x9] & 0x7f, m[0x9] > 0x7f); } else if(b0 == 0xe && apu.queue != NULL) { if(apu.queue->n == apu.queue->sz) { apu.queue->sz = apu.queue->sz < 4 ? 4 : apu.queue->sz * 2; apu.queue->dat = SDL_realloc(apu.queue->dat, apu.queue->sz * sizeof(*apu.queue->dat)); } if(apu.queue->is_envelope) - apu.queue->dat[apu.queue->n++] = genpeek16(m, 0xb) >> 1; + apu.queue->dat[apu.queue->n++] = mempeek16(m, 0xb) >> 1; else - apu.queue->dat[apu.queue->n++] = genpeek16(m, 0xb) + 0x8000; + apu.queue->dat[apu.queue->n++] = mempeek16(m, 0xb) + 0x8000; apu.queue->dat[apu.queue->n++] = (m[0xd] << 8) + b1; } else if(b0 == 0xf && apu.queue != NULL) apu.queue->finishes = 1; @@ -281,14 +280,14 @@ datetime_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1) time_t seconds = time(NULL); struct tm *t = localtime(&seconds); t->tm_year += 1900; - genpoke16(m, 0x0, t->tm_year); + mempoke16(m, 0x0, t->tm_year); m[0x2] = t->tm_mon; m[0x3] = t->tm_mday; m[0x4] = t->tm_hour; m[0x5] = t->tm_min; m[0x6] = t->tm_sec; m[0x7] = t->tm_wday; - genpoke16(m, 0x08, t->tm_yday); + mempoke16(m, 0x08, t->tm_yday); m[0xa] = t->tm_isdst; (void)u; (void)b0; @@ -327,14 +326,14 @@ start(Uxn *u) case SDL_KEYDOWN: case SDL_KEYUP: doctrl(u, &event, event.type == SDL_KEYDOWN); - evaluxn(u, genpeek16(devctrl->dat, 0)); + evaluxn(u, mempeek16(devctrl->dat, 0)); devctrl->dat[3] = 0; break; case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEMOTION: domouse(&event); - evaluxn(u, genpeek16(devmouse->dat, 0)); + evaluxn(u, mempeek16(devmouse->dat, 0)); break; case SDL_WINDOWEVENT: if(event.window.event == SDL_WINDOWEVENT_EXPOSED) @@ -342,7 +341,7 @@ start(Uxn *u) break; } } - evaluxn(u, genpeek16(devscreen->dat, 0)); + evaluxn(u, mempeek16(devscreen->dat, 0)); SDL_UnlockAudioDevice(audio_id); if(reqdraw) redraw(ppu.output, u); @@ -387,8 +386,8 @@ main(int argc, char **argv) apu.channel_ptr = &devapu->dat[0xa]; /* Write screen size to dev/screen */ - genpoke16(devscreen->dat, 2, ppu.hor * 8); - genpoke16(devscreen->dat, 4, ppu.ver * 8); + mempoke16(devscreen->dat, 2, ppu.hor * 8); + mempoke16(devscreen->dat, 4, ppu.ver * 8); start(&u); quit(); diff --git a/src/ppu.c b/src/ppu.c @@ -44,10 +44,16 @@ clear(Ppu *p) } void -drawpixel(Ppu *p, Uint16 x, Uint16 y, Uint8 color) +putcolors(Ppu *p, Uint8 *addr) { - if(x >= p->pad && x <= p->width - p->pad - 1 && y >= p->pad && y <= p->height - p->pad - 1) - p->output[y * p->width + x] = p->colors[color]; + int i; + for(i = 0; i < 4; ++i) { + Uint8 + r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f, + g = (*(addr + 2 + i / 2) >> (!(i % 2) << 2)) & 0x0f, + b = (*(addr + 4 + i / 2) >> (!(i % 2) << 2)) & 0x0f; + p->colors[i] = (r << 20) + (r << 16) + (g << 12) + (g << 8) + (b << 4) + b; + } } void @@ -91,6 +97,15 @@ putchr(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color) } } +/* output */ + +void +drawpixel(Ppu *p, Uint16 x, Uint16 y, Uint8 color) +{ + if(x >= p->pad && x <= p->width - p->pad - 1 && y >= p->pad && y <= p->height - p->pad - 1) + p->output[y * p->width + x] = p->colors[color]; +} + void drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr) { @@ -111,20 +126,7 @@ drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr) } void -getcolors(Ppu *p, Uint8 *addr) -{ - int i; - for(i = 0; i < 4; ++i) { - Uint8 - r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f, - g = (*(addr + 2 + i / 2) >> (!(i % 2) << 2)) & 0x0f, - b = (*(addr + 4 + i / 2) >> (!(i % 2) << 2)) & 0x0f; - p->colors[i] = (r << 20) + (r << 16) + (g << 12) + (g << 8) + (b << 4) + b; - } -} - -void -drawppu(Ppu *p) +draw(Ppu *p) { Uint16 x, y; for(y = 0; y < p->ver; ++y) diff --git a/src/ppu.h b/src/ppu.h @@ -24,9 +24,9 @@ typedef struct Ppu { } Ppu; int initppu(Ppu *p, Uint8 hor, Uint8 ver, Uint8 pad); -void drawppu(Ppu *p); -void drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr); -void getcolors(Ppu *p, Uint8 *addr); +void putcolors(Ppu *p, Uint8 *addr); void putpixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color); void puticn(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color); -void putchr(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color); -\ No newline at end of file +void putchr(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color); +void draw(Ppu *p); +void drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr); +\ No newline at end of file diff --git a/src/uxn.c b/src/uxn.c @@ -19,21 +19,21 @@ WITH REGARD TO THIS SOFTWARE. void push8(Stack *s, Uint8 a) { if (s->ptr == 0xff) { s->error = 2; return; } s->dat[s->ptr++] = a; } Uint8 pop8(Stack *s) { if (s->ptr == 0) { s->error = 1; return 0; } return s->dat[--s->ptr]; } Uint8 peek8(Stack *s, Uint8 a) { if (s->ptr < a + 1) s->error = 1; return s->dat[s->ptr - a - 1]; } -void mempoke8(Uxn *u, Uint16 a, Uint8 b) { u->ram.dat[a] = b; } -Uint8 mempeek8(Uxn *u, Uint16 a) { return u->ram.dat[a]; } +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 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(Uxn *u, Uint16 a, Uint16 b) { mempoke8(u, a, b >> 8); mempoke8(u, a + 1, b); } -Uint16 mempeek16(Uxn *u, Uint16 a) { return (mempeek8(u, a) << 8) + mempeek8(u, a + 1); } +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); } /* Stack */ 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, u->ram.ptr++)); } +void op_lit(Uxn *u) { push8(u->src, mempeek8(&u->ram.dat[0], u->ram.ptr++)); } void op_pop(Uxn *u) { pop8(u->src); } void op_dup(Uxn *u) { push8(u->src, peek8(u->src, 0)); } void op_swp(Uxn *u) { Uint8 b = pop8(u->src), a = pop8(u->src); push8(u->src, b); push8(u->src, a); } @@ -49,10 +49,10 @@ void op_lts(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, (S 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); } /* Memory */ -void op_pek(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, mempeek8(u, a)); } -void op_pok(Uxn *u) { Uint8 a = pop8(u->src); Uint8 b = pop8(u->src); mempoke8(u, a, b); } -void op_ldr(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, mempeek16(u, a)); } -void op_str(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); mempoke16(u, a, b); } +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); } +void op_ldr(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, mempeek16(&u->ram.dat[0], a)); } +void op_str(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); mempoke16(&u->ram.dat[0], a, b); } 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), b = pop8(u->src); if (b) 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; } @@ -67,7 +67,7 @@ void op_ora(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b 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, u->ram.ptr++)); u->ram.ptr++; } +void op_lit16(Uxn *u) { push16(u->src, mempeek16(&u->ram.dat[0], u->ram.ptr++)); u->ram.ptr++; } void op_pop16(Uxn *u) { pop16(u->src); } void op_dup16(Uxn *u) { push16(u->src, peek16(u->src, 0)); } void op_swp16(Uxn *u) { Uint16 b = pop16(u->src), a = pop16(u->src); push16(u->src, b); push16(u->src, a); } @@ -83,10 +83,10 @@ void op_lts16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->sr 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); } /* Memory(16-bits) */ -void op_pek16(Uxn *u) { Uint16 a = pop16(u->src); push8(u->src, mempeek8(u, a)); } -void op_pok16(Uxn *u) { Uint16 a = pop16(u->src); Uint8 b = pop8(u->src); mempoke8(u, a, b); } -void op_ldr16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->src, mempeek16(u, a)); } -void op_str16(Uxn *u) { Uint16 a = pop16(u->src); Uint16 b = pop16(u->src); mempoke16(u, a, b); } +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); } +void op_ldr16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->src, mempeek16(&u->ram.dat[0], a)); } +void op_str16(Uxn *u) { Uint16 a = pop16(u->src); Uint16 b = pop16(u->src); mempoke16(&u->ram.dat[0], a, b); } void op_jmp16(Uxn *u) { u->ram.ptr = pop16(u->src); } void op_jnz16(Uxn *u) { Uint16 a = pop16(u->src); Uint8 b = pop8(u->src); if (b) u->ram.ptr = a; } void op_jsr16(Uxn *u) { push16(u->dst, u->ram.ptr); u->ram.ptr = pop16(u->src); } diff --git a/src/uxn.h b/src/uxn.h @@ -18,13 +18,6 @@ typedef signed short Sint16; #define PAGE_PROGRAM 0x0100 -#define genpeek16(ptr, i) ((ptr[i] << 8) + ptr[i + 1]) -#define genpoke16(ptr, i, v) \ - do { \ - ptr[i] = v >> 8; \ - ptr[i + 1] = v & 0xff; \ - } while(0) - typedef struct { Uint8 ptr, error; Uint8 dat[256]; @@ -38,7 +31,7 @@ typedef struct { struct Uxn; typedef struct Device { - Uint8 addr, dat[16]; + Uint8 addr, dat[16], *mem; Uint8 (*poke)(struct Uxn *u, Uint8 *devmem, Uint8, Uint8); } Device; @@ -51,7 +44,7 @@ typedef struct Uxn { int loaduxn(Uxn *c, char *filepath); int bootuxn(Uxn *c); int evaluxn(Uxn *u, Uint16 vec); -void mempoke16(Uxn *u, Uint16 a, Uint16 b); -Uint16 mempeek16(Uxn *u, Uint16 a); +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