src/gba/overrides.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include <mgba/internal/gba/overrides.h>
7
8#include <mgba/internal/gba/gba.h>
9#include <mgba/internal/gba/hardware.h>
10
11#include <mgba-util/configuration.h>
12
13static const struct GBACartridgeOverride _overrides[] = {
14 // Advance Wars
15 { "AWRE", SAVEDATA_FLASH512, HW_NONE, 0x8038810, false },
16 { "AWRP", SAVEDATA_FLASH512, HW_NONE, 0x8038810, false },
17
18 // Advance Wars 2: Black Hole Rising
19 { "AW2E", SAVEDATA_FLASH512, HW_NONE, 0x8036E08, false },
20 { "AW2P", SAVEDATA_FLASH512, HW_NONE, 0x803719C, false },
21
22 // Boktai: The Sun is in Your Hand
23 { "U3IJ", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE, false },
24 { "U3IE", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE, false },
25 { "U3IP", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE, false },
26
27 // Boktai 2: Solar Boy Django
28 { "U32J", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE, false },
29 { "U32E", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE, false },
30 { "U32P", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE, false },
31
32 // Crash Bandicoot 2 - N-Tranced
33 { "AC8J", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
34 { "AC8E", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
35 { "AC8P", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
36
37 // Dragon Ball Z - The Legacy of Goku
38 { "ALGP", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
39
40 // Dragon Ball Z - The Legacy of Goku II
41 { "ALFJ", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
42 { "ALFE", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
43 { "ALFP", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
44
45 // Dragon Ball Z - Taiketsu
46 { "BDBE", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
47 { "BDBP", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
48
49 // Drill Dozer
50 { "V49J", SAVEDATA_SRAM, HW_RUMBLE, IDLE_LOOP_NONE, false },
51 { "V49E", SAVEDATA_SRAM, HW_RUMBLE, IDLE_LOOP_NONE, false },
52
53 // e-Reader
54 { "PEAJ", SAVEDATA_FLASH1M, HW_EREADER, IDLE_LOOP_NONE },
55 { "PSAJ", SAVEDATA_FLASH1M, HW_EREADER, IDLE_LOOP_NONE },
56 { "PSAE", SAVEDATA_FLASH1M, HW_EREADER, IDLE_LOOP_NONE },
57
58 // Final Fantasy Tactics Advance
59 { "AFXE", SAVEDATA_FLASH512, HW_NONE, 0x8000428, false },
60
61 // F-Zero - Climax
62 { "BFTJ", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
63
64 // Iridion II
65 { "AI2E", SAVEDATA_FORCE_NONE, HW_NONE, IDLE_LOOP_NONE, false },
66 { "AI2P", SAVEDATA_FORCE_NONE, HW_NONE, IDLE_LOOP_NONE, false },
67
68 // Golden Sun: The Lost Age
69 { "AGFE", SAVEDATA_FLASH512, HW_NONE, 0x801353A, false },
70
71 // Koro Koro Puzzle - Happy Panechu!
72 { "KHPJ", SAVEDATA_EEPROM, HW_TILT, IDLE_LOOP_NONE, false },
73
74 // Legendz - Yomigaeru Shiren no Shima
75 { "BLJJ", SAVEDATA_FLASH512, HW_RTC, IDLE_LOOP_NONE, false },
76 { "BLJK", SAVEDATA_FLASH512, HW_RTC, IDLE_LOOP_NONE, false },
77
78 // Legendz - Sign of Nekuromu
79 { "BLVJ", SAVEDATA_FLASH512, HW_RTC, IDLE_LOOP_NONE, false },
80
81 // Mega Man Battle Network
82 { "AREE", SAVEDATA_SRAM, HW_NONE, 0x800032E, false },
83
84 // Mega Man Zero
85 { "AZCE", SAVEDATA_SRAM, HW_NONE, 0x80004E8, false },
86
87 // Metal Slug Advance
88 { "BSME", SAVEDATA_EEPROM, HW_NONE, 0x8000290, false },
89
90 // Pokemon Ruby
91 { "AXVJ", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
92 { "AXVE", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
93 { "AXVP", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
94 { "AXVI", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
95 { "AXVS", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
96 { "AXVD", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
97 { "AXVF", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
98
99 // Pokemon Sapphire
100 { "AXPJ", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
101 { "AXPE", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
102 { "AXPP", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
103 { "AXPI", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
104 { "AXPS", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
105 { "AXPD", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
106 { "AXPF", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
107
108 // Pokemon Emerald
109 { "BPEJ", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6, false },
110 { "BPEE", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6, false },
111 { "BPEP", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6, false },
112 { "BPEI", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6, false },
113 { "BPES", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6, false },
114 { "BPED", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6, false },
115 { "BPEF", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6, false },
116
117 // Pokemon Mystery Dungeon
118 { "B24J", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
119 { "B24E", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
120 { "B24P", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
121 { "B24U", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
122
123 // Pokemon FireRed
124 { "BPRJ", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
125 { "BPRE", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
126 { "BPRP", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
127 { "BPRI", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
128 { "BPRS", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
129 { "BPRD", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
130 { "BPRF", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
131
132 // Pokemon LeafGreen
133 { "BPGJ", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
134 { "BPGE", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
135 { "BPGP", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
136 { "BPGI", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
137 { "BPGS", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
138 { "BPGD", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
139 { "BPGF", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE, false },
140
141 // RockMan EXE 4.5 - Real Operation
142 { "BR4J", SAVEDATA_FLASH512, HW_RTC, IDLE_LOOP_NONE, false },
143
144 // Rocky
145 { "AR8E", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
146 { "AROP", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
147
148 // Sennen Kazoku
149 { "BKAJ", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE, false },
150
151 // Shin Bokura no Taiyou: Gyakushuu no Sabata
152 { "U33J", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE, false },
153
154 // Super Mario Advance 2
155 { "AA2J", SAVEDATA_EEPROM, HW_NONE, 0x800052E, false },
156 { "AA2E", SAVEDATA_EEPROM, HW_NONE, 0x800052E, false },
157 { "AA2P", SAVEDATA_AUTODETECT, HW_NONE, 0x800052E, false },
158
159 // Super Mario Advance 3
160 { "A3AJ", SAVEDATA_EEPROM, HW_NONE, 0x8002B9C, false },
161 { "A3AE", SAVEDATA_EEPROM, HW_NONE, 0x8002B9C, false },
162 { "A3AP", SAVEDATA_EEPROM, HW_NONE, 0x8002B9C, false },
163
164 // Super Mario Advance 4
165 { "AX4J", SAVEDATA_FLASH1M, HW_NONE, 0x800072A, false },
166 { "AX4E", SAVEDATA_FLASH1M, HW_NONE, 0x800072A, false },
167 { "AX4P", SAVEDATA_FLASH1M, HW_NONE, 0x800072A, false },
168
169 // Super Monkey Ball Jr.
170 { "ALUE", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
171 { "ALUP", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
172
173 // Top Gun - Combat Zones
174 { "A2YE", SAVEDATA_FORCE_NONE, HW_NONE, IDLE_LOOP_NONE, false },
175
176 // Ueki no Housoku - Jingi Sakuretsu! Nouryokusha Battle
177 { "BUHJ", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
178
179 // Wario Ware Twisted
180 { "RZWJ", SAVEDATA_SRAM, HW_RUMBLE | HW_GYRO, IDLE_LOOP_NONE, false },
181 { "RZWE", SAVEDATA_SRAM, HW_RUMBLE | HW_GYRO, IDLE_LOOP_NONE, false },
182 { "RZWP", SAVEDATA_SRAM, HW_RUMBLE | HW_GYRO, IDLE_LOOP_NONE, false },
183
184 // Yoshi's Universal Gravitation
185 { "KYGJ", SAVEDATA_EEPROM, HW_TILT, IDLE_LOOP_NONE, false },
186 { "KYGE", SAVEDATA_EEPROM, HW_TILT, IDLE_LOOP_NONE, false },
187 { "KYGP", SAVEDATA_EEPROM, HW_TILT, IDLE_LOOP_NONE, false },
188
189 // Aging cartridge
190 { "TCHK", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
191
192 // Famicom Mini series 3 (FDS), some aren't mirrored (22 - 28)
193 // See https://forum.no-intro.org/viewtopic.php?f=2&t=4221 for discussion
194 { "FNMJ", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
195 { "FMRJ", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
196 { "FPTJ", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
197 { "FLBJ", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
198 { "FFMJ", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
199 { "FTKJ", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
200 { "FTUJ", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE, false },
201
202 { { 0, 0, 0, 0 }, 0, 0, IDLE_LOOP_NONE, false }
203};
204
205bool GBAOverrideFind(const struct Configuration* config, struct GBACartridgeOverride* override) {
206 override->savetype = SAVEDATA_AUTODETECT;
207 override->hardware = HW_NONE;
208 override->idleLoop = IDLE_LOOP_NONE;
209 override->mirroring = false;
210 bool found = false;
211
212 int i;
213 for (i = 0; _overrides[i].id[0]; ++i) {
214 if (memcmp(override->id, _overrides[i].id, sizeof(override->id)) == 0) {
215 *override = _overrides[i];
216 found = true;
217 break;
218 }
219 }
220 if (!found && override->id[0] == 'F') {
221 // Classic NES Series
222 override->savetype = SAVEDATA_EEPROM;
223 override->mirroring = true;
224 found = true;
225 }
226
227 if (config) {
228 char sectionName[16];
229 snprintf(sectionName, sizeof(sectionName), "override.%c%c%c%c", override->id[0], override->id[1], override->id[2], override->id[3]);
230 const char* savetype = ConfigurationGetValue(config, sectionName, "savetype");
231 const char* hardware = ConfigurationGetValue(config, sectionName, "hardware");
232 const char* idleLoop = ConfigurationGetValue(config, sectionName, "idleLoop");
233
234 if (savetype) {
235 if (strcasecmp(savetype, "SRAM") == 0) {
236 found = true;
237 override->savetype = SAVEDATA_SRAM;
238 } else if (strcasecmp(savetype, "EEPROM") == 0) {
239 found = true;
240 override->savetype = SAVEDATA_EEPROM;
241 } else if (strcasecmp(savetype, "EEPROM512") == 0) {
242 found = true;
243 override->savetype = SAVEDATA_EEPROM512;
244 } else if (strcasecmp(savetype, "FLASH512") == 0) {
245 found = true;
246 override->savetype = SAVEDATA_FLASH512;
247 } else if (strcasecmp(savetype, "FLASH1M") == 0) {
248 found = true;
249 override->savetype = SAVEDATA_FLASH1M;
250 } else if (strcasecmp(savetype, "NONE") == 0) {
251 found = true;
252 override->savetype = SAVEDATA_FORCE_NONE;
253 }
254 }
255
256 if (hardware) {
257 char* end;
258 long type = strtoul(hardware, &end, 0);
259 if (end && !*end) {
260 override->hardware = type;
261 found = true;
262 }
263 }
264
265 if (idleLoop) {
266 char* end;
267 uint32_t address = strtoul(idleLoop, &end, 16);
268 if (end && !*end) {
269 override->idleLoop = address;
270 found = true;
271 }
272 }
273 }
274 return found;
275}
276
277void GBAOverrideSave(struct Configuration* config, const struct GBACartridgeOverride* override) {
278 char sectionName[16];
279 snprintf(sectionName, sizeof(sectionName), "override.%c%c%c%c", override->id[0], override->id[1], override->id[2], override->id[3]);
280 const char* savetype = 0;
281 switch (override->savetype) {
282 case SAVEDATA_SRAM:
283 savetype = "SRAM";
284 break;
285 case SAVEDATA_EEPROM:
286 savetype = "EEPROM";
287 break;
288 case SAVEDATA_EEPROM512:
289 savetype = "EEPROM512";
290 break;
291 case SAVEDATA_FLASH512:
292 savetype = "FLASH512";
293 break;
294 case SAVEDATA_FLASH1M:
295 savetype = "FLASH1M";
296 break;
297 case SAVEDATA_FORCE_NONE:
298 savetype = "NONE";
299 break;
300 case SAVEDATA_AUTODETECT:
301 break;
302 }
303 ConfigurationSetValue(config, sectionName, "savetype", savetype);
304
305 if (override->hardware != HW_NO_OVERRIDE) {
306 ConfigurationSetIntValue(config, sectionName, "hardware", override->hardware);
307 } else {
308 ConfigurationClearValue(config, sectionName, "hardware");
309 }
310
311 if (override->idleLoop != IDLE_LOOP_NONE) {
312 ConfigurationSetUIntValue(config, sectionName, "idleLoop", override->idleLoop);
313 } else {
314 ConfigurationClearValue(config, sectionName, "idleLoop");
315 }
316}
317
318void GBAOverrideApply(struct GBA* gba, const struct GBACartridgeOverride* override) {
319 if (override->savetype != SAVEDATA_AUTODETECT) {
320 GBASavedataForceType(&gba->memory.savedata, override->savetype);
321 }
322
323 if (override->hardware != HW_NO_OVERRIDE) {
324 GBAHardwareClear(&gba->memory.hw);
325
326 if (override->hardware & HW_RTC) {
327 GBAHardwareInitRTC(&gba->memory.hw);
328 }
329
330 if (override->hardware & HW_GYRO) {
331 GBAHardwareInitGyro(&gba->memory.hw);
332 }
333
334 if (override->hardware & HW_RUMBLE) {
335 GBAHardwareInitRumble(&gba->memory.hw);
336 }
337
338 if (override->hardware & HW_LIGHT_SENSOR) {
339 GBAHardwareInitLight(&gba->memory.hw);
340 }
341
342 if (override->hardware & HW_TILT) {
343 GBAHardwareInitTilt(&gba->memory.hw);
344 }
345
346 if (override->hardware & HW_EREADER) {
347 GBAHardwareInitEReader(&gba->memory.hw);
348 }
349
350 if (override->hardware & HW_GB_PLAYER_DETECTION) {
351 gba->memory.hw.devices |= HW_GB_PLAYER_DETECTION;
352 } else {
353 gba->memory.hw.devices &= ~HW_GB_PLAYER_DETECTION;
354 }
355 }
356
357 if (override->idleLoop != IDLE_LOOP_NONE) {
358 gba->idleLoop = override->idleLoop;
359 if (gba->idleOptimization == IDLE_LOOP_DETECT) {
360 gba->idleOptimization = IDLE_LOOP_REMOVE;
361 }
362 }
363
364 if (override->mirroring) {
365 gba->memory.mirroring = true;
366 }
367}
368
369void GBAOverrideApplyDefaults(struct GBA* gba, const struct Configuration* overrides) {
370 struct GBACartridgeOverride override = { .idleLoop = IDLE_LOOP_NONE };
371 const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
372 if (cart) {
373 memcpy(override.id, &cart->id, sizeof(override.id));
374
375 if (!strncmp("pokemon red version", &((const char*) gba->memory.rom)[0x108], 20) && gba->romCrc32 != 0xDD88761C) {
376 // Enable FLASH1M and RTC on Pokémon FireRed ROM hacks
377 override.savetype = SAVEDATA_FLASH1M;
378 override.hardware = HW_RTC;
379 GBAOverrideApply(gba, &override);
380 } else if (GBAOverrideFind(overrides, &override)) {
381 GBAOverrideApply(gba, &override);
382 }
383 }
384}