all repos — mgba @ 94c077703d9b56680f6bcaede01734b6a0ed1760

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