all repos — mgba @ 7edf934a59ef29ad76e23aa935cdef8557a178b2

mGBA Game Boy Advance Emulator

src/gba/gba.c (view raw)

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