all repos — mgba @ 4d2ccd5df438b988c21af6959c3704aec590a075

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