Switched to faster unsigned ints for PPU memory

This commit is contained in:
Andrew Alderwick 2021-09-20 22:32:42 +01:00
parent b6fe4302d1
commit c0e42f1322
3 changed files with 19 additions and 15 deletions

View File

@ -23,22 +23,22 @@ static void
ppu_clear(Ppu *p) ppu_clear(Ppu *p)
{ {
int i; int i;
for(i = 0; i < p->width / 2 * p->height; ++i) for(i = 0; i < p->stride * p->height; ++i)
p->dat[i] = 0; p->dat[i] = 0;
} }
int unsigned int
ppu_pixel(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color) ppu_pixel(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color)
{ {
unsigned int i = (x + y * p->width) / 2, shift = (x % 2) * 4; unsigned int i = x / PPW + y * p->stride, shift = x % PPW * 4;
int ret = p->dat[i]; unsigned int ret = p->dat[i];
if(fg) shift += 2; if(fg) shift += 2;
p->dat[i] &= ~(3 << shift); p->dat[i] &= ~(3 << shift);
p->dat[i] |= color << shift; p->dat[i] |= color << shift;
return ret ^ p->dat[i]; return ret ^ p->dat[i];
} }
int unsigned int
ppu_1bpp(Ppu *p, int fg, 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; Uint16 v, h;
@ -56,7 +56,7 @@ ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 f
return ret; return ret;
} }
int unsigned int
ppu_2bpp(Ppu *p, int fg, 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; Uint16 v, h;
@ -81,11 +81,10 @@ ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 f
int int
ppu_set_size(Ppu *p, Uint16 width, Uint16 height) ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
{ {
/* round width up to nearest multiple of 2 */
width += width % 2;
p->width = width; p->width = width;
p->stride = (width + PPW - 1) / PPW;
p->height = height; p->height = height;
p->dat = realloc(p->dat, p->width / 2 * p->height); p->dat = realloc(p->dat, p->stride * p->height * sizeof(unsigned int));
if(p->dat == NULL) return 0; if(p->dat == NULL) return 0;
ppu_clear(p); ppu_clear(p);
return 1; return 1;

View File

@ -13,16 +13,20 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE. WITH REGARD TO THIS SOFTWARE.
*/ */
/* pixels per word in ppu.dat */
#define PPW (sizeof(unsigned int) * 2)
typedef unsigned char Uint8; typedef unsigned char Uint8;
typedef unsigned short Uint16; typedef unsigned short Uint16;
typedef unsigned int Uint32; typedef unsigned int Uint32;
typedef struct Ppu { typedef struct Ppu {
Uint16 width, height; Uint16 width, height;
Uint8 *dat; unsigned int *dat, stride;
} Ppu; } Ppu;
int ppu_set_size(Ppu *p, Uint16 width, Uint16 height); int ppu_set_size(Ppu *p, Uint16 width, Uint16 height);
int ppu_pixel(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color); unsigned 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); unsigned 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); unsigned int ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);

View File

@ -38,7 +38,8 @@ static Apu apu[POLYPHONY];
static Device *devsystem, *devscreen, *devmouse, *devctrl, *devaudio0, *devconsole; static Device *devsystem, *devscreen, *devmouse, *devctrl, *devaudio0, *devconsole;
static Uint32 *ppu_screen, stdin_event, audio0_event, palette[4]; static Uint32 *ppu_screen, stdin_event, audio0_event, palette[4];
static Uint8 zoom = 1, reqdraw = 0; static Uint8 zoom = 1;
static unsigned int reqdraw = 0;
static Uint8 font[][8] = { static Uint8 font[][8] = {
{0x00, 0x7c, 0x82, 0x82, 0x82, 0x82, 0x82, 0x7c}, {0x00, 0x7c, 0x82, 0x82, 0x82, 0x82, 0x82, 0x7c},
@ -114,7 +115,7 @@ inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
static Uint8 static Uint8
get_pixel(int x, int y) get_pixel(int x, int y)
{ {
unsigned int i = (x + y * ppu.width) / 2, shift = (x % 2) * 4; unsigned int i = x / PPW + y * ppu.stride, shift = x % PPW * 4;
return (ppu.dat[i] >> shift) & 0xf; return (ppu.dat[i] >> shift) & 0xf;
} }