uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

commit 1e61770acc4f780a63ae53e20c2a67d6c8ae704b
parent b324bac097bc5b8acf77e8606f71ac6609bb893a
Author: Devine Lu Linvega <aliceffekt@gmail.com>
Date:   Sun, 19 Jan 2025 11:46:41 -0800

(system.c) Housekeeping

Diffstat:
Msrc/devices/system.c | 76+++++++++++++++++++++++++---------------------------------------------------
Msrc/devices/system.h | 6++----
Msrc/uxncli.c | 2+-
Msrc/uxnemu.c | 26+++++++++-----------------
4 files changed, 37 insertions(+), 73 deletions(-)

diff --git a/src/devices/system.c b/src/devices/system.c @@ -6,7 +6,7 @@ #include "system.h" /* -Copyright (c) 2022-2024 Devine Lu Linvega, Andrew Alderwick +Copyright (c) 2022-2025 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 @@ -16,31 +16,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ -char *boot_rom; - -static void -system_zero(int soft) -{ - 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; -} - -static int -system_load(Uint8 *ram, char *filename) -{ - FILE *f = fopen(filename, "rb"); - if(f) { - int i = 0, l = fread(ram, 0x10000 - PAGE_PROGRAM, 1, f); - while(l && ++i < RAM_PAGES) - l = fread(ram + 0x10000 * i - PAGE_PROGRAM, 0x10000, 1, f); - fclose(f); - } - return !!f; -} +char *boot_path; static void system_print(Stack *s) @@ -51,25 +27,6 @@ system_print(Stack *s) fprintf(stderr, "< \n"); } -void -system_inspect(void) -{ - fprintf(stderr, "WST "), system_print(&uxn.wst); - fprintf(stderr, "RST "), system_print(&uxn.rst); -} - -void -system_image(void) -{ - int i, len = 0; - FILE *f = fopen("image.rom", "wb"); - for(i = PAGE_PROGRAM; i < 0x10000; i++) - if(uxn.ram[i]) - len = i; - fwrite(uxn.ram + PAGE_PROGRAM, len - PAGE_PROGRAM + 1, 1, f), fclose(f); - printf("Saved image.rom, %d bytes\n", len); -} - int system_error(char *msg, const char *err) { @@ -77,19 +34,35 @@ system_error(char *msg, const char *err) return 0; } +static int +system_load(Uint8 *ram, char *rom_path) +{ + FILE *f = fopen(rom_path, "rb"); + if(f) { + int i = 0, l = fread(ram, 0x10000 - PAGE_PROGRAM, 1, f); + while(l && ++i < RAM_PAGES) + l = fread(ram + 0x10000 * i - PAGE_PROGRAM, 0x10000, 1, f); + fclose(f); + } + return !!f; +} + void system_reboot(int soft) { - system_zero(soft); - if(system_load(&uxn.ram[PAGE_PROGRAM], boot_rom)) + 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); } int -system_boot(Uint8 *ram, char *boot) +system_boot(Uint8 *ram, char *rom_path) { - uxn.ram = ram, boot_rom = boot; - return system_load(uxn.ram + PAGE_PROGRAM, boot); + uxn.ram = ram, boot_path = rom_path; + return system_load(uxn.ram + PAGE_PROGRAM, rom_path); } /* IO */ @@ -144,7 +117,8 @@ system_deo(Uint8 port) uxn.rst.ptr = uxn.dev[5]; break; case 0xe: - system_inspect(); + fprintf(stderr, "WST "), system_print(&uxn.wst); + fprintf(stderr, "RST "), system_print(&uxn.rst); break; } } diff --git a/src/devices/system.h b/src/devices/system.h @@ -1,5 +1,5 @@ /* -Copyright (c) 2024 Devine Lu Linvega, Andrew Alderwick +Copyright (c) 2022-2025 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 @@ -12,10 +12,8 @@ WITH REGARD TO THIS SOFTWARE. #define RAM_PAGES 0x10 void system_reboot(int soft); -void system_inspect(void); -void system_image(void); int system_error(char *msg, const char *err); -int system_boot(Uint8 *ram, char *rom); +int system_boot(Uint8 *ram, char *rompath); Uint8 system_dei(Uint8 addr); void system_deo(Uint8 addr); \ No newline at end of file diff --git a/src/uxncli.c b/src/uxncli.c @@ -47,7 +47,7 @@ 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, 18 Jan 2025.\n"); + return !fprintf(stdout, "Uxn(cli) - Varvara Emulator, 19 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++])) diff --git a/src/uxnemu.c b/src/uxnemu.c @@ -30,7 +30,7 @@ #pragma clang diagnostic pop /* -Copyright (c) 2021-2023 Devine Lu Linvega, Andrew Alderwick +Copyright (c) 2021-2025 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 @@ -184,14 +184,6 @@ set_borderless(int value) SDL_SetWindowBordered(emu_window, !value); } -static void -set_debugger(int value) -{ - uxn.dev[0x0e] = value; - screen_fill(uxn_screen.fg, 0); - screen_redraw(); -} - /* emulator primitives */ int @@ -350,7 +342,7 @@ handle_events(void) else if(event.key.keysym.sym == SDLK_F1) set_zoom(zoom == 3 ? 1 : zoom + 1, 1); else if(event.key.keysym.sym == SDLK_F2) - set_debugger(!uxn.dev[0x0e]); + emu_deo(0xe, 0x1); else if(event.key.keysym.sym == SDLK_F3) uxn.dev[0x0f] = 0xff; else if(event.key.keysym.sym == SDLK_F4) @@ -398,7 +390,7 @@ handle_events(void) } static int -emu_run(char *rom) +emu_run(char *rom_path) { Uint64 next_refresh = 0; Uint64 frame_interval = SDL_GetPerformanceFrequency() / 60; @@ -406,7 +398,7 @@ emu_run(char *rom) window_created = 1; if(fullscreen) window_flags = window_flags | SDL_WINDOW_FULLSCREEN_DESKTOP; - emu_window = SDL_CreateWindow(rom, + emu_window = SDL_CreateWindow(rom_path, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, (uxn_screen.width + PAD2) * zoom, @@ -461,11 +453,11 @@ int main(int argc, char **argv) { int i = 1; - char *rom; + char *rom_path; /* flags */ if(argc > 1 && argv[i][0] == '-') { if(!strcmp(argv[i], "-v")) - return system_error("Uxn(gui) - Varvara Emulator", "18 Jan 2025."); + return system_error("Uxn(gui) - Varvara Emulator", "19 Jan 2025."); else if(!strcmp(argv[i], "-2x")) set_zoom(2, 0); else if(!strcmp(argv[i], "-3x")) @@ -475,8 +467,8 @@ main(int argc, char **argv) i++; } /* start */ - rom = i == argc ? "boot.rom" : argv[i++]; - if(!system_boot((Uint8 *)calloc(0x10000 * RAM_PAGES + 1, sizeof(Uint8)), rom)) + rom_path = i == argc ? "boot.rom" : argv[i++]; + if(!system_boot((Uint8 *)calloc(0x10000 * RAM_PAGES + 1, sizeof(Uint8)), rom_path)) return system_error("usage:", "uxnemu [-v | -f | -2x | -3x] file.rom [args...]"); if(!emu_init()) return system_error("Init", "Failed to initialize varvara."); @@ -484,7 +476,7 @@ main(int argc, char **argv) uxn.dev[0x17] = argc > i; if(uxn_eval(PAGE_PROGRAM)) { console_listen(i, argc, argv); - emu_run(rom); + emu_run(rom_path); } return emu_end(); }