~rabbits/uxn

e20b572c2055a8205c386a4d3950bde66f3162cd — Devine Lu Linvega a month ago b0b60ca
Moved all system memory functions into system.c
6 files changed, 48 insertions(+), 47 deletions(-)

M src/devices/file.c
M src/devices/file.h
M src/devices/system.c
M src/devices/system.h
M src/uxncli.c
M src/uxnemu.c
M src/devices/file.c => src/devices/file.c +0 -16
@@ 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;
}

M src/devices/file.h => src/devices/file.h +0 -1
@@ 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);

M src/devices/system.c => src/devices/system.c +38 -22
@@ 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;
}


M src/devices/system.h => src/devices/system.h +6 -4
@@ 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);

M src/uxncli.c => src/uxncli.c +2 -2
@@ 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");

M src/uxnemu.c => src/uxnemu.c +2 -2
@@ 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))