all repos — mgba @ 3202811e416e8b7c6ddc8a68442536510aa2b85e

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