all repos — mgba @ b4fa4fe77e23ed774e749608144fc0d1330b941c

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