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
24#ifdef USE_ELF
25#include <mgba-util/elf-read.h>
26#endif
27
28mLOG_DEFINE_CATEGORY(GBA, "GBA", "gba");
29mLOG_DEFINE_CATEGORY(GBA_DEBUG, "GBA Debug", "gba.debug");
30
31const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;
32
33static const size_t GBA_ROM_MAGIC_OFFSET = 3;
34static const uint8_t GBA_ROM_MAGIC[] = { 0xEA };
35
36static const size_t GBA_MB_MAGIC_OFFSET = 0xC0;
37
38static void GBAInit(void* cpu, struct mCPUComponent* component);
39static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh);
40static void GBAProcessEvents(struct ARMCore* cpu);
41static void GBAHitStub(struct ARMCore* cpu, uint32_t opcode);
42static void GBAIllegal(struct ARMCore* cpu, uint32_t opcode);
43static void GBABreakpoint(struct ARMCore* cpu, int immediate);
44
45static bool _setSoftwareBreakpoint(struct ARMDebugger*, uint32_t address, enum ExecutionMode mode, uint32_t* opcode);
46static bool _clearSoftwareBreakpoint(struct ARMDebugger*, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
47
48
49#ifdef FIXED_ROM_BUFFER
50extern uint32_t* romBuffer;
51extern size_t romBufferSize;
52#endif
53
54void GBACreate(struct GBA* gba) {
55 gba->d.id = GBA_COMPONENT_MAGIC;
56 gba->d.init = GBAInit;
57 gba->d.deinit = 0;
58}
59
60static void GBAInit(void* cpu, struct mCPUComponent* component) {
61 struct GBA* gba = (struct GBA*) component;
62 gba->cpu = cpu;
63 gba->debugger = 0;
64 gba->sync = 0;
65
66 GBAInterruptHandlerInit(&gba->cpu->irqh);
67 GBAMemoryInit(gba);
68
69 gba->memory.savedata.timing = &gba->timing;
70 GBASavedataInit(&gba->memory.savedata, NULL);
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 GBAHardwareInit(&gba->memory.hw, NULL);
84
85 gba->springIRQ = 0;
86 gba->keySource = 0;
87 gba->rotationSource = 0;
88 gba->luminanceSource = 0;
89 gba->rtcSource = 0;
90 gba->rumble = 0;
91 gba->rr = 0;
92
93 gba->romVf = 0;
94 gba->biosVf = 0;
95
96 gba->stream = NULL;
97 gba->keyCallback = NULL;
98 mCoreCallbacksListInit(&gba->coreCallbacks, 0);
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->isPristine = false;
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->isPristine) {
120 if (gba->yankedRomSize) {
121 gba->yankedRomSize = 0;
122 }
123 mappedMemoryFree(gba->memory.rom, SIZE_CART0);
124 }
125
126 if (gba->romVf) {
127#ifndef FIXED_ROM_BUFFER
128 gba->romVf->unmap(gba->romVf, gba->memory.rom, gba->pristineRomSize);
129#endif
130 gba->romVf->close(gba->romVf);
131 gba->romVf = NULL;
132 }
133 gba->memory.rom = NULL;
134 gba->isPristine = false;
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 mCoreCallbacksListDeinit(&gba->coreCallbacks);
160}
161
162void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {
163 irqh->reset = GBAReset;
164 irqh->processEvents = GBAProcessEvents;
165 irqh->swi16 = GBASwi16;
166 irqh->swi32 = GBASwi32;
167 irqh->hitIllegal = GBAIllegal;
168 irqh->readCPSR = GBATestIRQ;
169 irqh->hitStub = GBAHitStub;
170 irqh->bkpt16 = GBABreakpoint;
171 irqh->bkpt32 = GBABreakpoint;
172}
173
174void GBAReset(struct ARMCore* cpu) {
175 ARMSetPrivilegeMode(cpu, MODE_IRQ);
176 cpu->gprs[ARM_SP] = SP_BASE_IRQ;
177 ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
178 cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
179 ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
180 cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
181
182 struct GBA* gba = (struct GBA*) cpu->master;
183 if (!gba->rr || (!gba->rr->isPlaying(gba->rr) && !gba->rr->isRecording(gba->rr))) {
184 GBASavedataUnmask(&gba->memory.savedata);
185 }
186
187 gba->cpuBlocked = false;
188 gba->earlyExit = false;
189 if (gba->yankedRomSize) {
190 gba->memory.romSize = gba->yankedRomSize;
191 gba->memory.romMask = toPow2(gba->memory.romSize) - 1;
192 gba->yankedRomSize = 0;
193 }
194 mTimingClear(&gba->timing);
195 GBAMemoryReset(gba);
196 GBAVideoReset(&gba->video);
197 GBAAudioReset(&gba->audio);
198 GBAIOInit(gba);
199 GBATimerInit(gba);
200
201 GBASIOReset(&gba->sio);
202
203 gba->lastJump = 0;
204 gba->haltPending = false;
205 gba->idleDetectionStep = 0;
206 gba->idleDetectionFailures = 0;
207
208 gba->debug = false;
209 memset(gba->debugString, 0, sizeof(gba->debugString));
210
211 if (!gba->romVf) {
212 GBASkipBIOS(gba);
213 }
214}
215
216void GBASkipBIOS(struct GBA* gba) {
217 struct ARMCore* cpu = gba->cpu;
218 if (cpu->gprs[ARM_PC] == BASE_RESET + WORD_SIZE_ARM) {
219 if (gba->memory.rom) {
220 cpu->gprs[ARM_PC] = BASE_CART0;
221 } else {
222 cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
223 }
224 gba->memory.io[REG_VCOUNT >> 1] = 0x7E;
225 gba->memory.io[REG_POSTFLG >> 1] = 1;
226 int currentCycles = 0;
227 ARM_WRITE_PC;
228 }
229}
230
231static void GBAProcessEvents(struct ARMCore* cpu) {
232 struct GBA* gba = (struct GBA*) cpu->master;
233
234 gba->bus = cpu->prefetch[1];
235 if (cpu->executionMode == MODE_THUMB) {
236 gba->bus |= cpu->prefetch[1] << 16;
237 }
238
239 if (gba->springIRQ && !cpu->cpsr.i) {
240 ARMRaiseIRQ(cpu);
241 gba->springIRQ = 0;
242 }
243
244 int32_t nextEvent = cpu->nextEvent;
245 while (cpu->cycles >= nextEvent) {
246 int32_t cycles = cpu->cycles;
247
248 cpu->cycles = 0;
249 cpu->nextEvent = INT_MAX;
250
251#ifndef NDEBUG
252 if (cycles < 0) {
253 mLOG(GBA, FATAL, "Negative cycles passed: %i", cycles);
254 }
255#endif
256 nextEvent = cycles;
257 do {
258 nextEvent = mTimingTick(&gba->timing, nextEvent);
259 } while (gba->cpuBlocked);
260
261 cpu->nextEvent = nextEvent;
262
263 if (gba->earlyExit) {
264 gba->earlyExit = false;
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 }
279}
280
281#ifdef USE_DEBUGGERS
282void GBAAttachDebugger(struct GBA* gba, struct mDebugger* debugger) {
283 gba->debugger = (struct ARMDebugger*) debugger->platform;
284 gba->debugger->setSoftwareBreakpoint = _setSoftwareBreakpoint;
285 gba->debugger->clearSoftwareBreakpoint = _clearSoftwareBreakpoint;
286 gba->cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
287 ARMHotplugAttach(gba->cpu, CPU_COMPONENT_DEBUGGER);
288}
289
290void GBADetachDebugger(struct GBA* gba) {
291 if (gba->debugger) {
292 ARMHotplugDetach(gba->cpu, CPU_COMPONENT_DEBUGGER);
293 }
294 gba->cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
295 gba->debugger = NULL;
296}
297#endif
298
299bool GBALoadNull(struct GBA* gba) {
300 GBAUnloadROM(gba);
301 gba->romVf = NULL;
302 gba->pristineRomSize = 0;
303 gba->memory.wram = anonymousMemoryMap(SIZE_WORKING_RAM);
304#ifndef FIXED_ROM_BUFFER
305 gba->memory.rom = anonymousMemoryMap(SIZE_CART0);
306#else
307 gba->memory.rom = romBuffer;
308#endif
309 gba->isPristine = false;
310 gba->yankedRomSize = 0;
311 gba->memory.romSize = SIZE_CART0;
312 gba->memory.romMask = SIZE_CART0 - 1;
313 gba->memory.mirroring = false;
314 gba->romCrc32 = 0;
315 return true;
316}
317
318bool GBALoadMB(struct GBA* gba, struct VFile* vf) {
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_WORKING_RAM) {
324 gba->pristineRomSize = SIZE_WORKING_RAM;
325 }
326 gba->isPristine = true;
327 gba->memory.wram = anonymousMemoryMap(SIZE_WORKING_RAM);
328 memset(gba->memory.wram, 0, SIZE_WORKING_RAM);
329 vf->read(vf, gba->memory.wram, gba->pristineRomSize);
330 if (!gba->memory.wram) {
331 mLOG(GBA, WARN, "Couldn't map ROM");
332 return false;
333 }
334 gba->yankedRomSize = 0;
335 gba->memory.romSize = 0;
336 gba->memory.romMask = 0;
337 gba->romCrc32 = doCrc32(gba->memory.wram, gba->pristineRomSize);
338 return true;
339}
340
341bool GBALoadROM(struct GBA* gba, struct VFile* vf) {
342 if (!vf) {
343 return false;
344 }
345 GBAUnloadROM(gba);
346 gba->romVf = vf;
347 gba->pristineRomSize = vf->size(vf);
348 vf->seek(vf, 0, SEEK_SET);
349 if (gba->pristineRomSize > SIZE_CART0) {
350 gba->pristineRomSize = SIZE_CART0;
351 }
352 gba->isPristine = true;
353#ifdef FIXED_ROM_BUFFER
354 if (gba->pristineRomSize <= romBufferSize) {
355 gba->memory.rom = romBuffer;
356 vf->read(vf, romBuffer, gba->pristineRomSize);
357 }
358#else
359 gba->memory.rom = vf->map(vf, gba->pristineRomSize, MAP_READ);
360#endif
361 if (!gba->memory.rom) {
362 mLOG(GBA, WARN, "Couldn't map ROM");
363 return false;
364 }
365 gba->yankedRomSize = 0;
366 gba->memory.romSize = gba->pristineRomSize;
367 gba->memory.romMask = toPow2(gba->memory.romSize) - 1;
368 gba->memory.mirroring = false;
369 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
370 GBAHardwareInit(&gba->memory.hw, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
371 GBAVFameDetect(&gba->memory.vfame, gba->memory.rom, gba->memory.romSize);
372 if (popcount32(gba->memory.romSize) != 1) {
373 // This ROM is either a bad dump or homebrew. Emulate flash cart behavior.
374#ifndef FIXED_ROM_BUFFER
375 void* newRom = anonymousMemoryMap(SIZE_CART0);
376 memcpy(newRom, gba->memory.rom, gba->pristineRomSize);
377 gba->memory.rom = newRom;
378#endif
379 gba->memory.romSize = SIZE_CART0;
380 gba->isPristine = false;
381 }
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 void* newRom = anonymousMemoryMap(SIZE_CART0);
429 if (!patch->applyPatch(patch, gba->memory.rom, gba->pristineRomSize, newRom, patchedSize)) {
430 mappedMemoryFree(newRom, SIZE_CART0);
431 return;
432 }
433 if (gba->romVf) {
434#ifndef FIXED_ROM_BUFFER
435 gba->romVf->unmap(gba->romVf, gba->memory.rom, gba->pristineRomSize);
436#endif
437 gba->romVf->close(gba->romVf);
438 gba->romVf = NULL;
439 }
440 gba->isPristine = false;
441 gba->memory.rom = newRom;
442 gba->memory.hw.gpioBase = &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1];
443 gba->memory.romSize = patchedSize;
444 gba->memory.romMask = SIZE_CART0 - 1;
445 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
446}
447
448void GBAWriteIE(struct GBA* gba, uint16_t value) {
449 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
450 ARMRaiseIRQ(gba->cpu);
451 }
452}
453
454void GBAWriteIME(struct GBA* gba, uint16_t value) {
455 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
456 ARMRaiseIRQ(gba->cpu);
457 }
458}
459
460void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
461 gba->memory.io[REG_IF >> 1] |= 1 << irq;
462
463 if (gba->memory.io[REG_IE >> 1] & 1 << irq) {
464 gba->cpu->halted = 0;
465 if (gba->memory.io[REG_IME >> 1]) {
466 ARMRaiseIRQ(gba->cpu);
467 }
468 }
469}
470
471void GBATestIRQ(struct ARMCore* cpu) {
472 struct GBA* gba = (struct GBA*) cpu->master;
473 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
474 gba->springIRQ = gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1];
475 gba->cpu->nextEvent = gba->cpu->cycles;
476 }
477}
478
479void GBAHalt(struct GBA* gba) {
480 ARMHalt(gba->cpu);
481}
482
483void GBAStop(struct GBA* gba) {
484 size_t c;
485 for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
486 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
487 if (callbacks->sleep) {
488 callbacks->sleep(callbacks->context);
489 }
490 }
491 gba->cpu->nextEvent = gba->cpu->cycles;
492}
493
494void GBADebug(struct GBA* gba, uint16_t flags) {
495 gba->debugFlags = flags;
496 if (GBADebugFlagsIsSend(gba->debugFlags)) {
497 int level = 1 << GBADebugFlagsGetLevel(gba->debugFlags);
498 level &= 0x1F;
499 char oolBuf[0x101];
500 strncpy(oolBuf, gba->debugString, sizeof(gba->debugString));
501 oolBuf[0x100] = '\0';
502 mLog(_mLOG_CAT_GBA_DEBUG(), level, "%s", oolBuf);
503 }
504 gba->debugFlags = GBADebugFlagsClearSend(gba->debugFlags);
505}
506
507bool GBAIsROM(struct VFile* vf) {
508#ifdef USE_ELF
509 struct ELF* elf = ELFOpen(vf);
510 if (elf) {
511 uint32_t entry = ELFEntry(elf);
512 bool isGBA = true;
513 isGBA = isGBA && ELFMachine(elf) == EM_ARM;
514 isGBA = isGBA && (entry == BASE_CART0 || entry == BASE_WORKING_RAM);
515 ELFClose(elf);
516 return isGBA;
517 }
518#endif
519 if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
520 return false;
521 }
522 uint8_t signature[sizeof(GBA_ROM_MAGIC)];
523 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
524 return false;
525 }
526 if (GBAIsBIOS(vf)) {
527 return false;
528 }
529 return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
530}
531
532bool GBAIsMB(struct VFile* vf) {
533 if (!GBAIsROM(vf)) {
534 return false;
535 }
536#ifdef USE_ELF
537 struct ELF* elf = ELFOpen(vf);
538 if (elf) {
539 bool isMB = ELFEntry(elf) == BASE_WORKING_RAM;
540 ELFClose(elf);
541 return isMB;
542 }
543#endif
544 if (vf->size(vf) > SIZE_WORKING_RAM) {
545 return false;
546 }
547 if (vf->seek(vf, GBA_MB_MAGIC_OFFSET, SEEK_SET) < 0) {
548 return false;
549 }
550 uint32_t signature;
551 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
552 return false;
553 }
554 uint32_t opcode;
555 LOAD_32(opcode, 0, &signature);
556 struct ARMInstructionInfo info;
557 ARMDecodeARM(opcode, &info);
558 if (info.branchType != ARM_BRANCH) {
559 return false;
560 }
561 if (info.op1.immediate <= 0) {
562 return false;
563 } else if (info.op1.immediate == 28) {
564 // Ancient toolchain that is known to throw MB detection for a loop
565 return false;
566 } else if (info.op1.immediate != 24) {
567 return true;
568 }
569 // Found a libgba-linked cart...these are a bit harder to detect.
570 return false;
571}
572
573bool GBAIsBIOS(struct VFile* vf) {
574 if (vf->seek(vf, 0, SEEK_SET) < 0) {
575 return false;
576 }
577 uint8_t interruptTable[7 * 4];
578 if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
579 return false;
580 }
581 int i;
582 for (i = 0; i < 7; ++i) {
583 if (interruptTable[4 * i + 3] != 0xEA || interruptTable[4 * i + 2]) {
584 return false;
585 }
586 }
587 return true;
588}
589
590void GBAGetGameCode(const struct GBA* gba, char* out) {
591 memset(out, 0, 8);
592 if (!gba->memory.rom) {
593 return;
594 }
595
596 memcpy(out, "AGB-", 4);
597 memcpy(&out[4], &((struct GBACartridge*) gba->memory.rom)->id, 4);
598}
599
600void GBAGetGameTitle(const struct GBA* gba, char* out) {
601 if (gba->memory.rom) {
602 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
603 return;
604 }
605 if (gba->isPristine && gba->memory.wram) {
606 memcpy(out, &((struct GBACartridge*) gba->memory.wram)->title, 12);
607 return;
608 }
609 strncpy(out, "(BIOS)", 12);
610}
611
612void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
613 struct GBA* gba = (struct GBA*) cpu->master;
614#ifdef USE_DEBUGGERS
615 if (gba->debugger) {
616 struct mDebuggerEntryInfo info = {
617 .address = _ARMPCAddress(cpu),
618 .opcode = opcode
619 };
620 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
621 }
622#endif
623 // TODO: More sensible category?
624 mLOG(GBA, ERROR, "Stub opcode: %08x", opcode);
625}
626
627void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
628 struct GBA* gba = (struct GBA*) cpu->master;
629 if (!gba->yankedRomSize) {
630 // TODO: More sensible category?
631 mLOG(GBA, WARN, "Illegal opcode: %08x", opcode);
632 }
633 if (cpu->executionMode == MODE_THUMB && (opcode & 0xFFC0) == 0xE800) {
634 mLOG(GBA, DEBUG, "Hit Wii U VC opcode: %08x", opcode);
635 return;
636 }
637#ifdef USE_DEBUGGERS
638 if (gba->debugger) {
639 struct mDebuggerEntryInfo info = {
640 .address = _ARMPCAddress(cpu),
641 .opcode = opcode
642 };
643 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
644 } else
645#endif
646 {
647 ARMRaiseUndefined(cpu);
648 }
649}
650
651void GBABreakpoint(struct ARMCore* cpu, int immediate) {
652 struct GBA* gba = (struct GBA*) cpu->master;
653 if (immediate >= CPU_COMPONENT_MAX) {
654 return;
655 }
656 switch (immediate) {
657#ifdef USE_DEBUGGERS
658 case CPU_COMPONENT_DEBUGGER:
659 if (gba->debugger) {
660 struct mDebuggerEntryInfo info = {
661 .address = _ARMPCAddress(cpu),
662 .breakType = BREAKPOINT_SOFTWARE
663 };
664 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_BREAKPOINT, &info);
665 }
666 break;
667#endif
668 case CPU_COMPONENT_CHEAT_DEVICE:
669 if (gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
670 struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
671 struct GBACheatHook* hook = 0;
672 size_t i;
673 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
674 struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
675 if (cheats->hook && cheats->hook->address == _ARMPCAddress(cpu)) {
676 mCheatRefresh(device, &cheats->d);
677 hook = cheats->hook;
678 }
679 }
680 if (hook) {
681 ARMRunFake(cpu, hook->patchedOpcode);
682 }
683 }
684 break;
685 default:
686 break;
687 }
688}
689
690void GBAFrameStarted(struct GBA* gba) {
691 GBATestKeypadIRQ(gba);
692
693 size_t c;
694 for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
695 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
696 if (callbacks->videoFrameStarted) {
697 callbacks->videoFrameStarted(callbacks->context);
698 }
699 }
700}
701
702void GBAFrameEnded(struct GBA* gba) {
703 GBASavedataClean(&gba->memory.savedata, gba->video.frameCounter);
704
705 if (gba->rr) {
706 gba->rr->nextFrame(gba->rr);
707 }
708
709 if (gba->cpu->components && gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
710 struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
711 size_t i;
712 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
713 struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
714 mCheatRefresh(device, &cheats->d);
715 }
716 }
717
718 if (gba->stream && gba->stream->postVideoFrame) {
719 const color_t* pixels;
720 size_t stride;
721 gba->video.renderer->getPixels(gba->video.renderer, &stride, (const void**) &pixels);
722 gba->stream->postVideoFrame(gba->stream, pixels, stride);
723 }
724
725 if (gba->memory.hw.devices & (HW_GB_PLAYER | HW_GB_PLAYER_DETECTION)) {
726 GBAHardwarePlayerUpdate(gba);
727 }
728
729 size_t c;
730 for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
731 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
732 if (callbacks->videoFrameEnded) {
733 callbacks->videoFrameEnded(callbacks->context);
734 }
735 }
736}
737
738void GBATestKeypadIRQ(struct GBA* gba) {
739 uint16_t keycnt = gba->memory.io[REG_KEYCNT >> 1];
740 if (!(keycnt & 0x4000)) {
741 return;
742 }
743 int isAnd = keycnt & 0x8000;
744 uint16_t keyInput;
745
746 if (!gba->keySource) {
747 // TODO?
748 return;
749 }
750
751 keycnt &= 0x3FF;
752 keyInput = *gba->keySource;
753
754 if (isAnd && keycnt == keyInput) {
755 GBARaiseIRQ(gba, IRQ_KEYPAD);
756 } else if (!isAnd && keycnt & keyInput) {
757 GBARaiseIRQ(gba, IRQ_KEYPAD);
758 }
759}
760
761void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
762 size_t immediate;
763 for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) {
764 if (gba->cpu->components[immediate] == component) {
765 break;
766 }
767 }
768 if (immediate == gba->cpu->numComponents) {
769 return;
770 }
771 if (mode == MODE_ARM) {
772 int32_t value;
773 int32_t old;
774 value = 0xE1200070;
775 value |= immediate & 0xF;
776 value |= (immediate & 0xFFF0) << 4;
777 GBAPatch32(gba->cpu, address, value, &old);
778 *opcode = old;
779 } else {
780 int16_t value;
781 int16_t old;
782 value = 0xBE00;
783 value |= immediate & 0xFF;
784 GBAPatch16(gba->cpu, address, value, &old);
785 *opcode = (uint16_t) old;
786 }
787}
788
789void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
790 if (mode == MODE_ARM) {
791 GBAPatch32(gba->cpu, address, opcode, 0);
792 } else {
793 GBAPatch16(gba->cpu, address, opcode, 0);
794 }
795}
796
797static bool _setSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
798 GBASetBreakpoint((struct GBA*) debugger->cpu->master, &debugger->d.p->d, address, mode, opcode);
799 return true;
800}
801
802static bool _clearSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
803 GBAClearBreakpoint((struct GBA*) debugger->cpu->master, address, mode, opcode);
804 return true;
805}