Cleaned up PPU

This commit is contained in:
neauoire 2021-09-30 10:44:40 -07:00
parent 9de513ad47
commit 0fe8b39447
3 changed files with 32 additions and 65 deletions

View File

@ -12,12 +12,6 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE. WITH REGARD TO THIS SOFTWARE.
*/ */
/*
pixel 0001 0002
layer fgbg fgbg
byte 1010 1010
*/
static Uint8 blending[5][16] = { static Uint8 blending[5][16] = {
{0, 0, 0, 0, 1, 0, 1, 1, 2, 2, 0, 2, 3, 3, 3, 0}, {0, 0, 0, 0, 1, 0, 1, 1, 2, 2, 0, 2, 3, 3, 3, 0},
{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3}, {0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3},
@ -28,45 +22,44 @@ static Uint8 blending[5][16] = {
static void static void
ppu_clear(Ppu *p) ppu_clear(Ppu *p)
{ {
int row; Uint32 row, bound = p->height * p->width / 2;
for(row = 0; row < p->height * p->width / 2; ++row) for(row = 0; row < bound; ++row)
p->pixels[row] = 0; p->pixels[row] = 0;
} }
Uint8
ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
{
ppu_clear(p);
p->width = width;
p->height = height;
p->pixels = realloc(p->pixels, p->width * p->height * sizeof(Uint8) / 2);
ppu_clear(p);
return !!p->pixels;
}
Uint8 Uint8
ppu_read(Ppu *p, Uint16 x, Uint16 y) ppu_read(Ppu *p, Uint16 x, Uint16 y)
{ {
int row = (x + y * p->width) / 0x2; Uint32 row = (x + y * p->width) / 0x2;
Uint8 seg = !(x & 0x1) << 2; Uint8 shift = !(x & 0x1) << 2;
Uint8 byte = p->pixels[row] >> seg; Uint8 pix = p->pixels[row] >> shift;
return (byte & 0x0c ? (byte >> 2) : byte) & 0x3; if(pix & 0x0c)
pix = pix >> 2;
return pix & 0x3;
} }
void void
ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color) ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
{ {
int row = (x + y * p->width) / 0x2; Uint32 row = (x + y * p->width) / 0x2;
Uint8 original = p->pixels[row]; Uint8 shift = (!(x & 0x1) << 2) + (layer << 1);
Uint8 next = 0x0; Uint8 pix = p->pixels[row];
if(x % 2) { Uint8 mask = ~(0x3 << shift);
if(layer) { Uint8 pixnew = (pix & mask) + (color << shift);
next |= original & 0xf3; if(x < p->width && y < p->height)
next |= color << 0x02; p->pixels[row] = pixnew;
} else { if(pix != pixnew)
next |= original & 0xfc;
next |= color;
}
} else {
if(layer) {
next |= original & 0x3f;
next |= color << 0x06;
} else {
next |= original & 0xcf;
next |= color << 0x04;
}
}
p->pixels[row] = next;
if(original != next)
p->reqdraw = 1; p->reqdraw = 1;
} }
@ -103,16 +96,3 @@ ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Ui
blending[ch][color]); blending[ch][color]);
} }
} }
/* output */
int
ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
{
ppu_clear(p);
p->width = width;
p->height = height;
p->pixels = realloc(p->pixels, p->width * p->height * sizeof(Uint8) / 2);
ppu_clear(p);
return !!p->pixels;
}

View File

@ -13,10 +13,6 @@ 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;
@ -26,10 +22,9 @@ typedef struct Ppu {
Uint16 width, height; Uint16 width, height;
} Ppu; } Ppu;
Uint8 ppu_set_size(Ppu *p, Uint16 width, Uint16 height);
Uint8 ppu_read(Ppu *p, Uint16 x, Uint16 y); Uint8 ppu_read(Ppu *p, Uint16 x, Uint16 y);
void ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color); void ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color);
void ppu_frame(Ppu *p); void ppu_frame(Ppu *p);
void ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); void ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
void ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); void ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
int ppu_set_size(Ppu *p, Uint16 width, Uint16 height);

View File

@ -121,13 +121,6 @@ set_palette(Uint8 *addr)
ppu.reqdraw = 1; ppu.reqdraw = 1;
} }
static void
set_inspect(Uint8 flag)
{
devsystem->dat[0xe] = flag;
ppu.reqdraw = 1;
}
static void static void
set_window_size(SDL_Window *window, int w, int h) set_window_size(SDL_Window *window, int w, int h)
{ {
@ -191,7 +184,6 @@ static void
draw_inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory) draw_inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
{ {
Uint8 i, x, y, b; Uint8 i, x, y, b;
for(i = 0; i < 0x20; ++i) { for(i = 0; i < 0x20; ++i) {
x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i]; x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
/* working stack */ /* working stack */
@ -324,7 +316,7 @@ doctrl(SDL_Event *event, int z)
case SDLK_LEFT: flag = 0x40; break; case SDLK_LEFT: flag = 0x40; break;
case SDLK_RIGHT: flag = 0x80; break; case SDLK_RIGHT: flag = 0x80; break;
case SDLK_F1: if(z) set_zoom(zoom > 2 ? 1 : zoom + 1); break; case SDLK_F1: if(z) set_zoom(zoom > 2 ? 1 : zoom + 1); break;
case SDLK_F2: if(z) set_inspect(!devsystem->dat[0xe]); break; case SDLK_F2: if(z) devsystem->dat[0xe] = !devsystem->dat[0xe]; break;
case SDLK_F3: if(z) capture_screen(); break; case SDLK_F3: if(z) capture_screen(); break;
} }
/* clang-format on */ /* clang-format on */
@ -391,7 +383,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
Uint16 x = peek16(d->dat, 0x8); Uint16 x = peek16(d->dat, 0x8);
Uint16 y = peek16(d->dat, 0xa); Uint16 y = peek16(d->dat, 0xa);
Uint8 layer = d->dat[0xe] & 0x40; Uint8 layer = d->dat[0xe] & 0x40;
ppu_write(&ppu, layer, x, y, d->dat[0xe] & 0x3); ppu_write(&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] & 0x01) poke16(d->dat, 0x8, x + 1); /* auto x+1 */
if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 1); /* auto y+1 */ if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 1); /* auto y+1 */
break; break;
@ -402,10 +394,10 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
Uint8 layer = d->dat[0xf] & 0x40; Uint8 layer = d->dat[0xf] & 0x40;
Uint8 *addr = &d->mem[peek16(d->dat, 0xc)]; Uint8 *addr = &d->mem[peek16(d->dat, 0xc)];
if(d->dat[0xf] & 0x80) { if(d->dat[0xf] & 0x80) {
ppu_2bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20); 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 */ if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 16); /* auto addr+16 */
} else { } else {
ppu_1bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20); 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] & 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 */ if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 8); /* auto x+8 */