all repos — mgba @ 7e90eb2631c437ab2af551a09d0dbb529714a286

mGBA Game Boy Advance Emulator

src/gba/gba.c (view raw)

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