all repos — mgba @ 10fc916425cbd4650bf8c8a56f8c9428bed052d7

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