Browse code

Switched to linear PPU memory

Andrew Alderwick authored on 19/09/2021 23:04:40
Showing 3 changed files
... ...
@@ -22,38 +22,24 @@ static Uint8 blending[5][16] = {
22 22
 static void
23 23
 ppu_clear(Ppu *p)
24 24
 {
25
-	int x, y;
26
-	for(y = 0; y < p->height; ++y) {
27
-		for(x = 0; x < p->width; ++x) {
28
-			ppu_pixel(p, p->bg, x, y, 0);
29
-			ppu_pixel(p, p->fg, x, y, 0);
30
-		}
31
-	}
25
+	int i;
26
+	for(i = 0; i < p->width / 2 * p->height; ++i)
27
+		p->dat[i] = 0;
32 28
 }
33 29
 
34 30
 int
35
-ppu_pixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color)
31
+ppu_pixel(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color)
36 32
 {
37
-	int row = (y % 8) + ((x / 8 + y / 8 * p->width / 8) * 16), col = x % 8, ret;
38
-	Uint8 w;
39
-	if(x >= p->width || y >= p->height)
40
-		return 0;
41
-	w = layer[row];
42
-	if(color == 0 || color == 2)
43
-		layer[row] &= ~(1UL << (7 - col));
44
-	else
45
-		layer[row] |= 1UL << (7 - col);
46
-	ret = w ^ layer[row];
47
-	w = layer[row + 8];
48
-	if(color == 0 || color == 1)
49
-		layer[row + 8] &= ~(1UL << (7 - col));
50
-	else
51
-		layer[row + 8] |= 1UL << (7 - col);
52
-	return ret | (w ^ layer[row + 8]);
33
+	unsigned int i = (x + y * p->width) / 2, shift = (x % 2) * 4;
34
+	int ret = p->dat[i];
35
+	if(fg) shift += 2;
36
+	p->dat[i] &= ~(3 << shift);
37
+	p->dat[i] |= color << shift;
38
+	return ret ^ p->dat[i];
53 39
 }
54 40
 
55 41
 int
56
-ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
42
+ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
57 43
 {
58 44
 	Uint16 v, h;
59 45
 	int ret = 0;
... ...
@@ -62,7 +48,7 @@ ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U
62 48
 			Uint8 ch1 = (sprite[v] >> (7 - h)) & 0x1;
63 49
 			if(ch1 || blending[4][color])
64 50
 				ret |= ppu_pixel(p,
65
-					layer,
51
+					fg,
66 52
 					x + (flipx ? 7 - h : h),
67 53
 					y + (flipy ? 7 - v : v),
68 54
 					blending[ch1][color]);
... ...
@@ -71,7 +57,7 @@ ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U
71 57
 }
72 58
 
73 59
 int
74
-ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
60
+ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
75 61
 {
76 62
 	Uint16 v, h;
77 63
 	int ret = 0;
... ...
@@ -82,7 +68,7 @@ ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U
82 68
 			Uint8 ch = ch1 + ch2 * 2;
83 69
 			if(ch || blending[4][color])
84 70
 				ret |= ppu_pixel(p,
85
-					layer,
71
+					fg,
86 72
 					x + (flipx ? 7 - h : h),
87 73
 					y + (flipy ? 7 - v : v),
88 74
 					blending[ch][color]);
... ...
@@ -95,13 +81,12 @@ ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U
95 81
 int
96 82
 ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
97 83
 {
98
-	/* round width and height up to nearest multiple of 8 */
99
-	width = ((width - 1) | 0x7) + 1;
100
-	height = ((height - 1) | 0x7) + 1;
84
+	/* round width up to nearest multiple of 2 */
85
+	width += width % 2;
101 86
 	p->width = width;
102 87
 	p->height = height;
103
-	p->bg = realloc(p->bg, p->width / 4 * p->height * sizeof(Uint8));
104
-	p->fg = realloc(p->fg, p->width / 4 * p->height * sizeof(Uint8));
88
+	p->dat = realloc(p->dat, p->width / 2 * p->height);
89
+	if(p->dat == NULL) return 0;
105 90
 	ppu_clear(p);
106
-	return p->bg && p->fg;
91
+	return 1;
107 92
 }
108 93
\ No newline at end of file
... ...
@@ -19,10 +19,10 @@ typedef unsigned int Uint32;
19 19
 
20 20
 typedef struct Ppu {
21 21
 	Uint16 width, height;
22
-	Uint8 *bg, *fg;
22
+	Uint8 *dat;
23 23
 } Ppu;
24 24
 
25 25
 int ppu_set_size(Ppu *p, Uint16 width, Uint16 height);
26
-int ppu_pixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color);
27
-int ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
28
-int ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
26
+int ppu_pixel(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color);
27
+int ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
28
+int ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
... ...
@@ -90,38 +90,32 @@ inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
90 90
 	Uint8 i, x, y, b;
91 91
 	for(i = 0; i < 0x20; ++i) { /* stack */
92 92
 		x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
93
-		ppu_1bpp(p, ppu.fg, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
94
-		ppu_1bpp(p, ppu.fg, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
93
+		ppu_1bpp(p, 1, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
94
+		ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
95 95
 	}
96 96
 	/* return pointer */
97
-	ppu_1bpp(p, ppu.fg, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
98
-	ppu_1bpp(p, ppu.fg, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
97
+	ppu_1bpp(p, 1, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
98
+	ppu_1bpp(p, 1, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
99 99
 	for(i = 0; i < 0x20; ++i) { /* memory */
100 100
 		x = ((i % 8) * 3 + 1) * 8, y = 0x38 + (i / 8 + 1) * 8, b = memory[i];
101
-		ppu_1bpp(p, ppu.fg, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
102
-		ppu_1bpp(p, ppu.fg, x + 8, y, font[b & 0xf], 3, 0, 0);
101
+		ppu_1bpp(p, 1, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
102
+		ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 3, 0, 0);
103 103
 	}
104 104
 	for(x = 0; x < 0x10; ++x) { /* guides */
105
-		ppu_pixel(p, ppu.fg, x, p->height / 2, 2);
106
-		ppu_pixel(p, ppu.fg, p->width - x, p->height / 2, 2);
107
-		ppu_pixel(p, ppu.fg, p->width / 2, p->height - x, 2);
108
-		ppu_pixel(p, ppu.fg, p->width / 2, x, 2);
109
-		ppu_pixel(p, ppu.fg, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
110
-		ppu_pixel(p, ppu.fg, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
105
+		ppu_pixel(p, 1, x, p->height / 2, 2);
106
+		ppu_pixel(p, 1, p->width - x, p->height / 2, 2);
107
+		ppu_pixel(p, 1, p->width / 2, p->height - x, 2);
108
+		ppu_pixel(p, 1, p->width / 2, x, 2);
109
+		ppu_pixel(p, 1, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
110
+		ppu_pixel(p, 1, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
111 111
 	}
112 112
 }
113 113
 
114 114
 static Uint8
115 115
 get_pixel(int x, int y)
116 116
 {
117
-	int ch1, ch2, r = (y % 8) + ((x / 8 + y / 8 * ppu.width / 8) * 16);
118
-	ch1 = (ppu.fg[r] >> (7 - x % 8)) & 1;
119
-	ch2 = (ppu.fg[r + 8] >> (7 - x % 8)) & 1;
120
-	if(!ch1 && !ch2) {
121
-		ch1 = (ppu.bg[r] >> (7 - x % 8)) & 1;
122
-		ch2 = (ppu.bg[r + 8] >> (7 - x % 8)) & 1;
123
-	}
124
-	return ch1 + (ch2 << 1);
117
+	unsigned int i = (x + y * ppu.width) / 2, shift = (x % 2) * 4;
118
+	return (ppu.dat[i] >> shift) & 0xf;
125 119
 }
126 120
 
127 121
 static void
... ...
@@ -356,7 +350,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
356 350
 			Uint16 x = peek16(d->dat, 0x8);
357 351
 			Uint16 y = peek16(d->dat, 0xa);
358 352
 			Uint8 layer = d->dat[0xe] & 0x40;
359
-			reqdraw |= ppu_pixel(&ppu, layer ? ppu.fg : ppu.bg, x, y, d->dat[0xe] & 0x3);
353
+			reqdraw |= ppu_pixel(&ppu, layer, x, y, d->dat[0xe] & 0x3);
360 354
 			if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 1); /* auto x+1 */
361 355
 			if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 1); /* auto y+1 */
362 356
 			break;
... ...
@@ -367,10 +361,10 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
367 361
 			Uint8 layer = d->dat[0xf] & 0x40;
368 362
 			Uint8 *addr = &d->mem[peek16(d->dat, 0xc)];
369 363
 			if(d->dat[0xf] & 0x80) {
370
-				reqdraw |= ppu_2bpp(&ppu, layer ? ppu.fg : ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
364
+				reqdraw |= ppu_2bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
371 365
 				if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 16); /* auto addr+16 */
372 366
 			} else {
373
-				reqdraw |= ppu_1bpp(&ppu, layer ? ppu.fg : ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
367
+				reqdraw |= ppu_1bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
374 368
 				if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 8); /* auto addr+8 */
375 369
 			}
376 370
 			if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 8); /* auto x+8 */