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) {
531 return false;
532 }
533 if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
534 return false;
535 }
536 uint8_t signature[sizeof(GBA_ROM_MAGIC)];
537 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
538 return false;
539 }
540 if (GBAIsBIOS(vf)) {
541 return false;
542 }
543 return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
544}
545
546bool GBAIsMB(struct VFile* vf) {
547 if (!GBAIsROM(vf)) {
548 return false;
549 }
550#ifdef USE_ELF
551 struct ELF* elf = ELFOpen(vf);
552 if (elf) {
553 bool isMB = ELFEntry(elf) == BASE_WORKING_RAM;
554 ELFClose(elf);
555 return isMB;
556 }
557#endif
558 if (vf->size(vf) > SIZE_WORKING_RAM) {
559 return false;
560 }
561 if (vf->seek(vf, GBA_MB_MAGIC_OFFSET, SEEK_SET) < 0) {
562 return false;
563 }
564 uint32_t signature;
565 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
566 return false;
567 }
568 uint32_t opcode;
569 LOAD_32(opcode, 0, &signature);
570 struct ARMInstructionInfo info;
571 ARMDecodeARM(opcode, &info);
572 if (info.branchType == ARM_BRANCH) {
573 if (info.op1.immediate <= 0) {
574 return false;
575 } else if (info.op1.immediate == 28) {
576 // Ancient toolchain that is known to throw MB detection for a loop
577 return false;
578 } else if (info.op1.immediate != 24) {
579 return true;
580 }
581 }
582
583 uint32_t pc = GBA_MB_MAGIC_OFFSET;
584 int i;
585 for (i = 0; i < 80; ++i) {
586 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
587 break;
588 }
589 pc += 4;
590 LOAD_32(opcode, 0, &signature);
591 ARMDecodeARM(opcode, &info);
592 if (info.mnemonic != ARM_MN_LDR) {
593 continue;
594 }
595 if ((info.operandFormat & ARM_OPERAND_MEMORY) && info.memory.baseReg == ARM_PC && info.memory.format & ARM_MEMORY_IMMEDIATE_OFFSET) {
596 uint32_t immediate = info.memory.offset.immediate;
597 if (info.memory.format & ARM_MEMORY_OFFSET_SUBTRACT) {
598 immediate = -immediate;
599 }
600 immediate += pc + 8;
601 if (vf->seek(vf, immediate, SEEK_SET) < 0) {
602 break;
603 }
604 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
605 break;
606 }
607 LOAD_32(immediate, 0, &signature);
608 if (vf->seek(vf, pc, SEEK_SET) < 0) {
609 break;
610 }
611 if ((immediate & ~0x7FF) == BASE_WORKING_RAM) {
612 return true;
613 }
614 }
615 }
616 // Found a libgba-linked cart...these are a bit harder to detect.
617 return false;
618}
619
620bool GBAIsBIOS(struct VFile* vf) {
621 if (vf->seek(vf, 0, SEEK_SET) < 0) {
622 return false;
623 }
624 uint8_t interruptTable[7 * 4];
625 if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
626 return false;
627 }
628 int i;
629 for (i = 0; i < 7; ++i) {
630 if (interruptTable[4 * i + 3] != 0xEA || interruptTable[4 * i + 2]) {
631 return false;
632 }
633 }
634 return true;
635}
636
637void GBAGetGameCode(const struct GBA* gba, char* out) {
638 memset(out, 0, 8);
639 if (!gba->memory.rom) {
640 return;
641 }
642
643 memcpy(out, "AGB-", 4);
644 memcpy(&out[4], &((struct GBACartridge*) gba->memory.rom)->id, 4);
645}
646
647void GBAGetGameTitle(const struct GBA* gba, char* out) {
648 if (gba->memory.rom) {
649 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
650 return;
651 }
652 if (gba->isPristine && gba->memory.wram) {
653 memcpy(out, &((struct GBACartridge*) gba->memory.wram)->title, 12);
654 return;
655 }
656 strncpy(out, "(BIOS)", 12);
657}
658
659void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
660 struct GBA* gba = (struct GBA*) cpu->master;
661#ifdef USE_DEBUGGERS
662 if (gba->debugger) {
663 struct mDebuggerEntryInfo info = {
664 .address = _ARMPCAddress(cpu),
665 .opcode = opcode
666 };
667 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
668 }
669#endif
670 // TODO: More sensible category?
671 mLOG(GBA, ERROR, "Stub opcode: %08x", opcode);
672}
673
674void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
675 struct GBA* gba = (struct GBA*) cpu->master;
676 if (!gba->yankedRomSize) {
677 // TODO: More sensible category?
678 mLOG(GBA, WARN, "Illegal opcode: %08x", opcode);
679 }
680 if (cpu->executionMode == MODE_THUMB && (opcode & 0xFFC0) == 0xE800) {
681 mLOG(GBA, DEBUG, "Hit Wii U VC opcode: %08x", opcode);
682 return;
683 }
684#ifdef USE_DEBUGGERS
685 if (gba->debugger) {
686 struct mDebuggerEntryInfo info = {
687 .address = _ARMPCAddress(cpu),
688 .opcode = opcode
689 };
690 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
691 } else
692#endif
693 {
694 ARMRaiseUndefined(cpu);
695 }
696}
697
698void GBABreakpoint(struct ARMCore* cpu, int immediate) {
699 struct GBA* gba = (struct GBA*) cpu->master;
700 if (immediate >= CPU_COMPONENT_MAX) {
701 return;
702 }
703 switch (immediate) {
704#ifdef USE_DEBUGGERS
705 case CPU_COMPONENT_DEBUGGER:
706 if (gba->debugger) {
707 struct mDebuggerEntryInfo info = {
708 .address = _ARMPCAddress(cpu),
709 .breakType = BREAKPOINT_SOFTWARE
710 };
711 mDebuggerEnter(gba->debugger->d.p, DEBUGGER_ENTER_BREAKPOINT, &info);
712 }
713 break;
714#endif
715 case CPU_COMPONENT_CHEAT_DEVICE:
716 if (gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
717 struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
718 struct GBACheatHook* hook = 0;
719 size_t i;
720 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
721 struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
722 if (cheats->hook && cheats->hook->address == _ARMPCAddress(cpu)) {
723 mCheatRefresh(device, &cheats->d);
724 hook = cheats->hook;
725 }
726 }
727 if (hook) {
728 ARMRunFake(cpu, hook->patchedOpcode);
729 }
730 }
731 break;
732 default:
733 break;
734 }
735}
736
737void GBAFrameStarted(struct GBA* gba) {
738 GBATestKeypadIRQ(gba);
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->videoFrameStarted) {
744 callbacks->videoFrameStarted(callbacks->context);
745 }
746 }
747}
748
749void GBAFrameEnded(struct GBA* gba) {
750 GBASavedataClean(&gba->memory.savedata, gba->video.frameCounter);
751
752 if (gba->rr) {
753 gba->rr->nextFrame(gba->rr);
754 }
755
756 if (gba->cpu->components && gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
757 struct mCheatDevice* device = (struct mCheatDevice*) gba->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
758 size_t i;
759 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
760 struct GBACheatSet* cheats = (struct GBACheatSet*) *mCheatSetsGetPointer(&device->cheats, i);
761 mCheatRefresh(device, &cheats->d);
762 }
763 }
764
765 if (gba->stream && gba->stream->postVideoFrame) {
766 const color_t* pixels;
767 size_t stride;
768 gba->video.renderer->getPixels(gba->video.renderer, &stride, (const void**) &pixels);
769 gba->stream->postVideoFrame(gba->stream, pixels, stride);
770 }
771
772 if (gba->memory.hw.devices & (HW_GB_PLAYER | HW_GB_PLAYER_DETECTION)) {
773 GBAHardwarePlayerUpdate(gba);
774 }
775
776 size_t c;
777 for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) {
778 struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c);
779 if (callbacks->videoFrameEnded) {
780 callbacks->videoFrameEnded(callbacks->context);
781 }
782 }
783}
784
785void GBATestKeypadIRQ(struct GBA* gba) {
786 uint16_t keycnt = gba->memory.io[REG_KEYCNT >> 1];
787 if (!(keycnt & 0x4000)) {
788 return;
789 }
790 int isAnd = keycnt & 0x8000;
791 if (!gba->keySource) {
792 // TODO?
793 return;
794 }
795
796 keycnt &= 0x3FF;
797 uint16_t keyInput = *gba->keySource & keycnt;
798
799 if (isAnd && keycnt == keyInput) {
800 GBARaiseIRQ(gba, IRQ_KEYPAD);
801 } else if (!isAnd && keyInput) {
802 GBARaiseIRQ(gba, IRQ_KEYPAD);
803 }
804}
805
806void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
807 size_t immediate;
808 for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) {
809 if (gba->cpu->components[immediate] == component) {
810 break;
811 }
812 }
813 if (immediate == gba->cpu->numComponents) {
814 return;
815 }
816 if (mode == MODE_ARM) {
817 int32_t value;
818 int32_t old;
819 value = 0xE1200070;
820 value |= immediate & 0xF;
821 value |= (immediate & 0xFFF0) << 4;
822 GBAPatch32(gba->cpu, address, value, &old);
823 *opcode = old;
824 } else {
825 int16_t value;
826 int16_t old;
827 value = 0xBE00;
828 value |= immediate & 0xFF;
829 GBAPatch16(gba->cpu, address, value, &old);
830 *opcode = (uint16_t) old;
831 }
832}
833
834void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
835 if (mode == MODE_ARM) {
836 GBAPatch32(gba->cpu, address, opcode, 0);
837 } else {
838 GBAPatch16(gba->cpu, address, opcode, 0);
839 }
840}
841
842static bool _setSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
843 GBASetBreakpoint((struct GBA*) debugger->cpu->master, &debugger->d.p->d, address, mode, opcode);
844 return true;
845}
846
847static bool _clearSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
848 GBAClearBreakpoint((struct GBA*) debugger->cpu->master, address, mode, opcode);
849 return true;
850}