From 29f057ba44fce3f5a31b1549775c1234c4294e76 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Mon, 17 Apr 2023 11:36:17 -0700 Subject: [PATCH] Brought changes for the console --- build.sh | 2 +- src/devices/console.c | 35 ----------------- src/devices/console.h | 13 ------- src/devices/system.c | 89 ++++++++++++++++++++++++++++--------------- src/devices/system.h | 12 ++++-- src/uxn11.c | 7 ++-- src/uxncli.c | 20 +++++----- 7 files changed, 82 insertions(+), 96 deletions(-) delete mode 100644 src/devices/console.c delete mode 100644 src/devices/console.h diff --git a/build.sh b/build.sh index e0af39d..ffb3960 100755 --- a/build.sh +++ b/build.sh @@ -2,7 +2,7 @@ RELEASE_FLAGS="-Os -DNDEBUG -g0 -s" 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" -CORE_DEVICES="src/uxn.c src/devices/system.c src/devices/console.c src/devices/file.c src/devices/datetime.c" +CORE_DEVICES="src/uxn.c src/devices/system.c src/devices/file.c src/devices/datetime.c" EMU_INC="${CORE_DEVICES} src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/uxn11.c -o bin/uxn11 -lX11" CLI_INC="${CORE_DEVICES} src/uxncli.c -o bin/uxncli" diff --git a/src/devices/console.c b/src/devices/console.c deleted file mode 100644 index bda96a2..0000000 --- a/src/devices/console.c +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include - -#include "../uxn.h" -#include "console.h" - -/* -Copyright (c) 2022-2023 Devine Lu Linvega, Andrew Alderwick - -Permission to use, copy, modify, and distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE. -*/ - -int -console_input(Uxn *u, char c) -{ - Uint8 *d = &u->dev[0x10]; - d[0x02] = c; - return uxn_eval(u, PEEK2(d)); -} - -void -console_deo(Uint8 *d, Uint8 port) -{ - FILE *fd = port == 0x8 ? stdout : port == 0x9 ? stderr : - 0; - if(fd) { - fputc(d[port], fd); - fflush(fd); - } -} \ No newline at end of file diff --git a/src/devices/console.h b/src/devices/console.h deleted file mode 100644 index 965daf8..0000000 --- a/src/devices/console.h +++ /dev/null @@ -1,13 +0,0 @@ -/* -Copyright (c) 2021 Devine Lu Linvega, Andrew Alderwick - -Permission to use, copy, modify, and distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE. -*/ - -int console_input(Uxn *u, char c); -void console_deo(Uint8 *d, Uint8 port); diff --git a/src/devices/system.c b/src/devices/system.c index da54ab2..cda4e07 100644 --- a/src/devices/system.c +++ b/src/devices/system.c @@ -35,10 +35,10 @@ system_print(Stack *s, char *name) static void system_cmd(Uint8 *ram, Uint16 addr) { - if(ram[addr] == 0x01) { - Uint16 i, length = PEEK16(ram + addr + 1); - Uint16 a_page = PEEK16(ram + addr + 1 + 2), a_addr = PEEK16(ram + addr + 1 + 4); - Uint16 b_page = PEEK16(ram + addr + 1 + 6), b_addr = PEEK16(ram + addr + 1 + 8); + if(ram[addr] == 0x1) { + Uint16 i, length = PEEK2(ram + addr + 1); + Uint16 a_page = PEEK2(ram + addr + 1 + 2), a_addr = PEEK2(ram + addr + 1 + 4); + Uint16 b_page = PEEK2(ram + addr + 1 + 6), b_addr = PEEK2(ram + addr + 1 + 8); int src = (a_page % RAM_PAGES) * 0x10000, dst = (b_page % RAM_PAGES) * 0x10000; for(i = 0; i < length; i++) ram[dst + (Uint16)(b_addr + i)] = ram[src + (Uint16)(a_addr + i)]; @@ -49,35 +49,10 @@ int system_error(char *msg, const char *err) { fprintf(stderr, "%s: %s\n", msg, err); - return 1; -} - -int -uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr) -{ - Uint8 *d = &u->dev[0x00]; - Uint16 handler = PEEK2(d); - if(handler) { - u->wst.ptr = 4; - u->wst.dat[0] = addr >> 0x8; - u->wst.dat[1] = addr & 0xff; - u->wst.dat[2] = instr; - u->wst.dat[3] = err; - return uxn_eval(u, handler); - } else { - system_inspect(u); - fprintf(stderr, "%s %s, by %02x at 0x%04x.\n", (instr & 0x40) ? "Return-stack" : "Working-stack", errors[err - 1], instr, addr); - } + fflush(stderr); return 0; } -void -system_inspect(Uxn *u) -{ - system_print(&u->wst, "wst"); - system_print(&u->rst, "rst"); -} - int system_load(Uxn *u, char *filename) { @@ -92,6 +67,13 @@ system_load(Uxn *u, char *filename) return 1; } +void +system_inspect(Uxn *u) +{ + system_print(&u->wst, "wst"); + system_print(&u->rst, "rst"); +} + /* IO */ void @@ -106,3 +88,50 @@ system_deo(Uxn *u, Uint8 *d, Uint8 port) break; } } + +/* Errors */ + +int +uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr) +{ + Uint8 *d = &u->dev[0]; + Uint16 handler = PEEK2(d); + if(handler) { + u->wst.ptr = 4; + u->wst.dat[0] = addr >> 0x8; + u->wst.dat[1] = addr & 0xff; + u->wst.dat[2] = instr; + u->wst.dat[3] = err; + return uxn_eval(u, handler); + } else { + system_inspect(u); + fprintf(stderr, "%s %s, by %02x at 0x%04x.\n", (instr & 0x40) ? "Return-stack" : "Working-stack", errors[err - 1], instr, addr); + } + return 0; +} + +/* Console */ + +int +console_input(Uxn *u, char c, int type) +{ + Uint8 *d = &u->dev[0x10]; + d[0x2] = c; + d[0x7] = type; + return uxn_eval(u, PEEK2(d)); +} + +void +console_deo(Uint8 *d, Uint8 port) +{ + switch(port) { + case 0x8: + fputc(d[port], stdout); + fflush(stdout); + return; + case 0x9: + fputc(d[port], stderr); + fflush(stderr); + return; + } +} diff --git a/src/devices/system.h b/src/devices/system.h index cbd9217..9059736 100644 --- a/src/devices/system.h +++ b/src/devices/system.h @@ -10,9 +10,15 @@ WITH REGARD TO THIS SOFTWARE. */ #define RAM_PAGES 0x10 -#define PEEK16(d) ((d)[0] << 8 | (d)[1]) -int system_error(char *msg, const char *err); +#define CONSOLE_STD 0x1 +#define CONSOLE_ARG 0x2 +#define CONSOLE_EOA 0x3 +#define CONSOLE_END 0x4 + int system_load(Uxn *u, char *filename); -void system_deo(Uxn *u, Uint8 *d, Uint8 port); void system_inspect(Uxn *u); +int system_error(char *msg, const char *err); +void system_deo(Uxn *u, Uint8 *d, Uint8 port); +int console_input(Uxn *u, char c, int type); +void console_deo(Uint8 *d, Uint8 port); diff --git a/src/uxn11.c b/src/uxn11.c index 6877b5d..f728071 100644 --- a/src/uxn11.c +++ b/src/uxn11.c @@ -9,7 +9,6 @@ #include "uxn.h" #include "devices/system.h" -#include "devices/console.h" #include "devices/screen.h" #include "devices/controller.h" #include "devices/mouse.h" @@ -213,8 +212,8 @@ main(int argc, char **argv) /* console vector */ for(i = 2; i < argc; i++) { char *p = argv[i]; - while(*p) console_input(&u, *p++); - console_input(&u, '\n'); + while(*p) console_input(&u, *p++, CONSOLE_ARG); + console_input(&u, '\n', i == argc ? CONSOLE_END : CONSOLE_EOA); } /* timer */ fds[0].fd = XConnectionNumber(display); @@ -236,7 +235,7 @@ main(int argc, char **argv) n = read(fds[2].fd, coninp, CONINBUFSIZE - 1); coninp[n] = 0; for(i = 0; i < n; i++) - console_input(&u, coninp[i]); + console_input(&u, coninp[i], CONSOLE_STD); } if(uxn_screen.fg.changed || uxn_screen.bg.changed) emu_draw(); diff --git a/src/uxncli.c b/src/uxncli.c index 009467b..56d0b8e 100644 --- a/src/uxncli.c +++ b/src/uxncli.c @@ -3,7 +3,6 @@ #include "uxn.h" #include "devices/system.h" -#include "devices/console.h" #include "devices/file.h" #include "devices/datetime.h" @@ -46,23 +45,24 @@ int main(int argc, char **argv) { Uxn u; - int i; - if(argc < 2) + int i = 1; + if(i == argc) return system_error("usage", "uxncli game.rom args"); if(!uxn_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)))) - return system_error("boot", "Failed"); - if(!system_load(&u, argv[1])) - return system_error("load", "Failed"); + return system_error("Boot", "Failed"); + if(!system_load(&u, argv[i++])) + return system_error("Load", "Failed"); + u.dev[0x17] = i != argc; if(!uxn_eval(&u, PAGE_PROGRAM)) return u.dev[0x0f] & 0x7f; - for(i = 2; i < argc; i++) { + for(; i < argc; i++) { char *p = argv[i]; - while(*p) console_input(&u, *p++); - console_input(&u, '\n'); + while(*p) console_input(&u, *p++, CONSOLE_ARG); + console_input(&u, '\n', i == argc - 1 ? CONSOLE_END : CONSOLE_EOA); } while(!u.dev[0x0f]) { int c = fgetc(stdin); - if(c != EOF) console_input(&u, (Uint8)c); + if(c != EOF) console_input(&u, (Uint8)c, CONSOLE_STD); } return u.dev[0x0f] & 0x7f; }