uxn

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

commit e20b572c2055a8205c386a4d3950bde66f3162cd
parent b0b60ca20bc20c3b82ac20001f851ae9e6db1d76
Author: Devine Lu Linvega <aliceffekt@gmail.com>
Date:   Tue, 31 Jan 2023 09:38:06 -0800

Moved all system memory functions into system.c

Diffstat:
Msrc/devices/file.c | 16----------------
Msrc/devices/file.h | 1-
Msrc/devices/system.c | 60++++++++++++++++++++++++++++++++++++++----------------------
Msrc/devices/system.h | 10++++++----
Msrc/uxncli.c | 4++--
Msrc/uxnemu.c | 4++--
6 files changed, 48 insertions(+), 47 deletions(-)

diff --git a/src/devices/file.c b/src/devices/file.c @@ -286,19 +286,3 @@ file_dei(Uint8 id, Uint8 *d, Uint8 port) } return d[port]; } - -/* Boot */ - -int -load_rom(Uxn *u, char *filename) -{ - int l, i = 0; - FILE *f = fopen(filename, "rb"); - if(!f) - return 0; - l = fread(&u->ram[PAGE_PROGRAM], 1, 0x10000 - PAGE_PROGRAM, f); - while(l && ++i < 15) - l = fread(u->ram + 0x10000 * i, 1, 0x10000, f); - fclose(f); - return 1; -} diff --git a/src/devices/file.h b/src/devices/file.h @@ -14,4 +14,3 @@ WITH REGARD TO THIS SOFTWARE. void file_deo(Uint8 id, Uint8 *ram, Uint8 *d, Uint8 port); Uint8 file_dei(Uint8 id, Uint8 *d, Uint8 port); -int load_rom(Uxn *u, char *filename); diff --git a/src/devices/system.c b/src/devices/system.c @@ -39,31 +39,11 @@ system_inspect(Uxn *u) system_print(u->rst, "rst"); } -int -uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr) -{ - Uint8 *d = &u->dev[0x00]; - Uint16 handler = GETVEC(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; -} - -/* MMU */ +/* RAM */ Uint8 * -mmu_init(Mmu *m, Uint16 pages) +system_init(Mmu *m, Uint16 pages) { - m->length = pages; m->pages = (Uint8 *)calloc(0x10000 * pages, sizeof(Uint8)); return m->pages; } @@ -82,6 +62,20 @@ mmu_eval(Uint8 *ram, Uint16 addr) } } +int +system_load(Uxn *u, char *filename) +{ + int l, i = 0; + FILE *f = fopen(filename, "rb"); + if(!f) + return 0; + l = fread(&u->ram[PAGE_PROGRAM], 1, 0x10000 - PAGE_PROGRAM, f); + while(l && ++i < RAM_PAGES) + l = fread(u->ram + 0x10000 * i, 1, 0x10000, f); + fclose(f); + return 1; +} + /* IO */ void @@ -98,3 +92,25 @@ system_deo(Uxn *u, Uint8 *d, Uint8 port) break; } } + +/* Error */ + +int +uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr) +{ + Uint8 *d = &u->dev[0x00]; + Uint16 handler = GETVEC(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; +} + diff --git a/src/devices/system.h b/src/devices/system.h @@ -9,11 +9,13 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ -void system_inspect(Uxn *u); -void system_deo(Uxn *u, Uint8 *d, Uint8 port); +#define RAM_PAGES 0x10 typedef struct { - Uint8 length, *pages; + Uint8 *pages; } Mmu; -Uint8 *mmu_init(Mmu *m, Uint16 pages); +Uint8 *system_init(Mmu *m, Uint16 pages); +int system_load(Uxn *u, char *filename); +void system_inspect(Uxn *u); +void system_deo(Uxn *u, Uint8 *d, Uint8 port); diff --git a/src/uxncli.c b/src/uxncli.c @@ -81,9 +81,9 @@ main(int argc, char **argv) Mmu mmu; if(argc < 2) return emu_error("Usage", "uxncli game.rom args"); - if(!uxn_boot(&u, mmu_init(&mmu, 16), emu_dei, emu_deo)) + if(!uxn_boot(&u, system_init(&mmu, RAM_PAGES), emu_dei, emu_deo)) return emu_error("Boot", "Failed"); - if(!load_rom(&u, argv[1])) + if(!system_load(&u, argv[1])) return emu_error("Load", "Failed"); if(!uxn_eval(&u, PAGE_PROGRAM)) return emu_error("Init", "Failed"); diff --git a/src/uxnemu.c b/src/uxnemu.c @@ -264,9 +264,9 @@ static int start(Uxn *u, char *rom) { free(mmu.pages); - if(!uxn_boot(u, mmu_init(&mmu, 16), emu_dei, emu_deo)) + if(!uxn_boot(u, system_init(&mmu, RAM_PAGES), emu_dei, emu_deo)) return error("Boot", "Failed to start uxn."); - if(!load_rom(u, rom)) + if(!system_load(u, rom)) return error("Boot", "Failed to load rom."); exec_deadline = SDL_GetPerformanceCounter() + deadline_interval; if(!uxn_eval(u, PAGE_PROGRAM))