Progress on new bitpacking

This commit is contained in:
neauoire 2021-09-29 20:44:15 -07:00
parent 253be6f50c
commit bac54f1fd9
3 changed files with 45 additions and 44 deletions

View File

@ -12,6 +12,11 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE. WITH REGARD TO THIS SOFTWARE.
*/ */
/*
fgbg fgbg
byte [0000 0000]
*/
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},
@ -31,8 +36,8 @@ ppu_clear(Ppu *p)
int x, y; int x, y;
for(y = 0; y < p->height; ++y) { for(y = 0; y < p->height; ++y) {
for(x = 0; x < p->width; ++x) { for(x = 0; x < p->width; ++x) {
ppu_write(p, p->bg, x, y, 0); ppu_write(p, 0, x, y, 0);
ppu_write(p, p->fg, x, y, 0); ppu_write(p, 1, x, y, 0);
} }
} }
} }
@ -40,34 +45,30 @@ ppu_clear(Ppu *p)
Uint8 Uint8
ppu_read(Ppu *p, Uint16 x, Uint16 y) ppu_read(Ppu *p, Uint16 x, Uint16 y)
{ {
Uint16 ch1, ch2, row = ppu_row(p, x, y); int row = (x + y * p->width) / 0x2;
ch1 = (p->fg[row] >> (7 - x % 8)) & 1; int shift = (1 - (x & 0x1)) * 0x4;
ch2 = (p->fg[row + 8] >> (7 - x % 8)) & 1;
if(!ch1 && !ch2) { return p->pixels[row] & 0x3;
ch1 = (p->bg[row] >> (7 - x % 8)) & 1;
ch2 = (p->bg[row + 8] >> (7 - x % 8)) & 1;
}
return ch1 + (ch2 << 1);
} }
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)
{ {
Uint16 row = ppu_row(p, x, y), col = 7 - (x % 8); int row = (x + y * p->width) / 0x2;
if(x >= p->width || y >= p->height) int original = p->pixels[row];
return; Uint8 next = 0x0;
if(color == 0 || color == 2) if(x % 2) {
layer[row] &= ~(1UL << col); next |= original & 0xf0;
else next |= color << (layer * 2);
layer[row] |= 1UL << col; } else {
if(color == 0 || color == 1) next |= original & 0x0f;
layer[row + 8] &= ~(1UL << col); next |= color << (4 + (layer * 2));
else }
layer[row + 8] |= 1UL << col; p->pixels[row] = next;
} }
void void
ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
{ {
Uint16 v, h; Uint16 v, h;
for(v = 0; v < 8; v++) for(v = 0; v < 8; v++)
@ -83,7 +84,7 @@ ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U
} }
void void
ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
{ {
Uint16 v, h; Uint16 v, h;
for(v = 0; v < 8; v++) for(v = 0; v < 8; v++)
@ -108,9 +109,9 @@ ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
ppu_clear(p); ppu_clear(p);
p->width = width; p->width = width;
p->height = height; p->height = height;
p->pixels = realloc(p->bg, p->width / 4 * p->height * sizeof(Uint8) * 2); p->pixels = realloc(p->bg, p->width * p->height * sizeof(Uint8) * 2);
p->bg = p->pixels; p->bg = p->pixels;
p->fg = p->pixels + (p->width / 4 * p->height * sizeof(Uint8)); p->fg = p->pixels + (p->width * p->height * sizeof(Uint8));
ppu_clear(p); ppu_clear(p);
return p->bg && p->fg; return p->bg && p->fg;
} }

View File

@ -27,9 +27,9 @@ typedef struct Ppu {
} Ppu; } Ppu;
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); int ppu_set_size(Ppu *p, Uint16 width, Uint16 height);

View File

@ -195,25 +195,25 @@ draw_inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
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 */
ppu_1bpp(p, ppu.fg, x, y, font[(b >> 4) & 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, ppu.fg, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0); ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
y = 0x28 + (i / 8 + 1) * 8; y = 0x28 + (i / 8 + 1) * 8;
b = memory[i]; b = memory[i];
/* return stack */ /* return stack */
ppu_1bpp(p, ppu.fg, x, y, font[(b >> 4) & 0xf], 3, 0, 0); ppu_1bpp(p, 1, 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 + 8, y, font[b & 0xf], 3, 0, 0);
} }
/* return pointer */ /* return pointer */
ppu_1bpp(p, ppu.fg, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0); ppu_1bpp(p, 1, 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, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
/* guides */ /* guides */
for(x = 0; x < 0x10; ++x) { for(x = 0; x < 0x10; ++x) {
ppu_write(p, ppu.fg, x, p->height / 2, 2); ppu_write(p, 1, x, p->height / 2, 2);
ppu_write(p, ppu.fg, p->width - x, p->height / 2, 2); ppu_write(p, 1, p->width - x, p->height / 2, 2);
ppu_write(p, ppu.fg, p->width / 2, p->height - x, 2); ppu_write(p, 1, p->width / 2, p->height - x, 2);
ppu_write(p, ppu.fg, p->width / 2, x, 2); ppu_write(p, 1, p->width / 2, x, 2);
ppu_write(p, ppu.fg, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2); ppu_write(p, 1, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
ppu_write(p, ppu.fg, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2); ppu_write(p, 1, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
} }
} }
@ -391,7 +391,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 ? ppu.fg : ppu.bg, 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 +402,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 ? ppu.fg : ppu.bg, 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 ? ppu.fg : ppu.bg, 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 */