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