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