commit 7adc008

Devine Lu Linvega  ·  2025-05-17 23:05:31 +0000 UTC
parent 2997738
Limit system expansion
2 files changed,  +12, -8
+7, -5
 1@@ -40,7 +40,9 @@ void emu_deo(Uint8 addr, Uint8 value);
 2 
 3 /* clang-format off */
 4 
 5-#define RAM_PAGES 0x10
 6+#define BANKS 0x10
 7+#define BANKS_CAP BANKS * 0x10000
 8+
 9 #define WIDTH (64 * 8)
10 #define HEIGHT (40 * 8)
11 #define PEEK2(d) (*(d) << 8 | (d)[1])
12@@ -152,7 +154,7 @@ system_load(char *rom_path)
13 	FILE *f = fopen(rom_path, "rb");
14 	if(f) {
15 		int i = 0, l = fread(ram + 0x100, 0x10000 - 0x100, 1, f);
16-		while(l && ++i < RAM_PAGES)
17+		while(l && ++i < BANKS)
18 			l = fread(ram + i * 0x10000, 0x10000, 1, f);
19 		fclose(f);
20 	}
21@@ -162,7 +164,7 @@ system_load(char *rom_path)
22 static int
23 system_boot(char *rom_path, int has_args)
24 {
25-	ram = (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8));
26+	ram = (Uint8 *)calloc(BANKS * 0x10000, sizeof(Uint8));
27 	system_boot_path = rom_path;
28 	dev[0x17] = has_args;
29 	return ram && system_load(rom_path);
30@@ -187,12 +189,12 @@ system_expansion(Uint16 addr)
31 		unsigned int a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5);
32 		unsigned int b = a + length;
33 		unsigned int value = ram[addr + 7];
34-		for(; a < b; ram[a++] = value);
35+		for(b = b < BANKS_CAP ? b : BANKS_CAP; a < b; ram[a++] = value);
36 	} else if(ram[addr] == 0x1) {
37 		unsigned int a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5);
38 		unsigned int b = a + length;
39 		unsigned int c = PEEK2(aptr + 7) * 0x10000 + PEEK2(aptr + 9);
40-		for(; a < b; ram[c++] = ram[a++]);
41+		for(b = b < BANKS_CAP ? b : BANKS_CAP; a < b; ram[c++] = ram[a++]);
42 	} else if(ram[addr] == 0x2) {
43 		unsigned int a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5);
44 		unsigned int b = a + length;
+5, -3
 1@@ -32,6 +32,8 @@ void emu_deo(Uint8 addr, Uint8 value);
 2 /* clang-format off */
 3 
 4 #define BANKS 0x10
 5+#define BANKS_CAP BANKS * 0x10000
 6+
 7 #define PEEK2(d) (*(d) << 8 | (d)[1])
 8 #define POKE2(d, v) { *(d) = (v) >> 8; (d)[1] = (v); }
 9 
10@@ -142,7 +144,7 @@ system_load(char *rom_path)
11 	if(f) {
12 		unsigned int i = 0, l = fread(ram + 0x100, 0x10000 - 0x100, 1, f);
13 		while(l && ++i < BANKS)
14-			l = fread(ram + (i * 0x10000), 0x10000, 1, f);
15+			l = fread(ram + i * 0x10000, 0x10000, 1, f);
16 		fclose(f);
17 	}
18 	return !!f;
19@@ -166,12 +168,12 @@ system_expansion(Uint16 addr)
20 		unsigned int a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5);
21 		unsigned int b = a + length;
22 		unsigned int value = ram[addr + 7];
23-		for(; a < b; ram[a++] = value);
24+		for(b = b < BANKS_CAP ? b : BANKS_CAP; a < b; ram[a++] = value);
25 	} else if(ram[addr] == 0x1) {
26 		unsigned int a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5);
27 		unsigned int b = a + length;
28 		unsigned int c = PEEK2(aptr + 7) * 0x10000 + PEEK2(aptr + 9);
29-		for(; a < b; ram[c++] = ram[a++]);
30+		for(b = b < BANKS_CAP ? b : BANKS_CAP; a < b; ram[c++] = ram[a++]);
31 	} else if(ram[addr] == 0x2) {
32 		unsigned int a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5);
33 		unsigned int b = a + length;