commit 2dff217

Devine Lu Linvega  ·  2024-01-21 00:32:44 +0000 UTC
parent 707d7d6
Optimized changed region code
4 files changed,  +76, -38
+2, -2
 1@@ -17,9 +17,9 @@
 2 
 3 @on-mouse ( -> )
 4 	( | clear background )
 5-	#0000 DUP2 .Screen/x DEO2
 6+	( #0000 DUP2 .Screen/x DEO2
 7 	.Screen/y DEO2
 8-	#80 .Screen/pixel DEO
 9+	#80 .Screen/pixel DEO )
10 	<draw-guide>
11 	( | cursor )
12 	#41 ;cursor-icn <update-cursor>
+69, -32
  1@@ -25,35 +25,15 @@ static Uint8 blending[4][16] = {
  2 	{1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1},
  3 	{2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2}};
  4 
  5-void
  6-screen_change(Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2)
  7+static int
  8+twos(Uint16 value)
  9 {
 10-	if(x1 > uxn_screen.width && x2 > x1) return;
 11-	if(y1 > uxn_screen.height && y2 > y1) return;
 12-	if(x1 > x2) x1 = 0;
 13-	if(y1 > y2) y1 = 0;
 14-	if(x1 < uxn_screen.x1) uxn_screen.x1 = x1;
 15-	if(y1 < uxn_screen.y1) uxn_screen.y1 = y1;
 16-	if(x2 > uxn_screen.x2) uxn_screen.x2 = x2;
 17-	if(y2 > uxn_screen.y2) uxn_screen.y2 = y2;
 18-}
 19-
 20-void
 21-screen_fill(Uint8 *layer, int color)
 22-{
 23-	int i, length = uxn_screen.width * uxn_screen.height;
 24-	for(i = 0; i < length; i++)
 25-		layer[i] = color;
 26+	if(value & (1 << (sizeof(Uint16) * 8 - 1)))
 27+		return (int)value - (1 << sizeof(Uint16) * 8);
 28+	else
 29+		return (int)value;
 30 }
 31 
 32-void
 33-screen_rect(Uint8 *layer, Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2, int color)
 34-{
 35-	int row, x, y, w = uxn_screen.width, h = uxn_screen.height;
 36-	for(y = y1; y < y2 && y < h; y++)
 37-		for(x = x1, row = y * w; x < x2 && x < w; x++)
 38-			layer[x + row] = color;
 39-}
 40 
 41 static void
 42 screen_2bpp(Uint8 *layer, Uint8 *addr, Uint16 x1, Uint16 y1, Uint16 color, int fx, int fy)
 43@@ -89,6 +69,54 @@ screen_1bpp(Uint8 *layer, Uint8 *addr, Uint16 x1, Uint16 y1, Uint16 color, int f
 44 	}
 45 }
 46 
 47+int
 48+screen_changed(void)
 49+{
 50+	if(uxn_screen.x1 < 0)
 51+		uxn_screen.x1 = 0;
 52+	else if(uxn_screen.x1 >= uxn_screen.width)
 53+		uxn_screen.x1 = uxn_screen.width;
 54+	if(uxn_screen.y1 < 0)
 55+		uxn_screen.y1 = 0;
 56+	else if(uxn_screen.y1 >= uxn_screen.height)
 57+		uxn_screen.y1 = uxn_screen.height;
 58+	if(uxn_screen.x2 < 0)
 59+		uxn_screen.x2 = 0;
 60+	else if(uxn_screen.x2 >= uxn_screen.width)
 61+		uxn_screen.x2 = uxn_screen.width;
 62+	if(uxn_screen.y2 < 0)
 63+		uxn_screen.y2 = 0;
 64+	else if(uxn_screen.y2 >= uxn_screen.height)
 65+		uxn_screen.y2 = uxn_screen.height;
 66+	return uxn_screen.x2 > uxn_screen.x1 || uxn_screen.y2 > uxn_screen.y1;
 67+}
 68+
 69+void
 70+screen_change(int x1, int y1, int x2, int y2)
 71+{
 72+	if(x1 < uxn_screen.x1) uxn_screen.x1 = x1;
 73+	if(y1 < uxn_screen.y1) uxn_screen.y1 = y1;
 74+	if(x2 > uxn_screen.x2) uxn_screen.x2 = x2;
 75+	if(y2 > uxn_screen.y2) uxn_screen.y2 = y2;
 76+}
 77+
 78+void
 79+screen_fill(Uint8 *layer, int color)
 80+{
 81+	int i, length = uxn_screen.width * uxn_screen.height;
 82+	for(i = 0; i < length; i++)
 83+		layer[i] = color;
 84+}
 85+
 86+void
 87+screen_rect(Uint8 *layer, Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2, int color)
 88+{
 89+	int row, x, y, w = uxn_screen.width, h = uxn_screen.height;
 90+	for(y = y1; y < y2 && y < h; y++)
 91+		for(x = x1, row = y * w; x < x2 && x < w; x++)
 92+			layer[x + row] = color;
 93+}
 94+
 95 /* clang-format off */
 96 
 97 static Uint8 icons[] = {
 98@@ -197,7 +225,7 @@ screen_redraw(Uxn *u)
 99 	Uint16 x1 = uxn_screen.x1, y1 = uxn_screen.y1;
100 	Uint16 x2 = uxn_screen.x2 > w ? w : uxn_screen.x2, y2 = uxn_screen.y2 > h ? h : uxn_screen.y2;
101 	Uint32 palette[16], *pixels = uxn_screen.pixels;
102-	uxn_screen.x1 = uxn_screen.y1 = 0xffff;
103+	uxn_screen.x1 = uxn_screen.y1 = 9000;
104 	uxn_screen.x2 = uxn_screen.y2 = 0;
105 	if(u->dev[0x0e])
106 		screen_debugger(u);
107@@ -219,7 +247,7 @@ screen_redraw(Uxn *u)
108 
109 /* screen registers */
110 
111-static Uint16 rX, rY, rA, rMX, rMY, rMA, rML, rDX, rDY;
112+static int rX, rY, rA, rMX, rMY, rMA, rML, rDX, rDY;
113 
114 Uint8
115 screen_dei(Uxn *u, Uint8 addr)
116@@ -247,9 +275,9 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
117 	case 0x5: screen_resize(uxn_screen.width, PEEK2(d + 4), uxn_screen.scale); return;
118 	case 0x6: rMX = d[0x6] & 0x1, rMY = d[0x6] & 0x2, rMA = d[0x6] & 0x4, rML = d[0x6] >> 4, rDX = rMX << 3, rDY = rMY << 2; return;
119 	case 0x8:
120-	case 0x9: rX = (d[0x8] << 8) | d[0x9]; return;
121+	case 0x9: rX = twos((d[0x8] << 8) | d[0x9]); return;
122 	case 0xa:
123-	case 0xb: rY = (d[0xa] << 8) | d[0xb]; return;
124+	case 0xb: rY = twos((d[0xa] << 8) | d[0xb]); return;
125 	case 0xc:
126 	case 0xd: rA = (d[0xc] << 8) | d[0xd]; return;
127 	case 0xe: {
128@@ -289,14 +317,23 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
129 		Uint8 *layer = ctrl & 0x40 ? uxn_screen.fg : uxn_screen.bg;
130 		int fx = ctrl & 0x10 ? -1 : 1;
131 		int fy = ctrl & 0x20 ? -1 : 1;
132-		Uint16 dxy = rDX * fy, dyx = rDY * fx, addr_incr = rMA << (1 + twobpp);
133+		int x1, x2, y1, y2;
134+		int dxy = rDX * fy, dyx = rDY * fx, addr_incr = rMA << (1 + twobpp);
135 		if(twobpp)
136 			for(i = 0; i <= rML; i++, rA += addr_incr)
137 				screen_2bpp(layer, &ram[rA], rX + dyx * i, rY + dxy * i, color, fx, fy);
138 		else
139 			for(i = 0; i <= rML; i++, rA += addr_incr)
140 				screen_1bpp(layer, &ram[rA], rX + dyx * i, rY + dxy * i, color, fx, fy);
141-		screen_change(rX, rY, rX + dyx * rML + 8, rY + dxy * rML + 8);
142+		if(fx == -1)
143+			x1 = rX + dyx * rML, x2 = rX + 8;
144+		else
145+			x1 = rX, x2 = rX + dyx * rML + 8;
146+		if(fy == -1)
147+			y1 = rY + dxy * rML, y2 = rY + 8;
148+		else
149+			y1 = rY, y2 = rY + dxy * rML + 8;
150+		screen_change(x1, y1, x2, y2);
151 		if(rMX) rX += rDX * fx;
152 		if(rMY) rY += rDY * fy;
153 		return;
+3, -2
 1@@ -21,12 +21,13 @@ typedef struct UxnScreen {
 2 
 3 extern UxnScreen uxn_screen;
 4 extern int emu_resize(int width, int height);
 5-
 6+int screen_changed(void);
 7+void normalize_rect(void);
 8 void screen_fill(Uint8 *layer, int color);
 9 void screen_rect(Uint8 *layer, Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2, int color);
10 void screen_palette(Uint8 *addr);
11 void screen_resize(Uint16 width, Uint16 height, int scale);
12-void screen_change(Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2);
13+void screen_change(int x1, int y1, int x2, int y2);
14 void screen_redraw(Uxn *u);
15 
16 Uint8 screen_dei(Uxn *u, Uint8 addr);
+2, -2
 1@@ -243,7 +243,7 @@ emu_run(Uxn *u, char *rom)
 2 		if(poll(&fds[1], 1, 0)) {
 3 			read(fds[1].fd, expirations, 8);
 4 			uxn_eval(u, u->dev[0x20] << 8 | u->dev[0x21]);
 5-			if(uxn_screen.x2) {
 6+			if(screen_changed()) {
 7 				int x = uxn_screen.x1 * uxn_screen.scale, y = uxn_screen.y1 * uxn_screen.scale;
 8 				int w = uxn_screen.x2 * uxn_screen.scale - x, h = uxn_screen.y2 * uxn_screen.scale - y;
 9 				screen_redraw(u);
10@@ -270,7 +270,7 @@ main(int argc, char **argv)
11 		return 0;
12 	}
13 	if(argv[i][0] == '-' && argv[i][1] == 'v') {
14-		fprintf(stdout, "Uxn11 - Varvara Emulator, 16 Jan 2023.\n");
15+		fprintf(stdout, "Uxn11 - Varvara Emulator, 20 Jan 2023.\n");
16 		i++;
17 	}
18 	if(!system_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++])) {