all repos — mgba @ a9ccb0fdd723cfa45a226925e2a059c05057389c

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 "gba.h"
  7
  8#include "core/thread.h"
  9
 10#include "arm/decoder.h"
 11#include "arm/debugger/debugger.h"
 12#include "arm/isa-inlines.h"
 13
 14#include "gba/bios.h"
 15#include "gba/cheats.h"
 16#include "gba/io.h"
 17#include "gba/overrides.h"
 18#include "gba/rr/rr.h"
 19#include "gba/serialize.h"
 20#include "gba/sio.h"
 21#include "gba/timer.h"
 22#include "gba/vfame.h"
 23
 24#include "util/crc32.h"
 25#include "util/memory.h"
 26#include "util/math.h"
 27#include "util/patch.h"
 28#include "util/vfs.h"
 29
 30mLOG_DEFINE_CATEGORY(GBA, "GBA");
 31mLOG_DEFINE_CATEGORY(GBA_DEBUG, "GBA Debug");
 32
 33const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;
 34
 35static const size_t GBA_ROM_MAGIC_OFFSET = 3;
 36static const uint8_t GBA_ROM_MAGIC[] = { 0xEA };
 37
 38static const size_t GBA_MB_MAGIC_OFFSET = 0xC0;
 39
 40static void GBAInit(void* cpu, struct mCPUComponent* component);
 41static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh);
 42static void GBAProcessEvents(struct ARMCore* cpu);
 43static void GBAHitStub(struct ARMCore* cpu, uint32_t opcode);
 44static void GBAIllegal(struct ARMCore* cpu, uint32_t opcode);
 45static void GBABreakpoint(struct ARMCore* cpu, int immediate);
 46
 47static bool _setSoftwareBreakpoint(struct ARMDebugger*, uint32_t address, enum ExecutionMode mode, uint32_t* opcode);
 48static bool _clearSoftwareBreakpoint(struct ARMDebugger*, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
 49
 50
 51#ifdef _3DS
 52extern uint32_t* romBuffer;
 53extern size_t romBufferSize;
 54#endif
 55
 56void GBACreate(struct GBA* gba) {
 57	gba->d.id = GBA_COMPONENT_MAGIC;
 58	gba->d.init = GBAInit;
 59	gba->d.deinit = 0;
 60}
 61
 62static void GBAInit(void* cpu, struct mCPUComponent* component) {
 63	struct GBA* gba = (struct GBA*) component;
 64	gba->cpu = cpu;
 65	gba->debugger = 0;
 66	gba->sync = 0;
 67
 68	GBAInterruptHandlerInit(&gba->cpu->irqh);
 69	GBAMemoryInit(gba);
 70	GBASavedataInit(&gba->memory.savedata, 0);
 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	gba->springIRQ = 0;
 84	gba->keySource = 0;
 85	gba->rotationSource = 0;
 86	gba->luminanceSource = 0;
 87	gba->rtcSource = 0;
 88	gba->rumble = 0;
 89	gba->rr = 0;
 90
 91	gba->romVf = 0;
 92	gba->biosVf = 0;
 93
 94	gba->stream = NULL;
 95	gba->keyCallback = NULL;
 96	gba->stopCallback = NULL;
 97	gba->stopCallback = NULL;
 98	gba->coreCallbacks = NULL;
 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->pristineRom = 0;
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->pristineRom != gba->memory.rom) {
120		if (gba->yankedRomSize) {
121			gba->yankedRomSize = 0;
122		}
123		mappedMemoryFree(gba->memory.rom, SIZE_CART0);
124	}
125	gba->memory.rom = 0;
126
127	if (gba->romVf) {
128#ifndef _3DS
129		gba->romVf->unmap(gba->romVf, gba->pristineRom, gba->pristineRomSize);
130#endif
131		gba->romVf->close(gba->romVf);
132		gba->romVf = 0;
133	}
134	gba->pristineRom = 0;
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}
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
276void GBAAttachDebugger(struct GBA* gba, struct mDebugger* debugger) {
277	gba->debugger = (struct ARMDebugger*) debugger->platform;
278	gba->debugger->setSoftwareBreakpoint = _setSoftwareBreakpoint;
279	gba->debugger->clearSoftwareBreakpoint = _clearSoftwareBreakpoint;
280	gba->cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
281	ARMHotplugAttach(gba->cpu, CPU_COMPONENT_DEBUGGER);
282}
283
284void GBADetachDebugger(struct GBA* gba) {
285	gba->debugger = 0;
286	ARMHotplugDetach(gba->cpu, CPU_COMPONENT_DEBUGGER);
287	gba->cpu->components[CPU_COMPONENT_DEBUGGER] = 0;
288}
289
290bool GBALoadMB(struct GBA* gba, struct VFile* vf) {
291	GBAUnloadROM(gba);
292	gba->romVf = vf;
293	gba->pristineRomSize = vf->size(vf);
294	vf->seek(vf, 0, SEEK_SET);
295	if (gba->pristineRomSize > SIZE_WORKING_RAM) {
296		gba->pristineRomSize = SIZE_WORKING_RAM;
297	}
298#ifdef _3DS
299	gba->pristineRom = 0;
300	if (gba->pristineRomSize <= romBufferSize) {
301		gba->pristineRom = romBuffer;
302		vf->read(vf, romBuffer, gba->pristineRomSize);
303	}
304#else
305	gba->pristineRom = vf->map(vf, gba->pristineRomSize, MAP_READ);
306#endif
307	if (!gba->pristineRom) {
308		mLOG(GBA, WARN, "Couldn't map ROM");
309		return false;
310	}
311	gba->yankedRomSize = 0;
312	gba->memory.romSize = 0;
313	gba->memory.romMask = 0;
314	gba->romCrc32 = doCrc32(gba->pristineRom, gba->pristineRomSize);
315	return true;
316}
317
318bool GBALoadROM(struct GBA* gba, struct VFile* vf) {
319	if (!vf) {
320		return false;
321	}
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_CART0) {
327		gba->pristineRomSize = SIZE_CART0;
328	}
329#ifdef _3DS
330	gba->pristineRom = 0;
331	if (gba->pristineRomSize <= romBufferSize) {
332		gba->pristineRom = romBuffer;
333		vf->read(vf, romBuffer, gba->pristineRomSize);
334	}
335#else
336	gba->pristineRom = vf->map(vf, gba->pristineRomSize, MAP_READ);
337#endif
338	if (!gba->pristineRom) {
339		mLOG(GBA, WARN, "Couldn't map ROM");
340		return false;
341	}
342	gba->yankedRomSize = 0;
343	gba->memory.rom = gba->pristineRom;
344	gba->memory.romSize = gba->pristineRomSize;
345	gba->memory.romMask = toPow2(gba->memory.romSize) - 1;
346	gba->memory.mirroring = false;
347	gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
348	GBAHardwareInit(&gba->memory.hw, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
349	GBAVFameDetect(&gba->memory.vfame, gba->memory.rom, gba->memory.romSize);
350	// TODO: error check
351	return true;
352}
353
354bool GBALoadSave(struct GBA* gba, struct VFile* sav) {
355	GBASavedataInit(&gba->memory.savedata, sav);
356	return true;
357}
358
359void GBAYankROM(struct GBA* gba) {
360	gba->yankedRomSize = gba->memory.romSize;
361	gba->memory.romSize = 0;
362	gba->memory.romMask = 0;
363	GBARaiseIRQ(gba, IRQ_GAMEPAK);
364}
365
366void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
367	gba->biosVf = vf;
368	uint32_t* bios = vf->map(vf, SIZE_BIOS, MAP_READ);
369	if (!bios) {
370		mLOG(GBA, WARN, "Couldn't map BIOS");
371		return;
372	}
373	gba->memory.bios = bios;
374	gba->memory.fullBios = 1;
375	uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
376	mLOG(GBA, DEBUG, "BIOS Checksum: 0x%X", checksum);
377	if (checksum == GBA_BIOS_CHECKSUM) {
378		mLOG(GBA, INFO, "Official GBA BIOS detected");
379	} else if (checksum == GBA_DS_BIOS_CHECKSUM) {
380		mLOG(GBA, INFO, "Official GBA (DS) BIOS detected");
381	} else {
382		mLOG(GBA, WARN, "BIOS checksum incorrect");
383	}
384	gba->biosChecksum = checksum;
385	if (gba->memory.activeRegion == REGION_BIOS) {
386		gba->cpu->memory.activeRegion = gba->memory.bios;
387	}
388	// TODO: error check
389}
390
391void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
392	size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
393	if (!patchedSize || patchedSize > SIZE_CART0) {
394		return;
395	}
396	gba->memory.rom = anonymousMemoryMap(SIZE_CART0);
397	if (!patch->applyPatch(patch, gba->pristineRom, gba->pristineRomSize, gba->memory.rom, patchedSize)) {
398		mappedMemoryFree(gba->memory.rom, patchedSize);
399		gba->memory.rom = gba->pristineRom;
400		return;
401	}
402	gba->memory.romSize = patchedSize;
403	gba->memory.romMask = SIZE_CART0 - 1;
404	gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
405}
406
407void GBAWriteIE(struct GBA* gba, uint16_t value) {
408	if (value & (1 << IRQ_KEYPAD)) {
409		mLOG(GBA, STUB, "Keypad interrupts not implemented");
410	}
411
412	if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
413		ARMRaiseIRQ(gba->cpu);
414	}
415}
416
417void GBAWriteIME(struct GBA* gba, uint16_t value) {
418	if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
419		ARMRaiseIRQ(gba->cpu);
420	}
421}
422
423void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
424	gba->memory.io[REG_IF >> 1] |= 1 << irq;
425
426	if (gba->memory.io[REG_IE >> 1] & 1 << irq) {
427		gba->cpu->halted = 0;
428		if (gba->memory.io[REG_IME >> 1]) {
429			ARMRaiseIRQ(gba->cpu);
430		}
431	}
432}
433
434void GBATestIRQ(struct ARMCore* cpu) {
435	struct GBA* gba = (struct GBA*) cpu->master;
436	if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
437		gba->springIRQ = gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1];
438		gba->cpu->nextEvent = gba->cpu->cycles;
439	}
440}
441
442void GBAHalt(struct GBA* gba) {
443	gba->cpu->nextEvent = gba->cpu->cycles;
444	gba->cpu->halted = 1;
445}
446
447void GBAStop(struct GBA* gba) {
448	if (!gba->stopCallback) {
449		return;
450	}
451	gba->cpu->nextEvent = gba->cpu->cycles;
452	gba->stopCallback->stop(gba->stopCallback);
453}
454
455void GBADebug(struct GBA* gba, uint16_t flags) {
456	gba->debugFlags = flags;
457	if (GBADebugFlagsIsSend(gba->debugFlags)) {
458		int level = 1 << GBADebugFlagsGetLevel(gba->debugFlags);
459		level &= 0x1F;
460		char oolBuf[0x101];
461		strncpy(oolBuf, gba->debugString, sizeof(gba->debugString));
462		oolBuf[0x100] = '\0';
463		mLog(_mLOG_CAT_GBA_DEBUG(), level, "%s", oolBuf);
464	}
465	gba->debugFlags = GBADebugFlagsClearSend(gba->debugFlags);
466}
467
468bool GBAIsROM(struct VFile* vf) {
469	if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
470		return false;
471	}
472	uint8_t signature[sizeof(GBA_ROM_MAGIC)];
473	if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
474		return false;
475	}
476	if (GBAIsBIOS(vf)) {
477		return false;
478	}
479	return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
480}
481
482bool GBAIsMB(struct VFile* vf) {
483	if (!GBAIsROM(vf)) {
484		return false;
485	}
486	if (vf->size(vf) > SIZE_WORKING_RAM) {
487		return false;
488	}
489	if (vf->seek(vf, GBA_MB_MAGIC_OFFSET, SEEK_SET) < 0) {
490		return false;
491	}
492	uint32_t signature;
493	if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
494		return false;
495	}
496	uint32_t opcode;
497	LOAD_32(opcode, 0, &signature);
498	struct ARMInstructionInfo info;
499	ARMDecodeARM(opcode, &info);
500	if (info.branchType != ARM_BRANCH) {
501		return false;
502	}
503	if (info.op1.immediate <= 0) {
504		return false;
505	} else if (info.op1.immediate == 28) {
506		// Ancient toolchain that is known to throw MB detection for a loop
507		return false;
508	} else if (info.op1.immediate != 24) {
509		return true;
510	}
511	// Found a libgba-linked cart...these are a bit harder to detect.
512	return false;
513}
514
515bool GBAIsBIOS(struct VFile* vf) {
516	if (vf->seek(vf, 0, SEEK_SET) < 0) {
517		return false;
518	}
519	uint8_t interruptTable[7 * 4];
520	if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
521		return false;
522	}
523	int i;
524	for (i = 0; i < 7; ++i) {
525		if (interruptTable[4 * i + 3] != 0xEA || interruptTable[4 * i + 2]) {
526			return false;
527		}
528	}
529	return true;
530}
531
532void GBAGetGameCode(const struct GBA* gba, char* out) {
533	memset(out, 0, 8);
534	if (!gba->memory.rom) {
535		return;
536	}
537
538	memcpy(out, "AGB-", 4);
539	memcpy(&out[4], &((struct GBACartridge*) gba->memory.rom)->id, 4);
540}
541
542void GBAGetGameTitle(const struct GBA* gba, char* out) {
543	if (gba->memory.rom) {
544		memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
545		return;
546	}
547	if (gba->pristineRom) {
548		memcpy(out, &((struct GBACartridge*) gba->pristineRom)->title, 12);
549		return;
550	}
551	strncpy(out, "(BIOS)", 12);
552}
553
554void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
555	struct GBA* gba = (struct GBA*) cpu->master;
556#ifdef USE_DEBUGGERS
557	if (gba->debugger) {
558		struct mDebuggerEntryInfo info = {
559			.address = _ARMPCAddress(cpu),
560			.opcode = opcode
561		};
562		mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
563	}
564#endif
565	// TODO: More sensible category?
566	mLOG(GBA, ERROR, "Stub opcode: %08x", opcode);
567}
568
569void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
570	struct GBA* gba = (struct GBA*) cpu->master;
571	if (!gba->yankedRomSize) {
572		// TODO: More sensible category?
573		mLOG(GBA, WARN, "Illegal opcode: %08x", opcode);
574	}
575#ifdef USE_DEBUGGERS
576	if (gba->debugger) {
577		struct mDebuggerEntryInfo info = {
578			.address = _ARMPCAddress(cpu),
579			.opcode = opcode
580		};
581		mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
582	} else
583#endif
584	{
585		ARMRaiseUndefined(cpu);
586	}
587}
588
589void GBABreakpoint(struct ARMCore* cpu, int immediate) {
590	struct GBA* gba = (struct GBA*) cpu->master;
591	if (immediate >= CPU_COMPONENT_MAX) {
592		return;
593	}
594	switch (immediate) {
595#ifdef USE_DEBUGGERS
596	case CPU_COMPONENT_DEBUGGER:
597		if (gba->debugger) {
598			struct mDebuggerEntryInfo info = {
599				.address = _ARMPCAddress(cpu),
600				.breakType = BREAKPOINT_SOFTWARE
601			};
602			mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_BREAKPOINT, &info);
603		}
604		break;
605#endif
606	case CPU_COMPONENT_CHEAT_DEVICE:
607		if (gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
608			struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
609			struct GBACheatHook* hook = 0;
610			size_t i;
611			for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
612				struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
613				if (cheats->hook && cheats->hook->address == _ARMPCAddress(cpu)) {
614					mCheatRefresh(device, &cheats->d);
615					hook = cheats->hook;
616				}
617			}
618			if (hook) {
619				ARMRunFake(cpu, hook->patchedOpcode);
620			}
621		}
622		break;
623	default:
624		break;
625	}
626}
627
628void GBAFrameStarted(struct GBA* gba) {
629	UNUSED(gba);
630
631	struct mCoreCallbacks* callbacks = gba->coreCallbacks;
632	if (callbacks && callbacks->videoFrameStarted) {
633		callbacks->videoFrameStarted(callbacks->context);
634	}
635}
636
637void GBAFrameEnded(struct GBA* gba) {
638	GBASavedataClean(&gba->memory.savedata, gba->video.frameCounter);
639
640	if (gba->rr) {
641		gba->rr->nextFrame(gba->rr);
642	}
643
644	if (gba->cpu->components && gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
645		struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
646		size_t i;
647		for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
648			struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
649			mCheatRefresh(device, &cheats->d);
650		}
651	}
652
653	if (gba->stream && gba->stream->postVideoFrame) {
654		const color_t* pixels;
655		size_t stride;
656		gba->video.renderer->getPixels(gba->video.renderer, &stride, (const void**) &pixels);
657		gba->stream->postVideoFrame(gba->stream, pixels, stride);
658	}
659
660	if (gba->memory.hw.devices & (HW_GB_PLAYER | HW_GB_PLAYER_DETECTION)) {
661		GBAHardwarePlayerUpdate(gba);
662	}
663
664	struct mCoreCallbacks* callbacks = gba->coreCallbacks;
665	if (callbacks && callbacks->videoFrameEnded) {
666		callbacks->videoFrameEnded(callbacks->context);
667	}
668}
669
670void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
671	size_t immediate;
672	for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) {
673		if (gba->cpu->components[immediate] == component) {
674			break;
675		}
676	}
677	if (immediate == gba->cpu->numComponents) {
678		return;
679	}
680	if (mode == MODE_ARM) {
681		int32_t value;
682		int32_t old;
683		value = 0xE1200070;
684		value |= immediate & 0xF;
685		value |= (immediate & 0xFFF0) << 4;
686		GBAPatch32(gba->cpu, address, value, &old);
687		*opcode = old;
688	} else {
689		int16_t value;
690		int16_t old;
691		value = 0xBE00;
692		value |= immediate & 0xFF;
693		GBAPatch16(gba->cpu, address, value, &old);
694		*opcode = (uint16_t) old;
695	}
696}
697
698void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
699	if (mode == MODE_ARM) {
700		GBAPatch32(gba->cpu, address, opcode, 0);
701	} else {
702		GBAPatch16(gba->cpu, address, opcode, 0);
703	}
704}
705
706static bool _setSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
707	GBASetBreakpoint((struct GBA*) debugger->cpu->master, &debugger->d.p->d, address, mode, opcode);
708	return true;
709}
710
711static bool _clearSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
712	GBAClearBreakpoint((struct GBA*) debugger->cpu->master, address, mode, opcode);
713	return true;
714}