Browse code

Replaced some macros

Devine Lu Linvega authored on 01/03/2023 18:35:42
Showing 5 changed files
... ...
@@ -156,35 +156,26 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
156 156
 	switch(port) {
157 157
 	case 0x3:
158 158
 		if(!FIXED_SIZE) {
159
-			Uint16 w;
160
-			PEKDEV(w, 0x2);
161
-			screen_resize(&uxn_screen, clamp(w, 1, 1024), uxn_screen.height);
159
+			screen_resize(&uxn_screen, clamp(PEEK16(d + 2), 1, 1024), uxn_screen.height);
162 160
 		}
163 161
 		break;
164 162
 	case 0x5:
165 163
 		if(!FIXED_SIZE) {
166
-			Uint16 h;
167
-			PEKDEV(h, 0x4);
168
-			screen_resize(&uxn_screen, uxn_screen.width, clamp(h, 1, 1024));
164
+			screen_resize(&uxn_screen, uxn_screen.width, clamp(PEEK16(d + 4), 1, 1024));
169 165
 		}
170 166
 		break;
171 167
 	case 0xe: {
172
-		Uint16 x, y;
168
+		Uint16 x = PEEK16(d + 0x8), y = PEEK16(d + 0xa);
173 169
 		Uint8 layer = d[0xe] & 0x40;
174
-		PEKDEV(x, 0x8);
175
-		PEKDEV(y, 0xa);
176 170
 		screen_write(&uxn_screen, layer ? &uxn_screen.fg : &uxn_screen.bg, x, y, d[0xe] & 0x3);
177
-		if(d[0x6] & 0x01) POKDEV(0x8, x + 1); /* auto x+1 */
178
-		if(d[0x6] & 0x02) POKDEV(0xa, y + 1); /* auto y+1 */
171
+		if(d[0x6] & 0x01) POKE16(d + 0x8, x + 1); /* auto x+1 */
172
+		if(d[0x6] & 0x02) POKE16(d + 0xa, y + 1); /* auto y+1 */
179 173
 		break;
180 174
 	}
181 175
 	case 0xf: {
182
-		Uint16 x, y, dx, dy, addr;
176
+		Uint16 x = PEEK16(d + 0x8), y = PEEK16(d + 0xa), dx, dy, addr = PEEK16(d + 0xc);
183 177
 		Uint8 i, n, twobpp = !!(d[0xf] & 0x80);
184 178
 		Layer *layer = (d[0xf] & 0x40) ? &uxn_screen.fg : &uxn_screen.bg;
185
-		PEKDEV(x, 0x8);
186
-		PEKDEV(y, 0xa);
187
-		PEKDEV(addr, 0xc);
188 179
 		n = d[0x6] >> 4;
189 180
 		dx = (d[0x6] & 0x01) << 3;
190 181
 		dy = (d[0x6] & 0x02) << 2;
... ...
@@ -198,9 +189,9 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
198 189
 				addr += (d[0x6] & 0x04) << (1 + twobpp);
199 190
 			}
200 191
 		}
201
-		POKDEV(0xc, addr);   /* auto addr+length */
202
-		POKDEV(0x8, x + dx); /* auto x+8 */
203
-		POKDEV(0xa, y + dy); /* auto y+8 */
192
+		POKE16(d + 0xc, addr);   /* auto addr+length */
193
+		POKE16(d + 0x8, x + dx); /* auto x+8 */
194
+		POKE16(d + 0xa, y + dy); /* auto y+8 */
204 195
 		break;
205 196
 	}
206 197
 	}
... ...
@@ -71,11 +71,9 @@ system_load(Uxn *u, char *filename)
71 71
 void
72 72
 system_deo(Uxn *u, Uint8 *d, Uint8 port)
73 73
 {
74
-	Uint16 a;
75 74
 	switch(port) {
76 75
 	case 0x3:
77
-		PEKDEV(a, 0x2);
78
-		system_cmd(u->ram, a);
76
+		system_cmd(u->ram, PEEK16(d + 2));
79 77
 		break;
80 78
 	case 0xe:
81 79
 		if(u->wst->ptr || u->rst->ptr) system_inspect(u);
... ...
@@ -17,19 +17,14 @@ WITH REGARD TO THIS SOFTWARE.
17 17
 
18 18
 #define HALT(c) { return uxn_halt(u, instr, (c), pc - 1); }
19 19
 #define JUMP(x) { if(m2) pc = (x); else pc += (Sint8)(x); }
20
-
21 20
 #define PUSH8(x) { if(s->ptr == 0xff) HALT(2) s->dat[s->ptr++] = (x); }
22
-#define PUSH16(x) { if((tsp = s->ptr) >= 0xfe) HALT(2) t = (x); s->dat[tsp] = t >> 8; s->dat[tsp + 1] = t; s->ptr = tsp + 2; }
21
+#define PUSH16(x) { if((tsp = s->ptr) >= 0xfe) HALT(2) t = (x); POKE16(&s->dat[tsp], t); s->ptr = tsp + 2; }
23 22
 #define PUSH(x) { if(m2) { PUSH16(x) } else { PUSH8(x) } }
24
-
25 23
 #define POP8(o) { if(*sp == 0x00) HALT(1) o = s->dat[--*sp]; }
26
-#define POP16(o) { if((tsp = *sp) <= 0x01) HALT(1) o = s->dat[tsp - 1] | (s->dat[tsp - 2] << 8); *sp = tsp - 2; }
24
+#define POP16(o) { if((tsp = *sp) <= 0x01) HALT(1) o = PEEK16(&s->dat[tsp - 2]); *sp = tsp - 2; }
27 25
 #define POP(o) { if(m2) { POP16(o) } else { POP8(o) } }
28
-
29
-#define POKE(x, y) { if(m2) { t = (y); u->ram[(x)] = t >> 8; u->ram[(x) + 1] = t; } else { u->ram[(x)] = (y); } }
30
-#define PEEK16(o, x) { o = (u->ram[(x)] << 8) | u->ram[(x) + 1]; }
31
-#define PEEK(o, x) { if(m2) PEEK16(o, x) else o = u->ram[(x)]; }
32
-
26
+#define POKE(x, y) { if(m2) { t = (y); POKE16(u->ram + x, t) } else { u->ram[(x)] = (y); } }
27
+#define PEEK(o, x) { if(m2) { o = PEEK16(u->ram + x); } else o = u->ram[(x)]; }
33 28
 #define DEVR(o, x) { o = u->dei(u, x); if(m2) o = (o << 8) | u->dei(u, (x) + 1); }
34 29
 #define DEVW(x, y) { if(m2) { u->deo(u, (x), (y) >> 8); u->deo(u, (x) + 1, (y)); } else { u->deo(u, x, (y)); } }
35 30
 
... ...
@@ -55,12 +50,12 @@ uxn_eval(Uxn *u, Uint16 pc)
55 50
 		/* Immediate */
56 51
 		case -0x0: /* BRK */ return 1;
57 52
 		case -0x1: /* JCI */ POP8(b) if(!b) { pc += 2; break; }
58
-		case -0x2: /* JMI */ PEEK16(a, pc) pc += a + 2; break;
59
-		case -0x3: /* JSI */ s = u->rst; PUSH16(pc + 2) PEEK16(a, pc) pc += a + 2; break;
53
+		case -0x2: /* JMI */ pc += PEEK16(u->ram + pc) + 2; break;
54
+		case -0x3: /* JSI */ s = u->rst; PUSH16(pc + 2) pc += PEEK16(u->ram + pc) + 2; break;
60 55
 		case -0x4: /* LIT */
61 56
 		case -0x6: /* LITr */ a = u->ram[pc++]; PUSH8(a) break;
62 57
 		case -0x5: /* LIT2 */
63
-		case -0x7: /* LIT2r */ PEEK16(a, pc) PUSH16(a) pc += 2; break;
58
+		case -0x7: /* LIT2r */ PUSH16(PEEK16(u->ram + pc)) pc += 2; break;
64 59
 		/* ALU */
65 60
 		case 0x01: /* INC */ POP(a) PUSH(a + 1) break;
66 61
 		case 0x02: /* POP */ POP(a) break;
... ...
@@ -9,22 +9,25 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9
 WITH REGARD TO THIS SOFTWARE.
10 10
 */
11 11
 
12
-typedef unsigned char Uint8;
13
-typedef signed char Sint8;
14
-typedef unsigned short Uint16;
15
-typedef signed short Sint16;
16
-typedef unsigned int Uint32;
17
-
18 12
 #define PAGE_PROGRAM 0x0100
19 13
 
20 14
 /* clang-format off */
21 15
 
16
+#define POKE16(d, v) { (d)[0] = (v) >> 8; (d)[1] = (v); }
17
+#define PEEK16(d) ((d)[0] << 8 | (d)[1])
18
+
22 19
 #define GETVEC(d) ((d)[0] << 8 | (d)[1])
23 20
 #define POKDEV(x, y) { d[(x)] = (y) >> 8; d[(x) + 1] = (y); }
24 21
 #define PEKDEV(o, x) { (o) = (d[(x)] << 8) + d[(x) + 1]; }
25 22
 
26 23
 /* clang-format on */
27 24
 
25
+typedef unsigned char Uint8;
26
+typedef signed char Sint8;
27
+typedef unsigned short Uint16;
28
+typedef signed short Sint16;
29
+typedef unsigned int Uint32;
30
+
28 31
 typedef struct {
29 32
 	Uint8 dat[255], ptr;
30 33
 } Stack;
... ...
@@ -41,4 +44,4 @@ typedef void Deo(Uxn *u, Uint8 addr, Uint8 value);
41 44
 
42 45
 int uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr);
43 46
 int uxn_boot(Uxn *u, Uint8 *ram, Dei *dei, Deo *deo);
44
-int uxn_eval(Uxn *u, Uint16 pc);
47
+int uxn_eval(Uxn *u, Uint16 pc);
45 48
\ No newline at end of file
... ...
@@ -87,7 +87,7 @@ audio_dei(int instance, Uint8 *d, Uint8 port)
87 87
 	if(!audio_id) return d[port];
88 88
 	switch(port) {
89 89
 	case 0x4: return audio_get_vu(instance);
90
-	case 0x2: POKDEV(0x2, audio_get_position(instance)); /* fall through */
90
+	case 0x2: POKE16(d + 0x2, audio_get_position(instance)); /* fall through */
91 91
 	default: return d[port];
92 92
 	}
93 93
 }