Browse code

Cleaned up PPU

neauoire authored on 30/09/2021 17:44:40
Showing 3 changed files
... ...
@@ -12,12 +12,6 @@ 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
-pixel 0001 0002
17
-layer fgbg fgbg
18
-byte  1010 1010
19
-*/
20
-
21 15
 static Uint8 blending[5][16] = {
22 16
 	{0, 0, 0, 0, 1, 0, 1, 1, 2, 2, 0, 2, 3, 3, 3, 0},
23 17
 	{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] = {
28 22
 static void
29 23
 ppu_clear(Ppu *p)
30 24
 {
31
-	int row;
32
-	for(row = 0; row < p->height * p->width / 2; ++row)
25
+	Uint32 row, bound = p->height * p->width / 2;
26
+	for(row = 0; row < bound; ++row)
33 27
 		p->pixels[row] = 0;
34 28
 }
35 29
 
30
+Uint8
31
+ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
32
+{
33
+	ppu_clear(p);
34
+	p->width = width;
35
+	p->height = height;
36
+	p->pixels = realloc(p->pixels, p->width * p->height * sizeof(Uint8) / 2);
37
+	ppu_clear(p);
38
+	return !!p->pixels;
39
+}
40
+
36 41
 Uint8
37 42
 ppu_read(Ppu *p, Uint16 x, Uint16 y)
38 43
 {
39
-	int row = (x + y * p->width) / 0x2;
40
-	Uint8 seg = !(x & 0x1) << 2;
41
-	Uint8 byte = p->pixels[row] >> seg;
42
-	return (byte & 0x0c ? (byte >> 2) : byte) & 0x3;
44
+	Uint32 row = (x + y * p->width) / 0x2;
45
+	Uint8 shift = !(x & 0x1) << 2;
46
+	Uint8 pix = p->pixels[row] >> shift;
47
+	if(pix & 0x0c)
48
+		pix = pix >> 2;
49
+	return pix & 0x3;
43 50
 }
44 51
 
45 52
 void
46 53
 ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
47 54
 {
48
-	int row = (x + y * p->width) / 0x2;
49
-	Uint8 original = p->pixels[row];
50
-	Uint8 next = 0x0;
51
-	if(x % 2) {
52
-		if(layer) {
53
-			next |= original & 0xf3;
54
-			next |= color << 0x02;
55
-		} else {
56
-			next |= original & 0xfc;
57
-			next |= color;
58
-		}
59
-	} else {
60
-		if(layer) {
61
-			next |= original & 0x3f;
62
-			next |= color << 0x06;
63
-		} else {
64
-			next |= original & 0xcf;
65
-			next |= color << 0x04;
66
-		}
67
-	}
68
-	p->pixels[row] = next;
69
-	if(original != next)
55
+	Uint32 row = (x + y * p->width) / 0x2;
56
+	Uint8 shift = (!(x & 0x1) << 2) + (layer << 1);
57
+	Uint8 pix = p->pixels[row];
58
+	Uint8 mask = ~(0x3 << shift);
59
+	Uint8 pixnew = (pix & mask) + (color << shift);
60
+	if(x < p->width && y < p->height)
61
+		p->pixels[row] = pixnew;
62
+	if(pix != pixnew)
70 63
 		p->reqdraw = 1;
71 64
 }
72 65
 
... ...
@@ -103,16 +96,3 @@ ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Ui
103 96
 					blending[ch][color]);
104 97
 		}
105 98
 }
106
-
107
-/* output */
108
-
109
-int
110
-ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
111
-{
112
-	ppu_clear(p);
113
-	p->width = width;
114
-	p->height = height;
115
-	p->pixels = realloc(p->pixels, p->width * p->height * sizeof(Uint8) / 2);
116
-	ppu_clear(p);
117
-	return !!p->pixels;
118
-}
... ...
@@ -13,10 +13,6 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 13
 WITH REGARD TO THIS SOFTWARE.
14 14
 */
15 15
 
16
-/* pixels per word in ppu.dat */
17
-
18
-#define PPW (sizeof(unsigned int) * 2)
19
-
20 16
 typedef unsigned char Uint8;
21 17
 typedef unsigned short Uint16;
22 18
 typedef unsigned int Uint32;
... ...
@@ -26,10 +22,9 @@ typedef struct Ppu {
26 22
 	Uint16 width, height;
27 23
 } Ppu;
28 24
 
25
+Uint8 ppu_set_size(Ppu *p, Uint16 width, Uint16 height);
29 26
 Uint8 ppu_read(Ppu *p, Uint16 x, Uint16 y);
30 27
 void ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color);
31 28
 void ppu_frame(Ppu *p);
32
-
33 29
 void ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
34 30
 void ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
35
-int ppu_set_size(Ppu *p, Uint16 width, Uint16 height);
... ...
@@ -121,13 +121,6 @@ set_palette(Uint8 *addr)
121 121
 	ppu.reqdraw = 1;
122 122
 }
123 123
 
124
-static void
125
-set_inspect(Uint8 flag)
126
-{
127
-	devsystem->dat[0xe] = flag;
128
-	ppu.reqdraw = 1;
129
-}
130
-
131 124
 static void
132 125
 set_window_size(SDL_Window *window, int w, int h)
133 126
 {
... ...
@@ -191,7 +184,6 @@ static void
191 184
 draw_inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
192 185
 {
193 186
 	Uint8 i, x, y, b;
194
-
195 187
 	for(i = 0; i < 0x20; ++i) {
196 188
 		x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
197 189
 		/* working stack */
... ...
@@ -324,7 +316,7 @@ doctrl(SDL_Event *event, int z)
324 316
 	case SDLK_LEFT: flag = 0x40; break;
325 317
 	case SDLK_RIGHT: flag = 0x80; break;
326 318
 	case SDLK_F1: if(z) set_zoom(zoom > 2 ? 1 : zoom + 1); break;
327
-	case SDLK_F2: if(z) set_inspect(!devsystem->dat[0xe]); break;
319
+	case SDLK_F2: if(z) devsystem->dat[0xe] = !devsystem->dat[0xe]; break;
328 320
 	case SDLK_F3: if(z) capture_screen(); break;
329 321
 	}
330 322
 	/* clang-format on */
... ...
@@ -391,7 +383,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
391 383
 			Uint16 x = peek16(d->dat, 0x8);
392 384
 			Uint16 y = peek16(d->dat, 0xa);
393 385
 			Uint8 layer = d->dat[0xe] & 0x40;
394
-			ppu_write(&ppu, layer, x, y, d->dat[0xe] & 0x3);
386
+			ppu_write(&ppu, !!layer, x, y, d->dat[0xe] & 0x3);
395 387
 			if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 1); /* auto x+1 */
396 388
 			if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 1); /* auto y+1 */
397 389
 			break;
... ...
@@ -402,10 +394,10 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
402 394
 			Uint8 layer = d->dat[0xf] & 0x40;
403 395
 			Uint8 *addr = &d->mem[peek16(d->dat, 0xc)];
404 396
 			if(d->dat[0xf] & 0x80) {
405
-				ppu_2bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
397
+				ppu_2bpp(&ppu, !!layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
406 398
 				if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 16); /* auto addr+16 */
407 399
 			} else {
408
-				ppu_1bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
400
+				ppu_1bpp(&ppu, !!layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
409 401
 				if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 8); /* auto addr+8 */
410 402
 			}
411 403
 			if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 8); /* auto x+8 */