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.
*/
/*
fgbg fgbg
byte [0000 0000]
*/
static Uint8 blending[5][16] = {
{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},
@ -31,8 +36,8 @@ ppu_clear(Ppu *p)
int x, y;
for(y = 0; y < p->height; ++y) {
for(x = 0; x < p->width; ++x) {
ppu_write(p, p->bg, x, y, 0);
ppu_write(p, p->fg, x, y, 0);
ppu_write(p, 0, x, y, 0);
ppu_write(p, 1, x, y, 0);
}
}
}
@ -40,34 +45,30 @@ ppu_clear(Ppu *p)
Uint8
ppu_read(Ppu *p, Uint16 x, Uint16 y)
{
Uint16 ch1, ch2, row = ppu_row(p, x, y);
ch1 = (p->fg[row] >> (7 - x % 8)) & 1;
ch2 = (p->fg[row + 8] >> (7 - x % 8)) & 1;
if(!ch1 && !ch2) {
ch1 = (p->bg[row] >> (7 - x % 8)) & 1;
ch2 = (p->bg[row + 8] >> (7 - x % 8)) & 1;
}
return ch1 + (ch2 << 1);
int row = (x + y * p->width) / 0x2;
int shift = (1 - (x & 0x1)) * 0x4;
return p->pixels[row] & 0x3;
}
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);
if(x >= p->width || y >= p->height)
return;
if(color == 0 || color == 2)
layer[row] &= ~(1UL << col);
else
layer[row] |= 1UL << col;
if(color == 0 || color == 1)
layer[row + 8] &= ~(1UL << col);
else
layer[row + 8] |= 1UL << col;
int row = (x + y * p->width) / 0x2;
int original = p->pixels[row];
Uint8 next = 0x0;
if(x % 2) {
next |= original & 0xf0;
next |= color << (layer * 2);
} else {
next |= original & 0x0f;
next |= color << (4 + (layer * 2));
}
p->pixels[row] = next;
}
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;
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
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;
for(v = 0; v < 8; v++)
@ -108,9 +109,9 @@ ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
ppu_clear(p);
p->width = width;
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->fg = p->pixels + (p->width / 4 * p->height * sizeof(Uint8));
p->fg = p->pixels + (p->width * p->height * sizeof(Uint8));
ppu_clear(p);
return p->bg && p->fg;
}

View File

@ -27,9 +27,9 @@ typedef struct Ppu {
} Ppu;
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_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_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);
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) {
x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
/* working stack */
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);
y = 0x28 + (i / 8 + 1) * 8;
b = memory[i];
/* return stack */
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);
}
/* 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);
/* guides */
for(x = 0; x < 0x10; ++x) {
ppu_write(p, ppu.fg, x, p->height / 2, 2);
ppu_write(p, ppu.fg, p->width - x, p->height / 2, 2);
ppu_write(p, ppu.fg, p->width / 2, p->height - x, 2);
ppu_write(p, ppu.fg, p->width / 2, x, 2);
ppu_write(p, ppu.fg, 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, x, p->height / 2, 2);
ppu_write(p, 1, p->width - x, p->height / 2, 2);
ppu_write(p, 1, p->width / 2, p->height - x, 2);
ppu_write(p, 1, p->width / 2, x, 2);
ppu_write(p, 1, p->width / 2 - 0x10 / 2 + x, p->height / 2, 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 y = peek16(d->dat, 0xa);
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] & 0x02) poke16(d->dat, 0xa, y + 1); /* auto y+1 */
break;
@ -402,10 +402,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) {
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 */
} 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] & 0x01) poke16(d->dat, 0x8, x + 8); /* auto x+8 */