Browse code

Moved all system memory functions into system.c

Devine Lu Linvega authored on 31/01/2023 17:38:06
Showing 6 changed files
... ...
@@ -286,19 +286,3 @@ file_dei(Uint8 id, Uint8 *d, Uint8 port)
286 286
 	}
287 287
 	return d[port];
288 288
 }
289
-
290
-/* Boot */
291
-
292
-int
293
-load_rom(Uxn *u, char *filename)
294
-{
295
-	int l, i = 0;
296
-	FILE *f = fopen(filename, "rb");
297
-	if(!f)
298
-		return 0;
299
-	l = fread(&u->ram[PAGE_PROGRAM], 1, 0x10000 - PAGE_PROGRAM, f);
300
-	while(l && ++i < 15)
301
-		l = fread(u->ram + 0x10000 * i, 1, 0x10000, f);
302
-	fclose(f);
303
-	return 1;
304
-}
... ...
@@ -14,4 +14,3 @@ WITH REGARD TO THIS SOFTWARE.
14 14
 
15 15
 void file_deo(Uint8 id, Uint8 *ram, Uint8 *d, Uint8 port);
16 16
 Uint8 file_dei(Uint8 id, Uint8 *d, Uint8 port);
17
-int load_rom(Uxn *u, char *filename);
... ...
@@ -39,31 +39,11 @@ system_inspect(Uxn *u)
39 39
 	system_print(u->rst, "rst");
40 40
 }
41 41
 
42
-int
43
-uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr)
44
-{
45
-	Uint8 *d = &u->dev[0x00];
46
-	Uint16 handler = GETVEC(d);
47
-	if(handler) {
48
-		u->wst->ptr = 4;
49
-		u->wst->dat[0] = addr >> 0x8;
50
-		u->wst->dat[1] = addr & 0xff;
51
-		u->wst->dat[2] = instr;
52
-		u->wst->dat[3] = err;
53
-		return uxn_eval(u, handler);
54
-	} else {
55
-		system_inspect(u);
56
-		fprintf(stderr, "%s %s, by %02x at 0x%04x.\n", (instr & 0x40) ? "Return-stack" : "Working-stack", errors[err - 1], instr, addr);
57
-	}
58
-	return 0;
59
-}
60
-
61
-/* MMU */
42
+/* RAM */
62 43
 
63 44
 Uint8 *
64
-mmu_init(Mmu *m, Uint16 pages)
45
+system_init(Mmu *m, Uint16 pages)
65 46
 {
66
-	m->length = pages;
67 47
 	m->pages = (Uint8 *)calloc(0x10000 * pages, sizeof(Uint8));
68 48
 	return m->pages;
69 49
 }
... ...
@@ -82,6 +62,20 @@ mmu_eval(Uint8 *ram, Uint16 addr)
82 62
 	}
83 63
 }
84 64
 
65
+int
66
+system_load(Uxn *u, char *filename)
67
+{
68
+	int l, i = 0;
69
+	FILE *f = fopen(filename, "rb");
70
+	if(!f)
71
+		return 0;
72
+	l = fread(&u->ram[PAGE_PROGRAM], 1, 0x10000 - PAGE_PROGRAM, f);
73
+	while(l && ++i < RAM_PAGES)
74
+		l = fread(u->ram + 0x10000 * i, 1, 0x10000, f);
75
+	fclose(f);
76
+	return 1;
77
+}
78
+
85 79
 /* IO */
86 80
 
87 81
 void
... ...
@@ -98,3 +92,25 @@ system_deo(Uxn *u, Uint8 *d, Uint8 port)
98 92
 		break;
99 93
 	}
100 94
 }
95
+
96
+/* Error */
97
+
98
+int
99
+uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr)
100
+{
101
+	Uint8 *d = &u->dev[0x00];
102
+	Uint16 handler = GETVEC(d);
103
+	if(handler) {
104
+		u->wst->ptr = 4;
105
+		u->wst->dat[0] = addr >> 0x8;
106
+		u->wst->dat[1] = addr & 0xff;
107
+		u->wst->dat[2] = instr;
108
+		u->wst->dat[3] = err;
109
+		return uxn_eval(u, handler);
110
+	} else {
111
+		system_inspect(u);
112
+		fprintf(stderr, "%s %s, by %02x at 0x%04x.\n", (instr & 0x40) ? "Return-stack" : "Working-stack", errors[err - 1], instr, addr);
113
+	}
114
+	return 0;
115
+}
116
+
... ...
@@ -9,11 +9,13 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9
 WITH REGARD TO THIS SOFTWARE.
10 10
 */
11 11
 
12
-void system_inspect(Uxn *u);
13
-void system_deo(Uxn *u, Uint8 *d, Uint8 port);
12
+#define RAM_PAGES 0x10
14 13
 
15 14
 typedef struct {
16
-	Uint8 length, *pages;
15
+	Uint8 *pages;
17 16
 } Mmu;
18 17
 
19
-Uint8 *mmu_init(Mmu *m, Uint16 pages);
18
+Uint8 *system_init(Mmu *m, Uint16 pages);
19
+int system_load(Uxn *u, char *filename);
20
+void system_inspect(Uxn *u);
21
+void system_deo(Uxn *u, Uint8 *d, Uint8 port);
... ...
@@ -81,9 +81,9 @@ main(int argc, char **argv)
81 81
 	Mmu mmu;
82 82
 	if(argc < 2)
83 83
 		return emu_error("Usage", "uxncli game.rom args");
84
-	if(!uxn_boot(&u, mmu_init(&mmu, 16), emu_dei, emu_deo))
84
+	if(!uxn_boot(&u, system_init(&mmu, RAM_PAGES), emu_dei, emu_deo))
85 85
 		return emu_error("Boot", "Failed");
86
-	if(!load_rom(&u, argv[1]))
86
+	if(!system_load(&u, argv[1]))
87 87
 		return emu_error("Load", "Failed");
88 88
 	if(!uxn_eval(&u, PAGE_PROGRAM))
89 89
 		return emu_error("Init", "Failed");
... ...
@@ -264,9 +264,9 @@ static int
264 264
 start(Uxn *u, char *rom)
265 265
 {
266 266
 	free(mmu.pages);
267
-	if(!uxn_boot(u, mmu_init(&mmu, 16), emu_dei, emu_deo))
267
+	if(!uxn_boot(u, system_init(&mmu, RAM_PAGES), emu_dei, emu_deo))
268 268
 		return error("Boot", "Failed to start uxn.");
269
-	if(!load_rom(u, rom))
269
+	if(!system_load(u, rom))
270 270
 		return error("Boot", "Failed to load rom.");
271 271
 	exec_deadline = SDL_GetPerformanceCounter() + deadline_interval;
272 272
 	if(!uxn_eval(u, PAGE_PROGRAM))