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 && gba->memory.rom) {
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 gba->cpu->nextEvent = gba->cpu->cycles;
481 gba->cpu->halted = 1;
482}
483
484void GBAStop(struct GBA* gba) {
485 size_t c;
486 for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
487 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
488 if (callbacks->sleep) {
489 callbacks->sleep(callbacks->context);
490 }
491 }
492 gba->cpu->nextEvent = gba->cpu->cycles;
493}
494
495void GBADebug(struct GBA* gba, uint16_t flags) {
496 gba->debugFlags = flags;
497 if (GBADebugFlagsIsSend(gba->debugFlags)) {
498 int level = 1 << GBADebugFlagsGetLevel(gba->debugFlags);
499 level &= 0x1F;
500 char oolBuf[0x101];
501 strncpy(oolBuf, gba->debugString, sizeof(gba->debugString));
502 oolBuf[0x100] = '\0';
503 mLog(_mLOG_CAT_GBA_DEBUG(), level, "%s", oolBuf);
504 }
505 gba->debugFlags = GBADebugFlagsClearSend(gba->debugFlags);
506}
507
508bool GBAIsROM(struct VFile* vf) {
509#ifdef USE_ELF
510 struct ELF* elf = ELFOpen(vf);
511 if (elf) {
512 uint32_t entry = ELFEntry(elf);
513 bool isGBA = true;
514 isGBA = isGBA && ELFMachine(elf) == EM_ARM;
515 isGBA = isGBA && (entry == BASE_CART0 || entry == BASE_WORKING_RAM);
516 ELFClose(elf);
517 return isGBA;
518 }
519#endif
520 if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
521 return false;
522 }
523 uint8_t signature[sizeof(GBA_ROM_MAGIC)];
524 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
525 return false;
526 }
527 if (GBAIsBIOS(vf)) {
528 return false;
529 }
530 return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
531}
532
533bool GBAIsMB(struct VFile* vf) {
534 if (!GBAIsROM(vf)) {
535 return false;
536 }
537#ifdef USE_ELF
538 struct ELF* elf = ELFOpen(vf);
539 if (elf) {
540 bool isMB = ELFEntry(elf) == BASE_WORKING_RAM;
541 ELFClose(elf);
542 return isMB;
543 }
544#endif
545 if (vf->size(vf) > SIZE_WORKING_RAM) {
546 return false;
547 }
548 if (vf->seek(vf, GBA_MB_MAGIC_OFFSET, SEEK_SET) < 0) {
549 return false;
550 }
551 uint32_t signature;
552 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
553 return false;
554 }
555 uint32_t opcode;
556 LOAD_32(opcode, 0, &signature);
557 struct ARMInstructionInfo info;
558 ARMDecodeARM(opcode, &info);
559 if (info.branchType != ARM_BRANCH) {
560 return false;
561 }
562 if (info.op1.immediate <= 0) {
563 return false;
564 } else if (info.op1.immediate == 28) {
565 // Ancient toolchain that is known to throw MB detection for a loop
566 return false;
567 } else if (info.op1.immediate != 24) {
568 return true;
569 }
570 // Found a libgba-linked cart...these are a bit harder to detect.
571 return false;
572}
573
574bool GBAIsBIOS(struct VFile* vf) {
575 if (vf->seek(vf, 0, SEEK_SET) < 0) {
576 return false;
577 }
578 uint8_t interruptTable[7 * 4];
579 if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
580 return false;
581 }
582 int i;
583 for (i = 0; i < 7; ++i) {
584 if (interruptTable[4 * i + 3] != 0xEA || interruptTable[4 * i + 2]) {
585 return false;
586 }
587 }
588 return true;
589}
590
591void GBAGetGameCode(const struct GBA* gba, char* out) {
592 memset(out, 0, 8);
593 if (!gba->memory.rom) {
594 return;
595 }
596
597 memcpy(out, "AGB-", 4);
598 memcpy(&out[4], &((struct GBACartridge*) gba->memory.rom)->id, 4);
599}
600
601void GBAGetGameTitle(const struct GBA* gba, char* out) {
602 if (gba->memory.rom) {
603 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
604 return;
605 }
606 if (gba->isPristine && gba->memory.wram) {
607 memcpy(out, &((struct GBACartridge*) gba->memory.wram)->title, 12);
608 return;
609 }
610 strncpy(out, "(BIOS)", 12);
611}
612
613void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
614 struct GBA* gba = (struct GBA*) cpu->master;
615#ifdef USE_DEBUGGERS
616 if (gba->debugger) {
617 struct mDebuggerEntryInfo info = {
618 .address = _ARMPCAddress(cpu),
619 .opcode = opcode
620 };
621 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
622 }
623#endif
624 // TODO: More sensible category?
625 mLOG(GBA, ERROR, "Stub opcode: %08x", opcode);
626}
627
628void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
629 struct GBA* gba = (struct GBA*) cpu->master;
630 if (!gba->yankedRomSize) {
631 // TODO: More sensible category?
632 mLOG(GBA, WARN, "Illegal opcode: %08x", opcode);
633 }
634 if (cpu->executionMode == MODE_THUMB && (opcode & 0xFFC0) == 0xE800) {
635 mLOG(GBA, DEBUG, "Hit Wii U VC opcode: %08x", opcode);
636 return;
637 }
638#ifdef USE_DEBUGGERS
639 if (gba->debugger) {
640 struct mDebuggerEntryInfo info = {
641 .address = _ARMPCAddress(cpu),
642 .opcode = opcode
643 };
644 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
645 } else
646#endif
647 {
648 ARMRaiseUndefined(cpu);
649 }
650}
651
652void GBABreakpoint(struct ARMCore* cpu, int immediate) {
653 struct GBA* gba = (struct GBA*) cpu->master;
654 if (immediate >= CPU_COMPONENT_MAX) {
655 return;
656 }
657 switch (immediate) {
658#ifdef USE_DEBUGGERS
659 case CPU_COMPONENT_DEBUGGER:
660 if (gba->debugger) {
661 struct mDebuggerEntryInfo info = {
662 .address = _ARMPCAddress(cpu),
663 .breakType = BREAKPOINT_SOFTWARE
664 };
665 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_BREAKPOINT, &info);
666 }
667 break;
668#endif
669 case CPU_COMPONENT_CHEAT_DEVICE:
670 if (gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
671 struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
672 struct GBACheatHook* hook = 0;
673 size_t i;
674 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
675 struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
676 if (cheats->hook && cheats->hook->address == _ARMPCAddress(cpu)) {
677 mCheatRefresh(device, &cheats->d);
678 hook = cheats->hook;
679 }
680 }
681 if (hook) {
682 ARMRunFake(cpu, hook->patchedOpcode);
683 }
684 }
685 break;
686 default:
687 break;
688 }
689}
690
691void GBAFrameStarted(struct GBA* gba) {
692 GBATestKeypadIRQ(gba);
693
694 size_t c;
695 for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
696 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
697 if (callbacks->videoFrameStarted) {
698 callbacks->videoFrameStarted(callbacks->context);
699 }
700 }
701}
702
703void GBAFrameEnded(struct GBA* gba) {
704 GBASavedataClean(&gba->memory.savedata, gba->video.frameCounter);
705
706 if (gba->rr) {
707 gba->rr->nextFrame(gba->rr);
708 }
709
710 if (gba->cpu->components && gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
711 struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
712 size_t i;
713 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
714 struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
715 mCheatRefresh(device, &cheats->d);
716 }
717 }
718
719 if (gba->stream && gba->stream->postVideoFrame) {
720 const color_t* pixels;
721 size_t stride;
722 gba->video.renderer->getPixels(gba->video.renderer, &stride, (const void**) &pixels);
723 gba->stream->postVideoFrame(gba->stream, pixels, stride);
724 }
725
726 if (gba->memory.hw.devices & (HW_GB_PLAYER | HW_GB_PLAYER_DETECTION)) {
727 GBAHardwarePlayerUpdate(gba);
728 }
729
730 size_t c;
731 for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
732 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
733 if (callbacks->videoFrameEnded) {
734 callbacks->videoFrameEnded(callbacks->context);
735 }
736 }
737}
738
739void GBATestKeypadIRQ(struct GBA* gba) {
740 uint16_t keycnt = gba->memory.io[REG_KEYCNT >> 1];
741 if (!(keycnt & 0x4000)) {
742 return;
743 }
744 int isAnd = keycnt & 0x8000;
745 uint16_t keyInput;
746
747 if (!gba->keySource) {
748 // TODO?
749 return;
750 }
751
752 keycnt &= 0x3FF;
753 keyInput = *gba->keySource;
754
755 if (isAnd && keycnt == keyInput) {
756 GBARaiseIRQ(gba, IRQ_KEYPAD);
757 } else if (!isAnd && keycnt & keyInput) {
758 GBARaiseIRQ(gba, IRQ_KEYPAD);
759 }
760}
761
762void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
763 size_t immediate;
764 for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) {
765 if (gba->cpu->components[immediate] == component) {
766 break;
767 }
768 }
769 if (immediate == gba->cpu->numComponents) {
770 return;
771 }
772 if (mode == MODE_ARM) {
773 int32_t value;
774 int32_t old;
775 value = 0xE1200070;
776 value |= immediate & 0xF;
777 value |= (immediate & 0xFFF0) << 4;
778 GBAPatch32(gba->cpu, address, value, &old);
779 *opcode = old;
780 } else {
781 int16_t value;
782 int16_t old;
783 value = 0xBE00;
784 value |= immediate & 0xFF;
785 GBAPatch16(gba->cpu, address, value, &old);
786 *opcode = (uint16_t) old;
787 }
788}
789
790void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
791 if (mode == MODE_ARM) {
792 GBAPatch32(gba->cpu, address, opcode, 0);
793 } else {
794 GBAPatch16(gba->cpu, address, opcode, 0);
795 }
796}
797
798static bool _setSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
799 GBASetBreakpoint((struct GBA*) debugger->cpu->master, &debugger->d.p->d, address, mode, opcode);
800 return true;
801}
802
803static bool _clearSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
804 GBAClearBreakpoint((struct GBA*) debugger->cpu->master, address, mode, opcode);
805 return true;
806}