src/gba/context/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 "overrides.h"
7
8#include "gba/gba.h"
9#include "gba/hardware.h"
10
11#include "util/configuration.h"
12
13static const struct GBACartridgeOverride _overrides[] = {
14 // Advance Wars
15 { "AWRE", SAVEDATA_FLASH512, HW_NONE, 0x8038810 },
16 { "AWRP", SAVEDATA_FLASH512, HW_NONE, 0x8038810 },
17
18 // Advance Wars 2: Black Hole Rising
19 { "AW2E", SAVEDATA_FLASH512, HW_NONE, 0x8036E08 },
20 { "AW2P", SAVEDATA_FLASH512, HW_NONE, 0x803719C },
21
22 // Boktai: The Sun is in Your Hand
23 { "U3IJ", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE },
24 { "U3IE", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE },
25 { "U3IP", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE },
26
27 // Boktai 2: Solar Boy Django
28 { "U32J", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE },
29 { "U32E", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE },
30 { "U32P", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE },
31
32 // Dragon Ball Z - The Legacy of Goku
33 { "ALGP", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE },
34
35 // Dragon Ball Z - Taiketsu
36 { "BDBE", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE },
37 { "BDBP", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE },
38
39 // Drill Dozer
40 { "V49J", SAVEDATA_SRAM, HW_RUMBLE, IDLE_LOOP_NONE },
41 { "V49E", SAVEDATA_SRAM, HW_RUMBLE, IDLE_LOOP_NONE },
42
43 // Final Fantasy Tactics Advance
44 { "AFXE", SAVEDATA_FLASH512, HW_NONE, 0x8000428 },
45
46 // F-Zero - Climax
47 { "BFTJ", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
48
49 // Iridion II
50 { "AI2E", SAVEDATA_FORCE_NONE, HW_NONE, IDLE_LOOP_NONE },
51 { "AI2P", SAVEDATA_FORCE_NONE, HW_NONE, IDLE_LOOP_NONE },
52
53 // Golden Sun: The Lost Age
54 { "AGFE", SAVEDATA_FLASH512, HW_NONE, 0x801353A },
55
56 // Koro Koro Puzzle - Happy Panechu!
57 { "KHPJ", SAVEDATA_EEPROM, HW_TILT, IDLE_LOOP_NONE },
58
59 // Mega Man Battle Network
60 { "AREE", SAVEDATA_SRAM, HW_NONE, 0x800032E },
61
62 // Mega Man Zero
63 { "AZCE", SAVEDATA_SRAM, HW_NONE, 0x80004E8 },
64
65 // Metal Slug Advance
66 { "BSME", SAVEDATA_EEPROM, HW_NONE, 0x8000290 },
67
68 // Pokemon Ruby
69 { "AXVJ", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
70 { "AXVE", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
71 { "AXVP", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
72 { "AXVI", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
73 { "AXVS", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
74 { "AXVD", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
75 { "AXVF", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
76
77 // Pokemon Sapphire
78 { "AXPJ", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
79 { "AXPE", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
80 { "AXPP", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
81 { "AXPI", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
82 { "AXPS", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
83 { "AXPD", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
84 { "AXPF", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
85
86 // Pokemon Emerald
87 { "BPEJ", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6 },
88 { "BPEE", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6 },
89 { "BPEP", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6 },
90 { "BPEI", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6 },
91 { "BPES", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6 },
92 { "BPED", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6 },
93 { "BPEF", SAVEDATA_FLASH1M, HW_RTC, 0x80008C6 },
94
95 // Pokemon Mystery Dungeon
96 { "B24J", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
97 { "B24E", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
98 { "B24P", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
99 { "B24U", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
100
101 // Pokemon FireRed
102 { "BPRJ", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
103 { "BPRE", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
104 { "BPRP", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
105 { "BPRI", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
106 { "BPRS", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
107 { "BPRD", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
108 { "BPRF", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
109
110 // Pokemon LeafGreen
111 { "BPGJ", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
112 { "BPGE", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
113 { "BPGP", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
114 { "BPGI", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
115 { "BPGS", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
116 { "BPGD", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
117 { "BPGF", SAVEDATA_FLASH1M, HW_NONE, IDLE_LOOP_NONE },
118
119 // RockMan EXE 4.5 - Real Operation
120 { "BR4J", SAVEDATA_FLASH512, HW_RTC, IDLE_LOOP_NONE },
121
122 // Rocky
123 { "AR8E", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE },
124 { "AROP", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE },
125
126 // Sennen Kazoku
127 { "BKAJ", SAVEDATA_FLASH1M, HW_RTC, IDLE_LOOP_NONE },
128
129 // Shin Bokura no Taiyou: Gyakushuu no Sabata
130 { "U33J", SAVEDATA_EEPROM, HW_RTC | HW_LIGHT_SENSOR, IDLE_LOOP_NONE },
131
132 // Super Mario Advance 2
133 { "AA2J", SAVEDATA_EEPROM, HW_NONE, 0x800052E },
134 { "AA2E", SAVEDATA_EEPROM, HW_NONE, 0x800052E },
135 { "AA2P", SAVEDATA_AUTODETECT, HW_NONE, 0x800052E },
136
137 // Super Mario Advance 3
138 { "A3AJ", SAVEDATA_EEPROM, HW_NONE, 0x8002B9C },
139 { "A3AE", SAVEDATA_EEPROM, HW_NONE, 0x8002B9C },
140 { "A3AP", SAVEDATA_EEPROM, HW_NONE, 0x8002B9C },
141
142 // Super Mario Advance 4
143 { "AX4J", SAVEDATA_FLASH1M, HW_NONE, 0x800072A },
144 { "AX4E", SAVEDATA_FLASH1M, HW_NONE, 0x800072A },
145 { "AX4P", SAVEDATA_FLASH1M, HW_NONE, 0x800072A },
146
147 // Super Monkey Ball Jr.
148 { "ALUE", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE },
149 { "ALUP", SAVEDATA_EEPROM, HW_NONE, IDLE_LOOP_NONE },
150
151 // Top Gun - Combat Zones
152 { "A2YE", SAVEDATA_FORCE_NONE, HW_NONE, IDLE_LOOP_NONE },
153
154 // Wario Ware Twisted
155 { "RZWJ", SAVEDATA_SRAM, HW_RUMBLE | HW_GYRO, IDLE_LOOP_NONE },
156 { "RZWE", SAVEDATA_SRAM, HW_RUMBLE | HW_GYRO, IDLE_LOOP_NONE },
157 { "RZWP", SAVEDATA_SRAM, HW_RUMBLE | HW_GYRO, IDLE_LOOP_NONE },
158
159 // Yoshi's Universal Gravitation
160 { "KYGJ", SAVEDATA_EEPROM, HW_TILT, IDLE_LOOP_NONE },
161 { "KYGE", SAVEDATA_EEPROM, HW_TILT, IDLE_LOOP_NONE },
162 { "KYGP", SAVEDATA_EEPROM, HW_TILT, IDLE_LOOP_NONE },
163
164 { { 0, 0, 0, 0 }, 0, 0, IDLE_LOOP_NONE }
165};
166
167bool GBAOverrideFind(const struct Configuration* config, struct GBACartridgeOverride* override) {
168 override->savetype = SAVEDATA_AUTODETECT;
169 override->hardware = HW_NONE;
170 override->idleLoop = IDLE_LOOP_NONE;
171 bool found = false;
172
173 if (override->id[0] == 'F') {
174 // Classic NES Series
175 override->savetype = SAVEDATA_EEPROM;
176 found = true;
177 } else {
178 int i;
179 for (i = 0; _overrides[i].id[0]; ++i) {
180 if (memcmp(override->id, _overrides[i].id, sizeof(override->id)) == 0) {
181 *override = _overrides[i];
182 found = true;
183 break;
184 }
185 }
186 }
187
188 if (config) {
189 char sectionName[16];
190 snprintf(sectionName, sizeof(sectionName), "override.%c%c%c%c", override->id[0], override->id[1], override->id[2], override->id[3]);
191 const char* savetype = ConfigurationGetValue(config, sectionName, "savetype");
192 const char* hardware = ConfigurationGetValue(config, sectionName, "hardware");
193 const char* idleLoop = ConfigurationGetValue(config, sectionName, "idleLoop");
194
195 if (savetype) {
196 if (strcasecmp(savetype, "SRAM") == 0) {
197 found = true;
198 override->savetype = SAVEDATA_SRAM;
199 } else if (strcasecmp(savetype, "EEPROM") == 0) {
200 found = true;
201 override->savetype = SAVEDATA_EEPROM;
202 } else if (strcasecmp(savetype, "FLASH512") == 0) {
203 found = true;
204 override->savetype = SAVEDATA_FLASH512;
205 } else if (strcasecmp(savetype, "FLASH1M") == 0) {
206 found = true;
207 override->savetype = SAVEDATA_FLASH1M;
208 } else if (strcasecmp(savetype, "NONE") == 0) {
209 found = true;
210 override->savetype = SAVEDATA_FORCE_NONE;
211 }
212 }
213
214 if (hardware) {
215 char* end;
216 long type = strtoul(hardware, &end, 0);
217 if (end && !*end) {
218 override->hardware = type;
219 found = true;
220 }
221 }
222
223 if (idleLoop) {
224 char* end;
225 uint32_t address = strtoul(idleLoop, &end, 16);
226 if (end && !*end) {
227 override->idleLoop = address;
228 found = true;
229 }
230 }
231 }
232 return found;
233}
234
235void GBAOverrideSave(struct Configuration* config, const struct GBACartridgeOverride* override) {
236 char sectionName[16];
237 snprintf(sectionName, sizeof(sectionName), "override.%c%c%c%c", override->id[0], override->id[1], override->id[2], override->id[3]);
238 const char* savetype = 0;
239 switch (override->savetype) {
240 case SAVEDATA_SRAM:
241 savetype = "SRAM";
242 break;
243 case SAVEDATA_EEPROM:
244 savetype = "EEPROM";
245 break;
246 case SAVEDATA_FLASH512:
247 savetype = "FLASH512";
248 break;
249 case SAVEDATA_FLASH1M:
250 savetype = "FLASH1M";
251 break;
252 case SAVEDATA_FORCE_NONE:
253 savetype = "NONE";
254 break;
255 case SAVEDATA_AUTODETECT:
256 break;
257 }
258 ConfigurationSetValue(config, sectionName, "savetype", savetype);
259
260 if (override->hardware != HW_NO_OVERRIDE) {
261 ConfigurationSetIntValue(config, sectionName, "hardware", override->hardware);
262 } else {
263 ConfigurationClearValue(config, sectionName, "hardware");
264 }
265
266 if (override->idleLoop != IDLE_LOOP_NONE) {
267 ConfigurationSetUIntValue(config, sectionName, "idleLoop", override->idleLoop);
268 } else {
269 ConfigurationClearValue(config, sectionName, "idleLoop");
270 }
271}
272
273void GBAOverrideApply(struct GBA* gba, const struct GBACartridgeOverride* override) {
274 if (override->savetype != SAVEDATA_AUTODETECT) {
275 GBASavedataForceType(&gba->memory.savedata, override->savetype, gba->realisticTiming);
276 }
277
278 if (override->hardware != HW_NO_OVERRIDE) {
279 GBAHardwareClear(&gba->memory.hw);
280
281 if (override->hardware & HW_RTC) {
282 GBAHardwareInitRTC(&gba->memory.hw);
283 }
284
285 if (override->hardware & HW_GYRO) {
286 GBAHardwareInitGyro(&gba->memory.hw);
287 }
288
289 if (override->hardware & HW_RUMBLE) {
290 GBAHardwareInitRumble(&gba->memory.hw);
291 }
292
293 if (override->hardware & HW_LIGHT_SENSOR) {
294 GBAHardwareInitLight(&gba->memory.hw);
295 }
296
297 if (override->hardware & HW_TILT) {
298 GBAHardwareInitTilt(&gba->memory.hw);
299 }
300
301 if (override->hardware & HW_GB_PLAYER_DETECTION) {
302 gba->memory.hw.devices |= HW_GB_PLAYER_DETECTION;
303 } else {
304 gba->memory.hw.devices &= ~HW_GB_PLAYER_DETECTION;
305 }
306 }
307
308 if (override->idleLoop != IDLE_LOOP_NONE) {
309 gba->idleLoop = override->idleLoop;
310 if (gba->idleOptimization == IDLE_LOOP_DETECT) {
311 gba->idleOptimization = IDLE_LOOP_REMOVE;
312 }
313 }
314}
315
316void GBAOverrideApplyDefaults(struct GBA* gba) {
317 struct GBACartridgeOverride override;
318 const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
319 memcpy(override.id, &cart->id, sizeof(override.id));
320 if (GBAOverrideFind(0, &override)) {
321 GBAOverrideApply(gba, &override);
322 }
323}