all repos — mgba @ 38187bac7a5f1fca58f82f64af06881e1999dc68

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