Browse code

Progress on new bitpacking

neauoire authored on 30/09/2021 03:44:15
Showing 3 changed files
... ...
@@ -12,6 +12,11 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 12
 WITH REGARD TO THIS SOFTWARE.
13 13
 */
14 14
 
15
+/*
16
+      fgbg fgbg
17
+byte [0000 0000]
18
+*/
19
+
15 20
 static Uint8 blending[5][16] = {
16 21
 	{0, 0, 0, 0, 1, 0, 1, 1, 2, 2, 0, 2, 3, 3, 3, 0},
17 22
 	{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3},
... ...
@@ -31,8 +36,8 @@ ppu_clear(Ppu *p)
31 36
 	int x, y;
32 37
 	for(y = 0; y < p->height; ++y) {
33 38
 		for(x = 0; x < p->width; ++x) {
34
-			ppu_write(p, p->bg, x, y, 0);
35
-			ppu_write(p, p->fg, x, y, 0);
39
+			ppu_write(p, 0, x, y, 0);
40
+			ppu_write(p, 1, x, y, 0);
36 41
 		}
37 42
 	}
38 43
 }
... ...
@@ -40,34 +45,30 @@ ppu_clear(Ppu *p)
40 45
 Uint8
41 46
 ppu_read(Ppu *p, Uint16 x, Uint16 y)
42 47
 {
43
-	Uint16 ch1, ch2, row = ppu_row(p, x, y);
44
-	ch1 = (p->fg[row] >> (7 - x % 8)) & 1;
45
-	ch2 = (p->fg[row + 8] >> (7 - x % 8)) & 1;
46
-	if(!ch1 && !ch2) {
47
-		ch1 = (p->bg[row] >> (7 - x % 8)) & 1;
48
-		ch2 = (p->bg[row + 8] >> (7 - x % 8)) & 1;
49
-	}
50
-	return ch1 + (ch2 << 1);
48
+	int row = (x + y * p->width) / 0x2;
49
+	int shift = (1 - (x & 0x1)) * 0x4;
50
+
51
+	return p->pixels[row] & 0x3;
51 52
 }
52 53
 
53 54
 void
54
-ppu_write(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color)
55
+ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
55 56
 {
56
-	Uint16 row = ppu_row(p, x, y), col = 7 - (x % 8);
57
-	if(x >= p->width || y >= p->height)
58
-		return;
59
-	if(color == 0 || color == 2)
60
-		layer[row] &= ~(1UL << col);
61
-	else
62
-		layer[row] |= 1UL << col;
63
-	if(color == 0 || color == 1)
64
-		layer[row + 8] &= ~(1UL << col);
65
-	else
66
-		layer[row + 8] |= 1UL << col;
57
+	int row = (x + y * p->width) / 0x2;
58
+	int original = p->pixels[row];
59
+	Uint8 next = 0x0;
60
+	if(x % 2) {
61
+		next |= original & 0xf0;
62
+		next |= color << (layer * 2);
63
+	} else {
64
+		next |= original & 0x0f;
65
+		next |= color << (4 + (layer * 2));
66
+	}
67
+	p->pixels[row] = next;
67 68
 }
68 69
 
69 70
 void
70
-ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
71
+ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
71 72
 {
72 73
 	Uint16 v, h;
73 74
 	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
83 84
 }
84 85
 
85 86
 void
86
-ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
87
+ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
87 88
 {
88 89
 	Uint16 v, h;
89 90
 	for(v = 0; v < 8; v++)
... ...
@@ -108,9 +109,9 @@ ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
108 109
 	ppu_clear(p);
109 110
 	p->width = width;
110 111
 	p->height = height;
111
-	p->pixels = realloc(p->bg, p->width / 4 * p->height * sizeof(Uint8) * 2);
112
+	p->pixels = realloc(p->bg, p->width * p->height * sizeof(Uint8) * 2);
112 113
 	p->bg = p->pixels;
113
-	p->fg = p->pixels + (p->width / 4 * p->height * sizeof(Uint8));
114
+	p->fg = p->pixels + (p->width * p->height * sizeof(Uint8));
114 115
 	ppu_clear(p);
115 116
 	return p->bg && p->fg;
116 117
 }
... ...
@@ -27,9 +27,9 @@ typedef struct Ppu {
27 27
 } Ppu;
28 28
 
29 29
 Uint8 ppu_read(Ppu *p, Uint16 x, Uint16 y);
30
-void ppu_write(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color);
30
+void ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color);
31 31
 void ppu_frame(Ppu *p);
32 32
 
33
-void ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
34
-void ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
33
+void ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
34
+void ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
35 35
 int ppu_set_size(Ppu *p, Uint16 width, Uint16 height);
... ...
@@ -195,25 +195,25 @@ draw_inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
195 195
 	for(i = 0; i < 0x20; ++i) {
196 196
 		x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
197 197
 		/* working stack */
198
-		ppu_1bpp(p, ppu.fg, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
199
-		ppu_1bpp(p, ppu.fg, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
198
+		ppu_1bpp(p, 1, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
199
+		ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
200 200
 		y = 0x28 + (i / 8 + 1) * 8;
201 201
 		b = memory[i];
202 202
 		/* return stack */
203
-		ppu_1bpp(p, ppu.fg, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
204
-		ppu_1bpp(p, ppu.fg, x + 8, y, font[b & 0xf], 3, 0, 0);
203
+		ppu_1bpp(p, 1, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
204
+		ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 3, 0, 0);
205 205
 	}
206 206
 	/* return pointer */
207
-	ppu_1bpp(p, ppu.fg, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
208
-	ppu_1bpp(p, ppu.fg, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
207
+	ppu_1bpp(p, 1, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
208
+	ppu_1bpp(p, 1, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
209 209
 	/* guides */
210 210
 	for(x = 0; x < 0x10; ++x) {
211
-		ppu_write(p, ppu.fg, x, p->height / 2, 2);
212
-		ppu_write(p, ppu.fg, p->width - x, p->height / 2, 2);
213
-		ppu_write(p, ppu.fg, p->width / 2, p->height - x, 2);
214
-		ppu_write(p, ppu.fg, p->width / 2, x, 2);
215
-		ppu_write(p, ppu.fg, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
216
-		ppu_write(p, ppu.fg, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
211
+		ppu_write(p, 1, x, p->height / 2, 2);
212
+		ppu_write(p, 1, p->width - x, p->height / 2, 2);
213
+		ppu_write(p, 1, p->width / 2, p->height - x, 2);
214
+		ppu_write(p, 1, p->width / 2, x, 2);
215
+		ppu_write(p, 1, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
216
+		ppu_write(p, 1, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
217 217
 	}
218 218
 }
219 219
 
... ...
@@ -391,7 +391,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
391 391
 			Uint16 x = peek16(d->dat, 0x8);
392 392
 			Uint16 y = peek16(d->dat, 0xa);
393 393
 			Uint8 layer = d->dat[0xe] & 0x40;
394
-			ppu_write(&ppu, layer ? ppu.fg : ppu.bg, x, y, d->dat[0xe] & 0x3);
394
+			ppu_write(&ppu, layer, x, y, d->dat[0xe] & 0x3);
395 395
 			if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 1); /* auto x+1 */
396 396
 			if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 1); /* auto y+1 */
397 397
 			break;
... ...
@@ -402,10 +402,10 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
402 402
 			Uint8 layer = d->dat[0xf] & 0x40;
403 403
 			Uint8 *addr = &d->mem[peek16(d->dat, 0xc)];
404 404
 			if(d->dat[0xf] & 0x80) {
405
-				ppu_2bpp(&ppu, layer ? ppu.fg : ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
405
+				ppu_2bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
406 406
 				if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 16); /* auto addr+16 */
407 407
 			} else {
408
-				ppu_1bpp(&ppu, layer ? ppu.fg : ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
408
+				ppu_1bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
409 409
 				if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 8); /* auto addr+8 */
410 410
 			}
411 411
 			if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 8); /* auto x+8 */