src/gba/gba.c (view raw)
1#include "gba.h"
2
3#include "gba-bios.h"
4#include "gba-io.h"
5#include "gba-thread.h"
6#include "memory.h"
7
8#include "debugger.h"
9
10#include <limits.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/stat.h>
15
16const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;
17
18enum {
19 SP_BASE_SYSTEM = 0x03FFFF00,
20 SP_BASE_IRQ = 0x03FFFFA0,
21 SP_BASE_SUPERVISOR = 0x03FFFFE0
22};
23
24struct GBACartridgeOverride {
25 const char id[4];
26 enum SavedataType type;
27 int gpio;
28};
29
30static const struct GBACartridgeOverride _overrides[] = {
31 // Boktai: The Sun is in Your Hand
32 { "U3IE", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
33 { "U3IP", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
34
35 // Boktai 2: Solar Boy Django
36 { "U32E", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
37 { "U32P", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
38
39 // Drill Dozer
40 { "V49J", SAVEDATA_SRAM, GPIO_RUMBLE },
41 { "V49E", SAVEDATA_SRAM, GPIO_RUMBLE },
42
43 // Pokemon Ruby
44 { "AXVJ", SAVEDATA_FLASH1M, GPIO_RTC },
45 { "AXVE", SAVEDATA_FLASH1M, GPIO_RTC },
46 { "AXVP", SAVEDATA_FLASH1M, GPIO_RTC },
47 { "AXVI", SAVEDATA_FLASH1M, GPIO_RTC },
48 { "AXVS", SAVEDATA_FLASH1M, GPIO_RTC },
49 { "AXVD", SAVEDATA_FLASH1M, GPIO_RTC },
50 { "AXVF", SAVEDATA_FLASH1M, GPIO_RTC },
51
52 // Pokemon Sapphire
53 { "AXPJ", SAVEDATA_FLASH1M, GPIO_RTC },
54 { "AXPE", SAVEDATA_FLASH1M, GPIO_RTC },
55 { "AXPP", SAVEDATA_FLASH1M, GPIO_RTC },
56 { "AXPI", SAVEDATA_FLASH1M, GPIO_RTC },
57 { "AXPS", SAVEDATA_FLASH1M, GPIO_RTC },
58 { "AXPD", SAVEDATA_FLASH1M, GPIO_RTC },
59 { "AXPF", SAVEDATA_FLASH1M, GPIO_RTC },
60
61 // Pokemon Emerald
62 { "BPEJ", SAVEDATA_FLASH1M, GPIO_RTC },
63 { "BPEE", SAVEDATA_FLASH1M, GPIO_RTC },
64 { "BPEP", SAVEDATA_FLASH1M, GPIO_RTC },
65 { "BPEI", SAVEDATA_FLASH1M, GPIO_RTC },
66 { "BPES", SAVEDATA_FLASH1M, GPIO_RTC },
67 { "BPED", SAVEDATA_FLASH1M, GPIO_RTC },
68 { "BPEF", SAVEDATA_FLASH1M, GPIO_RTC },
69
70 // Pokemon FireRed
71 { "BPRJ", SAVEDATA_FLASH1M, GPIO_NONE },
72 { "BPRE", SAVEDATA_FLASH1M, GPIO_NONE },
73 { "BPRP", SAVEDATA_FLASH1M, GPIO_NONE },
74
75 // Pokemon LeafGreen
76 { "BPGJ", SAVEDATA_FLASH1M, GPIO_NONE },
77 { "BPGE", SAVEDATA_FLASH1M, GPIO_NONE },
78 { "BPGP", SAVEDATA_FLASH1M, GPIO_NONE },
79
80 // RockMan EXE 4.5 - Real Operation
81 { "BR4J", SAVEDATA_FLASH512, GPIO_RTC },
82
83 // Super Mario Advance 4
84 { "AX4J", SAVEDATA_FLASH1M, GPIO_NONE },
85 { "AX4E", SAVEDATA_FLASH1M, GPIO_NONE },
86 { "AX4P", SAVEDATA_FLASH1M, GPIO_NONE },
87
88 // Wario Ware Twisted
89 { "RWZJ", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
90 { "RWZE", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
91 { "RWZP", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
92
93 { { 0, 0, 0, 0 }, 0, 0 }
94};
95
96static void GBAProcessEvents(struct ARMBoard* board);
97static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
98static void GBAHitStub(struct ARMBoard* board, uint32_t opcode);
99static void GBAIllegal(struct ARMBoard* board, uint32_t opcode);
100
101static void _checkOverrides(struct GBA* gba, uint32_t code);
102
103void GBAInit(struct GBA* gba) {
104 gba->debugger = 0;
105 gba->savefile = 0;
106
107 ARMInit(&gba->cpu);
108
109 gba->memory.p = gba;
110 GBAMemoryInit(&gba->memory);
111 ARMAssociateMemory(&gba->cpu, &gba->memory.d);
112
113 gba->board.p = gba;
114 GBABoardInit(&gba->board);
115 ARMAssociateBoard(&gba->cpu, &gba->board.d);
116
117 gba->video.p = gba;
118 GBAVideoInit(&gba->video);
119
120 gba->audio.p = gba;
121 GBAAudioInit(&gba->audio);
122
123 GBAIOInit(gba);
124
125 gba->timersEnabled = 0;
126 memset(gba->timers, 0, sizeof(gba->timers));
127
128 gba->springIRQ = 0;
129 gba->keySource = 0;
130 gba->rotationSource = 0;
131 gba->rumble = 0;
132
133 gba->logLevel = GBA_LOG_INFO | GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
134
135 gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
136
137 ARMReset(&gba->cpu);
138}
139
140void GBADeinit(struct GBA* gba) {
141 GBAMemoryDeinit(&gba->memory);
142 GBAVideoDeinit(&gba->video);
143 GBAAudioDeinit(&gba->audio);
144}
145
146void GBABoardInit(struct GBABoard* board) {
147 board->d.reset = GBABoardReset;
148 board->d.processEvents = GBAProcessEvents;
149 board->d.swi16 = GBASwi16;
150 board->d.swi32 = GBASwi32;
151 board->d.hitIllegal = GBAIllegal;
152 board->d.readCPSR = GBATestIRQ;
153 board->d.hitStub = GBAHitStub;
154}
155
156void GBABoardReset(struct ARMBoard* board) {
157 struct ARMCore* cpu = board->cpu;
158 ARMSetPrivilegeMode(cpu, MODE_IRQ);
159 cpu->gprs[ARM_SP] = SP_BASE_IRQ;
160 ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
161 cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
162 ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
163 cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
164}
165
166static void GBAProcessEvents(struct ARMBoard* board) {
167 do {
168 struct GBABoard* gbaBoard = (struct GBABoard*) board;
169 int32_t cycles = board->cpu->cycles;
170 int32_t nextEvent = INT_MAX;
171 int32_t testEvent;
172
173 if (gbaBoard->p->springIRQ) {
174 ARMRaiseIRQ(&gbaBoard->p->cpu);
175 gbaBoard->p->springIRQ = 0;
176 }
177
178 testEvent = GBAVideoProcessEvents(&gbaBoard->p->video, cycles);
179 if (testEvent < nextEvent) {
180 nextEvent = testEvent;
181 }
182
183 testEvent = GBAAudioProcessEvents(&gbaBoard->p->audio, cycles);
184 if (testEvent < nextEvent) {
185 nextEvent = testEvent;
186 }
187
188 testEvent = GBATimersProcessEvents(gbaBoard->p, cycles);
189 if (testEvent < nextEvent) {
190 nextEvent = testEvent;
191 }
192
193 testEvent = GBAMemoryRunDMAs(&gbaBoard->p->memory, cycles);
194 if (testEvent < nextEvent) {
195 nextEvent = testEvent;
196 }
197
198 board->cpu->cycles -= cycles;
199 board->cpu->nextEvent = nextEvent;
200 } while (board->cpu->cycles >= board->cpu->nextEvent);
201}
202
203static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
204 int32_t nextEvent = INT_MAX;
205 if (gba->timersEnabled) {
206 struct GBATimer* timer;
207 struct GBATimer* nextTimer;
208
209 timer = &gba->timers[0];
210 if (timer->enable) {
211 timer->nextEvent -= cycles;
212 timer->lastEvent -= cycles;
213 if (timer->nextEvent <= 0) {
214 timer->lastEvent = timer->nextEvent;
215 timer->nextEvent += timer->overflowInterval;
216 gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
217 timer->oldReload = timer->reload;
218
219 if (timer->doIrq) {
220 GBARaiseIRQ(gba, IRQ_TIMER0);
221 }
222
223 if (gba->audio.enable) {
224 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 0) {
225 GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
226 }
227
228 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 0) {
229 GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
230 }
231 }
232
233 nextTimer = &gba->timers[1];
234 if (nextTimer->countUp) {
235 ++gba->memory.io[REG_TM1CNT_LO >> 1];
236 if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
237 nextTimer->nextEvent = 0;
238 }
239 }
240 }
241 nextEvent = timer->nextEvent;
242 }
243
244 timer = &gba->timers[1];
245 if (timer->enable) {
246 timer->nextEvent -= cycles;
247 timer->lastEvent -= cycles;
248 if (timer->nextEvent <= 0) {
249 timer->lastEvent = timer->nextEvent;
250 timer->nextEvent += timer->overflowInterval;
251 gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
252 timer->oldReload = timer->reload;
253
254 if (timer->doIrq) {
255 GBARaiseIRQ(gba, IRQ_TIMER1);
256 }
257
258 if (gba->audio.enable) {
259 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 1) {
260 GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
261 }
262
263 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 1) {
264 GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
265 }
266 }
267
268 if (timer->countUp) {
269 timer->nextEvent = INT_MAX;
270 }
271
272 nextTimer = &gba->timers[2];
273 if (nextTimer->countUp) {
274 ++gba->memory.io[REG_TM2CNT_LO >> 1];
275 if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
276 nextTimer->nextEvent = 0;
277 }
278 }
279 }
280 if (timer->nextEvent < nextEvent) {
281 nextEvent = timer->nextEvent;
282 }
283 }
284
285 timer = &gba->timers[2];
286 if (timer->enable) {
287 timer->nextEvent -= cycles;
288 timer->lastEvent -= cycles;
289 nextEvent = timer->nextEvent;
290 if (timer->nextEvent <= 0) {
291 timer->lastEvent = timer->nextEvent;
292 timer->nextEvent += timer->overflowInterval;
293 gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
294 timer->oldReload = timer->reload;
295
296 if (timer->doIrq) {
297 GBARaiseIRQ(gba, IRQ_TIMER2);
298 }
299
300 if (timer->countUp) {
301 timer->nextEvent = INT_MAX;
302 }
303
304 nextTimer = &gba->timers[3];
305 if (nextTimer->countUp) {
306 ++gba->memory.io[REG_TM3CNT_LO >> 1];
307 if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
308 nextTimer->nextEvent = 0;
309 }
310 }
311 }
312 if (timer->nextEvent < nextEvent) {
313 nextEvent = timer->nextEvent;
314 }
315 }
316
317 timer = &gba->timers[3];
318 if (timer->enable) {
319 timer->nextEvent -= cycles;
320 timer->lastEvent -= cycles;
321 nextEvent = timer->nextEvent;
322 if (timer->nextEvent <= 0) {
323 timer->lastEvent = timer->nextEvent;
324 timer->nextEvent += timer->overflowInterval;
325 gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
326 timer->oldReload = timer->reload;
327
328 if (timer->doIrq) {
329 GBARaiseIRQ(gba, IRQ_TIMER3);
330 }
331
332 if (timer->countUp) {
333 timer->nextEvent = INT_MAX;
334 }
335 }
336 if (timer->nextEvent < nextEvent) {
337 nextEvent = timer->nextEvent;
338 }
339 }
340 }
341 return nextEvent;
342}
343
344void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
345 ARMDebuggerInit(debugger, &gba->cpu);
346 gba->debugger = debugger;
347}
348
349void GBALoadROM(struct GBA* gba, int fd, const char* fname) {
350 struct stat info;
351 gba->memory.rom = fileMemoryMap(fd, SIZE_CART0, MEMORY_READ);
352 gba->activeFile = fname;
353 fstat(fd, &info);
354 gba->memory.romSize = info.st_size;
355 if (gba->savefile) {
356 GBASavedataInit(&gba->memory.savedata, gba->savefile);
357 }
358 GBAGPIOInit(&gba->memory.gpio, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
359 _checkOverrides(gba, ((struct GBACartridge*) gba->memory.rom)->id);
360 // TODO: error check
361}
362
363void GBALoadBIOS(struct GBA* gba, int fd) {
364 gba->memory.bios = fileMemoryMap(fd, SIZE_BIOS, MEMORY_READ);
365 gba->memory.fullBios = 1;
366 uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
367 GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
368 if (checksum == GBA_BIOS_CHECKSUM) {
369 GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
370 } else if (checksum == GBA_DS_BIOS_CHECKSUM) {
371 GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
372 } else {
373 GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
374 }
375 gba->biosChecksum = checksum;
376 if ((gba->cpu.gprs[ARM_PC] >> BASE_OFFSET) == BASE_BIOS) {
377 gba->memory.d.setActiveRegion(&gba->memory.d, gba->cpu.gprs[ARM_PC]);
378 }
379 // TODO: error check
380}
381
382void GBATimerUpdateRegister(struct GBA* gba, int timer) {
383 struct GBATimer* currentTimer = &gba->timers[timer];
384 if (currentTimer->enable && !currentTimer->countUp) {
385 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu.cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
386 }
387}
388
389void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
390 gba->timers[timer].reload = reload;
391}
392
393void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
394 struct GBATimer* currentTimer = &gba->timers[timer];
395 GBATimerUpdateRegister(gba, timer);
396
397 int oldPrescale = currentTimer->prescaleBits;
398 switch (control & 0x0003) {
399 case 0x0000:
400 currentTimer->prescaleBits = 0;
401 break;
402 case 0x0001:
403 currentTimer->prescaleBits = 6;
404 break;
405 case 0x0002:
406 currentTimer->prescaleBits = 8;
407 break;
408 case 0x0003:
409 currentTimer->prescaleBits = 10;
410 break;
411 }
412 currentTimer->countUp = !!(control & 0x0004);
413 currentTimer->doIrq = !!(control & 0x0040);
414 currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
415 int wasEnabled = currentTimer->enable;
416 currentTimer->enable = !!(control & 0x0080);
417 if (!wasEnabled && currentTimer->enable) {
418 if (!currentTimer->countUp) {
419 currentTimer->nextEvent = gba->cpu.cycles + currentTimer->overflowInterval;
420 } else {
421 currentTimer->nextEvent = INT_MAX;
422 }
423 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
424 currentTimer->oldReload = currentTimer->reload;
425 gba->timersEnabled |= 1 << timer;
426 } else if (wasEnabled && !currentTimer->enable) {
427 if (!currentTimer->countUp) {
428 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu.cycles - currentTimer->lastEvent) >> oldPrescale);
429 }
430 gba->timersEnabled &= ~(1 << timer);
431 } else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
432 // FIXME: this might be before present
433 currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
434 }
435
436 if (currentTimer->nextEvent < gba->cpu.nextEvent) {
437 gba->cpu.nextEvent = currentTimer->nextEvent;
438 }
439};
440
441void GBAWriteIE(struct GBA* gba, uint16_t value) {
442 if (value & (1 << IRQ_SIO)) {
443 GBALog(gba, GBA_LOG_STUB, "SIO interrupts not implemented");
444 }
445
446 if (value & (1 << IRQ_KEYPAD)) {
447 GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
448 }
449
450 if (value & (1 << IRQ_GAMEPAK)) {
451 GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
452 }
453
454 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
455 ARMRaiseIRQ(&gba->cpu);
456 }
457}
458
459void GBAWriteIME(struct GBA* gba, uint16_t value) {
460 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
461 ARMRaiseIRQ(&gba->cpu);
462 }
463}
464
465void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
466 gba->memory.io[REG_IF >> 1] |= 1 << irq;
467
468 if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
469 ARMRaiseIRQ(&gba->cpu);
470 }
471}
472
473void GBATestIRQ(struct ARMBoard* board) {
474 struct GBABoard* gbaBoard = (struct GBABoard*) board;
475 struct GBA* gba = gbaBoard->p;
476 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
477 gba->springIRQ = 1;
478 gba->cpu.nextEvent = 0;
479 }
480}
481
482int GBAWaitForIRQ(struct GBA* gba) {
483 int irqs = gba->memory.io[REG_IF >> 1];
484 int newIRQs = 0;
485 gba->memory.io[REG_IF >> 1] = 0;
486 while (1) {
487 if (gba->cpu.nextEvent == INT_MAX) {
488 break;
489 } else {
490 gba->cpu.cycles = gba->cpu.nextEvent;
491 GBAProcessEvents(&gba->board.d);
492 if (gba->memory.io[REG_IF >> 1]) {
493 newIRQs = gba->memory.io[REG_IF >> 1];
494 break;
495 }
496 }
497 }
498 gba->memory.io[REG_IF >> 1] = newIRQs | irqs;
499 return newIRQs;
500}
501
502int GBAHalt(struct GBA* gba) {
503 return GBAWaitForIRQ(gba);
504}
505
506static void _GBAVLog(struct GBA* gba, enum GBALogLevel level, const char* format, va_list args) {
507 if (!gba) {
508 struct GBAThread* threadContext = GBAThreadGetContext();
509 if (threadContext) {
510 gba = threadContext->gba;
511 }
512 }
513
514 if (gba && gba->logHandler) {
515 gba->logHandler(gba, level, format, args);
516 return;
517 }
518
519 if (gba && !(level & gba->logLevel) && level != GBA_LOG_FATAL) {
520 return;
521 }
522
523 vprintf(format, args);
524 printf("\n");
525
526 if (level == GBA_LOG_FATAL) {
527 abort();
528 }
529}
530
531void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
532 va_list args;
533 va_start(args, format);
534 _GBAVLog(gba, level, format, args);
535 va_end(args);
536}
537
538void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...) {
539 struct GBABoard* gbaBoard = (struct GBABoard*) debugger->cpu->board;
540
541 enum GBALogLevel gbaLevel;
542 switch (level) {
543 case DEBUGGER_LOG_DEBUG:
544 gbaLevel = GBA_LOG_DEBUG;
545 break;
546 case DEBUGGER_LOG_INFO:
547 gbaLevel = GBA_LOG_INFO;
548 break;
549 case DEBUGGER_LOG_WARN:
550 gbaLevel = GBA_LOG_WARN;
551 break;
552 case DEBUGGER_LOG_ERROR:
553 gbaLevel = GBA_LOG_ERROR;
554 break;
555 }
556 va_list args;
557 va_start(args, format);
558 _GBAVLog(gbaBoard->p, gbaLevel, format, args);
559 va_end(args);
560}
561
562
563void GBAHitStub(struct ARMBoard* board, uint32_t opcode) {
564 struct GBABoard* gbaBoard = (struct GBABoard*) board;
565 enum GBALogLevel level = GBA_LOG_FATAL;
566 if (gbaBoard->p->debugger) {
567 level = GBA_LOG_STUB;
568 ARMDebuggerEnter(gbaBoard->p->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
569 }
570 GBALog(gbaBoard->p, level, "Stub opcode: %08x", opcode);
571}
572
573void GBAIllegal(struct ARMBoard* board, uint32_t opcode) {
574 struct GBABoard* gbaBoard = (struct GBABoard*) board;
575 GBALog(gbaBoard->p, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
576 if (gbaBoard->p->debugger) {
577 ARMDebuggerEnter(gbaBoard->p->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
578 }
579}
580
581void _checkOverrides(struct GBA* gba, uint32_t id) {
582 int i;
583 for (i = 0; _overrides[i].id[0]; ++i) {
584 const uint32_t* overrideId = (const uint32_t*) _overrides[i].id;
585 if (*overrideId == id) {
586 switch (_overrides[i].type) {
587 case SAVEDATA_FLASH512:
588 case SAVEDATA_FLASH1M:
589 gba->memory.savedata.type = _overrides[i].type;
590 GBASavedataInitFlash(&gba->memory.savedata);
591 break;
592 case SAVEDATA_EEPROM:
593 GBASavedataInitEEPROM(&gba->memory.savedata);
594 break;
595 case SAVEDATA_SRAM:
596 GBASavedataInitSRAM(&gba->memory.savedata);
597 break;
598 case SAVEDATA_NONE:
599 break;
600 }
601
602 if (_overrides[i].gpio & GPIO_RTC) {
603 GBAGPIOInitRTC(&gba->memory.gpio);
604 }
605
606 if (_overrides[i].gpio & GPIO_GYRO) {
607 GBAGPIOInitGyro(&gba->memory.gpio);
608 }
609
610 if (_overrides[i].gpio & GPIO_RUMBLE) {
611 GBAGPIOInitRumble(&gba->memory.gpio);
612 }
613 return;
614 }
615 }
616}