From b6fe4302d11667e1a6c171530b1fcbd69863a04d Mon Sep 17 00:00:00 2001 From: Andrew Alderwick Date: Mon, 20 Sep 2021 00:04:40 +0100 Subject: [PATCH] Switched to linear PPU memory --- src/devices/ppu.c | 53 +++++++++++++++++------------------------------ src/devices/ppu.h | 8 +++---- src/uxnemu.c | 40 +++++++++++++++-------------------- 3 files changed, 40 insertions(+), 61 deletions(-) diff --git a/src/devices/ppu.c b/src/devices/ppu.c index ec99ef0..f5d50dc 100644 --- a/src/devices/ppu.c +++ b/src/devices/ppu.c @@ -22,38 +22,24 @@ static Uint8 blending[5][16] = { static void ppu_clear(Ppu *p) { - int x, y; - for(y = 0; y < p->height; ++y) { - for(x = 0; x < p->width; ++x) { - ppu_pixel(p, p->bg, x, y, 0); - ppu_pixel(p, p->fg, x, y, 0); - } - } + int i; + for(i = 0; i < p->width / 2 * p->height; ++i) + p->dat[i] = 0; } int -ppu_pixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color) +ppu_pixel(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color) { - int row = (y % 8) + ((x / 8 + y / 8 * p->width / 8) * 16), col = x % 8, ret; - Uint8 w; - if(x >= p->width || y >= p->height) - return 0; - w = layer[row]; - if(color == 0 || color == 2) - layer[row] &= ~(1UL << (7 - col)); - else - layer[row] |= 1UL << (7 - col); - ret = w ^ layer[row]; - w = layer[row + 8]; - if(color == 0 || color == 1) - layer[row + 8] &= ~(1UL << (7 - col)); - else - layer[row + 8] |= 1UL << (7 - col); - return ret | (w ^ layer[row + 8]); + unsigned int i = (x + y * p->width) / 2, shift = (x % 2) * 4; + int ret = p->dat[i]; + if(fg) shift += 2; + p->dat[i] &= ~(3 << shift); + p->dat[i] |= color << shift; + return ret ^ p->dat[i]; } int -ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) +ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) { Uint16 v, h; int ret = 0; @@ -62,7 +48,7 @@ ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U Uint8 ch1 = (sprite[v] >> (7 - h)) & 0x1; if(ch1 || blending[4][color]) ret |= ppu_pixel(p, - layer, + fg, x + (flipx ? 7 - h : h), y + (flipy ? 7 - v : v), blending[ch1][color]); @@ -71,7 +57,7 @@ ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U } int -ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) +ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) { Uint16 v, h; int ret = 0; @@ -82,7 +68,7 @@ ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U Uint8 ch = ch1 + ch2 * 2; if(ch || blending[4][color]) ret |= ppu_pixel(p, - layer, + fg, x + (flipx ? 7 - h : h), y + (flipy ? 7 - v : v), blending[ch][color]); @@ -95,13 +81,12 @@ ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U int ppu_set_size(Ppu *p, Uint16 width, Uint16 height) { - /* round width and height up to nearest multiple of 8 */ - width = ((width - 1) | 0x7) + 1; - height = ((height - 1) | 0x7) + 1; + /* round width up to nearest multiple of 2 */ + width += width % 2; p->width = width; p->height = height; - p->bg = realloc(p->bg, p->width / 4 * p->height * sizeof(Uint8)); - p->fg = realloc(p->fg, p->width / 4 * p->height * sizeof(Uint8)); + p->dat = realloc(p->dat, p->width / 2 * p->height); + if(p->dat == NULL) return 0; ppu_clear(p); - return p->bg && p->fg; + return 1; } \ No newline at end of file diff --git a/src/devices/ppu.h b/src/devices/ppu.h index b5e2071..3c056f0 100644 --- a/src/devices/ppu.h +++ b/src/devices/ppu.h @@ -19,10 +19,10 @@ typedef unsigned int Uint32; typedef struct Ppu { Uint16 width, height; - Uint8 *bg, *fg; + Uint8 *dat; } Ppu; int ppu_set_size(Ppu *p, Uint16 width, Uint16 height); -int ppu_pixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color); -int ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); -int ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); +int ppu_pixel(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color); +int ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); +int ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); diff --git a/src/uxnemu.c b/src/uxnemu.c index 0d1dd06..0aad157 100644 --- a/src/uxnemu.c +++ b/src/uxnemu.c @@ -90,38 +90,32 @@ inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory) Uint8 i, x, y, b; for(i = 0; i < 0x20; ++i) { /* stack */ x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i]; - ppu_1bpp(p, ppu.fg, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0); - ppu_1bpp(p, ppu.fg, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0); + ppu_1bpp(p, 1, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0); + ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0); } /* return pointer */ - ppu_1bpp(p, ppu.fg, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0); - ppu_1bpp(p, ppu.fg, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0); + ppu_1bpp(p, 1, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0); + ppu_1bpp(p, 1, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0); for(i = 0; i < 0x20; ++i) { /* memory */ x = ((i % 8) * 3 + 1) * 8, y = 0x38 + (i / 8 + 1) * 8, b = memory[i]; - ppu_1bpp(p, ppu.fg, x, y, font[(b >> 4) & 0xf], 3, 0, 0); - ppu_1bpp(p, ppu.fg, x + 8, y, font[b & 0xf], 3, 0, 0); + ppu_1bpp(p, 1, x, y, font[(b >> 4) & 0xf], 3, 0, 0); + ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 3, 0, 0); } for(x = 0; x < 0x10; ++x) { /* guides */ - ppu_pixel(p, ppu.fg, x, p->height / 2, 2); - ppu_pixel(p, ppu.fg, p->width - x, p->height / 2, 2); - ppu_pixel(p, ppu.fg, p->width / 2, p->height - x, 2); - ppu_pixel(p, ppu.fg, p->width / 2, x, 2); - ppu_pixel(p, ppu.fg, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2); - ppu_pixel(p, ppu.fg, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2); + ppu_pixel(p, 1, x, p->height / 2, 2); + ppu_pixel(p, 1, p->width - x, p->height / 2, 2); + ppu_pixel(p, 1, p->width / 2, p->height - x, 2); + ppu_pixel(p, 1, p->width / 2, x, 2); + ppu_pixel(p, 1, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2); + ppu_pixel(p, 1, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2); } } static Uint8 get_pixel(int x, int y) { - int ch1, ch2, r = (y % 8) + ((x / 8 + y / 8 * ppu.width / 8) * 16); - ch1 = (ppu.fg[r] >> (7 - x % 8)) & 1; - ch2 = (ppu.fg[r + 8] >> (7 - x % 8)) & 1; - if(!ch1 && !ch2) { - ch1 = (ppu.bg[r] >> (7 - x % 8)) & 1; - ch2 = (ppu.bg[r + 8] >> (7 - x % 8)) & 1; - } - return ch1 + (ch2 << 1); + unsigned int i = (x + y * ppu.width) / 2, shift = (x % 2) * 4; + return (ppu.dat[i] >> shift) & 0xf; } static void @@ -356,7 +350,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w) Uint16 x = peek16(d->dat, 0x8); Uint16 y = peek16(d->dat, 0xa); Uint8 layer = d->dat[0xe] & 0x40; - reqdraw |= ppu_pixel(&ppu, layer ? ppu.fg : ppu.bg, x, y, d->dat[0xe] & 0x3); + reqdraw |= ppu_pixel(&ppu, layer, x, y, d->dat[0xe] & 0x3); if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 1); /* auto x+1 */ if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 1); /* auto y+1 */ break; @@ -367,10 +361,10 @@ screen_talk(Device *d, Uint8 b0, Uint8 w) Uint8 layer = d->dat[0xf] & 0x40; Uint8 *addr = &d->mem[peek16(d->dat, 0xc)]; if(d->dat[0xf] & 0x80) { - reqdraw |= ppu_2bpp(&ppu, layer ? ppu.fg : ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20); + reqdraw |= ppu_2bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20); if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 16); /* auto addr+16 */ } else { - reqdraw |= ppu_1bpp(&ppu, layer ? ppu.fg : ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20); + reqdraw |= ppu_1bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20); if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 8); /* auto addr+8 */ } if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 8); /* auto x+8 */