all repos — mgba @ 81812fa81858c6297de788fc8b5e3186dd743646

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