Browse code

(uxn-fast.c) Removed function pointers, instead use exterm

Devine Lu Linvega authored on 05/03/2023 18:44:23
Showing 5 changed files
... ...
@@ -31,7 +31,7 @@ Build the assembler and emulator by running the `build.sh` script. The assembler
31 31
 If you wish to build the emulator without graphics mode:
32 32
 
33 33
 ```sh
34
-cc src/devices/datetime.c src/devices/system.c src/devices/file.c src/uxn.c -DNDEBUG -Os -g0 -s src/uxncli.c -o bin/uxncli
34
+cc src/devices/datetime.c src/devices/system.c src/devices/file.c src/uxn-fast.c -DNDEBUG -Os -g0 -s src/uxncli.c -o bin/uxncli
35 35
 ```
36 36
 
37 37
 ### Plan 9 
... ...
@@ -34,15 +34,15 @@ WITH REGARD TO THIS SOFTWARE.
34 34
 #define PUT2(o, v) { tmp = (v); s->dat[s->ptr - o - 2] = tmp >> 8; s->dat[s->ptr - o - 1] = tmp; }
35 35
 #define PUSH(stack, v) { if(s->ptr > 254) HALT(2) stack->dat[stack->ptr++] = (v); }
36 36
 #define PUSH2(stack, v) { if(s->ptr > 253) HALT(2) tmp = (v); stack->dat[stack->ptr] = (v) >> 8; stack->dat[stack->ptr + 1] = (v); stack->ptr += 2; }
37
-#define SEND(a, b) { u->dev[a] = b; if((send_events[(a) >> 4] >> ((a) & 0xf)) & 0x1) u->deo(u, a); }
38
-#define LISTEN(a, b) { PUT(a, ((receive_events[(b) >> 4] >> ((b) & 0xf)) & 0x1) ? u->dei(u, b) : u->dev[b])  }
37
+#define DEO(a, b) { u->dev[a] = b; if((deo_masks[(a) >> 4] >> ((a) & 0xf)) & 0x1) uxn_deo(u, a); }
38
+#define DEI(a, b) { PUT(a, ((dei_masks[(b) >> 4] >> ((b) & 0xf)) & 0x1) ? uxn_dei(u, b) : u->dev[b])  }
39 39
 
40 40
 static 
41
-Uint16 send_events[] = {
41
+Uint16 deo_masks[] = {
42 42
 	0x6a08, 0x0300, 0xc028, 0x8000, 0x8000, 0x8000, 0x8000, 0x0000,
43 43
 	0x0000, 0x0000, 0xa260, 0xa260, 0x0000, 0x0000, 0x0000, 0x0000 };
44 44
 static
45
-Uint16 receive_events[] = {
45
+Uint16 dei_masks[] = {
46 46
 	0x0000, 0x0000, 0x0014, 0x0014, 0x0014, 0x0014, 0x0014, 0x0000,
47 47
 	0x0000, 0x0000, 0x0000, 0x0000, 0x07fd, 0x0000, 0x0000, 0x0000 };
48 48
 
... ...
@@ -111,10 +111,10 @@ uxn_eval(Uxn *u, Uint16 pc)
111 111
 			case 0x14: /* LDA  */ t=T2;           INC(2,-1) PUT(0, u->ram[t]) break;
112 112
 			case 0x35: /* STA2 */ t=T2;n=N2;      DEC(4, 0) POKE16(u->ram + t, n) break;
113 113
 			case 0x15: /* STA  */ t=T2;n=L;       DEC(3, 0) u->ram[t] = n; break;
114
-			case 0x36: /* DEI2 */ t=T;            INC(1, 1) LISTEN(1, t) LISTEN(0, t + 1) break;
115
-			case 0x16: /* DEI  */ t=T;            INC(1, 0) LISTEN(0, t) break;
116
-			case 0x37: /* DEO2 */ t=T;n=N;l=L;    DEC(3, 0) SEND(t, l) SEND(t + 1, n) break;
117
-			case 0x17: /* DEO  */ t=T;n=N;        DEC(2, 0) SEND(t, n) break;
114
+			case 0x36: /* DEI2 */ t=T;            INC(1, 1) DEI(1, t) DEI(0, t + 1) break;
115
+			case 0x16: /* DEI  */ t=T;            INC(1, 0) DEI(0, t) break;
116
+			case 0x37: /* DEO2 */ t=T;n=N;l=L;    DEC(3, 0) DEO(t, l) DEO(t + 1, n) break;
117
+			case 0x17: /* DEO  */ t=T;n=N;        DEC(2, 0) DEO(t, n) break;
118 118
 			case 0x38: /* ADD2 */ t=T2;n=N2;      INC(4,-2) PUT2(0, n + t) break;
119 119
 			case 0x18: /* ADD  */ t=T;n=N;        INC(2,-1) PUT(0, n + t) break;
120 120
 			case 0x39: /* SUB2 */ t=T2;n=N2;      INC(4,-2) PUT2(0, n - t) break;
... ...
@@ -136,7 +136,7 @@ uxn_eval(Uxn *u, Uint16 pc)
136 136
 }
137 137
 
138 138
 int
139
-uxn_boot(Uxn *u, Uint8 *ram, Dei *dei, Deo *deo)
139
+uxn_boot(Uxn *u, Uint8 *ram)
140 140
 {
141 141
 	Uint32 i;
142 142
 	char *cptr = (char *)u;
... ...
@@ -146,7 +146,5 @@ uxn_boot(Uxn *u, Uint8 *ram, Dei *dei, Deo *deo)
146 146
 	u->rst = (Stack *)(ram + 0xf0100);
147 147
 	u->dev = (Uint8 *)(ram + 0xf0200);
148 148
 	u->ram = ram;
149
-	u->dei = dei;
150
-	u->deo = deo;
151 149
 	return 1;
152 150
 }
... ...
@@ -35,9 +35,13 @@ typedef struct Uxn {
35 35
 	void (*deo)(struct Uxn *u, Uint8 addr);
36 36
 } Uxn;
37 37
 
38
-typedef Uint8 Dei(Uxn *u, Uint8 addr);
39
-typedef void Deo(Uxn *u, Uint8 addr);
38
+/* required functions */
40 39
 
41
-int uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr);
42
-int uxn_boot(Uxn *u, Uint8 *ram, Dei *dei, Deo *deo);
43
-int uxn_eval(Uxn *u, Uint16 pc);
44 40
\ No newline at end of file
41
+extern Uint8 uxn_dei(Uxn *u, Uint8 addr);
42
+extern void uxn_deo(Uxn *u, Uint8 addr);
43
+extern int uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr);
44
+
45
+/* built-ins */
46
+
47
+int uxn_boot(Uxn *u, Uint8 *ram);
48
+int uxn_eval(Uxn *u, Uint16 pc);
... ...
@@ -47,8 +47,8 @@ console_deo(Uint8 *d, Uint8 port)
47 47
 	}
48 48
 }
49 49
 
50
-static Uint8
51
-emu_dei(Uxn *u, Uint8 addr)
50
+Uint8
51
+uxn_dei(Uxn *u, Uint8 addr)
52 52
 {
53 53
 	switch(addr & 0xf0) {
54 54
 	case 0xc0: return datetime_dei(u, addr);
... ...
@@ -56,8 +56,8 @@ emu_dei(Uxn *u, Uint8 addr)
56 56
 	return u->dev[addr];
57 57
 }
58 58
 
59
-static void
60
-emu_deo(Uxn *u, Uint8 addr)
59
+void
60
+uxn_deo(Uxn *u, Uint8 addr)
61 61
 {
62 62
 	Uint8 p = addr & 0x0f, d = addr & 0xf0;
63 63
 	switch(d) {
... ...
@@ -75,7 +75,7 @@ main(int argc, char **argv)
75 75
 	int i;
76 76
 	if(argc < 2)
77 77
 		return emu_error("Usage", "uxncli game.rom args");
78
-	if(!uxn_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), emu_dei, emu_deo))
78
+	if(!uxn_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8))))
79 79
 		return emu_error("Boot", "Failed");
80 80
 	if(!system_load(&u, argv[1]))
81 81
 		return emu_error("Load", "Failed");
... ...
@@ -108,8 +108,8 @@ audio_deo(int instance, Uint8 *d, Uint8 port, Uxn *u)
108 108
 	}
109 109
 }
110 110
 
111
-static Uint8
112
-emu_dei(Uxn *u, Uint8 addr)
111
+Uint8
112
+uxn_dei(Uxn *u, Uint8 addr)
113 113
 {
114 114
 	Uint8 p = addr & 0x0f, d = addr & 0xf0;
115 115
 	switch(d) {
... ...
@@ -123,8 +123,8 @@ emu_dei(Uxn *u, Uint8 addr)
123 123
 	return u->dev[addr];
124 124
 }
125 125
 
126
-static void
127
-emu_deo(Uxn *u, Uint8 addr)
126
+void
127
+uxn_deo(Uxn *u, Uint8 addr)
128 128
 {
129 129
 	Uint8 p = addr & 0x0f, d = addr & 0xf0;
130 130
 	switch(d) {
... ...
@@ -263,7 +263,7 @@ static int
263 263
 start(Uxn *u, char *rom)
264 264
 {
265 265
 	free(u->ram);
266
-	if(!uxn_boot(u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), emu_dei, emu_deo))
266
+	if(!uxn_boot(u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8))))
267 267
 		return error("Boot", "Failed to start uxn.");
268 268
 	if(!system_load(u, rom))
269 269
 		return error("Boot", "Failed to load rom.");