commit 29f057b
Devine Lu Linvega
·
2023-04-17 18:36:17 +0000 UTC
parent b733d4d
Brought changes for the console
7 files changed,
+82,
-96
M
build.sh
+1,
-1
1@@ -2,7 +2,7 @@
2
3 RELEASE_FLAGS="-Os -DNDEBUG -g0 -s"
4 DEBUG_FLAGS="-std=c89 -D_POSIX_C_SOURCE=199309L -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined"
5-CORE_DEVICES="src/uxn.c src/devices/system.c src/devices/console.c src/devices/file.c src/devices/datetime.c"
6+CORE_DEVICES="src/uxn.c src/devices/system.c src/devices/file.c src/devices/datetime.c"
7 EMU_INC="${CORE_DEVICES} src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/uxn11.c -o bin/uxn11 -lX11"
8 CLI_INC="${CORE_DEVICES} src/uxncli.c -o bin/uxncli"
9
+0,
-35
1@@ -1,35 +0,0 @@
2-#include <stdio.h>
3-#include <stdlib.h>
4-
5-#include "../uxn.h"
6-#include "console.h"
7-
8-/*
9-Copyright (c) 2022-2023 Devine Lu Linvega, Andrew Alderwick
10-
11-Permission to use, copy, modify, and distribute this software for any
12-purpose with or without fee is hereby granted, provided that the above
13-copyright notice and this permission notice appear in all copies.
14-
15-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16-WITH REGARD TO THIS SOFTWARE.
17-*/
18-
19-int
20-console_input(Uxn *u, char c)
21-{
22- Uint8 *d = &u->dev[0x10];
23- d[0x02] = c;
24- return uxn_eval(u, PEEK2(d));
25-}
26-
27-void
28-console_deo(Uint8 *d, Uint8 port)
29-{
30- FILE *fd = port == 0x8 ? stdout : port == 0x9 ? stderr :
31- 0;
32- if(fd) {
33- fputc(d[port], fd);
34- fflush(fd);
35- }
36-}
+0,
-13
1@@ -1,13 +0,0 @@
2-/*
3-Copyright (c) 2021 Devine Lu Linvega, Andrew Alderwick
4-
5-Permission to use, copy, modify, and distribute this software for any
6-purpose with or without fee is hereby granted, provided that the above
7-copyright notice and this permission notice appear in all copies.
8-
9-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10-WITH REGARD TO THIS SOFTWARE.
11-*/
12-
13-int console_input(Uxn *u, char c);
14-void console_deo(Uint8 *d, Uint8 port);
+59,
-30
1@@ -35,10 +35,10 @@ system_print(Stack *s, char *name)
2 static void
3 system_cmd(Uint8 *ram, Uint16 addr)
4 {
5- if(ram[addr] == 0x01) {
6- Uint16 i, length = PEEK16(ram + addr + 1);
7- Uint16 a_page = PEEK16(ram + addr + 1 + 2), a_addr = PEEK16(ram + addr + 1 + 4);
8- Uint16 b_page = PEEK16(ram + addr + 1 + 6), b_addr = PEEK16(ram + addr + 1 + 8);
9+ if(ram[addr] == 0x1) {
10+ Uint16 i, length = PEEK2(ram + addr + 1);
11+ Uint16 a_page = PEEK2(ram + addr + 1 + 2), a_addr = PEEK2(ram + addr + 1 + 4);
12+ Uint16 b_page = PEEK2(ram + addr + 1 + 6), b_addr = PEEK2(ram + addr + 1 + 8);
13 int src = (a_page % RAM_PAGES) * 0x10000, dst = (b_page % RAM_PAGES) * 0x10000;
14 for(i = 0; i < length; i++)
15 ram[dst + (Uint16)(b_addr + i)] = ram[src + (Uint16)(a_addr + i)];
16@@ -49,35 +49,10 @@ int
17 system_error(char *msg, const char *err)
18 {
19 fprintf(stderr, "%s: %s\n", msg, err);
20- return 1;
21-}
22-
23-int
24-uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr)
25-{
26- Uint8 *d = &u->dev[0x00];
27- Uint16 handler = PEEK2(d);
28- if(handler) {
29- u->wst.ptr = 4;
30- u->wst.dat[0] = addr >> 0x8;
31- u->wst.dat[1] = addr & 0xff;
32- u->wst.dat[2] = instr;
33- u->wst.dat[3] = err;
34- return uxn_eval(u, handler);
35- } else {
36- system_inspect(u);
37- fprintf(stderr, "%s %s, by %02x at 0x%04x.\n", (instr & 0x40) ? "Return-stack" : "Working-stack", errors[err - 1], instr, addr);
38- }
39+ fflush(stderr);
40 return 0;
41 }
42
43-void
44-system_inspect(Uxn *u)
45-{
46- system_print(&u->wst, "wst");
47- system_print(&u->rst, "rst");
48-}
49-
50 int
51 system_load(Uxn *u, char *filename)
52 {
53@@ -92,6 +67,13 @@ system_load(Uxn *u, char *filename)
54 return 1;
55 }
56
57+void
58+system_inspect(Uxn *u)
59+{
60+ system_print(&u->wst, "wst");
61+ system_print(&u->rst, "rst");
62+}
63+
64 /* IO */
65
66 void
67@@ -106,3 +88,50 @@ system_deo(Uxn *u, Uint8 *d, Uint8 port)
68 break;
69 }
70 }
71+
72+/* Errors */
73+
74+int
75+uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr)
76+{
77+ Uint8 *d = &u->dev[0];
78+ Uint16 handler = PEEK2(d);
79+ if(handler) {
80+ u->wst.ptr = 4;
81+ u->wst.dat[0] = addr >> 0x8;
82+ u->wst.dat[1] = addr & 0xff;
83+ u->wst.dat[2] = instr;
84+ u->wst.dat[3] = err;
85+ return uxn_eval(u, handler);
86+ } else {
87+ system_inspect(u);
88+ fprintf(stderr, "%s %s, by %02x at 0x%04x.\n", (instr & 0x40) ? "Return-stack" : "Working-stack", errors[err - 1], instr, addr);
89+ }
90+ return 0;
91+}
92+
93+/* Console */
94+
95+int
96+console_input(Uxn *u, char c, int type)
97+{
98+ Uint8 *d = &u->dev[0x10];
99+ d[0x2] = c;
100+ d[0x7] = type;
101+ return uxn_eval(u, PEEK2(d));
102+}
103+
104+void
105+console_deo(Uint8 *d, Uint8 port)
106+{
107+ switch(port) {
108+ case 0x8:
109+ fputc(d[port], stdout);
110+ fflush(stdout);
111+ return;
112+ case 0x9:
113+ fputc(d[port], stderr);
114+ fflush(stderr);
115+ return;
116+ }
117+}
+9,
-3
1@@ -10,9 +10,15 @@ WITH REGARD TO THIS SOFTWARE.
2 */
3
4 #define RAM_PAGES 0x10
5-#define PEEK16(d) ((d)[0] << 8 | (d)[1])
6
7-int system_error(char *msg, const char *err);
8+#define CONSOLE_STD 0x1
9+#define CONSOLE_ARG 0x2
10+#define CONSOLE_EOA 0x3
11+#define CONSOLE_END 0x4
12+
13 int system_load(Uxn *u, char *filename);
14-void system_deo(Uxn *u, Uint8 *d, Uint8 port);
15 void system_inspect(Uxn *u);
16+int system_error(char *msg, const char *err);
17+void system_deo(Uxn *u, Uint8 *d, Uint8 port);
18+int console_input(Uxn *u, char c, int type);
19+void console_deo(Uint8 *d, Uint8 port);
+3,
-4
1@@ -9,7 +9,6 @@
2
3 #include "uxn.h"
4 #include "devices/system.h"
5-#include "devices/console.h"
6 #include "devices/screen.h"
7 #include "devices/controller.h"
8 #include "devices/mouse.h"
9@@ -213,8 +212,8 @@ main(int argc, char **argv)
10 /* console vector */
11 for(i = 2; i < argc; i++) {
12 char *p = argv[i];
13- while(*p) console_input(&u, *p++);
14- console_input(&u, '\n');
15+ while(*p) console_input(&u, *p++, CONSOLE_ARG);
16+ console_input(&u, '\n', i == argc ? CONSOLE_END : CONSOLE_EOA);
17 }
18 /* timer */
19 fds[0].fd = XConnectionNumber(display);
20@@ -236,7 +235,7 @@ main(int argc, char **argv)
21 n = read(fds[2].fd, coninp, CONINBUFSIZE - 1);
22 coninp[n] = 0;
23 for(i = 0; i < n; i++)
24- console_input(&u, coninp[i]);
25+ console_input(&u, coninp[i], CONSOLE_STD);
26 }
27 if(uxn_screen.fg.changed || uxn_screen.bg.changed)
28 emu_draw();
+10,
-10
1@@ -3,7 +3,6 @@
2
3 #include "uxn.h"
4 #include "devices/system.h"
5-#include "devices/console.h"
6 #include "devices/file.h"
7 #include "devices/datetime.h"
8
9@@ -46,23 +45,24 @@ int
10 main(int argc, char **argv)
11 {
12 Uxn u;
13- int i;
14- if(argc < 2)
15+ int i = 1;
16+ if(i == argc)
17 return system_error("usage", "uxncli game.rom args");
18 if(!uxn_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8))))
19- return system_error("boot", "Failed");
20- if(!system_load(&u, argv[1]))
21- return system_error("load", "Failed");
22+ return system_error("Boot", "Failed");
23+ if(!system_load(&u, argv[i++]))
24+ return system_error("Load", "Failed");
25+ u.dev[0x17] = i != argc;
26 if(!uxn_eval(&u, PAGE_PROGRAM))
27 return u.dev[0x0f] & 0x7f;
28- for(i = 2; i < argc; i++) {
29+ for(; i < argc; i++) {
30 char *p = argv[i];
31- while(*p) console_input(&u, *p++);
32- console_input(&u, '\n');
33+ while(*p) console_input(&u, *p++, CONSOLE_ARG);
34+ console_input(&u, '\n', i == argc - 1 ? CONSOLE_END : CONSOLE_EOA);
35 }
36 while(!u.dev[0x0f]) {
37 int c = fgetc(stdin);
38- if(c != EOF) console_input(&u, (Uint8)c);
39+ if(c != EOF) console_input(&u, (Uint8)c, CONSOLE_STD);
40 }
41 return u.dev[0x0f] & 0x7f;
42 }