Browse code

Switched to faster unsigned ints for PPU memory

Andrew Alderwick authored on 20/09/2021 21:32:42
Showing 3 changed files
... ...
@@ -23,22 +23,22 @@ static void
23 23
 ppu_clear(Ppu *p)
24 24
 {
25 25
 	int i;
26
-	for(i = 0; i < p->width / 2 * p->height; ++i)
26
+	for(i = 0; i < p->stride * p->height; ++i)
27 27
 		p->dat[i] = 0;
28 28
 }
29 29
 
30
-int
30
+unsigned int
31 31
 ppu_pixel(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color)
32 32
 {
33
-	unsigned int i = (x + y * p->width) / 2, shift = (x % 2) * 4;
34
-	int ret = p->dat[i];
33
+	unsigned int i = x / PPW + y * p->stride, shift = x % PPW * 4;
34
+	unsigned int ret = p->dat[i];
35 35
 	if(fg) shift += 2;
36 36
 	p->dat[i] &= ~(3 << shift);
37 37
 	p->dat[i] |= color << shift;
38 38
 	return ret ^ p->dat[i];
39 39
 }
40 40
 
41
-int
41
+unsigned int
42 42
 ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
43 43
 {
44 44
 	Uint16 v, h;
... ...
@@ -56,7 +56,7 @@ ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 f
56 56
 	return ret;
57 57
 }
58 58
 
59
-int
59
+unsigned int
60 60
 ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
61 61
 {
62 62
 	Uint16 v, h;
... ...
@@ -81,11 +81,10 @@ ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 f
81 81
 int
82 82
 ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
83 83
 {
84
-	/* round width up to nearest multiple of 2 */
85
-	width += width % 2;
86 84
 	p->width = width;
85
+	p->stride = (width + PPW - 1) / PPW;
87 86
 	p->height = height;
88
-	p->dat = realloc(p->dat, p->width / 2 * p->height);
87
+	p->dat = realloc(p->dat, p->stride * p->height * sizeof(unsigned int));
89 88
 	if(p->dat == NULL) return 0;
90 89
 	ppu_clear(p);
91 90
 	return 1;
... ...
@@ -13,16 +13,20 @@ 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
+
16 20
 typedef unsigned char Uint8;
17 21
 typedef unsigned short Uint16;
18 22
 typedef unsigned int Uint32;
19 23
 
20 24
 typedef struct Ppu {
21 25
 	Uint16 width, height;
22
-	Uint8 *dat;
26
+	unsigned int *dat, stride;
23 27
 } Ppu;
24 28
 
25 29
 int ppu_set_size(Ppu *p, Uint16 width, Uint16 height);
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);
30
+unsigned int ppu_pixel(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color);
31
+unsigned int ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
32
+unsigned int ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
... ...
@@ -38,7 +38,8 @@ static Apu apu[POLYPHONY];
38 38
 static Device *devsystem, *devscreen, *devmouse, *devctrl, *devaudio0, *devconsole;
39 39
 static Uint32 *ppu_screen, stdin_event, audio0_event, palette[4];
40 40
 
41
-static Uint8 zoom = 1, reqdraw = 0;
41
+static Uint8 zoom = 1;
42
+static unsigned int reqdraw = 0;
42 43
 
43 44
 static Uint8 font[][8] = {
44 45
 	{0x00, 0x7c, 0x82, 0x82, 0x82, 0x82, 0x82, 0x7c},
... ...
@@ -114,7 +115,7 @@ inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
114 115
 static Uint8
115 116
 get_pixel(int x, int y)
116 117
 {
117
-	unsigned int i = (x + y * ppu.width) / 2, shift = (x % 2) * 4;
118
+	unsigned int i = x / PPW + y * ppu.stride, shift = x % PPW * 4;
118 119
 	return (ppu.dat[i] >> shift) & 0xf;
119 120
 }
120 121