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 mCoreCallbacksListInit(&gba->coreCallbacks, 0);
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->isPristine = false;
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->isPristine) {
116 if (gba->yankedRomSize) {
117 gba->yankedRomSize = 0;
118 }
119 mappedMemoryFree(gba->memory.rom, SIZE_CART0);
120 }
121
122 if (gba->romVf) {
123#ifndef _3DS
124 gba->romVf->unmap(gba->romVf, gba->memory.rom, gba->pristineRomSize);
125#endif
126 gba->romVf->close(gba->romVf);
127 gba->romVf = NULL;
128 }
129 gba->memory.rom = NULL;
130 gba->isPristine = false;
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 mCoreCallbacksListDeinit(&gba->coreCallbacks);
156}
157
158void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {
159 irqh->reset = GBAReset;
160 irqh->processEvents = GBAProcessEvents;
161 irqh->swi16 = GBASwi16;
162 irqh->swi32 = GBASwi32;
163 irqh->hitIllegal = GBAIllegal;
164 irqh->readCPSR = GBATestIRQ;
165 irqh->hitStub = GBAHitStub;
166 irqh->bkpt16 = GBABreakpoint;
167 irqh->bkpt32 = GBABreakpoint;
168}
169
170void GBAReset(struct ARMCore* cpu) {
171 ARMSetPrivilegeMode(cpu, MODE_IRQ);
172 cpu->gprs[ARM_SP] = SP_BASE_IRQ;
173 ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
174 cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
175 ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
176 cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
177
178 struct GBA* gba = (struct GBA*) cpu->master;
179 if (!gba->rr || (!gba->rr->isPlaying(gba->rr) && !gba->rr->isRecording(gba->rr))) {
180 GBASavedataUnmask(&gba->memory.savedata);
181 }
182
183 gba->cpuBlocked = false;
184 gba->earlyExit = false;
185 if (gba->yankedRomSize) {
186 gba->memory.romSize = gba->yankedRomSize;
187 gba->memory.romMask = toPow2(gba->memory.romSize) - 1;
188 gba->yankedRomSize = 0;
189 }
190 mTimingClear(&gba->timing);
191 GBAMemoryReset(gba);
192 GBAVideoReset(&gba->video);
193 GBAAudioReset(&gba->audio);
194 GBAIOInit(gba);
195 GBATimerInit(gba);
196
197 GBASIOReset(&gba->sio);
198
199 gba->lastJump = 0;
200 gba->haltPending = false;
201 gba->idleDetectionStep = 0;
202 gba->idleDetectionFailures = 0;
203
204 gba->debug = false;
205 memset(gba->debugString, 0, sizeof(gba->debugString));
206}
207
208void GBASkipBIOS(struct GBA* gba) {
209 struct ARMCore* cpu = gba->cpu;
210 if (cpu->gprs[ARM_PC] == BASE_RESET + WORD_SIZE_ARM) {
211 if (gba->memory.rom) {
212 cpu->gprs[ARM_PC] = BASE_CART0;
213 } else {
214 cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
215 }
216 gba->memory.io[REG_VCOUNT >> 1] = 0x7E;
217 gba->memory.io[REG_POSTFLG >> 1] = 1;
218 int currentCycles = 0;
219 ARM_WRITE_PC;
220 }
221}
222
223static void GBAProcessEvents(struct ARMCore* cpu) {
224 struct GBA* gba = (struct GBA*) cpu->master;
225
226 gba->bus = cpu->prefetch[1];
227 if (cpu->executionMode == MODE_THUMB) {
228 gba->bus |= cpu->prefetch[1] << 16;
229 }
230
231 if (gba->springIRQ && !cpu->cpsr.i) {
232 ARMRaiseIRQ(cpu);
233 gba->springIRQ = 0;
234 }
235
236 int32_t nextEvent = cpu->nextEvent;
237 while (cpu->cycles >= nextEvent) {
238 int32_t cycles = cpu->cycles;
239
240 cpu->cycles = 0;
241 cpu->nextEvent = INT_MAX;
242
243#ifndef NDEBUG
244 if (cycles < 0) {
245 mLOG(GBA, FATAL, "Negative cycles passed: %i", cycles);
246 }
247#endif
248 nextEvent = cycles;
249 do {
250 nextEvent = mTimingTick(&gba->timing, nextEvent);
251 } while (gba->cpuBlocked);
252
253 cpu->nextEvent = nextEvent;
254
255 if (gba->earlyExit) {
256 gba->earlyExit = false;
257 break;
258 }
259 if (cpu->halted) {
260 cpu->cycles = nextEvent;
261 if (!gba->memory.io[REG_IME >> 1] || !gba->memory.io[REG_IE >> 1]) {
262 break;
263 }
264 }
265#ifndef NDEBUG
266 else if (nextEvent < 0) {
267 mLOG(GBA, FATAL, "Negative cycles will pass: %i", nextEvent);
268 }
269#endif
270 }
271}
272
273void GBAAttachDebugger(struct GBA* gba, struct mDebugger* debugger) {
274 gba->debugger = (struct ARMDebugger*) debugger->platform;
275 gba->debugger->setSoftwareBreakpoint = _setSoftwareBreakpoint;
276 gba->debugger->clearSoftwareBreakpoint = _clearSoftwareBreakpoint;
277 gba->cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
278 ARMHotplugAttach(gba->cpu, CPU_COMPONENT_DEBUGGER);
279}
280
281void GBADetachDebugger(struct GBA* gba) {
282 gba->debugger = 0;
283 ARMHotplugDetach(gba->cpu, CPU_COMPONENT_DEBUGGER);
284 gba->cpu->components[CPU_COMPONENT_DEBUGGER] = 0;
285}
286
287bool GBALoadMB(struct GBA* gba, struct VFile* vf) {
288 GBAUnloadROM(gba);
289 gba->romVf = vf;
290 gba->pristineRomSize = vf->size(vf);
291 vf->seek(vf, 0, SEEK_SET);
292 if (gba->pristineRomSize > SIZE_WORKING_RAM) {
293 gba->pristineRomSize = SIZE_WORKING_RAM;
294 }
295 gba->isPristine = true;
296#ifdef _3DS
297 if (gba->pristineRomSize <= romBufferSize) {
298 gba->memory.wram = romBuffer;
299 vf->read(vf, romBuffer, gba->pristineRomSize);
300 }
301#else
302 gba->memory.wram = vf->map(vf, gba->pristineRomSize, MAP_READ);
303#endif
304 if (!gba->memory.wram) {
305 mLOG(GBA, WARN, "Couldn't map ROM");
306 return false;
307 }
308 gba->yankedRomSize = 0;
309 gba->memory.romSize = 0;
310 gba->memory.romMask = 0;
311 gba->romCrc32 = doCrc32(gba->memory.wram, gba->pristineRomSize);
312 return true;
313}
314
315bool GBALoadROM(struct GBA* gba, struct VFile* vf) {
316 if (!vf) {
317 return false;
318 }
319 GBAUnloadROM(gba);
320 gba->romVf = vf;
321 gba->pristineRomSize = vf->size(vf);
322 vf->seek(vf, 0, SEEK_SET);
323 if (gba->pristineRomSize > SIZE_CART0) {
324 gba->pristineRomSize = SIZE_CART0;
325 }
326 gba->isPristine = true;
327#ifdef _3DS
328 if (gba->pristineRomSize <= romBufferSize) {
329 gba->memory.rom = romBuffer;
330 vf->read(vf, romBuffer, gba->pristineRomSize);
331 }
332#else
333 gba->memory.rom = vf->map(vf, gba->pristineRomSize, MAP_READ);
334#endif
335 if (!gba->memory.rom) {
336 mLOG(GBA, WARN, "Couldn't map ROM");
337 return false;
338 }
339 gba->yankedRomSize = 0;
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 void* newRom = anonymousMemoryMap(SIZE_CART0);
393 if (!patch->applyPatch(patch, gba->memory.rom, gba->pristineRomSize, newRom, patchedSize)) {
394 mappedMemoryFree(newRom, SIZE_CART0);
395 return;
396 }
397 if (gba->romVf) {
398#ifndef _3DS
399 gba->romVf->unmap(gba->romVf, gba->memory.rom, gba->pristineRomSize);
400#endif
401 gba->romVf->close(gba->romVf);
402 gba->romVf = NULL;
403 }
404 gba->isPristine = false;
405 gba->memory.rom = newRom;
406 gba->memory.hw.gpioBase = &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1];
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->isPristine && gba->memory.wram) {
553 memcpy(out, &((struct GBACartridge*) gba->memory.wram)->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 size_t c;
637 for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
638 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
639 if (callbacks->videoFrameStarted) {
640 callbacks->videoFrameStarted(callbacks->context);
641 }
642 }
643}
644
645void GBAFrameEnded(struct GBA* gba) {
646 GBASavedataClean(&gba->memory.savedata, gba->video.frameCounter);
647
648 if (gba->rr) {
649 gba->rr->nextFrame(gba->rr);
650 }
651
652 if (gba->cpu->components && gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
653 struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
654 size_t i;
655 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
656 struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
657 mCheatRefresh(device, &cheats->d);
658 }
659 }
660
661 if (gba->stream && gba->stream->postVideoFrame) {
662 const color_t* pixels;
663 size_t stride;
664 gba->video.renderer->getPixels(gba->video.renderer, &stride, (const void**) &pixels);
665 gba->stream->postVideoFrame(gba->stream, pixels, stride);
666 }
667
668 if (gba->memory.hw.devices & (HW_GB_PLAYER | HW_GB_PLAYER_DETECTION)) {
669 GBAHardwarePlayerUpdate(gba);
670 }
671
672 size_t c;
673 for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
674 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
675 if (callbacks->videoFrameEnded) {
676 callbacks->videoFrameEnded(callbacks->context);
677 }
678 }
679}
680
681void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
682 size_t immediate;
683 for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) {
684 if (gba->cpu->components[immediate] == component) {
685 break;
686 }
687 }
688 if (immediate == gba->cpu->numComponents) {
689 return;
690 }
691 if (mode == MODE_ARM) {
692 int32_t value;
693 int32_t old;
694 value = 0xE1200070;
695 value |= immediate & 0xF;
696 value |= (immediate & 0xFFF0) << 4;
697 GBAPatch32(gba->cpu, address, value, &old);
698 *opcode = old;
699 } else {
700 int16_t value;
701 int16_t old;
702 value = 0xBE00;
703 value |= immediate & 0xFF;
704 GBAPatch16(gba->cpu, address, value, &old);
705 *opcode = (uint16_t) old;
706 }
707}
708
709void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
710 if (mode == MODE_ARM) {
711 GBAPatch32(gba->cpu, address, opcode, 0);
712 } else {
713 GBAPatch16(gba->cpu, address, opcode, 0);
714 }
715}
716
717static bool _setSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
718 GBASetBreakpoint((struct GBA*) debugger->cpu->master, &debugger->d.p->d, address, mode, opcode);
719 return true;
720}
721
722static bool _clearSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
723 GBAClearBreakpoint((struct GBA*) debugger->cpu->master, address, mode, opcode);
724 return true;
725}