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 if (gba->yankedRomSize) {
188 gba->memory.romSize = gba->yankedRomSize;
189 gba->memory.romMask = toPow2(gba->memory.romSize) - 1;
190 gba->yankedRomSize = 0;
191 }
192 mTimingClear(&gba->timing);
193 GBAMemoryReset(gba);
194 GBAVideoReset(&gba->video);
195 GBAAudioReset(&gba->audio);
196 GBAIOInit(gba);
197 GBATimerInit(gba);
198
199 GBASIOReset(&gba->sio);
200
201 gba->lastJump = 0;
202 gba->haltPending = false;
203 gba->idleDetectionStep = 0;
204 gba->idleDetectionFailures = 0;
205
206 gba->debug = false;
207 memset(gba->debugString, 0, sizeof(gba->debugString));
208}
209
210void GBASkipBIOS(struct GBA* gba) {
211 struct ARMCore* cpu = gba->cpu;
212 if (cpu->gprs[ARM_PC] == BASE_RESET + WORD_SIZE_ARM) {
213 if (gba->memory.rom) {
214 cpu->gprs[ARM_PC] = BASE_CART0;
215 } else {
216 cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
217 }
218 gba->memory.io[REG_VCOUNT >> 1] = 0x7E;
219 gba->memory.io[REG_POSTFLG >> 1] = 1;
220 int currentCycles = 0;
221 ARM_WRITE_PC;
222 }
223}
224
225static void GBAProcessEvents(struct ARMCore* cpu) {
226 struct GBA* gba = (struct GBA*) cpu->master;
227
228 gba->bus = cpu->prefetch[1];
229 if (cpu->executionMode == MODE_THUMB) {
230 gba->bus |= cpu->prefetch[1] << 16;
231 }
232
233 if (gba->springIRQ && !cpu->cpsr.i) {
234 ARMRaiseIRQ(cpu);
235 gba->springIRQ = 0;
236 }
237
238 int32_t nextEvent;
239 do {
240 int32_t cycles = cpu->cycles;
241 int32_t testEvent;
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 mTimingTick(&gba->timing, nextEvent);
254 nextEvent = cpu->nextEvent;
255 } while (gba->cpuBlocked);
256
257 testEvent = GBASIOProcessEvents(&gba->sio, cycles);
258 if (testEvent < nextEvent) {
259 nextEvent = testEvent;
260 }
261
262 cpu->nextEvent = nextEvent;
263
264 if (nextEvent == 0) {
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 } while (cpu->cycles >= nextEvent);
279}
280
281void GBAAttachDebugger(struct GBA* gba, struct mDebugger* debugger) {
282 gba->debugger = (struct ARMDebugger*) debugger->platform;
283 gba->debugger->setSoftwareBreakpoint = _setSoftwareBreakpoint;
284 gba->debugger->clearSoftwareBreakpoint = _clearSoftwareBreakpoint;
285 gba->cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
286 ARMHotplugAttach(gba->cpu, CPU_COMPONENT_DEBUGGER);
287}
288
289void GBADetachDebugger(struct GBA* gba) {
290 gba->debugger = 0;
291 ARMHotplugDetach(gba->cpu, CPU_COMPONENT_DEBUGGER);
292 gba->cpu->components[CPU_COMPONENT_DEBUGGER] = 0;
293}
294
295bool GBALoadMB(struct GBA* gba, struct VFile* vf) {
296 GBAUnloadROM(gba);
297 gba->romVf = vf;
298 gba->pristineRomSize = vf->size(vf);
299 vf->seek(vf, 0, SEEK_SET);
300 if (gba->pristineRomSize > SIZE_WORKING_RAM) {
301 gba->pristineRomSize = SIZE_WORKING_RAM;
302 }
303#ifdef _3DS
304 gba->pristineRom = 0;
305 if (gba->pristineRomSize <= romBufferSize) {
306 gba->pristineRom = romBuffer;
307 vf->read(vf, romBuffer, gba->pristineRomSize);
308 }
309#else
310 gba->pristineRom = vf->map(vf, gba->pristineRomSize, MAP_READ);
311#endif
312 if (!gba->pristineRom) {
313 mLOG(GBA, WARN, "Couldn't map ROM");
314 return false;
315 }
316 gba->yankedRomSize = 0;
317 gba->memory.romSize = 0;
318 gba->memory.romMask = 0;
319 gba->romCrc32 = doCrc32(gba->pristineRom, gba->pristineRomSize);
320 return true;
321}
322
323bool GBALoadROM(struct GBA* gba, struct VFile* vf) {
324 if (!vf) {
325 return false;
326 }
327 GBAUnloadROM(gba);
328 gba->romVf = vf;
329 gba->pristineRomSize = vf->size(vf);
330 vf->seek(vf, 0, SEEK_SET);
331 if (gba->pristineRomSize > SIZE_CART0) {
332 gba->pristineRomSize = SIZE_CART0;
333 }
334#ifdef _3DS
335 gba->pristineRom = 0;
336 if (gba->pristineRomSize <= romBufferSize) {
337 gba->pristineRom = romBuffer;
338 vf->read(vf, romBuffer, gba->pristineRomSize);
339 }
340#else
341 gba->pristineRom = vf->map(vf, gba->pristineRomSize, MAP_READ);
342#endif
343 if (!gba->pristineRom) {
344 mLOG(GBA, WARN, "Couldn't map ROM");
345 return false;
346 }
347 gba->yankedRomSize = 0;
348 gba->memory.rom = gba->pristineRom;
349 gba->memory.romSize = gba->pristineRomSize;
350 gba->memory.romMask = toPow2(gba->memory.romSize) - 1;
351 gba->memory.mirroring = false;
352 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
353 GBAHardwareInit(&gba->memory.hw, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
354 GBAVFameDetect(&gba->memory.vfame, gba->memory.rom, gba->memory.romSize);
355 // TODO: error check
356 return true;
357}
358
359bool GBALoadSave(struct GBA* gba, struct VFile* sav) {
360 GBASavedataInit(&gba->memory.savedata, sav);
361 return true;
362}
363
364void GBAYankROM(struct GBA* gba) {
365 gba->yankedRomSize = gba->memory.romSize;
366 gba->memory.romSize = 0;
367 gba->memory.romMask = 0;
368 GBARaiseIRQ(gba, IRQ_GAMEPAK);
369}
370
371void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
372 gba->biosVf = vf;
373 uint32_t* bios = vf->map(vf, SIZE_BIOS, MAP_READ);
374 if (!bios) {
375 mLOG(GBA, WARN, "Couldn't map BIOS");
376 return;
377 }
378 gba->memory.bios = bios;
379 gba->memory.fullBios = 1;
380 uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
381 mLOG(GBA, DEBUG, "BIOS Checksum: 0x%X", checksum);
382 if (checksum == GBA_BIOS_CHECKSUM) {
383 mLOG(GBA, INFO, "Official GBA BIOS detected");
384 } else if (checksum == GBA_DS_BIOS_CHECKSUM) {
385 mLOG(GBA, INFO, "Official GBA (DS) BIOS detected");
386 } else {
387 mLOG(GBA, WARN, "BIOS checksum incorrect");
388 }
389 gba->biosChecksum = checksum;
390 if (gba->memory.activeRegion == REGION_BIOS) {
391 gba->cpu->memory.activeRegion = gba->memory.bios;
392 }
393 // TODO: error check
394}
395
396void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
397 size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
398 if (!patchedSize || patchedSize > SIZE_CART0) {
399 return;
400 }
401 gba->memory.rom = anonymousMemoryMap(SIZE_CART0);
402 if (!patch->applyPatch(patch, gba->pristineRom, gba->pristineRomSize, gba->memory.rom, patchedSize)) {
403 mappedMemoryFree(gba->memory.rom, patchedSize);
404 gba->memory.rom = gba->pristineRom;
405 return;
406 }
407 gba->memory.romSize = patchedSize;
408 gba->memory.romMask = SIZE_CART0 - 1;
409 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
410}
411
412void GBAWriteIE(struct GBA* gba, uint16_t value) {
413 if (value & (1 << IRQ_KEYPAD)) {
414 mLOG(GBA, STUB, "Keypad interrupts not implemented");
415 }
416
417 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
418 ARMRaiseIRQ(gba->cpu);
419 }
420}
421
422void GBAWriteIME(struct GBA* gba, uint16_t value) {
423 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
424 ARMRaiseIRQ(gba->cpu);
425 }
426}
427
428void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
429 gba->memory.io[REG_IF >> 1] |= 1 << irq;
430
431 if (gba->memory.io[REG_IE >> 1] & 1 << irq) {
432 gba->cpu->halted = 0;
433 if (gba->memory.io[REG_IME >> 1]) {
434 ARMRaiseIRQ(gba->cpu);
435 }
436 }
437}
438
439void GBATestIRQ(struct ARMCore* cpu) {
440 struct GBA* gba = (struct GBA*) cpu->master;
441 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
442 gba->springIRQ = gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1];
443 gba->cpu->nextEvent = gba->cpu->cycles;
444 }
445}
446
447void GBAHalt(struct GBA* gba) {
448 gba->cpu->nextEvent = gba->cpu->cycles;
449 gba->cpu->halted = 1;
450}
451
452void GBAStop(struct GBA* gba) {
453 if (!gba->stopCallback) {
454 return;
455 }
456 gba->cpu->nextEvent = gba->cpu->cycles;
457 gba->stopCallback->stop(gba->stopCallback);
458}
459
460void GBADebug(struct GBA* gba, uint16_t flags) {
461 gba->debugFlags = flags;
462 if (GBADebugFlagsIsSend(gba->debugFlags)) {
463 int level = 1 << GBADebugFlagsGetLevel(gba->debugFlags);
464 level &= 0x1F;
465 char oolBuf[0x101];
466 strncpy(oolBuf, gba->debugString, sizeof(gba->debugString));
467 oolBuf[0x100] = '\0';
468 mLog(_mLOG_CAT_GBA_DEBUG(), level, "%s", oolBuf);
469 }
470 gba->debugFlags = GBADebugFlagsClearSend(gba->debugFlags);
471}
472
473bool GBAIsROM(struct VFile* vf) {
474 if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
475 return false;
476 }
477 uint8_t signature[sizeof(GBA_ROM_MAGIC)];
478 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
479 return false;
480 }
481 if (GBAIsBIOS(vf)) {
482 return false;
483 }
484 return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
485}
486
487bool GBAIsMB(struct VFile* vf) {
488 if (!GBAIsROM(vf)) {
489 return false;
490 }
491 if (vf->size(vf) > SIZE_WORKING_RAM) {
492 return false;
493 }
494 if (vf->seek(vf, GBA_MB_MAGIC_OFFSET, SEEK_SET) < 0) {
495 return false;
496 }
497 uint32_t signature;
498 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
499 return false;
500 }
501 uint32_t opcode;
502 LOAD_32(opcode, 0, &signature);
503 struct ARMInstructionInfo info;
504 ARMDecodeARM(opcode, &info);
505 if (info.branchType != ARM_BRANCH) {
506 return false;
507 }
508 if (info.op1.immediate <= 0) {
509 return false;
510 } else if (info.op1.immediate == 28) {
511 // Ancient toolchain that is known to throw MB detection for a loop
512 return false;
513 } else if (info.op1.immediate != 24) {
514 return true;
515 }
516 // Found a libgba-linked cart...these are a bit harder to detect.
517 return false;
518}
519
520bool GBAIsBIOS(struct VFile* vf) {
521 if (vf->seek(vf, 0, SEEK_SET) < 0) {
522 return false;
523 }
524 uint8_t interruptTable[7 * 4];
525 if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
526 return false;
527 }
528 int i;
529 for (i = 0; i < 7; ++i) {
530 if (interruptTable[4 * i + 3] != 0xEA || interruptTable[4 * i + 2]) {
531 return false;
532 }
533 }
534 return true;
535}
536
537void GBAGetGameCode(const struct GBA* gba, char* out) {
538 memset(out, 0, 8);
539 if (!gba->memory.rom) {
540 return;
541 }
542
543 memcpy(out, "AGB-", 4);
544 memcpy(&out[4], &((struct GBACartridge*) gba->memory.rom)->id, 4);
545}
546
547void GBAGetGameTitle(const struct GBA* gba, char* out) {
548 if (gba->memory.rom) {
549 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
550 return;
551 }
552 if (gba->pristineRom) {
553 memcpy(out, &((struct GBACartridge*) gba->pristineRom)->title, 12);
554 return;
555 }
556 strncpy(out, "(BIOS)", 12);
557}
558
559void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
560 struct GBA* gba = (struct GBA*) cpu->master;
561#ifdef USE_DEBUGGERS
562 if (gba->debugger) {
563 struct mDebuggerEntryInfo info = {
564 .address = _ARMPCAddress(cpu),
565 .opcode = opcode
566 };
567 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
568 }
569#endif
570 // TODO: More sensible category?
571 mLOG(GBA, ERROR, "Stub opcode: %08x", opcode);
572}
573
574void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
575 struct GBA* gba = (struct GBA*) cpu->master;
576 if (!gba->yankedRomSize) {
577 // TODO: More sensible category?
578 mLOG(GBA, WARN, "Illegal opcode: %08x", opcode);
579 }
580#ifdef USE_DEBUGGERS
581 if (gba->debugger) {
582 struct mDebuggerEntryInfo info = {
583 .address = _ARMPCAddress(cpu),
584 .opcode = opcode
585 };
586 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
587 } else
588#endif
589 {
590 ARMRaiseUndefined(cpu);
591 }
592}
593
594void GBABreakpoint(struct ARMCore* cpu, int immediate) {
595 struct GBA* gba = (struct GBA*) cpu->master;
596 if (immediate >= CPU_COMPONENT_MAX) {
597 return;
598 }
599 switch (immediate) {
600#ifdef USE_DEBUGGERS
601 case CPU_COMPONENT_DEBUGGER:
602 if (gba->debugger) {
603 struct mDebuggerEntryInfo info = {
604 .address = _ARMPCAddress(cpu),
605 .breakType = BREAKPOINT_SOFTWARE
606 };
607 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_BREAKPOINT, &info);
608 }
609 break;
610#endif
611 case CPU_COMPONENT_CHEAT_DEVICE:
612 if (gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
613 struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
614 struct GBACheatHook* hook = 0;
615 size_t i;
616 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
617 struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
618 if (cheats->hook && cheats->hook->address == _ARMPCAddress(cpu)) {
619 mCheatRefresh(device, &cheats->d);
620 hook = cheats->hook;
621 }
622 }
623 if (hook) {
624 ARMRunFake(cpu, hook->patchedOpcode);
625 }
626 }
627 break;
628 default:
629 break;
630 }
631}
632
633void GBAFrameStarted(struct GBA* gba) {
634 UNUSED(gba);
635
636 struct mCoreCallbacks* callbacks = gba->coreCallbacks;
637 if (callbacks && callbacks->videoFrameStarted) {
638 callbacks->videoFrameStarted(callbacks->context);
639 }
640}
641
642void GBAFrameEnded(struct GBA* gba) {
643 GBASavedataClean(&gba->memory.savedata, gba->video.frameCounter);
644
645 if (gba->rr) {
646 gba->rr->nextFrame(gba->rr);
647 }
648
649 if (gba->cpu->components && gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
650 struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
651 size_t i;
652 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
653 struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
654 mCheatRefresh(device, &cheats->d);
655 }
656 }
657
658 if (gba->stream && gba->stream->postVideoFrame) {
659 const color_t* pixels;
660 size_t stride;
661 gba->video.renderer->getPixels(gba->video.renderer, &stride, (const void**) &pixels);
662 gba->stream->postVideoFrame(gba->stream, pixels, stride);
663 }
664
665 if (gba->memory.hw.devices & (HW_GB_PLAYER | HW_GB_PLAYER_DETECTION)) {
666 GBAHardwarePlayerUpdate(gba);
667 }
668
669 struct mCoreCallbacks* callbacks = gba->coreCallbacks;
670 if (callbacks && callbacks->videoFrameEnded) {
671 callbacks->videoFrameEnded(callbacks->context);
672 }
673}
674
675void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
676 size_t immediate;
677 for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) {
678 if (gba->cpu->components[immediate] == component) {
679 break;
680 }
681 }
682 if (immediate == gba->cpu->numComponents) {
683 return;
684 }
685 if (mode == MODE_ARM) {
686 int32_t value;
687 int32_t old;
688 value = 0xE1200070;
689 value |= immediate & 0xF;
690 value |= (immediate & 0xFFF0) << 4;
691 GBAPatch32(gba->cpu, address, value, &old);
692 *opcode = old;
693 } else {
694 int16_t value;
695 int16_t old;
696 value = 0xBE00;
697 value |= immediate & 0xFF;
698 GBAPatch16(gba->cpu, address, value, &old);
699 *opcode = (uint16_t) old;
700 }
701}
702
703void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
704 if (mode == MODE_ARM) {
705 GBAPatch32(gba->cpu, address, opcode, 0);
706 } else {
707 GBAPatch16(gba->cpu, address, opcode, 0);
708 }
709}
710
711static bool _setSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
712 GBASetBreakpoint((struct GBA*) debugger->cpu->master, &debugger->d.p->d, address, mode, opcode);
713 return true;
714}
715
716static bool _clearSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
717 GBAClearBreakpoint((struct GBA*) debugger->cpu->master, address, mode, opcode);
718 return true;
719}