all repos — mgba @ 0131a196d17494abcabf22dd6d07f6c582b6d806

mGBA Game Boy Advance Emulator

src/gba/gba.c (view raw)

  1/* Copyright (c) 2013-2015 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 <mgba/internal/gba/gba.h>
  7
  8#include <mgba/internal/arm/isa-inlines.h>
  9#include <mgba/internal/arm/debugger/debugger.h>
 10#include <mgba/internal/arm/decoder.h>
 11
 12#include <mgba/internal/gba/bios.h>
 13#include <mgba/internal/gba/cheats.h>
 14#include <mgba/internal/gba/io.h>
 15#include <mgba/internal/gba/overrides.h>
 16#include <mgba/internal/gba/rr/rr.h>
 17
 18#include <mgba-util/patch.h>
 19#include <mgba-util/crc32.h>
 20#include <mgba-util/math.h>
 21#include <mgba-util/memory.h>
 22#include <mgba-util/vfs.h>
 23
 24#ifdef USE_ELF
 25#include <mgba-util/elf-read.h>
 26#endif
 27
 28mLOG_DEFINE_CATEGORY(GBA, "GBA", "gba");
 29mLOG_DEFINE_CATEGORY(GBA_DEBUG, "GBA Debug", "gba.debug");
 30
 31const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;
 32
 33static const size_t GBA_ROM_MAGIC_OFFSET = 3;
 34static const uint8_t GBA_ROM_MAGIC[] = { 0xEA };
 35
 36static const size_t GBA_MB_MAGIC_OFFSET = 0xC0;
 37
 38static void GBAInit(void* cpu, struct mCPUComponent* component);
 39static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh);
 40static void GBAProcessEvents(struct ARMCore* cpu);
 41static void GBAHitStub(struct ARMCore* cpu, uint32_t opcode);
 42static void GBAIllegal(struct ARMCore* cpu, uint32_t opcode);
 43static void GBABreakpoint(struct ARMCore* cpu, int immediate);
 44
 45static bool _setSoftwareBreakpoint(struct ARMDebugger*, uint32_t address, enum ExecutionMode mode, uint32_t* opcode);
 46static bool _clearSoftwareBreakpoint(struct ARMDebugger*, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
 47
 48
 49#ifdef FIXED_ROM_BUFFER
 50extern uint32_t* romBuffer;
 51extern size_t romBufferSize;
 52#endif
 53
 54void GBACreate(struct GBA* gba) {
 55	gba->d.id = GBA_COMPONENT_MAGIC;
 56	gba->d.init = GBAInit;
 57	gba->d.deinit = 0;
 58}
 59
 60static void GBAInit(void* cpu, struct mCPUComponent* component) {
 61	struct GBA* gba = (struct GBA*) component;
 62	gba->cpu = cpu;
 63	gba->debugger = 0;
 64	gba->sync = 0;
 65
 66	GBAInterruptHandlerInit(&gba->cpu->irqh);
 67	GBAMemoryInit(gba);
 68
 69	gba->memory.savedata.timing = &gba->timing;
 70	GBASavedataInit(&gba->memory.savedata, NULL);
 71
 72	gba->video.p = gba;
 73	GBAVideoInit(&gba->video);
 74
 75	gba->audio.p = gba;
 76	GBAAudioInit(&gba->audio, GBA_AUDIO_SAMPLES);
 77
 78	GBAIOInit(gba);
 79
 80	gba->sio.p = gba;
 81	GBASIOInit(&gba->sio);
 82
 83	GBAHardwareInit(&gba->memory.hw, NULL);
 84
 85	gba->springIRQ = 0;
 86	gba->keySource = 0;
 87	gba->rotationSource = 0;
 88	gba->luminanceSource = 0;
 89	gba->rtcSource = 0;
 90	gba->rumble = 0;
 91	gba->rr = 0;
 92
 93	gba->romVf = 0;
 94	gba->biosVf = 0;
 95
 96	gba->stream = NULL;
 97	gba->keyCallback = NULL;
 98	mCoreCallbacksListInit(&gba->coreCallbacks, 0);
 99
100	gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
101
102	gba->idleOptimization = IDLE_LOOP_REMOVE;
103	gba->idleLoop = IDLE_LOOP_NONE;
104
105	gba->realisticTiming = true;
106	gba->hardCrash = true;
107	gba->allowOpposingDirections = true;
108
109	gba->performingDMA = false;
110
111	gba->isPristine = false;
112	gba->pristineRomSize = 0;
113	gba->yankedRomSize = 0;
114
115	mTimingInit(&gba->timing, &gba->cpu->cycles, &gba->cpu->nextEvent);
116}
117
118void GBAUnloadROM(struct GBA* gba) {
119	if (gba->memory.rom && !gba->isPristine) {
120		if (gba->yankedRomSize) {
121			gba->yankedRomSize = 0;
122		}
123		mappedMemoryFree(gba->memory.rom, SIZE_CART0);
124	}
125
126	if (gba->romVf) {
127#ifndef FIXED_ROM_BUFFER
128		gba->romVf->unmap(gba->romVf, gba->memory.rom, gba->pristineRomSize);
129#endif
130		gba->romVf->close(gba->romVf);
131		gba->romVf = NULL;
132	}
133	gba->memory.rom = NULL;
134	gba->isPristine = false;
135
136	GBASavedataDeinit(&gba->memory.savedata);
137	if (gba->memory.savedata.realVf) {
138		gba->memory.savedata.realVf->close(gba->memory.savedata.realVf);
139		gba->memory.savedata.realVf = 0;
140	}
141	gba->idleLoop = IDLE_LOOP_NONE;
142}
143
144void GBADestroy(struct GBA* gba) {
145	GBAUnloadROM(gba);
146
147	if (gba->biosVf) {
148		gba->biosVf->unmap(gba->biosVf, gba->memory.bios, SIZE_BIOS);
149		gba->biosVf->close(gba->biosVf);
150		gba->biosVf = 0;
151	}
152
153	GBAMemoryDeinit(gba);
154	GBAVideoDeinit(&gba->video);
155	GBAAudioDeinit(&gba->audio);
156	GBASIODeinit(&gba->sio);
157	gba->rr = 0;
158	mTimingDeinit(&gba->timing);
159	mCoreCallbacksListDeinit(&gba->coreCallbacks);
160}
161
162void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {
163	irqh->reset = GBAReset;
164	irqh->processEvents = GBAProcessEvents;
165	irqh->swi16 = GBASwi16;
166	irqh->swi32 = GBASwi32;
167	irqh->hitIllegal = GBAIllegal;
168	irqh->readCPSR = GBATestIRQ;
169	irqh->hitStub = GBAHitStub;
170	irqh->bkpt16 = GBABreakpoint;
171	irqh->bkpt32 = GBABreakpoint;
172}
173
174void GBAReset(struct ARMCore* cpu) {
175	ARMSetPrivilegeMode(cpu, MODE_IRQ);
176	cpu->gprs[ARM_SP] = SP_BASE_IRQ;
177	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
178	cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
179	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
180	cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
181
182	struct GBA* gba = (struct GBA*) cpu->master;
183	if (!gba->rr || (!gba->rr->isPlaying(gba->rr) && !gba->rr->isRecording(gba->rr))) {
184		GBASavedataUnmask(&gba->memory.savedata);
185	}
186
187	gba->cpuBlocked = false;
188	gba->earlyExit = false;
189	if (gba->yankedRomSize) {
190		gba->memory.romSize = gba->yankedRomSize;
191		gba->memory.romMask = toPow2(gba->memory.romSize) - 1;
192		gba->yankedRomSize = 0;
193	}
194	mTimingClear(&gba->timing);
195	GBAMemoryReset(gba);
196	GBAVideoReset(&gba->video);
197	GBAAudioReset(&gba->audio);
198	GBAIOInit(gba);
199	GBATimerInit(gba);
200
201	GBASIOReset(&gba->sio);
202
203	gba->lastJump = 0;
204	gba->haltPending = false;
205	gba->idleDetectionStep = 0;
206	gba->idleDetectionFailures = 0;
207
208	gba->debug = false;
209	memset(gba->debugString, 0, sizeof(gba->debugString));
210
211	if (!gba->romVf && gba->memory.rom) {
212		GBASkipBIOS(gba);
213	}
214}
215
216void GBASkipBIOS(struct GBA* gba) {
217	struct ARMCore* cpu = gba->cpu;
218	if (cpu->gprs[ARM_PC] == BASE_RESET + WORD_SIZE_ARM) {
219		if (gba->memory.rom) {
220			cpu->gprs[ARM_PC] = BASE_CART0;
221		} else {
222			cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
223		}
224		gba->memory.io[REG_VCOUNT >> 1] = 0x7E;
225		gba->memory.io[REG_POSTFLG >> 1] = 1;
226		int currentCycles = 0;
227		ARM_WRITE_PC;
228	}
229}
230
231static void GBAProcessEvents(struct ARMCore* cpu) {
232	struct GBA* gba = (struct GBA*) cpu->master;
233
234	gba->bus = cpu->prefetch[1];
235	if (cpu->executionMode == MODE_THUMB) {
236		gba->bus |= cpu->prefetch[1] << 16;
237	}
238
239	if (gba->springIRQ && !cpu->cpsr.i) {
240		ARMRaiseIRQ(cpu);
241		gba->springIRQ = 0;
242	}
243
244	int32_t nextEvent = cpu->nextEvent;
245	while (cpu->cycles >= nextEvent) {
246		int32_t cycles = cpu->cycles;
247
248		cpu->cycles = 0;
249		cpu->nextEvent = INT_MAX;
250
251#ifndef NDEBUG
252		if (cycles < 0) {
253			mLOG(GBA, FATAL, "Negative cycles passed: %i", cycles);
254		}
255#endif
256		nextEvent = cycles;
257		do {
258			nextEvent = mTimingTick(&gba->timing, nextEvent);
259		} while (gba->cpuBlocked);
260
261		cpu->nextEvent = nextEvent;
262
263		if (gba->earlyExit) {
264			gba->earlyExit = false;
265			break;
266		}
267		if (cpu->halted) {
268			cpu->cycles = nextEvent;
269			if (!gba->memory.io[REG_IME >> 1] || !gba->memory.io[REG_IE >> 1]) {
270				break;
271			}
272		}
273#ifndef NDEBUG
274		else if (nextEvent < 0) {
275			mLOG(GBA, FATAL, "Negative cycles will pass: %i", nextEvent);
276		}
277#endif
278	}
279}
280
281#ifdef USE_DEBUGGERS
282void GBAAttachDebugger(struct GBA* gba, struct mDebugger* debugger) {
283	gba->debugger = (struct ARMDebugger*) debugger->platform;
284	gba->debugger->setSoftwareBreakpoint = _setSoftwareBreakpoint;
285	gba->debugger->clearSoftwareBreakpoint = _clearSoftwareBreakpoint;
286	gba->cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
287	ARMHotplugAttach(gba->cpu, CPU_COMPONENT_DEBUGGER);
288}
289
290void GBADetachDebugger(struct GBA* gba) {
291	if (gba->debugger) {
292		ARMHotplugDetach(gba->cpu, CPU_COMPONENT_DEBUGGER);
293	}
294	gba->cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
295	gba->debugger = NULL;
296}
297#endif
298
299bool GBALoadNull(struct GBA* gba) {
300	GBAUnloadROM(gba);
301	gba->romVf = NULL;
302	gba->pristineRomSize = 0;
303#ifndef FIXED_ROM_BUFFER
304	gba->memory.rom = anonymousMemoryMap(SIZE_CART0);
305#else
306	gba->memory.rom = romBuffer;
307#endif
308	gba->isPristine = false;
309	gba->yankedRomSize = 0;
310	gba->memory.romSize = SIZE_CART0;
311	gba->memory.romMask = SIZE_CART0 - 1;
312	gba->memory.mirroring = false;
313	gba->romCrc32 = 0;
314
315	if (gba->cpu) {
316		gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
317	}
318	return true;
319}
320
321bool GBALoadMB(struct GBA* gba, struct VFile* vf) {
322	GBAUnloadROM(gba);
323	gba->romVf = vf;
324	gba->pristineRomSize = vf->size(vf);
325	vf->seek(vf, 0, SEEK_SET);
326	if (gba->pristineRomSize > SIZE_WORKING_RAM) {
327		gba->pristineRomSize = SIZE_WORKING_RAM;
328	}
329	gba->isPristine = true;
330	memset(gba->memory.wram, 0, SIZE_WORKING_RAM);
331	vf->read(vf, gba->memory.wram, gba->pristineRomSize);
332	if (!gba->memory.wram) {
333		mLOG(GBA, WARN, "Couldn't map ROM");
334		return false;
335	}
336	gba->yankedRomSize = 0;
337	gba->memory.romSize = 0;
338	gba->memory.romMask = 0;
339	gba->romCrc32 = doCrc32(gba->memory.wram, gba->pristineRomSize);
340	if (gba->cpu && gba->memory.activeRegion == REGION_WORKING_RAM) {
341		gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
342	}
343	return true;
344}
345
346bool GBALoadROM(struct GBA* gba, struct VFile* vf) {
347	if (!vf) {
348		return false;
349	}
350	GBAUnloadROM(gba);
351	gba->romVf = vf;
352	gba->pristineRomSize = vf->size(vf);
353	vf->seek(vf, 0, SEEK_SET);
354	if (gba->pristineRomSize > SIZE_CART0) {
355		gba->pristineRomSize = SIZE_CART0;
356	}
357	gba->isPristine = true;
358#ifdef FIXED_ROM_BUFFER
359	if (gba->pristineRomSize <= romBufferSize) {
360		gba->memory.rom = romBuffer;
361		vf->read(vf, romBuffer, gba->pristineRomSize);
362	}
363#else
364	gba->memory.rom = vf->map(vf, gba->pristineRomSize, MAP_READ);
365#endif
366	if (!gba->memory.rom) {
367		mLOG(GBA, WARN, "Couldn't map ROM");
368		return false;
369	}
370	gba->yankedRomSize = 0;
371	gba->memory.romSize = gba->pristineRomSize;
372	gba->memory.romMask = toPow2(gba->memory.romSize) - 1;
373	gba->memory.mirroring = false;
374	gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
375	GBAHardwareInit(&gba->memory.hw, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
376	GBAVFameDetect(&gba->memory.vfame, gba->memory.rom, gba->memory.romSize);
377	if (popcount32(gba->memory.romSize) != 1) {
378		// This ROM is either a bad dump or homebrew. Emulate flash cart behavior.
379#ifndef FIXED_ROM_BUFFER
380		void* newRom = anonymousMemoryMap(SIZE_CART0);
381		memcpy(newRom, gba->memory.rom, gba->pristineRomSize);
382		gba->memory.rom = newRom;
383#endif
384		gba->memory.romSize = SIZE_CART0;
385		gba->isPristine = false;
386	}
387	if (gba->cpu && gba->memory.activeRegion >= REGION_CART0) {
388		gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
389	}
390	// TODO: error check
391	return true;
392}
393
394bool GBALoadSave(struct GBA* gba, struct VFile* sav) {
395	GBASavedataInit(&gba->memory.savedata, sav);
396	return true;
397}
398
399void GBAYankROM(struct GBA* gba) {
400	gba->yankedRomSize = gba->memory.romSize;
401	gba->memory.romSize = 0;
402	gba->memory.romMask = 0;
403	GBARaiseIRQ(gba, IRQ_GAMEPAK);
404}
405
406void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
407	gba->biosVf = vf;
408	uint32_t* bios = vf->map(vf, SIZE_BIOS, MAP_READ);
409	if (!bios) {
410		mLOG(GBA, WARN, "Couldn't map BIOS");
411		return;
412	}
413	gba->memory.bios = bios;
414	gba->memory.fullBios = 1;
415	uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
416	mLOG(GBA, DEBUG, "BIOS Checksum: 0x%X", checksum);
417	if (checksum == GBA_BIOS_CHECKSUM) {
418		mLOG(GBA, INFO, "Official GBA BIOS detected");
419	} else if (checksum == GBA_DS_BIOS_CHECKSUM) {
420		mLOG(GBA, INFO, "Official GBA (DS) BIOS detected");
421	} else {
422		mLOG(GBA, WARN, "BIOS checksum incorrect");
423	}
424	gba->biosChecksum = checksum;
425	if (gba->memory.activeRegion == REGION_BIOS) {
426		gba->cpu->memory.activeRegion = gba->memory.bios;
427	}
428	// TODO: error check
429}
430
431void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
432	size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
433	if (!patchedSize || patchedSize > SIZE_CART0) {
434		return;
435	}
436	void* newRom = anonymousMemoryMap(SIZE_CART0);
437	if (!patch->applyPatch(patch, gba->memory.rom, gba->pristineRomSize, newRom, patchedSize)) {
438		mappedMemoryFree(newRom, SIZE_CART0);
439		return;
440	}
441	if (gba->romVf) {
442#ifndef FIXED_ROM_BUFFER
443		gba->romVf->unmap(gba->romVf, gba->memory.rom, gba->pristineRomSize);
444#endif
445		gba->romVf->close(gba->romVf);
446		gba->romVf = NULL;
447	}
448	gba->isPristine = false;
449	gba->memory.rom = newRom;
450	gba->memory.hw.gpioBase = &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1];
451	gba->memory.romSize = patchedSize;
452	gba->memory.romMask = SIZE_CART0 - 1;
453	gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
454}
455
456void GBAWriteIE(struct GBA* gba, uint16_t value) {
457	if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
458		ARMRaiseIRQ(gba->cpu);
459	}
460}
461
462void GBAWriteIME(struct GBA* gba, uint16_t value) {
463	if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
464		ARMRaiseIRQ(gba->cpu);
465	}
466}
467
468void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
469	gba->memory.io[REG_IF >> 1] |= 1 << irq;
470
471	if (gba->memory.io[REG_IE >> 1] & 1 << irq) {
472		gba->cpu->halted = 0;
473		if (gba->memory.io[REG_IME >> 1]) {
474			ARMRaiseIRQ(gba->cpu);
475		}
476	}
477}
478
479void GBATestIRQ(struct ARMCore* cpu) {
480	struct GBA* gba = (struct GBA*) cpu->master;
481	if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
482		gba->springIRQ = gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1];
483		gba->cpu->nextEvent = gba->cpu->cycles;
484	}
485}
486
487void GBAHalt(struct GBA* gba) {
488	gba->cpu->nextEvent = gba->cpu->cycles;
489	gba->cpu->halted = 1;
490}
491
492void GBAStop(struct GBA* gba) {
493	size_t c;
494	for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
495		struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
496		if (callbacks->sleep) {
497			callbacks->sleep(callbacks->context);
498		}
499	}
500	gba->cpu->nextEvent = gba->cpu->cycles;
501}
502
503void GBADebug(struct GBA* gba, uint16_t flags) {
504	gba->debugFlags = flags;
505	if (GBADebugFlagsIsSend(gba->debugFlags)) {
506		int level = 1 << GBADebugFlagsGetLevel(gba->debugFlags);
507		level &= 0x1F;
508		char oolBuf[0x101];
509		strncpy(oolBuf, gba->debugString, sizeof(gba->debugString));
510		oolBuf[0x100] = '\0';
511		mLog(_mLOG_CAT_GBA_DEBUG(), level, "%s", oolBuf);
512	}
513	gba->debugFlags = GBADebugFlagsClearSend(gba->debugFlags);
514}
515
516bool GBAIsROM(struct VFile* vf) {
517#ifdef USE_ELF
518	struct ELF* elf = ELFOpen(vf);
519	if (elf) {
520		uint32_t entry = ELFEntry(elf);
521		bool isGBA = true;
522		isGBA = isGBA && ELFMachine(elf) == EM_ARM;
523		isGBA = isGBA && (entry == BASE_CART0 || entry == BASE_WORKING_RAM);
524		ELFClose(elf);
525		return isGBA;
526	}
527#endif
528	if (!vf) {
529		return false;
530	}
531	if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
532		return false;
533	}
534	uint8_t signature[sizeof(GBA_ROM_MAGIC)];
535	if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
536		return false;
537	}
538	if (GBAIsBIOS(vf)) {
539		return false;
540	}
541	return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
542}
543
544bool GBAIsMB(struct VFile* vf) {
545	if (!GBAIsROM(vf)) {
546		return false;
547	}
548#ifdef USE_ELF
549	struct ELF* elf = ELFOpen(vf);
550	if (elf) {
551		bool isMB = ELFEntry(elf) == BASE_WORKING_RAM;
552		ELFClose(elf);
553		return isMB;
554	}
555#endif
556	if (vf->size(vf) > SIZE_WORKING_RAM) {
557		return false;
558	}
559	if (vf->seek(vf, GBA_MB_MAGIC_OFFSET, SEEK_SET) < 0) {
560		return false;
561	}
562	uint32_t signature;
563	if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
564		return false;
565	}
566	uint32_t opcode;
567	LOAD_32(opcode, 0, &signature);
568	struct ARMInstructionInfo info;
569	ARMDecodeARM(opcode, &info);
570	if (info.branchType == ARM_BRANCH) {
571		if (info.op1.immediate <= 0) {
572			return false;
573		} else if (info.op1.immediate == 28) {
574			// Ancient toolchain that is known to throw MB detection for a loop
575			return false;
576		} else if (info.op1.immediate != 24) {
577			return true;
578		}
579	}
580
581	uint32_t pc = GBA_MB_MAGIC_OFFSET;
582	int i;
583	for (i = 0; i < 80; ++i) {
584		if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
585			break;
586		}
587		pc += 4;
588		LOAD_32(opcode, 0, &signature);
589		ARMDecodeARM(opcode, &info);
590		if (info.mnemonic != ARM_MN_LDR) {
591			continue;
592		}
593		if ((info.operandFormat & ARM_OPERAND_MEMORY) && info.memory.baseReg == ARM_PC && info.memory.format & ARM_MEMORY_IMMEDIATE_OFFSET) {
594			uint32_t immediate = info.memory.offset.immediate;
595			if (info.memory.format & ARM_MEMORY_OFFSET_SUBTRACT) {
596				immediate = -immediate;
597			}
598			immediate += pc + 8;
599			if (vf->seek(vf, immediate, SEEK_SET) < 0) {
600				break;
601			}
602			if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
603				break;
604			}
605			LOAD_32(immediate, 0, &signature);
606			if (vf->seek(vf, pc, SEEK_SET) < 0) {
607				break;
608			}
609			if ((immediate & ~0x7FF) == BASE_WORKING_RAM) {
610				return true;
611			}
612		}
613	}
614	// Found a libgba-linked cart...these are a bit harder to detect.
615	return false;
616}
617
618bool GBAIsBIOS(struct VFile* vf) {
619	if (vf->seek(vf, 0, SEEK_SET) < 0) {
620		return false;
621	}
622	uint8_t interruptTable[7 * 4];
623	if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
624		return false;
625	}
626	int i;
627	for (i = 0; i < 7; ++i) {
628		if (interruptTable[4 * i + 3] != 0xEA || interruptTable[4 * i + 2]) {
629			return false;
630		}
631	}
632	return true;
633}
634
635void GBAGetGameCode(const struct GBA* gba, char* out) {
636	memset(out, 0, 8);
637	if (!gba->memory.rom) {
638		return;
639	}
640
641	memcpy(out, "AGB-", 4);
642	memcpy(&out[4], &((struct GBACartridge*) gba->memory.rom)->id, 4);
643}
644
645void GBAGetGameTitle(const struct GBA* gba, char* out) {
646	if (gba->memory.rom) {
647		memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
648		return;
649	}
650	if (gba->isPristine && gba->memory.wram) {
651		memcpy(out, &((struct GBACartridge*) gba->memory.wram)->title, 12);
652		return;
653	}
654	strncpy(out, "(BIOS)", 12);
655}
656
657void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
658	struct GBA* gba = (struct GBA*) cpu->master;
659#ifdef USE_DEBUGGERS
660	if (gba->debugger) {
661		struct mDebuggerEntryInfo info = {
662			.address = _ARMPCAddress(cpu),
663			.type.bp.opcode = opcode
664		};
665		mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
666	}
667#endif
668	// TODO: More sensible category?
669	mLOG(GBA, ERROR, "Stub opcode: %08x", opcode);
670}
671
672void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
673	struct GBA* gba = (struct GBA*) cpu->master;
674	if (!gba->yankedRomSize) {
675		// TODO: More sensible category?
676		mLOG(GBA, WARN, "Illegal opcode: %08x", opcode);
677	}
678	if (cpu->executionMode == MODE_THUMB && (opcode & 0xFFC0) == 0xE800) {
679		mLOG(GBA, DEBUG, "Hit Wii U VC opcode: %08x", opcode);
680		return;
681	}
682#ifdef USE_DEBUGGERS
683	if (gba->debugger) {
684		struct mDebuggerEntryInfo info = {
685			.address = _ARMPCAddress(cpu),
686			.type.bp.opcode = opcode
687		};
688		mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
689	} else
690#endif
691	{
692		ARMRaiseUndefined(cpu);
693	}
694}
695
696void GBABreakpoint(struct ARMCore* cpu, int immediate) {
697	struct GBA* gba = (struct GBA*) cpu->master;
698	if (immediate >= CPU_COMPONENT_MAX) {
699		return;
700	}
701	switch (immediate) {
702#ifdef USE_DEBUGGERS
703	case CPU_COMPONENT_DEBUGGER:
704		if (gba->debugger) {
705			struct mDebuggerEntryInfo info = {
706				.address = _ARMPCAddress(cpu),
707				.type.bp.breakType = BREAKPOINT_SOFTWARE
708			};
709			mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_BREAKPOINT, &info);
710		}
711		break;
712#endif
713	case CPU_COMPONENT_CHEAT_DEVICE:
714		if (gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
715			struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
716			struct GBACheatHook* hook = 0;
717			size_t i;
718			for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
719				struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
720				if (cheats->hook && cheats->hook->address == _ARMPCAddress(cpu)) {
721					mCheatRefresh(device, &cheats->d);
722					hook = cheats->hook;
723				}
724			}
725			if (hook) {
726				ARMRunFake(cpu, hook->patchedOpcode);
727			}
728		}
729		break;
730	default:
731		break;
732	}
733}
734
735void GBAFrameStarted(struct GBA* gba) {
736	GBATestKeypadIRQ(gba);
737
738	size_t c;
739	for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
740		struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
741		if (callbacks->videoFrameStarted) {
742			callbacks->videoFrameStarted(callbacks->context);
743		}
744	}
745}
746
747void GBAFrameEnded(struct GBA* gba) {
748	GBASavedataClean(&gba->memory.savedata, gba->video.frameCounter);
749
750	if (gba->rr) {
751		gba->rr->nextFrame(gba->rr);
752	}
753
754	if (gba->cpu->components && gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
755		struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
756		size_t i;
757		for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
758			struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
759			mCheatRefresh(device, &cheats->d);
760		}
761	}
762
763	if (gba->stream && gba->stream->postVideoFrame) {
764		const color_t* pixels;
765		size_t stride;
766		gba->video.renderer->getPixels(gba->video.renderer, &stride, (const void**) &pixels);
767		gba->stream->postVideoFrame(gba->stream, pixels, stride);
768	}
769
770	if (gba->memory.hw.devices & (HW_GB_PLAYER | HW_GB_PLAYER_DETECTION)) {
771		GBAHardwarePlayerUpdate(gba);
772	}
773
774	size_t c;
775	for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
776		struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
777		if (callbacks->videoFrameEnded) {
778			callbacks->videoFrameEnded(callbacks->context);
779		}
780	}
781}
782
783void GBATestKeypadIRQ(struct GBA* gba) {
784	uint16_t keycnt = gba->memory.io[REG_KEYCNT >> 1];
785	if (!(keycnt & 0x4000)) {
786		return;
787	}
788	int isAnd = keycnt & 0x8000;
789	if (!gba->keySource) {
790		// TODO?
791		return;
792	}
793
794	keycnt &= 0x3FF;
795	uint16_t keyInput = *gba->keySource & keycnt;
796
797	if (isAnd && keycnt == keyInput) {
798		GBARaiseIRQ(gba, IRQ_KEYPAD);
799	} else if (!isAnd && keyInput) {
800		GBARaiseIRQ(gba, IRQ_KEYPAD);
801	}
802}
803
804void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
805	size_t immediate;
806	for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) {
807		if (gba->cpu->components[immediate] == component) {
808			break;
809		}
810	}
811	if (immediate == gba->cpu->numComponents) {
812		return;
813	}
814	if (mode == MODE_ARM) {
815		int32_t value;
816		int32_t old;
817		value = 0xE1200070;
818		value |= immediate & 0xF;
819		value |= (immediate & 0xFFF0) << 4;
820		GBAPatch32(gba->cpu, address, value, &old);
821		*opcode = old;
822	} else {
823		int16_t value;
824		int16_t old;
825		value = 0xBE00;
826		value |= immediate & 0xFF;
827		GBAPatch16(gba->cpu, address, value, &old);
828		*opcode = (uint16_t) old;
829	}
830}
831
832void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
833	if (mode == MODE_ARM) {
834		GBAPatch32(gba->cpu, address, opcode, 0);
835	} else {
836		GBAPatch16(gba->cpu, address, opcode, 0);
837	}
838}
839
840static bool _setSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
841	GBASetBreakpoint((struct GBA*) debugger->cpu->master, &debugger->d.p->d, address, mode, opcode);
842	return true;
843}
844
845static bool _clearSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
846	GBAClearBreakpoint((struct GBA*) debugger->cpu->master, address, mode, opcode);
847	return true;
848}