diff --git a/src/devices/file.c b/src/devices/file.c index eb9871d..c5f3fbc 100644 --- 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 index 5edb9f5..2a6fa13 100644 --- 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 index 5339c41..9e9de93 100644 --- a/src/devices/system.c +++ b/src/devices/system.c @@ -39,6 +39,62 @@ system_inspect(Uxn *u) system_print(u->rst, "rst"); } +/* RAM */ + +Uint8 * +system_init(Mmu *m, Uint16 pages) +{ + m->pages = (Uint8 *)calloc(0x10000 * pages, sizeof(Uint8)); + return m->pages; +} + +void +mmu_eval(Uint8 *ram, Uint16 addr) +{ + Uint16 a = addr, i = 0; + Uint8 o = ram[a++]; + if(o == 1) { + Uint16 length = (ram[a++] << 8) + ram[a++]; + Uint16 src_page = ((ram[a++] << 8) + ram[a++]) % 16, src_addr = (ram[a++] << 8) + ram[a++]; + Uint16 dst_page = ((ram[a++] << 8) + ram[a++]) % 16, dst_addr = (ram[a++] << 8) + ram[a]; + for(i = 0; i < length; i++) + ram[dst_page * 0x10000 + dst_addr + i] = ram[src_page * 0x10000 + src_addr + i]; + } +} + +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 +system_deo(Uxn *u, Uint8 *d, Uint8 port) +{ + Uint16 a; + switch(port) { + case 0x3: + PEKDEV(a, 0x2); + mmu_eval(u->ram, a); + break; + case 0xe: + if(u->wst->ptr || u->rst->ptr) system_inspect(u); + break; + } +} + +/* Error */ + int uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr) { @@ -58,43 +114,3 @@ uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr) return 0; } -/* MMU */ - -Uint8 * -mmu_init(Mmu *m, Uint16 pages) -{ - m->length = pages; - m->pages = (Uint8 *)calloc(0x10000 * pages, sizeof(Uint8)); - return m->pages; -} - -void -mmu_eval(Uint8 *ram, Uint16 addr) -{ - Uint16 a = addr, i = 0; - Uint8 o = ram[a++]; - if(o == 1) { - Uint16 length = (ram[a++] << 8) + ram[a++]; - Uint16 src_page = ((ram[a++] << 8) + ram[a++]) % 16, src_addr = (ram[a++] << 8) + ram[a++]; - Uint16 dst_page = ((ram[a++] << 8) + ram[a++]) % 16, dst_addr = (ram[a++] << 8) + ram[a]; - for(i = 0; i < length; i++) - ram[dst_page * 0x10000 + dst_addr + i] = ram[src_page * 0x10000 + src_addr + i]; - } -} - -/* IO */ - -void -system_deo(Uxn *u, Uint8 *d, Uint8 port) -{ - Uint16 a; - switch(port) { - case 0x3: - PEKDEV(a, 0x2); - mmu_eval(u->ram, a); - break; - case 0xe: - if(u->wst->ptr || u->rst->ptr) system_inspect(u); - break; - } -} diff --git a/src/devices/system.h b/src/devices/system.h index 6be1005..d0e9130 100644 --- 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 index 94d7935..c8c9d97 100644 --- 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 index 4811138..a41e3ff 100644 --- 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))