From 5c0a638e297a4a1cf200debbf6cb87018df192de Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Tue, 21 Jan 2025 09:46:04 -0800 Subject: [PATCH] Simplified system booting --- src/devices/console.c | 5 +++-- src/devices/console.h | 2 +- src/devices/system.c | 39 +++++++++++++++++++++------------------ src/devices/system.h | 4 ++-- src/uxncli.c | 17 ++++------------- src/uxnemu.c | 5 ++--- 6 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/devices/console.c b/src/devices/console.c index 5c548c1..18bd0ac 100644 --- a/src/devices/console.c +++ b/src/devices/console.c @@ -25,11 +25,12 @@ console_input(int c, int type) } void -console_listen(int i, int argc, char **argv) +console_arguments(int i, int argc, char **argv) { for(; i < argc; i++) { char *p = argv[i]; - while(*p) console_input(*p++, CONSOLE_ARG); + while(*p) + console_input(*p++, CONSOLE_ARG); console_input('\n', i == argc - 1 ? CONSOLE_END : CONSOLE_EOA); } } diff --git a/src/devices/console.h b/src/devices/console.h index c78f349..e462f3d 100644 --- a/src/devices/console.h +++ b/src/devices/console.h @@ -15,6 +15,6 @@ WITH REGARD TO THIS SOFTWARE. #define CONSOLE_END 0x4 int console_input(int c, int type); -void console_listen(int i, int argc, char **argv); +void console_arguments(int i, int argc, char **argv); Uint8 console_dei(Uint8 addr); void console_deo(Uint8 addr); diff --git a/src/devices/system.c b/src/devices/system.c index fc87a11..826e4d1 100644 --- a/src/devices/system.c +++ b/src/devices/system.c @@ -27,13 +27,6 @@ system_print(Stack *s) fprintf(stderr, "< \n"); } -int -system_error(char *msg, const char *err) -{ - fprintf(stderr, "%s: %s\n", msg, err), fflush(stderr); - return 0; -} - static int system_load(Uint8 *ram, char *rom_path) { @@ -47,22 +40,32 @@ system_load(Uint8 *ram, char *rom_path) return !!f; } -void -system_reboot(int soft) +int +system_error(char *msg, const char *err) { - int i; - for(i = soft ? 0x100 : 0; i < 0x10000; i++) uxn.ram[i] = 0; - for(i = 0x0; i < 0x100; i++) uxn.dev[i] = 0; - uxn.wst.ptr = uxn.rst.ptr = 0; - if(system_load(&uxn.ram[PAGE_PROGRAM], boot_path)) - uxn_eval(PAGE_PROGRAM); + fprintf(stderr, "%s: %s\n", msg, err), fflush(stderr); + return 0; } int -system_boot(Uint8 *ram, char *rom_path) +system_boot(Uint8 *ram, char *rom_path, int has_args) { - uxn.ram = ram, boot_path = rom_path; - return system_load(uxn.ram + PAGE_PROGRAM, rom_path); + uxn.ram = ram; + boot_path = rom_path; + uxn.dev[0x17] = has_args; + if(ram && system_load(uxn.ram + PAGE_PROGRAM, rom_path)) + return uxn_eval(PAGE_PROGRAM); + return 0; +} + +int +system_reboot(int soft) +{ + int i; + for(i = 0x0; i < 0x100; i++) uxn.dev[i] = 0; + for(i = soft ? 0x100 : 0; i < 0x10000; i++) uxn.ram[i] = 0; + uxn.wst.ptr = uxn.rst.ptr = 0; + return system_boot(uxn.ram, boot_path, 0); } /* IO */ diff --git a/src/devices/system.h b/src/devices/system.h index 62053cb..1590d8e 100644 --- a/src/devices/system.h +++ b/src/devices/system.h @@ -11,9 +11,9 @@ WITH REGARD TO THIS SOFTWARE. #define RAM_PAGES 0x10 -void system_reboot(int soft); int system_error(char *msg, const char *err); -int system_boot(Uint8 *ram, char *rompath); +int system_boot(Uint8 *ram, char *rom_path, int has_args); +int system_reboot(int soft); Uint8 system_dei(Uint8 addr); void system_deo(Uint8 addr); diff --git a/src/uxncli.c b/src/uxncli.c index 7b8ed84..b9ee4d7 100644 --- a/src/uxncli.c +++ b/src/uxncli.c @@ -47,21 +47,12 @@ main(int argc, char **argv) { int i = 1; if(argc == 2 && argv[1][0] == '-' && argv[1][1] == 'v') - return !fprintf(stdout, "Uxn(cli) - Varvara Emulator, 19 Jan 2025.\n"); + return !fprintf(stdout, "Uxn(cli) - Varvara Emulator, 20 Jan 2025.\n"); else if(argc == 1) return !fprintf(stdout, "usage: %s [-v] file.rom [args..]\n", argv[0]); - else if(!system_boot((Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++])) + else if(!system_boot((Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++], argc > 2)) return !fprintf(stdout, "Could not load %s.\n", argv[i - 1]); - uxn.dev[0x17] = argc > 2; - if(uxn_eval(PAGE_PROGRAM) && uxn.dev[0x10]) { - /* arguments input */ - for(; i < argc; i++) { - char *p = argv[i]; - while(*p) console_input(*p++, CONSOLE_ARG); - console_input('\n', i == argc - 1 ? CONSOLE_END : CONSOLE_EOA); - } - /* console input */ - while(!uxn.dev[0x0f] && console_input(fgetc(stdin), 0x1)); - } + console_arguments(i, argc, argv); + while(!uxn.dev[0x0f] && console_input(fgetc(stdin), 0x1)); return uxn.dev[0x0f] & 0x7f; } diff --git a/src/uxnemu.c b/src/uxnemu.c index bd1b4df..a222e97 100644 --- a/src/uxnemu.c +++ b/src/uxnemu.c @@ -468,14 +468,13 @@ main(int argc, char **argv) } /* start */ rom_path = i == argc ? "boot.rom" : argv[i++]; - if(!system_boot((Uint8 *)calloc(0x10000 * RAM_PAGES + 1, sizeof(Uint8)), rom_path)) + if(!system_boot((Uint8 *)calloc(0x10000 * RAM_PAGES + 1, sizeof(Uint8)), rom_path, argc > i)) return system_error("usage:", "uxnemu [-v | -f | -2x | -3x] file.rom [args...]"); if(!emu_init()) return system_error("Init", "Failed to initialize varvara."); /* loop */ - uxn.dev[0x17] = argc > i; if(uxn_eval(PAGE_PROGRAM)) { - console_listen(i, argc, argv); + console_arguments(i, argc, argv); emu_run(rom_path); } return emu_end();