Moved all system memory functions into system.c

This commit is contained in:
Devine Lu Linvega 2023-01-31 09:38:06 -08:00
parent b0b60ca20b
commit e20b572c20
6 changed files with 66 additions and 65 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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");

View File

@ -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))