commit e20f3ff
Devine Lu Linvega
·
2025-05-16 16:20:04 +0000 UTC
parent c73712b
(uxncli) Cleanup
1 files changed,
+78,
-82
+78,
-82
1@@ -31,14 +31,10 @@ typedef struct {
2 Uint8 dat[0x100], ptr;
3 } Stack;
4
5-typedef struct Uxn {
6- Uint8 *ram, dev[0x100];
7- Stack wst, rst;
8-} Uxn;
9-
10 Uint8 emu_dei(Uint8 addr);
11 void emu_deo(Uint8 addr, Uint8 value);
12-static Uxn uxn;
13+static Uint8 *ram, dev[0x100];
14+static Stack wst, rst;
15
16 /* clang-format off */
17
18@@ -50,47 +46,47 @@ static Uxn uxn;
19 case 0x20|opc: {const int _2=1,_r=0;init body;} break;\
20 case 0x40|opc: {const int _2=0,_r=1;init body;} break;\
21 case 0x60|opc: {const int _2=1,_r=1;init body;} break;\
22- case 0x80|opc: {const int _2=0,_r=0,k=uxn.wst.ptr;init uxn.wst.ptr=k;body;} break;\
23- case 0xa0|opc: {const int _2=1,_r=0,k=uxn.wst.ptr;init uxn.wst.ptr=k;body;} break;\
24- case 0xc0|opc: {const int _2=0,_r=1,k=uxn.rst.ptr;init uxn.rst.ptr=k;body;} break;\
25- case 0xe0|opc: {const int _2=1,_r=1,k=uxn.rst.ptr;init uxn.rst.ptr=k;body;} break;\
26+ case 0x80|opc: {const int _2=0,_r=0,k=wst.ptr;init wst.ptr=k;body;} break;\
27+ case 0xa0|opc: {const int _2=1,_r=0,k=wst.ptr;init wst.ptr=k;body;} break;\
28+ case 0xc0|opc: {const int _2=0,_r=1,k=rst.ptr;init rst.ptr=k;body;} break;\
29+ case 0xe0|opc: {const int _2=1,_r=1,k=rst.ptr;init rst.ptr=k;body;} break;\
30 }
31
32 /* Microcode */
33
34-#define REM if(_r) uxn.rst.ptr -= 1 + _2; else uxn.wst.ptr -= 1 + _2;
35-#define JMI c = uxn.ram[pc] << 8 | uxn.ram[pc+1], pc += 2;
36+#define REM if(_r) rst.ptr -= 1 + _2; else wst.ptr -= 1 + _2;
37+#define JMI c = ram[pc] << 8 | ram[pc+1], pc += 2;
38 #define JMP(x) { if(_2) pc = x; else pc += (signed char)x; }
39-#define DEC(s) uxn.s.dat[--uxn.s.ptr]
40+#define DEC(s) s.dat[--s.ptr]
41 #define PO1(o) { if(_r) o = DEC(rst); else o = DEC(wst); }
42 #define PO2(o) { PO1(o) if(_r) o |= DEC(rst) << 8; else o |= DEC(wst) << 8; }
43 #define POx(o) { if(_2) PO2(o) else PO1(o) }
44 #define GOT(o) { if(_2) PO1(o[1]) PO1(o[0]) }
45-#define INC(s) uxn.s.dat[uxn.s.ptr++]
46+#define INC(s) s.dat[s.ptr++]
47 #define RP1(i) { if(_r) INC(wst) = i; else INC(rst) = i; }
48 #define PU1(i) { if(_r) INC(rst) = i; else INC(wst) = i; }
49 #define PUx(i) { if(_2) { c = (i); PU1(c >> 8) PU1(c) } else PU1(i) }
50 #define PUT(i) { PU1(i[0]) if(_2) PU1(i[1]) }
51 #define DEI(i,o) o[0] = emu_dei(i); if(_2) o[1] = emu_dei(i + 1); PUT(o)
52 #define DEO(i,j) emu_deo(i, j[0]); if(_2) emu_deo(i + 1, j[1]);
53-#define PEK(i,o,m) o[0] = uxn.ram[i]; if(_2) o[1] = uxn.ram[(i + 1) & m]; PUT(o)
54-#define POK(i,j,m) uxn.ram[i] = j[0]; if(_2) uxn.ram[(i + 1) & m] = j[1];
55+#define PEK(i,o,m) o[0] = ram[i]; if(_2) o[1] = ram[(i + 1) & m]; PUT(o)
56+#define POK(i,j,m) ram[i] = j[0]; if(_2) ram[(i + 1) & m] = j[1];
57
58 int
59 uxn_eval(Uint16 pc)
60 {
61 Uint32 a, b, c, x[2], y[2], z[2], step;
62- if(uxn.dev[0x0f]) return 0;
63+ if(dev[0x0f]) return 0;
64 for(step = STEP_MAX; step; step--) {
65- switch(uxn.ram[pc++]) {
66+ switch(ram[pc++]) {
67 /* BRK */ case 0x00: return 1;
68 /* JCI */ case 0x20: if(DEC(wst)) { JMI pc += c; break; } pc += 2; break;
69 /* JMI */ case 0x40: JMI pc += c; break;
70 /* JSI */ case 0x60: JMI INC(rst) = pc >> 8, INC(rst) = pc, pc += c; break;
71- /* LI2 */ case 0xa0: INC(wst) = uxn.ram[pc++]; /* fall-through */
72- /* LIT */ case 0x80: INC(wst) = uxn.ram[pc++]; break;
73- /* L2r */ case 0xe0: INC(rst) = uxn.ram[pc++]; /* fall-through */
74- /* LIr */ case 0xc0: INC(rst) = uxn.ram[pc++]; break;
75+ /* LI2 */ case 0xa0: INC(wst) = ram[pc++]; /* fall-through */
76+ /* LIT */ case 0x80: INC(wst) = ram[pc++]; break;
77+ /* L2r */ case 0xe0: INC(rst) = ram[pc++]; /* fall-through */
78+ /* LIr */ case 0xc0: INC(rst) = ram[pc++]; break;
79 /* INC */ OPC(0x01,POx(a),PUx(a + 1))
80 /* POP */ OPC(0x02,REM,{})
81 /* NIP */ OPC(0x03,GOT(x) REM ,PUT(x))
82@@ -145,13 +141,13 @@ system_print(char *name, Stack *s)
83 }
84
85 static int
86-system_load(Uint8 *ram, char *rom_path)
87+system_load(char *rom_path)
88 {
89 FILE *f = fopen(rom_path, "rb");
90 if(f) {
91- int i = 0, l = fread(ram, 0x10000 - PAGE_PROGRAM, 1, f);
92+ int i = 0, l = fread(ram + PAGE_PROGRAM, 0x10000 - PAGE_PROGRAM, 1, f);
93 while(l && ++i < RAM_PAGES)
94- l = fread(ram + 0x10000 * i - PAGE_PROGRAM, 0x10000, 1, f);
95+ l = fread(ram + PAGE_PROGRAM + 0x10000 * i - PAGE_PROGRAM, 0x10000, 1, f);
96 fclose(f);
97 }
98 return !!f;
99@@ -160,10 +156,10 @@ system_load(Uint8 *ram, char *rom_path)
100 static int
101 system_boot(char *rom_path, int has_args)
102 {
103- uxn.ram = (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8));
104+ ram = (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8));
105 system_boot_path = rom_path;
106- uxn.dev[0x17] = has_args;
107- if(system_load(uxn.ram + PAGE_PROGRAM, rom_path))
108+ dev[0x17] = has_args;
109+ if(system_load(rom_path))
110 return 1;
111 return 0;
112 }
113@@ -171,26 +167,26 @@ system_boot(char *rom_path, int has_args)
114 static void
115 system_expansion(Uint16 addr)
116 {
117- Uint8 *aptr = uxn.ram + addr;
118+ Uint8 *aptr = ram + addr;
119 Uint16 value, length = PEEK2(aptr + 1);
120- if(uxn.ram[addr] == 0x0) {
121+ if(ram[addr] == 0x0) {
122 Uint32 a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5);
123 Uint32 b = a + length;
124- value = uxn.ram[addr + 7];
125- for(; a < b; uxn.ram[a++] = value);
126- } else if(uxn.ram[addr] == 0x1) {
127+ value = ram[addr + 7];
128+ for(; a < b; ram[a++] = value);
129+ } else if(ram[addr] == 0x1) {
130 Uint32 a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5);
131 Uint32 b = a + length;
132 Uint32 c = PEEK2(aptr + 7) * 0x10000 + PEEK2(aptr + 9);
133- for(; a < b; uxn.ram[c++] = uxn.ram[a++]);
134- } else if(uxn.ram[addr] == 0x2) {
135+ for(; a < b; ram[c++] = ram[a++]);
136+ } else if(ram[addr] == 0x2) {
137 Uint32 a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5);
138 Uint32 b = a + length;
139 Uint32 c = PEEK2(aptr + 7) * 0x10000 + PEEK2(aptr + 9);
140 Uint32 d = c + length;
141- for(; b >= a; uxn.ram[--d] = uxn.ram[--b]);
142+ for(; b >= a; ram[--d] = ram[--b]);
143 } else
144- fprintf(stderr, "Unknown command: %s\n", &uxn.ram[addr]);
145+ fprintf(stderr, "Unknown command: %s\n", &ram[addr]);
146 }
147
148 /*
149@@ -266,16 +262,16 @@ start_fork_pipe(void)
150 {
151 pid_t pid;
152 pid_t parent_pid = getpid();
153- int addr = PEEK2(&uxn.dev[CMD_ADDR]);
154+ int addr = PEEK2(&dev[CMD_ADDR]);
155 fflush(stdout);
156 if(child_mode & 0x08) {
157- uxn.dev[CMD_EXIT] = uxn.dev[CMD_LIVE] = 0x00;
158+ dev[CMD_EXIT] = dev[CMD_LIVE] = 0x00;
159 return;
160 }
161 if(child_mode & 0x01) {
162 /* parent writes to child's stdin */
163 if(pipe(to_child_fd) == -1) {
164- uxn.dev[CMD_EXIT] = uxn.dev[CMD_LIVE] = 0xff;
165+ dev[CMD_EXIT] = dev[CMD_LIVE] = 0xff;
166 fprintf(stderr, "Pipe error to child.\n");
167 return;
168 }
169@@ -283,16 +279,16 @@ start_fork_pipe(void)
170 if(child_mode & (0x04 | 0x02)) {
171 /* parent reads from child's stdout and/or stderr */
172 if(pipe(from_child_fd) == -1) {
173- uxn.dev[CMD_EXIT] = uxn.dev[CMD_LIVE] = 0xff;
174+ dev[CMD_EXIT] = dev[CMD_LIVE] = 0xff;
175 fprintf(stderr, "Pipe error from child.\n");
176 return;
177 }
178 }
179
180- fork_args[2] = (char *)&uxn.ram[addr];
181+ fork_args[2] = (char *)&ram[addr];
182 pid = fork();
183 if(pid < 0) { /* failure */
184- uxn.dev[CMD_EXIT] = uxn.dev[CMD_LIVE] = 0xff;
185+ dev[CMD_EXIT] = dev[CMD_LIVE] = 0xff;
186 fprintf(stderr, "Fork failure.\n");
187 } else if(pid == 0) { /* child */
188
189@@ -319,8 +315,8 @@ start_fork_pipe(void)
190 exit(1);
191 } else { /*parent*/
192 child_pid = pid;
193- uxn.dev[CMD_LIVE] = 0x01;
194- uxn.dev[CMD_EXIT] = 0x00;
195+ dev[CMD_LIVE] = 0x01;
196+ dev[CMD_EXIT] = 0x00;
197 if(child_mode & 0x01) {
198 saved_out = dup(1);
199 dup2(to_child_fd[1], 1);
200@@ -340,12 +336,12 @@ check_child(void)
201 int wstatus;
202 if(child_pid) {
203 if(waitpid(child_pid, &wstatus, WNOHANG)) {
204- uxn.dev[CMD_LIVE] = 0xff;
205- uxn.dev[CMD_EXIT] = WEXITSTATUS(wstatus);
206+ dev[CMD_LIVE] = 0xff;
207+ dev[CMD_EXIT] = WEXITSTATUS(wstatus);
208 clean_after_child();
209 } else {
210- uxn.dev[CMD_LIVE] = 0x01;
211- uxn.dev[CMD_EXIT] = 0x00;
212+ dev[CMD_LIVE] = 0x01;
213+ dev[CMD_EXIT] = 0x00;
214 }
215 }
216 }
217@@ -357,8 +353,8 @@ kill_child(void)
218 if(child_pid) {
219 kill(child_pid, 9);
220 if(waitpid(child_pid, &wstatus, WNOHANG)) {
221- uxn.dev[CMD_LIVE] = 0xff;
222- uxn.dev[CMD_EXIT] = WEXITSTATUS(wstatus);
223+ dev[CMD_LIVE] = 0xff;
224+ dev[CMD_EXIT] = WEXITSTATUS(wstatus);
225 clean_after_child();
226 }
227 }
228@@ -369,7 +365,7 @@ start_fork(void)
229 {
230 fflush(stderr);
231 kill_child();
232- child_mode = uxn.dev[CMD_MODE];
233+ child_mode = dev[CMD_MODE];
234 start_fork_pipe();
235 }
236
237@@ -377,7 +373,7 @@ static int
238 console_input(int c, int type)
239 {
240 if(c == EOF) c = 0, type = 4;
241- uxn.dev[0x12] = c, uxn.dev[0x17] = type;
242+ dev[0x12] = c, dev[0x17] = type;
243 if(console_vector)
244 uxn_eval(console_vector);
245 return type != 4;
246@@ -533,7 +529,7 @@ static int
247 file_init(int id, Uint16 addr)
248 {
249 file_reset(id);
250- ufs[id].filepath = (char *)&uxn.ram[addr];
251+ ufs[id].filepath = (char *)&ram[addr];
252 return 0;
253 }
254
255@@ -550,7 +546,7 @@ file_not_ready(int id)
256 static int
257 file_read(int id, Uint16 addr, int len)
258 {
259- void *dest = &uxn.ram[addr];
260+ void *dest = &ram[addr];
261 if(file_not_ready(id))
262 return 0;
263 if(ufs[id].state != FILE_READ && ufs[id].state != DIR_READ) {
264@@ -582,7 +578,7 @@ file_write(int id, Uint16 addr, int len, Uint8 flags)
265 ufs[id].state = FILE_WRITE;
266 }
267 if(ufs[id].state == FILE_WRITE)
268- if((ret = fwrite(&uxn.ram[addr], 1, len, ufs[id].f)) > 0 && fflush(ufs[id].f) != 0)
269+ if((ret = fwrite(&ram[addr], 1, len, ufs[id].f)) > 0 && fflush(ufs[id].f) != 0)
270 ret = 0;
271 if(ufs[id].state == DIR_WRITE)
272 ret = is_dir_real(ufs[id].filepath);
273@@ -598,7 +594,7 @@ file_stat(int id, Uint16 addr, int len)
274 return 0;
275 err = stat(ufs[id].filepath, &st);
276 dir = S_ISDIR(st.st_mode);
277- return put_stat(&uxn.ram[addr], len, st.st_size, err, dir, 0);
278+ return put_stat(&ram[addr], len, st.st_size, err, dir, 0);
279 }
280
281 static int
282@@ -612,7 +608,7 @@ file_delete(int id)
283 static void
284 file_success(int port, int value)
285 {
286- POKE2(&uxn.dev[port], value);
287+ POKE2(&dev[port], value);
288 }
289
290 /* file registers */
291@@ -643,7 +639,7 @@ datetime_dei(Uint8 addr)
292 case 0xc8: return t->tm_yday >> 8;
293 case 0xc9: return t->tm_yday;
294 case 0xca: return t->tm_isdst;
295- default: return uxn.dev[addr];
296+ default: return dev[addr];
297 }
298 }
299
300@@ -655,46 +651,46 @@ emu_dei(Uint8 addr)
301 {
302 switch(addr) {
303 /* System */
304- case 0x04: return uxn.wst.ptr;
305- case 0x05: return uxn.rst.ptr;
306+ case 0x04: return wst.ptr;
307+ case 0x05: return rst.ptr;
308 /* Console */
309 case CMD_LIVE:
310 case CMD_EXIT: check_child(); break;
311 }
312 if((addr & 0xf0) == 0xc0)
313 return datetime_dei(addr);
314- return uxn.dev[addr];
315+ return dev[addr];
316 }
317
318 void
319 emu_deo(Uint8 addr, Uint8 value)
320 {
321- uxn.dev[addr] = value;
322+ dev[addr] = value;
323 switch(addr) {
324 /* System */
325- case 0x03: system_expansion(PEEK2(uxn.dev + 2)); break;
326- case 0x04: uxn.wst.ptr = uxn.dev[4]; break;
327- case 0x05: uxn.rst.ptr = uxn.dev[5]; break;
328- case 0x0e: system_print("WST", &uxn.wst), system_print("RST", &uxn.rst); break;
329+ case 0x03: system_expansion(PEEK2(dev + 2)); break;
330+ case 0x04: wst.ptr = dev[4]; break;
331+ case 0x05: rst.ptr = dev[5]; break;
332+ case 0x0e: system_print("WST", &wst), system_print("RST", &rst); break;
333 /* Console */
334- case 0x11: console_vector = PEEK2(&uxn.dev[0x10]); return;
335- case 0x18: fputc(uxn.dev[0x18], stdout), fflush(stdout); break;
336- case 0x19: fputc(uxn.dev[0x19], stderr), fflush(stderr); break;
337+ case 0x11: console_vector = PEEK2(&dev[0x10]); return;
338+ case 0x18: fputc(dev[0x18], stdout), fflush(stdout); break;
339+ case 0x19: fputc(dev[0x19], stderr), fflush(stderr); break;
340 case 0x1f: start_fork(); break;
341 /* File 1 */
342- case 0xab: rL1 = PEEK2(&uxn.dev[0xaa]); break;
343- case 0xa5: file_success(0xa2, file_stat(0, PEEK2(&uxn.dev[0xa4]), rL1)); break;
344+ case 0xab: rL1 = PEEK2(&dev[0xaa]); break;
345+ case 0xa5: file_success(0xa2, file_stat(0, PEEK2(&dev[0xa4]), rL1)); break;
346 case 0xa6: file_success(0xa2, file_delete(0)); break;
347- case 0xa9: file_success(0xa2, file_init(0, PEEK2(&uxn.dev[0xa8]))); break;
348- case 0xad: file_success(0xa2, file_read(0, PEEK2(&uxn.dev[0xac]), rL1)); break;
349- case 0xaf: file_success(0xa2, file_write(0, PEEK2(&uxn.dev[0xae]), rL1, uxn.dev[0xa7])); break;
350+ case 0xa9: file_success(0xa2, file_init(0, PEEK2(&dev[0xa8]))); break;
351+ case 0xad: file_success(0xa2, file_read(0, PEEK2(&dev[0xac]), rL1)); break;
352+ case 0xaf: file_success(0xa2, file_write(0, PEEK2(&dev[0xae]), rL1, dev[0xa7])); break;
353 /* File 2 */
354- case 0xbb: rL2 = PEEK2(&uxn.dev[0xba]); break;
355- case 0xb5: file_success(0xb2, file_stat(1, PEEK2(&uxn.dev[0xb4]), rL2)); break;
356+ case 0xbb: rL2 = PEEK2(&dev[0xba]); break;
357+ case 0xb5: file_success(0xb2, file_stat(1, PEEK2(&dev[0xb4]), rL2)); break;
358 case 0xb6: file_success(0xb2, file_delete(1)); break;
359- case 0xb9: file_success(0xb2, file_init(1, PEEK2(&uxn.dev[0xb8]))); break;
360- case 0xbd: file_success(0xb2, file_read(1, PEEK2(&uxn.dev[0xbc]), rL2)); break;
361- case 0xbf: file_success(0xb2, file_write(1, PEEK2(&uxn.dev[0xbe]), rL2, uxn.dev[0xb7])); break;
362+ case 0xb9: file_success(0xb2, file_init(1, PEEK2(&dev[0xb8]))); break;
363+ case 0xbd: file_success(0xb2, file_read(1, PEEK2(&dev[0xbc]), rL2)); break;
364+ case 0xbf: file_success(0xb2, file_write(1, PEEK2(&dev[0xbe]), rL2, dev[0xb7])); break;
365 }
366 }
367
368@@ -717,7 +713,7 @@ main(int argc, char **argv)
369 console_input(*p++, CONSOLE_ARG);
370 console_input('\n', i == argc - 1 ? CONSOLE_END : CONSOLE_EOA);
371 }
372- while(!uxn.dev[0x0f] && console_input(fgetc(stdin), 1));
373+ while(!dev[0x0f] && console_input(fgetc(stdin), 1));
374 }
375- return uxn.dev[0x0f] & 0x7f;
376+ return dev[0x0f] & 0x7f;
377 }