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
316 if (gba->cpu) {
317 gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
318 }
319 return true;
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 gba->isPristine = true;
331 gba->memory.wram = anonymousMemoryMap(SIZE_WORKING_RAM);
332 memset(gba->memory.wram, 0, SIZE_WORKING_RAM);
333 vf->read(vf, gba->memory.wram, gba->pristineRomSize);
334 if (!gba->memory.wram) {
335 mLOG(GBA, WARN, "Couldn't map ROM");
336 return false;
337 }
338 gba->yankedRomSize = 0;
339 gba->memory.romSize = 0;
340 gba->memory.romMask = 0;
341 gba->romCrc32 = doCrc32(gba->memory.wram, gba->pristineRomSize);
342 if (gba->cpu && gba->memory.activeRegion == REGION_WORKING_RAM) {
343 gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
344 }
345 return true;
346}
347
348bool GBALoadROM(struct GBA* gba, struct VFile* vf) {
349 if (!vf) {
350 return false;
351 }
352 GBAUnloadROM(gba);
353 gba->romVf = vf;
354 gba->pristineRomSize = vf->size(vf);
355 vf->seek(vf, 0, SEEK_SET);
356 if (gba->pristineRomSize > SIZE_CART0) {
357 gba->pristineRomSize = SIZE_CART0;
358 }
359 gba->isPristine = true;
360#ifdef FIXED_ROM_BUFFER
361 if (gba->pristineRomSize <= romBufferSize) {
362 gba->memory.rom = romBuffer;
363 vf->read(vf, romBuffer, gba->pristineRomSize);
364 }
365#else
366 gba->memory.rom = vf->map(vf, gba->pristineRomSize, MAP_READ);
367#endif
368 if (!gba->memory.rom) {
369 mLOG(GBA, WARN, "Couldn't map ROM");
370 return false;
371 }
372 gba->yankedRomSize = 0;
373 gba->memory.romSize = gba->pristineRomSize;
374 gba->memory.romMask = toPow2(gba->memory.romSize) - 1;
375 gba->memory.mirroring = false;
376 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
377 GBAHardwareInit(&gba->memory.hw, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
378 GBAVFameDetect(&gba->memory.vfame, gba->memory.rom, gba->memory.romSize);
379 if (popcount32(gba->memory.romSize) != 1) {
380 // This ROM is either a bad dump or homebrew. Emulate flash cart behavior.
381#ifndef FIXED_ROM_BUFFER
382 void* newRom = anonymousMemoryMap(SIZE_CART0);
383 memcpy(newRom, gba->memory.rom, gba->pristineRomSize);
384 gba->memory.rom = newRom;
385#endif
386 gba->memory.romSize = SIZE_CART0;
387 gba->isPristine = false;
388 }
389 if (gba->cpu && gba->memory.activeRegion >= REGION_CART0) {
390 gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
391 }
392 // TODO: error check
393 return true;
394}
395
396bool GBALoadSave(struct GBA* gba, struct VFile* sav) {
397 GBASavedataInit(&gba->memory.savedata, sav);
398 return true;
399}
400
401void GBAYankROM(struct GBA* gba) {
402 gba->yankedRomSize = gba->memory.romSize;
403 gba->memory.romSize = 0;
404 gba->memory.romMask = 0;
405 GBARaiseIRQ(gba, IRQ_GAMEPAK);
406}
407
408void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
409 gba->biosVf = vf;
410 uint32_t* bios = vf->map(vf, SIZE_BIOS, MAP_READ);
411 if (!bios) {
412 mLOG(GBA, WARN, "Couldn't map BIOS");
413 return;
414 }
415 gba->memory.bios = bios;
416 gba->memory.fullBios = 1;
417 uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
418 mLOG(GBA, DEBUG, "BIOS Checksum: 0x%X", checksum);
419 if (checksum == GBA_BIOS_CHECKSUM) {
420 mLOG(GBA, INFO, "Official GBA BIOS detected");
421 } else if (checksum == GBA_DS_BIOS_CHECKSUM) {
422 mLOG(GBA, INFO, "Official GBA (DS) BIOS detected");
423 } else {
424 mLOG(GBA, WARN, "BIOS checksum incorrect");
425 }
426 gba->biosChecksum = checksum;
427 if (gba->memory.activeRegion == REGION_BIOS) {
428 gba->cpu->memory.activeRegion = gba->memory.bios;
429 }
430 // TODO: error check
431}
432
433void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
434 size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
435 if (!patchedSize || patchedSize > SIZE_CART0) {
436 return;
437 }
438 void* newRom = anonymousMemoryMap(SIZE_CART0);
439 if (!patch->applyPatch(patch, gba->memory.rom, gba->pristineRomSize, newRom, patchedSize)) {
440 mappedMemoryFree(newRom, SIZE_CART0);
441 return;
442 }
443 if (gba->romVf) {
444#ifndef FIXED_ROM_BUFFER
445 gba->romVf->unmap(gba->romVf, gba->memory.rom, gba->pristineRomSize);
446#endif
447 gba->romVf->close(gba->romVf);
448 gba->romVf = NULL;
449 }
450 gba->isPristine = false;
451 gba->memory.rom = newRom;
452 gba->memory.hw.gpioBase = &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1];
453 gba->memory.romSize = patchedSize;
454 gba->memory.romMask = SIZE_CART0 - 1;
455 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
456}
457
458void GBAWriteIE(struct GBA* gba, uint16_t value) {
459 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
460 ARMRaiseIRQ(gba->cpu);
461 }
462}
463
464void GBAWriteIME(struct GBA* gba, uint16_t value) {
465 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
466 ARMRaiseIRQ(gba->cpu);
467 }
468}
469
470void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
471 gba->memory.io[REG_IF >> 1] |= 1 << irq;
472
473 if (gba->memory.io[REG_IE >> 1] & 1 << irq) {
474 gba->cpu->halted = 0;
475 if (gba->memory.io[REG_IME >> 1]) {
476 ARMRaiseIRQ(gba->cpu);
477 }
478 }
479}
480
481void GBATestIRQ(struct ARMCore* cpu) {
482 struct GBA* gba = (struct GBA*) cpu->master;
483 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
484 gba->springIRQ = gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1];
485 gba->cpu->nextEvent = gba->cpu->cycles;
486 }
487}
488
489void GBAHalt(struct GBA* gba) {
490 gba->cpu->nextEvent = gba->cpu->cycles;
491 gba->cpu->halted = 1;
492}
493
494void GBAStop(struct GBA* gba) {
495 size_t c;
496 for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
497 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
498 if (callbacks->sleep) {
499 callbacks->sleep(callbacks->context);
500 }
501 }
502 gba->cpu->nextEvent = gba->cpu->cycles;
503}
504
505void GBADebug(struct GBA* gba, uint16_t flags) {
506 gba->debugFlags = flags;
507 if (GBADebugFlagsIsSend(gba->debugFlags)) {
508 int level = 1 << GBADebugFlagsGetLevel(gba->debugFlags);
509 level &= 0x1F;
510 char oolBuf[0x101];
511 strncpy(oolBuf, gba->debugString, sizeof(gba->debugString));
512 oolBuf[0x100] = '\0';
513 mLog(_mLOG_CAT_GBA_DEBUG(), level, "%s", oolBuf);
514 }
515 gba->debugFlags = GBADebugFlagsClearSend(gba->debugFlags);
516}
517
518bool GBAIsROM(struct VFile* vf) {
519#ifdef USE_ELF
520 struct ELF* elf = ELFOpen(vf);
521 if (elf) {
522 uint32_t entry = ELFEntry(elf);
523 bool isGBA = true;
524 isGBA = isGBA && ELFMachine(elf) == EM_ARM;
525 isGBA = isGBA && (entry == BASE_CART0 || entry == BASE_WORKING_RAM);
526 ELFClose(elf);
527 return isGBA;
528 }
529#endif
530 if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
531 return false;
532 }
533 uint8_t signature[sizeof(GBA_ROM_MAGIC)];
534 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
535 return false;
536 }
537 if (GBAIsBIOS(vf)) {
538 return false;
539 }
540 return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
541}
542
543bool GBAIsMB(struct VFile* vf) {
544 if (!GBAIsROM(vf)) {
545 return false;
546 }
547#ifdef USE_ELF
548 struct ELF* elf = ELFOpen(vf);
549 if (elf) {
550 bool isMB = ELFEntry(elf) == BASE_WORKING_RAM;
551 ELFClose(elf);
552 return isMB;
553 }
554#endif
555 if (vf->size(vf) > SIZE_WORKING_RAM) {
556 return false;
557 }
558 if (vf->seek(vf, GBA_MB_MAGIC_OFFSET, SEEK_SET) < 0) {
559 return false;
560 }
561 uint32_t signature;
562 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
563 return false;
564 }
565 uint32_t opcode;
566 LOAD_32(opcode, 0, &signature);
567 struct ARMInstructionInfo info;
568 ARMDecodeARM(opcode, &info);
569 if (info.branchType != ARM_BRANCH) {
570 return false;
571 }
572 if (info.op1.immediate <= 0) {
573 return false;
574 } else if (info.op1.immediate == 28) {
575 // Ancient toolchain that is known to throw MB detection for a loop
576 return false;
577 } else if (info.op1.immediate != 24) {
578 return true;
579 }
580 // Found a libgba-linked cart...these are a bit harder to detect.
581 return false;
582}
583
584bool GBAIsBIOS(struct VFile* vf) {
585 if (vf->seek(vf, 0, SEEK_SET) < 0) {
586 return false;
587 }
588 uint8_t interruptTable[7 * 4];
589 if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
590 return false;
591 }
592 int i;
593 for (i = 0; i < 7; ++i) {
594 if (interruptTable[4 * i + 3] != 0xEA || interruptTable[4 * i + 2]) {
595 return false;
596 }
597 }
598 return true;
599}
600
601void GBAGetGameCode(const struct GBA* gba, char* out) {
602 memset(out, 0, 8);
603 if (!gba->memory.rom) {
604 return;
605 }
606
607 memcpy(out, "AGB-", 4);
608 memcpy(&out[4], &((struct GBACartridge*) gba->memory.rom)->id, 4);
609}
610
611void GBAGetGameTitle(const struct GBA* gba, char* out) {
612 if (gba->memory.rom) {
613 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
614 return;
615 }
616 if (gba->isPristine && gba->memory.wram) {
617 memcpy(out, &((struct GBACartridge*) gba->memory.wram)->title, 12);
618 return;
619 }
620 strncpy(out, "(BIOS)", 12);
621}
622
623void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
624 struct GBA* gba = (struct GBA*) cpu->master;
625#ifdef USE_DEBUGGERS
626 if (gba->debugger) {
627 struct mDebuggerEntryInfo info = {
628 .address = _ARMPCAddress(cpu),
629 .opcode = opcode
630 };
631 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
632 }
633#endif
634 // TODO: More sensible category?
635 mLOG(GBA, ERROR, "Stub opcode: %08x", opcode);
636}
637
638void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
639 struct GBA* gba = (struct GBA*) cpu->master;
640 if (!gba->yankedRomSize) {
641 // TODO: More sensible category?
642 mLOG(GBA, WARN, "Illegal opcode: %08x", opcode);
643 }
644 if (cpu->executionMode == MODE_THUMB && (opcode & 0xFFC0) == 0xE800) {
645 mLOG(GBA, DEBUG, "Hit Wii U VC opcode: %08x", opcode);
646 return;
647 }
648#ifdef USE_DEBUGGERS
649 if (gba->debugger) {
650 struct mDebuggerEntryInfo info = {
651 .address = _ARMPCAddress(cpu),
652 .opcode = opcode
653 };
654 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
655 } else
656#endif
657 {
658 ARMRaiseUndefined(cpu);
659 }
660}
661
662void GBABreakpoint(struct ARMCore* cpu, int immediate) {
663 struct GBA* gba = (struct GBA*) cpu->master;
664 if (immediate >= CPU_COMPONENT_MAX) {
665 return;
666 }
667 switch (immediate) {
668#ifdef USE_DEBUGGERS
669 case CPU_COMPONENT_DEBUGGER:
670 if (gba->debugger) {
671 struct mDebuggerEntryInfo info = {
672 .address = _ARMPCAddress(cpu),
673 .breakType = BREAKPOINT_SOFTWARE
674 };
675 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_BREAKPOINT, &info);
676 }
677 break;
678#endif
679 case CPU_COMPONENT_CHEAT_DEVICE:
680 if (gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
681 struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
682 struct GBACheatHook* hook = 0;
683 size_t i;
684 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
685 struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
686 if (cheats->hook && cheats->hook->address == _ARMPCAddress(cpu)) {
687 mCheatRefresh(device, &cheats->d);
688 hook = cheats->hook;
689 }
690 }
691 if (hook) {
692 ARMRunFake(cpu, hook->patchedOpcode);
693 }
694 }
695 break;
696 default:
697 break;
698 }
699}
700
701void GBAFrameStarted(struct GBA* gba) {
702 GBATestKeypadIRQ(gba);
703
704 size_t c;
705 for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
706 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
707 if (callbacks->videoFrameStarted) {
708 callbacks->videoFrameStarted(callbacks->context);
709 }
710 }
711}
712
713void GBAFrameEnded(struct GBA* gba) {
714 GBASavedataClean(&gba->memory.savedata, gba->video.frameCounter);
715
716 if (gba->rr) {
717 gba->rr->nextFrame(gba->rr);
718 }
719
720 if (gba->cpu->components && gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
721 struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
722 size_t i;
723 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
724 struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
725 mCheatRefresh(device, &cheats->d);
726 }
727 }
728
729 if (gba->stream && gba->stream->postVideoFrame) {
730 const color_t* pixels;
731 size_t stride;
732 gba->video.renderer->getPixels(gba->video.renderer, &stride, (const void**) &pixels);
733 gba->stream->postVideoFrame(gba->stream, pixels, stride);
734 }
735
736 if (gba->memory.hw.devices & (HW_GB_PLAYER | HW_GB_PLAYER_DETECTION)) {
737 GBAHardwarePlayerUpdate(gba);
738 }
739
740 size_t c;
741 for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
742 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
743 if (callbacks->videoFrameEnded) {
744 callbacks->videoFrameEnded(callbacks->context);
745 }
746 }
747}
748
749void GBATestKeypadIRQ(struct GBA* gba) {
750 uint16_t keycnt = gba->memory.io[REG_KEYCNT >> 1];
751 if (!(keycnt & 0x4000)) {
752 return;
753 }
754 int isAnd = keycnt & 0x8000;
755 uint16_t keyInput;
756
757 if (!gba->keySource) {
758 // TODO?
759 return;
760 }
761
762 keycnt &= 0x3FF;
763 keyInput = *gba->keySource;
764
765 if (isAnd && keycnt == keyInput) {
766 GBARaiseIRQ(gba, IRQ_KEYPAD);
767 } else if (!isAnd && keycnt & keyInput) {
768 GBARaiseIRQ(gba, IRQ_KEYPAD);
769 }
770}
771
772void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
773 size_t immediate;
774 for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) {
775 if (gba->cpu->components[immediate] == component) {
776 break;
777 }
778 }
779 if (immediate == gba->cpu->numComponents) {
780 return;
781 }
782 if (mode == MODE_ARM) {
783 int32_t value;
784 int32_t old;
785 value = 0xE1200070;
786 value |= immediate & 0xF;
787 value |= (immediate & 0xFFF0) << 4;
788 GBAPatch32(gba->cpu, address, value, &old);
789 *opcode = old;
790 } else {
791 int16_t value;
792 int16_t old;
793 value = 0xBE00;
794 value |= immediate & 0xFF;
795 GBAPatch16(gba->cpu, address, value, &old);
796 *opcode = (uint16_t) old;
797 }
798}
799
800void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
801 if (mode == MODE_ARM) {
802 GBAPatch32(gba->cpu, address, opcode, 0);
803 } else {
804 GBAPatch16(gba->cpu, address, opcode, 0);
805 }
806}
807
808static bool _setSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
809 GBASetBreakpoint((struct GBA*) debugger->cpu->master, &debugger->d.p->d, address, mode, opcode);
810 return true;
811}
812
813static bool _clearSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
814 GBAClearBreakpoint((struct GBA*) debugger->cpu->master, address, mode, opcode);
815 return true;
816}