Browse code

(screen.c) Always cache value before PEEK/POKE

neauoire authored on 30/08/2023 19:06:04
Showing 1 changed files
... ...
@@ -180,19 +180,25 @@ screen_dei(Uxn *u, Uint8 addr)
180 180
 void
181 181
 screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
182 182
 {
183
+	Uint8 *port_width, *port_height, *port_x, *port_y, *port_addr;
184
+	Uint16 x, y, dx, dy, dxy, dyx, addr, addr_incr;
183 185
 	switch(port) {
184 186
 	case 0x3:
185
-		screen_resize(PEEK2(d + 2), uxn_screen.height);
187
+		port_width = d + 0x2;
188
+		screen_resize(PEEK2(port_width), uxn_screen.height);
186 189
 		break;
187 190
 	case 0x5:
188
-		screen_resize(uxn_screen.width, PEEK2(d + 4));
191
+		port_height = d + 0x4;
192
+		screen_resize(uxn_screen.width, PEEK2(port_height));
189 193
 		break;
190 194
 	case 0xe: {
191 195
 		Uint8 ctrl = d[0xe];
192 196
 		Uint8 color = ctrl & 0x3;
193
-		Uint16 x = PEEK2(d + 0x8);
194
-		Uint16 y = PEEK2(d + 0xa);
195 197
 		Uint8 *layer = (ctrl & 0x40) ? uxn_screen.fg : uxn_screen.bg;
198
+		port_x = d + 0x8;
199
+		port_y = d + 0xa;
200
+		x = PEEK2(port_x);
201
+		y = PEEK2(port_y);
196 202
 		/* fill mode */
197 203
 		if(ctrl & 0x80) {
198 204
 			Uint16 x2 = uxn_screen.width;
... ...
@@ -208,9 +214,11 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
208 214
 			Uint16 height = uxn_screen.height;
209 215
 			if(x < width && y < height)
210 216
 				layer[x + y * width] = color;
211
-			screen_change(x, y, x + 1, y + 1);
212
-			if(d[0x6] & 0x1) POKE2(d + 0x8, x + 1); /* auto x+1 */
213
-			if(d[0x6] & 0x2) POKE2(d + 0xa, y + 1); /* auto y+1 */
217
+			x += 1;
218
+			y += 1;
219
+			screen_change(x, y, x, y);
220
+			if(d[0x6] & 0x1) POKE2(port_x, x);
221
+			if(d[0x6] & 0x2) POKE2(port_y, y);
214 222
 		}
215 223
 		break;
216 224
 	}
... ...
@@ -222,20 +230,28 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
222 230
 		Uint8 twobpp = !!(ctrl & 0x80);
223 231
 		Uint8 *layer = (ctrl & 0x40) ? uxn_screen.fg : uxn_screen.bg;
224 232
 		Uint8 color = ctrl & 0xf;
225
-		Uint16 x = PEEK2(d + 0x8), dx = (move & 0x1) << 3;
226
-		Uint16 y = PEEK2(d + 0xa), dy = (move & 0x2) << 2;
227
-		Uint16 addr = PEEK2(d + 0xc), addr_incr = (move & 0x4) << (1 + twobpp);
228 233
 		int flipx = (ctrl & 0x10), fx = flipx ? -1 : 1;
229 234
 		int flipy = (ctrl & 0x20), fy = flipy ? -1 : 1;
230
-		Uint16 dyx = dy * fx, dxy = dx * fy;
235
+		port_x = d + 0x8;
236
+		port_y = d + 0xa;
237
+		port_addr = d + 0xc;
238
+		x = PEEK2(port_x), dx = (move & 0x1) << 3, dxy = dx * fy;
239
+		y = PEEK2(port_y), dy = (move & 0x2) << 2, dyx = dy * fx;
240
+		addr = PEEK2(port_addr), addr_incr = (move & 0x4) << (1 + twobpp);
231 241
 		for(i = 0; i <= length; i++) {
232 242
 			screen_blit(layer, ram, addr, x + dyx * i, y + dxy * i, color, flipx, flipy, twobpp);
233 243
 			addr += addr_incr;
234 244
 		}
235 245
 		screen_change(x, y, x + dyx * length + 8, y + dxy * length + 8);
236
-		if(move & 0x1) POKE2(d + 0x8, x + dx * fx); /* auto x+8 */
237
-		if(move & 0x2) POKE2(d + 0xa, y + dy * fy); /* auto y+8 */
238
-		if(move & 0x4) POKE2(d + 0xc, addr);        /* auto addr+length */
246
+		if(move & 0x1) {
247
+			x = x + dx * fx;
248
+			POKE2(port_x, x);
249
+		}
250
+		if(move & 0x2) {
251
+			y = y + dy * fy;
252
+			POKE2(port_y, y);
253
+		}
254
+		if(move & 0x4) POKE2(port_addr, addr); /* auto addr+length */
239 255
 		break;
240 256
 	}
241 257
 	}