all repos — mgba @ 6456a886695e6290fee373255fef014f47c20562

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