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