commit 1677fca

Devine Lu Linvega  ·  2025-05-17 23:34:15 +0000 UTC
parent 94b5282
Minor optimizations
2 files changed,  +72, -78
+61, -67
  1@@ -139,7 +139,7 @@ uxn_eval(Uint16 pc)
  2 static char *system_boot_path;
  3 
  4 static void
  5-system_print(char *name, Stack *s)
  6+system_print(const char *name, const Stack *s)
  7 {
  8 	Uint8 i;
  9 	fprintf(stderr, "%s ", name);
 10@@ -149,7 +149,7 @@ system_print(char *name, Stack *s)
 11 }
 12 
 13 static unsigned int
 14-system_load(char *rom_path)
 15+system_load(const char *rom_path)
 16 {
 17 	FILE *f = fopen(rom_path, "rb");
 18 	if(f) {
 19@@ -162,7 +162,7 @@ system_load(char *rom_path)
 20 }
 21 
 22 static unsigned int
 23-system_boot(char *rom_path, unsigned int has_args)
 24+system_boot(char *rom_path, const unsigned int has_args)
 25 {
 26 	ram = (Uint8 *)calloc(BANKS * 0x10000, sizeof(Uint8));
 27 	system_boot_path = rom_path;
 28@@ -171,7 +171,7 @@ system_boot(char *rom_path, unsigned int has_args)
 29 }
 30 
 31 static unsigned int
 32-system_reboot(unsigned int soft)
 33+system_reboot(const unsigned int soft)
 34 {
 35 	unsigned int i;
 36 	for(i = 0x0; i < 0x100; i++) dev[i] = 0;
 37@@ -181,7 +181,7 @@ system_reboot(unsigned int soft)
 38 }
 39 
 40 static void
 41-system_expansion(Uint16 addr)
 42+system_expansion(const Uint16 addr)
 43 {
 44 	Uint8 *aptr = ram + addr;
 45 	Uint16 length = PEEK2(aptr + 1);
 46@@ -414,7 +414,7 @@ typedef struct UxnScreen {
 47 
 48 static unsigned int screen_vector;
 49 static UxnScreen uxn_screen;
 50-static Uint8 blending[4][16] = {
 51+static const Uint8 blending[4][16] = {
 52 	{0, 0, 0, 0, 1, 0, 1, 1, 2, 2, 0, 2, 3, 3, 3, 0},
 53 	{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3},
 54 	{1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1},
 55@@ -434,7 +434,7 @@ screen_changed(void)
 56 }
 57 
 58 static void
 59-screen_change(int x1, int y1, int x2, int y2)
 60+screen_change(const int x1, const int y1, const int x2, const int y2)
 61 {
 62 	if(x1 < uxn_screen.x1) uxn_screen.x1 = x1;
 63 	if(y1 < uxn_screen.y1) uxn_screen.y1 = y1;
 64@@ -481,11 +481,11 @@ screen_redraw(void)
 65 {
 66 	int i, x, y, k, l;
 67 	for(y = uxn_screen.y1; y < uxn_screen.y2; y++) {
 68-		int ys = y * uxn_screen.zoom;
 69+		const int ys = y * uxn_screen.zoom;
 70 		for(x = uxn_screen.x1, i = MAR(x) + MAR(y) * MAR2(uxn_screen.width); x < uxn_screen.x2; x++, i++) {
 71-			int c = uxn_screen.palette[uxn_screen.fg[i] << 2 | uxn_screen.bg[i]];
 72+			const int c = uxn_screen.palette[uxn_screen.fg[i] << 2 | uxn_screen.bg[i]];
 73 			for(k = 0; k < uxn_screen.zoom; k++) {
 74-				int oo = ((ys + k) * uxn_screen.width + x) * uxn_screen.zoom;
 75+				const int oo = ((ys + k) * uxn_screen.width + x) * uxn_screen.zoom;
 76 				for(l = 0; l < uxn_screen.zoom; l++)
 77 					uxn_screen.pixels[oo + l] = c;
 78 			}
 79@@ -502,9 +502,9 @@ static int rX, rY, rA, rMX, rMY, rMA, rML, rDX, rDY;
 80 static void
 81 screen_draw_pixel(void)
 82 {
 83-	int ctrl = dev[0x2e];
 84-	int color = ctrl & 0x3;
 85-	int len = MAR2(uxn_screen.width);
 86+	const int ctrl = dev[0x2e];
 87+	const int color = ctrl & 0x3;
 88+	const int len = MAR2(uxn_screen.width);
 89 	Uint8 *layer = ctrl & 0x40 ? uxn_screen.fg : uxn_screen.bg;
 90 	/* fill mode */
 91 	if(ctrl & 0x80) {
 92@@ -537,44 +537,44 @@ screen_draw_pixel(void)
 93 static void
 94 screen_draw_sprite(void)
 95 {
 96-	int ctrl = dev[0x2f];
 97-	int blend = ctrl & 0xf, opaque = blend % 5;
 98-	int fx = ctrl & 0x10 ? -1 : 1, fy = ctrl & 0x20 ? -1 : 1;
 99-	int qfx = fx > 0 ? 7 : 0, qfy = fy < 0 ? 7 : 0;
100-	int dxy = fy * rDX, dyx = fx * rDY;
101-	int wmar = MAR(uxn_screen.width), wmar2 = MAR2(uxn_screen.width);
102-	int hmar2 = MAR2(uxn_screen.height);
103-	int i, x1, x2, y1, y2, ax, ay, qx, qy, x = rX, y = rY;
104+	const int ctrl = dev[0x2f];
105+	const int blend = ctrl & 0xf, opaque = blend % 5;
106+	const int fx = ctrl & 0x10 ? -1 : 1, fy = ctrl & 0x20 ? -1 : 1;
107+	const int qfx = fx > 0 ? 7 : 0, qfy = fy < 0 ? 7 : 0;
108+	const int dxy = fy * rDX, dyx = fx * rDY;
109+	const int wmar = MAR(uxn_screen.width), wmar2 = MAR2(uxn_screen.width);
110+	const int hmar2 = MAR2(uxn_screen.height);
111 	Uint8 *layer = ctrl & 0x40 ? uxn_screen.fg : uxn_screen.bg;
112+	int i, x1, x2, y1, y2, ax, ay, qx, qy, x = rX, y = rY;
113 	if(ctrl & 0x80) {
114-		int addr_incr = rMA << 2;
115+		const int addr_incr = rMA << 2;
116 		for(i = 0; i <= rML; i++, x += dyx, y += dxy, rA += addr_incr) {
117-			Uint16 xmar = MAR(x), ymar = MAR(y);
118-			Uint16 xmar2 = MAR2(x), ymar2 = MAR2(y);
119+			const Uint16 xmar = MAR(x), ymar = MAR(y);
120+			const Uint16 xmar2 = MAR2(x), ymar2 = MAR2(y);
121 			if(xmar < wmar && ymar2 < hmar2) {
122-				Uint8 *sprite = &ram[rA];
123-				int by = ymar2 * wmar2;
124+				const Uint8 *sprite = &ram[rA];
125+				const int by = ymar2 * wmar2;
126 				for(ay = ymar * wmar2, qy = qfy; ay < by; ay += wmar2, qy += fy) {
127-					int ch1 = sprite[qy], ch2 = sprite[qy + 8] << 1, bx = xmar2 + ay;
128+					const int ch1 = sprite[qy], ch2 = sprite[qy + 8] << 1, bx = xmar2 + ay;
129 					for(ax = xmar + ay, qx = qfx; ax < bx; ax++, qx -= fx) {
130-						int color = ((ch1 >> qx) & 1) | ((ch2 >> qx) & 2);
131+						const int color = ((ch1 >> qx) & 1) | ((ch2 >> qx) & 2);
132 						if(opaque || color) layer[ax] = blending[color][blend];
133 					}
134 				}
135 			}
136 		}
137 	} else {
138-		int addr_incr = rMA << 1;
139+		const int addr_incr = rMA << 1;
140 		for(i = 0; i <= rML; i++, x += dyx, y += dxy, rA += addr_incr) {
141-			Uint16 xmar = MAR(x), ymar = MAR(y);
142-			Uint16 xmar2 = MAR2(x), ymar2 = MAR2(y);
143+			const Uint16 xmar = MAR(x), ymar = MAR(y);
144+			const Uint16 xmar2 = MAR2(x), ymar2 = MAR2(y);
145 			if(xmar < wmar && ymar2 < hmar2) {
146-				Uint8 *sprite = &ram[rA];
147-				int by = ymar2 * wmar2;
148+				const Uint8 *sprite = &ram[rA];
149+				const int by = ymar2 * wmar2;
150 				for(ay = ymar * wmar2, qy = qfy; ay < by; ay += wmar2, qy += fy) {
151-					int ch1 = sprite[qy], bx = xmar2 + ay;
152+					const int ch1 = sprite[qy], bx = xmar2 + ay;
153 					for(ax = xmar + ay, qx = qfx; ax < bx; ax++, qx -= fx) {
154-						int color = (ch1 >> qx) & 1;
155+						const int color = (ch1 >> qx) & 1;
156 						if(opaque || color) layer[ax] = blending[color][blend];
157 					}
158 				}
159@@ -758,7 +758,7 @@ put_fdir(Uint8 *dest, unsigned int len, const char *filepath, DIR *dir)
160 	unsigned int i;
161 	struct dirent *de = readdir(dir);
162 	for(_dirbuf = dirbuf; de != NULL; de = readdir(dir)) {
163-		char *name = de->d_name;
164+		const char *name = de->d_name;
165 		if(name[0] == '.' && (name[1] == '.' || name[1] == '\0'))
166 			continue;
167 		else
168@@ -904,29 +904,6 @@ static unsigned int rL1, rL2;
169 
170 #include <time.h>
171 
172-static unsigned int
173-datetime_dei(Uint8 addr)
174-{
175-	time_t seconds = time(NULL);
176-	struct tm zt = {0};
177-	struct tm *t = localtime(&seconds);
178-	if(t == NULL) t = &zt;
179-	switch(addr) {
180-	case 0xc0: return (t->tm_year + 1900) >> 8;
181-	case 0xc1: return (t->tm_year + 1900);
182-	case 0xc2: return t->tm_mon;
183-	case 0xc3: return t->tm_mday;
184-	case 0xc4: return t->tm_hour;
185-	case 0xc5: return t->tm_min;
186-	case 0xc6: return t->tm_sec;
187-	case 0xc7: return t->tm_wday;
188-	case 0xc8: return t->tm_yday >> 8;
189-	case 0xc9: return t->tm_yday;
190-	case 0xca: return t->tm_isdst;
191-	default: return dev[addr];
192-	}
193-}
194-
195 /*
196 @|Core -------------------------------------------------------------- */
197 
198@@ -954,8 +931,25 @@ emu_dei(Uint8 addr)
199 	case 0x2c: return rA >> 8;
200 	case 0x2d: return rA;
201 	}
202-	if((addr & 0xf0) == 0xc0)
203-		return datetime_dei(addr);
204+	if((addr & 0xf0) == 0xc0) {
205+		time_t seconds = time(NULL);
206+		struct tm zt = {0};
207+		struct tm *t = localtime(&seconds);
208+		if(t == NULL) t = &zt;
209+		switch(addr) {
210+		case 0xc0: return (t->tm_year + 1900) >> 8;
211+		case 0xc1: return (t->tm_year + 1900);
212+		case 0xc2: return t->tm_mon;
213+		case 0xc3: return t->tm_mday;
214+		case 0xc4: return t->tm_hour;
215+		case 0xc5: return t->tm_min;
216+		case 0xc6: return t->tm_sec;
217+		case 0xc7: return t->tm_wday;
218+		case 0xc8: return t->tm_yday >> 8;
219+		case 0xc9: return t->tm_yday;
220+		case 0xca: return t->tm_isdst;
221+		}
222+	}
223 	return dev[addr];
224 }
225 
226@@ -1017,10 +1011,10 @@ emu_deo(Uint8 addr, Uint8 value)
227 static void
228 emu_repaint(void)
229 {
230-	int x = uxn_screen.x1 * uxn_screen.zoom;
231-	int y = uxn_screen.y1 * uxn_screen.zoom;
232-	int w = uxn_screen.x2 * uxn_screen.zoom - x;
233-	int h = uxn_screen.y2 * uxn_screen.zoom - y;
234+	const int x = uxn_screen.x1 * uxn_screen.zoom;
235+	const int y = uxn_screen.y1 * uxn_screen.zoom;
236+	const int w = uxn_screen.x2 * uxn_screen.zoom - x;
237+	const int h = uxn_screen.y2 * uxn_screen.zoom - y;
238 	screen_redraw();
239 	XPutImage(display, window, DefaultGC(display, 0), ximage, x, y, x, y, w, h);
240 }
241@@ -1028,8 +1022,8 @@ emu_repaint(void)
242 void
243 emu_resize(void)
244 {
245-	int width = uxn_screen.width * uxn_screen.zoom;
246-	int height = uxn_screen.height * uxn_screen.zoom;
247+	const int width = uxn_screen.width * uxn_screen.zoom;
248+	const int height = uxn_screen.height * uxn_screen.zoom;
249 	XResizeWindow(display, window, width, height);
250 	uxn_screen.pixels = realloc(uxn_screen.pixels,
251 		uxn_screen.width * uxn_screen.height * sizeof(unsigned int) * uxn_screen.zoom * uxn_screen.zoom);
+11, -11
 1@@ -26,8 +26,8 @@ typedef struct {
 2 static Uint8 *ram, dev[0x100];
 3 static Stack wst, rst;
 4 
 5-unsigned int emu_dei(Uint8 addr);
 6-void emu_deo(Uint8 addr, Uint8 value);
 7+unsigned int emu_dei(Uint8 port);
 8+void emu_deo(Uint8 port, Uint8 value);
 9 
10 /* clang-format off */
11 
12@@ -469,7 +469,7 @@ put_fdir(Uint8 *dest, unsigned int len, const char *filepath, DIR *dir)
13 	unsigned int i;
14 	struct dirent *de = readdir(dir);
15 	for(_dirbuf = dirbuf; de != NULL; de = readdir(dir)) {
16-		char *name = de->d_name;
17+		const char *name = de->d_name;
18 		if(name[0] == '.' && (name[1] == '.' || name[1] == '\0'))
19 			continue;
20 		else
21@@ -619,9 +619,9 @@ static unsigned int rL1, rL2;
22 @|Core -------------------------------------------------------------- */
23 
24 unsigned int
25-emu_dei(Uint8 addr)
26+emu_dei(Uint8 port)
27 {
28-	switch(addr) {
29+	switch(port) {
30 	/* System */
31 	case 0x04: return wst.ptr;
32 	case 0x05: return rst.ptr;
33@@ -630,12 +630,12 @@ emu_dei(Uint8 addr)
34 	case CMD_EXIT: check_child(); break;
35 	}
36 	/* Datetime */
37-	if((addr & 0xf0) == 0xc0) {
38+	if((port & 0xf0) == 0xc0) {
39 		time_t seconds = time(NULL);
40 		struct tm zt = {0};
41 		struct tm *t = localtime(&seconds);
42 		if(t == NULL) t = &zt;
43-		switch(addr) {
44+		switch(port) {
45 		case 0xc0: return (t->tm_year + 1900) >> 8;
46 		case 0xc1: return (t->tm_year + 1900);
47 		case 0xc2: return t->tm_mon;
48@@ -649,14 +649,14 @@ emu_dei(Uint8 addr)
49 		case 0xca: return t->tm_isdst;
50 		}
51 	}
52-	return dev[addr];
53+	return dev[port];
54 }
55 
56 void
57-emu_deo(Uint8 addr, Uint8 value)
58+emu_deo(Uint8 port, Uint8 value)
59 {
60-	dev[addr] = value;
61-	switch(addr) {
62+	dev[port] = value;
63+	switch(port) {
64 	/* System */
65 	case 0x03: system_expansion(PEEK2(dev + 2)); break;
66 	case 0x04: wst.ptr = dev[4]; break;