all repos — mgba @ 823b97603a2538b6b103b8f2388a7124cdcde5fa

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