all repos — mgba @ ec52a47dd01ab35db1fa6a33b2b470ce63a04118

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