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