diff options
Diffstat (limited to 'apps/plugins/rockboy/loader.c')
-rw-r--r-- | apps/plugins/rockboy/loader.c | 171 |
1 files changed, 107 insertions, 64 deletions
diff --git a/apps/plugins/rockboy/loader.c b/apps/plugins/rockboy/loader.c index 1317362c2c..0dbd0b4591 100644 --- a/apps/plugins/rockboy/loader.c +++ b/apps/plugins/rockboy/loader.c | |||
@@ -1,8 +1,3 @@ | |||
1 | |||
2 | #include <stdio.h> | ||
3 | #include <string.h> | ||
4 | |||
5 | |||
6 | #include "rockmacros.h" | 1 | #include "rockmacros.h" |
7 | #include "defs.h" | 2 | #include "defs.h" |
8 | #include "regs.h" | 3 | #include "regs.h" |
@@ -13,11 +8,58 @@ | |||
13 | #include "save.h" | 8 | #include "save.h" |
14 | #include "sound.h" | 9 | #include "sound.h" |
15 | 10 | ||
11 | /* From http://www.semis.demon.co.uk/Gameboy/Gbspec.txt (4/17/2007) | ||
12 | * Cartridge type: | ||
13 | * 0 - ROM ONLY 12 - ROM+MBC3+RAM | ||
14 | * 1 - ROM+MBC1 13 - ROM+MBC3+RAM+BATT | ||
15 | * 2 - ROM+MBC1+RAM 19 - ROM+MBC5 | ||
16 | * 3 - ROM+MBC1+RAM+BATT 1A - ROM+MBC5+RAM | ||
17 | * 5 - ROM+MBC2 1B - ROM+MBC5+RAM+BATT | ||
18 | * 6 - ROM+MBC2+BATTERY 1C - ROM+MBC5+RUMBLE | ||
19 | * 8 - ROM+RAM 1D - ROM+MBC5+RUMBLE+SRAM | ||
20 | * 9 - ROM+RAM+BATTERY 1E - ROM+MBC5+RUMBLE+SRAM+BATT | ||
21 | * B - ROM+MMM01 1F - Pocket Camera | ||
22 | * C - ROM+MMM01+SRAM FD - Bandai TAMA5 | ||
23 | * D - ROM+MMM01+SRAM+BATT FE - Hudson HuC-3 | ||
24 | * F - ROM+MBC3+TIMER+BATT FF - Hudson HuC-1 | ||
25 | * 10 - ROM+MBC3+TIMER+RAM+BATT | ||
26 | * 11 - ROM+MBC3 | ||
27 | */ | ||
16 | 28 | ||
17 | static int mbc_table[256] = | 29 | static int mbc_table[256] = |
18 | { | 30 | { |
19 | 0, 1, 1, 1, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3, | 31 | MBC_NONE, |
20 | 3, 3, 3, 3, 0, 0, 0, 0, 0, 5, 5, 5, MBC_RUMBLE, MBC_RUMBLE, MBC_RUMBLE, 0, | 32 | MBC_MBC1, |
33 | MBC_MBC1, | ||
34 | MBC_MBC1 | MBC_BAT, | ||
35 | 0, | ||
36 | MBC_MBC2, | ||
37 | MBC_MBC2 | MBC_BAT, | ||
38 | 0, | ||
39 | 0, | ||
40 | MBC_BAT, | ||
41 | 0, | ||
42 | 0, | ||
43 | 0, | ||
44 | MBC_BAT, | ||
45 | 0, | ||
46 | MBC_MBC3 | MBC_BAT | MBC_RTC, | ||
47 | MBC_MBC3 | MBC_BAT | MBC_RTC, | ||
48 | MBC_MBC3, | ||
49 | MBC_MBC3, | ||
50 | MBC_MBC3 | MBC_BAT, | ||
51 | 0, | ||
52 | 0, | ||
53 | 0, | ||
54 | 0, | ||
55 | 0, | ||
56 | MBC_MBC5, | ||
57 | MBC_MBC5, | ||
58 | MBC_MBC5 | MBC_BAT, | ||
59 | MBC_RUMBLE, | ||
60 | MBC_RUMBLE, | ||
61 | MBC_RUMBLE | MBC_BAT, | ||
62 | 0, | ||
21 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 63 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
22 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 64 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
23 | 65 | ||
@@ -34,46 +76,29 @@ static int mbc_table[256] = | |||
34 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 76 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
35 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 77 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
36 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 78 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
37 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, MBC_HUC3, MBC_HUC1 | 79 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
38 | }; | 80 | MBC_HUC3, |
39 | 81 | MBC_HUC1 | |
40 | static int rtc_table[256] = | ||
41 | { | ||
42 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, | ||
43 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
44 | 0 | ||
45 | }; | ||
46 | |||
47 | static int batt_table[256] = | ||
48 | { | ||
49 | 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, | ||
50 | 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, | ||
51 | 0 | ||
52 | }; | 82 | }; |
53 | 83 | ||
54 | static int romsize_table[256] = | 84 | static unsigned short romsize_table[56] = |
55 | { | 85 | { |
56 | 2, 4, 8, 16, 32, 64, 128, 256, 512, | 86 | 2, 4, 8, 16, 32, 64, 128, 256, |
57 | 0, 0, 0, 0, 0, 0, 0, 0, | 87 | 512, 0, 0, 0, 0, 0, 0, 0, |
58 | 0, 0, 0, 0, 0, 0, 0, 0, | 88 | 0, 0, 0, 0, 0, 0, 0, 0, |
59 | 0, 0, 0, 0, 0, 0, 0, 0, | 89 | 0, 0, 0, 0, 0, 0, 0, 0, |
60 | 0, 0, 0, 0, 0, 0, 0, 0, | 90 | 0, 0, 0, 0, 0, 0, 0, 0, |
61 | 0, 0, 0, 0, 0, 0, 0, 0, | 91 | 0, 0, 0, 0, 0, 0, 0, 0, |
62 | 0, 0, 0, 0, 0, 0, 0, 0, | 92 | 0, 0, 0, 0, 128, 128, 128, 0 |
63 | 0, 0, 0, 0, 0, 0, 0, 0, | 93 | /* 0, 0, 0, 0, 72, 80, 96 -- actual values but bad to use these! */ |
64 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
65 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
66 | 0, 0, 128, 128, 128 | ||
67 | /* 0, 0, 72, 80, 96 -- actual values but bad to use these! */ | ||
68 | }; | 94 | }; |
69 | 95 | ||
70 | static int ramsize_table[256] = | 96 | /* Ram size should be no larger then 16 banks 1Mbit */ |
97 | static unsigned char ramsize_table[5] = | ||
71 | { | 98 | { |
72 | 1, 1, 1, 4, 16, | 99 | 0, 1, 1, 4, 16 |
73 | 4 /* FIXME - what value should this be?! */ | ||
74 | }; | 100 | }; |
75 | 101 | ||
76 | |||
77 | static char *romfile; | 102 | static char *romfile; |
78 | static char sramfile[500]; | 103 | static char sramfile[500]; |
79 | static char rtcfile[500]; | 104 | static char rtcfile[500]; |
@@ -100,24 +125,23 @@ static void initmem(void *mem, int size) | |||
100 | 125 | ||
101 | static byte *loadfile(int fd, int *len) | 126 | static byte *loadfile(int fd, int *len) |
102 | { | 127 | { |
103 | int c, l = 0, p = 0; | 128 | int c; |
104 | 129 | byte *d; | |
105 | byte *d, buf[512]; | 130 | |
106 | d=malloc(32768); | 131 | *len=lseek(fd,0,SEEK_END); |
107 | for(;;) | 132 | d=malloc((*len)*sizeof(char)+64); |
133 | if(d==0) | ||
108 | { | 134 | { |
109 | c = read(fd, buf, sizeof buf); | 135 | die("Not enough memory"); |
110 | if (c <= 0) break; | 136 | return 0; |
111 | l += c; | ||
112 | memcpy(d+p, buf, c); | ||
113 | p += c; | ||
114 | } | 137 | } |
115 | setmallocpos(d+p+64); | 138 | lseek(fd,0,SEEK_SET); |
116 | *len = l; | 139 | |
140 | c = read(fd, d, *len); | ||
141 | |||
117 | return d; | 142 | return d; |
118 | } | 143 | } |
119 | 144 | ||
120 | |||
121 | int rom_load(void) | 145 | int rom_load(void) |
122 | { | 146 | { |
123 | int fd; | 147 | int fd; |
@@ -126,13 +150,19 @@ int rom_load(void) | |||
126 | 150 | ||
127 | fd = open(romfile, O_RDONLY); | 151 | fd = open(romfile, O_RDONLY); |
128 | 152 | ||
129 | if (fd<0) { | 153 | if (fd<0) |
130 | die("cannot open rom file"); | 154 | { |
131 | die(romfile); | 155 | die("cannot open rom file %s", romfile); |
132 | return 1; | 156 | return 1; |
133 | } | 157 | } |
134 | 158 | ||
135 | data = loadfile(fd, &len); | 159 | data = loadfile(fd, &len); |
160 | close(fd); | ||
161 | if(data==0) | ||
162 | { | ||
163 | die("Not Enough Memory"); | ||
164 | return 1; | ||
165 | } | ||
136 | header = data; /* no zip. = decompress(data, &len); */ | 166 | header = data; /* no zip. = decompress(data, &len); */ |
137 | 167 | ||
138 | memcpy(rom.name, header+0x0134, 16); | 168 | memcpy(rom.name, header+0x0134, 16); |
@@ -141,17 +171,22 @@ int rom_load(void) | |||
141 | rom.name[16] = 0; | 171 | rom.name[16] = 0; |
142 | 172 | ||
143 | c = header[0x0147]; | 173 | c = header[0x0147]; |
144 | mbc.type = mbc_table[c]; | 174 | mbc.type = mbc_table[c]&(MBC_MBC1|MBC_MBC2|MBC_MBC3|MBC_MBC5|MBC_RUMBLE|MBC_HUC1|MBC_HUC3); |
145 | mbc.batt = (batt_table[c] && !nobatt) || forcebatt; | 175 | mbc.batt = ((mbc_table[c]&MBC_BAT) && !nobatt) || forcebatt; |
146 | rtc.batt = rtc_table[c]; | 176 | rtc.batt = mbc_table[c]&MBC_RTC; |
147 | mbc.romsize = romsize_table[header[0x0148]]; | ||
148 | mbc.ramsize = ramsize_table[header[0x0149]]; | ||
149 | 177 | ||
150 | if (!mbc.romsize) { | 178 | if(header[0x0148]<10 || (header[0x0148]>51 && header[0x0148]<55)) |
179 | mbc.romsize = romsize_table[header[0x0148]]; | ||
180 | else | ||
181 | { | ||
151 | die("unknown ROM size %02X\n", header[0x0148]); | 182 | die("unknown ROM size %02X\n", header[0x0148]); |
152 | return 1; | 183 | return 1; |
153 | } | 184 | } |
154 | if (!mbc.ramsize) { | 185 | |
186 | if(header[0x0149]<5) | ||
187 | mbc.ramsize = ramsize_table[header[0x0149]]; | ||
188 | else | ||
189 | { | ||
155 | die("unknown SRAM size %02X\n", header[0x0149]); | 190 | die("unknown SRAM size %02X\n", header[0x0149]); |
156 | return 1; | 191 | return 1; |
157 | } | 192 | } |
@@ -160,7 +195,17 @@ int rom_load(void) | |||
160 | rom.bank = (void *) data; /* realloc(data, rlen); */ | 195 | rom.bank = (void *) data; /* realloc(data, rlen); */ |
161 | if (rlen > len) memset(rom.bank[0]+len, 0xff, rlen - len); | 196 | if (rlen > len) memset(rom.bank[0]+len, 0xff, rlen - len); |
162 | 197 | ||
198 | /* This is the size of the ram on the cartridge | ||
199 | * See http://www.semis.demon.co.uk/Gameboy/Gbspec.txt | ||
200 | * for a full description. (8192*number of banks) | ||
201 | */ | ||
163 | ram.sbank = malloc(8192 * mbc.ramsize); | 202 | ram.sbank = malloc(8192 * mbc.ramsize); |
203 | if(ram.sbank==0 && mbc.ramsize!=0) | ||
204 | { | ||
205 | die("Not enough Memory"); | ||
206 | return 1; | ||
207 | } | ||
208 | |||
164 | /* ram.ibank = malloc(4096*8); */ | 209 | /* ram.ibank = malloc(4096*8); */ |
165 | 210 | ||
166 | initmem(ram.sbank, 8192 * mbc.ramsize); | 211 | initmem(ram.sbank, 8192 * mbc.ramsize); |
@@ -171,8 +216,6 @@ int rom_load(void) | |||
171 | 216 | ||
172 | c = header[0x0143]; | 217 | c = header[0x0143]; |
173 | hw.cgb = ((c == 0x80) || (c == 0xc0)) && !forcedmg; | 218 | hw.cgb = ((c == 0x80) || (c == 0xc0)) && !forcedmg; |
174 | |||
175 | close(fd); | ||
176 | 219 | ||
177 | return 0; | 220 | return 0; |
178 | } | 221 | } |