diff --git a/src/devices/system.c b/src/devices/system.c index e41daa4..57b3aa9 100644 --- a/src/devices/system.c +++ b/src/devices/system.c @@ -32,7 +32,7 @@ system_load(Uint8 *ram, char *rom_path) FILE *f = fopen(rom_path, "rb"); if(f) { int i = 0, l = fread(ram, PAGE_SIZE - PAGE_PROGRAM, 1, f); - while(l && ++i < RAM_PAGES) + while(l && ++i < BANKS) l = fread(ram + PAGE_SIZE * i - PAGE_PROGRAM, PAGE_SIZE, 1, f); fclose(f); } @@ -67,6 +67,31 @@ system_reboot(int soft) return system_boot(uxn.ram, boot_path, 0); } +static void +system_expansion(const Uint16 addr) +{ + Uint8 *aptr = uxn.ram + addr; + Uint16 length = PEEK2(aptr + 1); + if(uxn.ram[addr] == 0x0) { + unsigned int a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5); + unsigned int b = a + length; + unsigned int value = uxn.ram[addr + 7]; + for(b = b < BANKS_CAP ? b : BANKS_CAP; a < b; uxn.ram[a++] = value); + } else if(uxn.ram[addr] == 0x1) { + unsigned int a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5); + unsigned int b = a + length; + unsigned int c = PEEK2(aptr + 7) * 0x10000 + PEEK2(aptr + 9); + for(b = b < BANKS_CAP ? b : BANKS_CAP; a < b; uxn.ram[c++] = uxn.ram[a++]); + } else if(uxn.ram[addr] == 0x2) { + unsigned int a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5); + unsigned int b = a + length; + unsigned int c = PEEK2(aptr + 7) * 0x10000 + PEEK2(aptr + 9); + unsigned int d = c + length; + for(; b >= a; uxn.ram[--d] = uxn.ram[--b]); + } else + fprintf(stderr, "Unknown command: %s\n", &uxn.ram[addr]); +} + /* IO */ Uint8 @@ -84,28 +109,7 @@ system_deo(Uint8 port) { switch(port) { case 0x3: { - Uint16 value; - Uint16 addr = PEEK2(uxn.dev + 2); - Uint8 *aptr = uxn.ram + addr; - Uint16 length = PEEK2(aptr + 1); - if(uxn.ram[addr] == 0x0) { - unsigned int a = PEEK2(aptr + 3) * PAGE_SIZE + PEEK2(aptr + 5); - unsigned int b = a + length; - value = uxn.ram[addr + 7]; - for(; a < b; uxn.ram[a++] = value); - } else if(uxn.ram[addr] == 0x1) { - unsigned int a = PEEK2(aptr + 3) * PAGE_SIZE + PEEK2(aptr + 5); - unsigned int b = a + length; - unsigned int c = PEEK2(aptr + 7) * PAGE_SIZE + PEEK2(aptr + 9); - for(; a < b; uxn.ram[c++] = uxn.ram[a++]); - } else if(uxn.ram[addr] == 0x2) { - unsigned int a = PEEK2(aptr + 3) * PAGE_SIZE + PEEK2(aptr + 5); - unsigned int b = a + length; - unsigned int c = PEEK2(aptr + 7) * PAGE_SIZE + PEEK2(aptr + 9); - unsigned int d = c + length; - for(; b >= a; uxn.ram[--d] = uxn.ram[--b]); - } else - fprintf(stderr, "Unknown Expansion Command 0x%02x\n", uxn.ram[addr]); + system_expansion(PEEK2(uxn.dev + 2)); break; } case 0x4: diff --git a/src/devices/system.h b/src/devices/system.h index 1590d8e..a5d66be 100644 --- a/src/devices/system.h +++ b/src/devices/system.h @@ -9,7 +9,8 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ -#define RAM_PAGES 0x10 +#define BANKS 0x10 +#define BANKS_CAP BANKS * 0x10000 int system_error(char *msg, const char *err); int system_boot(Uint8 *ram, char *rom_path, int has_args); diff --git a/src/uxncli.c b/src/uxncli.c index 384d858..b8ef114 100644 --- a/src/uxncli.c +++ b/src/uxncli.c @@ -48,10 +48,10 @@ 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, 31 Jan 2025.\n"); + return !fprintf(stdout, "Uxn(cli) - Varvara Emulator, 27 Jun 2025.\n"); else if(argc == 1) return !fprintf(stdout, "usage: %s [-v] file.rom [args..]\n", argv[0]); - else if(!system_boot((Uint8 *)calloc(PAGE_SIZE * RAM_PAGES, sizeof(Uint8)), argv[i++], argc > 2)) + else if(!system_boot((Uint8 *)calloc(PAGE_SIZE * BANKS, sizeof(Uint8)), argv[i++], argc > 2)) return !fprintf(stdout, "Could not load %s.\n", argv[i - 1]); if(console_vector) { console_arguments(i, argc, argv); diff --git a/src/uxnemu.c b/src/uxnemu.c index 08d56b8..de8a19d 100644 --- a/src/uxnemu.c +++ b/src/uxnemu.c @@ -482,7 +482,7 @@ main(int argc, char **argv) rom_path = i == argc ? "boot.rom" : argv[i++]; if(!emu_init()) return system_error("Init", "Failed to initialize varvara."); - if(!system_boot((Uint8 *)calloc(PAGE_SIZE * RAM_PAGES + 1, sizeof(Uint8)), rom_path, argc > i)) + if(!system_boot((Uint8 *)calloc(PAGE_SIZE * BANKS + 1, sizeof(Uint8)), rom_path, argc > i)) return system_error("usage:", "uxnemu [-v | -f | -2x | -3x] file.rom [args...]"); /* start */ console_arguments(i, argc, argv);