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 "gba.h"
7
8#include "gba/bios.h"
9#include "gba/cheats.h"
10#include "gba/io.h"
11#include "gba/supervisor/rr.h"
12#include "gba/supervisor/thread.h"
13#include "gba/serialize.h"
14#include "gba/sio.h"
15
16#include "isa-inlines.h"
17
18#include "util/crc32.h"
19#include "util/memory.h"
20#include "util/patch.h"
21#include "util/vfs.h"
22
23const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;
24const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;
25
26static const size_t GBA_ROM_MAGIC_OFFSET = 3;
27static const uint8_t GBA_ROM_MAGIC[] = { 0xEA };
28
29static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component);
30static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh);
31static void GBAProcessEvents(struct ARMCore* cpu);
32static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
33static void GBAHitStub(struct ARMCore* cpu, uint32_t opcode);
34static void GBAIllegal(struct ARMCore* cpu, uint32_t opcode);
35static void GBABreakpoint(struct ARMCore* cpu, int immediate);
36
37static bool _setSoftwareBreakpoint(struct ARMDebugger*, uint32_t address, enum ExecutionMode mode, uint32_t* opcode);
38static bool _clearSoftwareBreakpoint(struct ARMDebugger*, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
39
40void GBACreate(struct GBA* gba) {
41 gba->d.id = GBA_COMPONENT_MAGIC;
42 gba->d.init = GBAInit;
43 gba->d.deinit = 0;
44}
45
46static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component) {
47 struct GBA* gba = (struct GBA*) component;
48 gba->cpu = cpu;
49 gba->debugger = 0;
50 gba->sync = 0;
51
52 GBAInterruptHandlerInit(&cpu->irqh);
53 GBAMemoryInit(gba);
54 GBASavedataInit(&gba->memory.savedata, 0);
55
56 gba->video.p = gba;
57 GBAVideoInit(&gba->video);
58
59 gba->audio.p = gba;
60 GBAAudioInit(&gba->audio, GBA_AUDIO_SAMPLES);
61
62 GBAIOInit(gba);
63
64 gba->sio.p = gba;
65 GBASIOInit(&gba->sio);
66
67 gba->timersEnabled = 0;
68 memset(gba->timers, 0, sizeof(gba->timers));
69
70 gba->springIRQ = 0;
71 gba->keySource = 0;
72 gba->rotationSource = 0;
73 gba->luminanceSource = 0;
74 gba->rtcSource = 0;
75 gba->rumble = 0;
76 gba->rr = 0;
77
78 gba->romVf = 0;
79 gba->biosVf = 0;
80
81 gba->logHandler = 0;
82 gba->logLevel = GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
83 gba->stream = 0;
84 gba->keyCallback = 0;
85
86 gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
87
88 gba->idleOptimization = IDLE_LOOP_REMOVE;
89 gba->idleLoop = IDLE_LOOP_NONE;
90 gba->lastJump = 0;
91 gba->haltPending = false;
92 gba->idleDetectionStep = 0;
93 gba->idleDetectionFailures = 0;
94
95 gba->realisticTiming = true;
96
97 gba->performingDMA = false;
98}
99
100void GBAUnloadROM(struct GBA* gba) {
101 if (gba->memory.rom && gba->pristineRom != gba->memory.rom) {
102 if (gba->yankedRomSize) {
103 gba->yankedRomSize = 0;
104 }
105 mappedMemoryFree(gba->memory.rom, SIZE_CART0);
106 }
107 gba->memory.rom = 0;
108
109 if (gba->romVf) {
110 gba->romVf->unmap(gba->romVf, gba->pristineRom, gba->pristineRomSize);
111 gba->pristineRom = 0;
112 gba->romVf = 0;
113 }
114}
115
116void GBADestroy(struct GBA* gba) {
117 GBAUnloadROM(gba);
118
119 if (gba->biosVf) {
120 gba->biosVf->unmap(gba->biosVf, gba->memory.bios, SIZE_BIOS);
121 }
122
123 GBAMemoryDeinit(gba);
124 GBAVideoDeinit(&gba->video);
125 GBAAudioDeinit(&gba->audio);
126 GBASIODeinit(&gba->sio);
127 gba->rr = 0;
128}
129
130void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {
131 irqh->reset = GBAReset;
132 irqh->processEvents = GBAProcessEvents;
133 irqh->swi16 = GBASwi16;
134 irqh->swi32 = GBASwi32;
135 irqh->hitIllegal = GBAIllegal;
136 irqh->readCPSR = GBATestIRQ;
137 irqh->hitStub = GBAHitStub;
138 irqh->bkpt16 = GBABreakpoint;
139 irqh->bkpt32 = GBABreakpoint;
140}
141
142void GBAReset(struct ARMCore* cpu) {
143 ARMSetPrivilegeMode(cpu, MODE_IRQ);
144 cpu->gprs[ARM_SP] = SP_BASE_IRQ;
145 ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
146 cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
147 ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
148 cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
149
150 struct GBA* gba = (struct GBA*) cpu->master;
151 if (!gba->rr || (!gba->rr->isPlaying(gba->rr) && !gba->rr->isRecording(gba->rr))) {
152 GBASavedataUnmask(&gba->memory.savedata);
153 }
154
155 if (gba->yankedRomSize) {
156 gba->memory.romSize = gba->yankedRomSize;
157 gba->yankedRomSize = 0;
158 }
159 GBAMemoryReset(gba);
160 GBAVideoReset(&gba->video);
161 GBAAudioReset(&gba->audio);
162 GBAIOInit(gba);
163
164 GBASIODeinit(&gba->sio);
165 GBASIOInit(&gba->sio);
166
167 gba->timersEnabled = 0;
168 memset(gba->timers, 0, sizeof(gba->timers));
169}
170
171void GBASkipBIOS(struct ARMCore* cpu) {
172 if (cpu->gprs[ARM_PC] == BASE_RESET + WORD_SIZE_ARM) {
173 cpu->gprs[ARM_PC] = BASE_CART0;
174 int currentCycles = 0;
175 ARM_WRITE_PC;
176 }
177}
178
179static void GBAProcessEvents(struct ARMCore* cpu) {
180 do {
181 struct GBA* gba = (struct GBA*) cpu->master;
182 int32_t cycles = cpu->nextEvent;
183 int32_t nextEvent = INT_MAX;
184 int32_t testEvent;
185
186 gba->bus = cpu->prefetch[1];
187 if (cpu->executionMode == MODE_THUMB) {
188 gba->bus |= cpu->prefetch[1] << 16;
189 }
190
191 if (gba->springIRQ) {
192 ARMRaiseIRQ(cpu);
193 gba->springIRQ = 0;
194 }
195
196 testEvent = GBAVideoProcessEvents(&gba->video, cycles);
197 if (testEvent < nextEvent) {
198 nextEvent = testEvent;
199 }
200
201 testEvent = GBAAudioProcessEvents(&gba->audio, cycles);
202 if (testEvent < nextEvent) {
203 nextEvent = testEvent;
204 }
205
206 testEvent = GBATimersProcessEvents(gba, cycles);
207 if (testEvent < nextEvent) {
208 nextEvent = testEvent;
209 }
210
211 testEvent = GBAMemoryRunDMAs(gba, cycles);
212 if (testEvent < nextEvent) {
213 nextEvent = testEvent;
214 }
215
216 testEvent = GBASIOProcessEvents(&gba->sio, cycles);
217 if (testEvent < nextEvent) {
218 nextEvent = testEvent;
219 }
220
221 cpu->cycles -= cycles;
222 cpu->nextEvent = nextEvent;
223
224 if (cpu->halted) {
225 cpu->cycles = cpu->nextEvent;
226 }
227 } while (cpu->cycles >= cpu->nextEvent);
228}
229
230static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
231 int32_t nextEvent = INT_MAX;
232 if (gba->timersEnabled) {
233 struct GBATimer* timer;
234 struct GBATimer* nextTimer;
235
236 timer = &gba->timers[0];
237 if (timer->enable) {
238 timer->nextEvent -= cycles;
239 timer->lastEvent -= cycles;
240 if (timer->nextEvent <= 0) {
241 timer->lastEvent = timer->nextEvent;
242 timer->nextEvent += timer->overflowInterval;
243 gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
244 timer->oldReload = timer->reload;
245
246 if (timer->doIrq) {
247 GBARaiseIRQ(gba, IRQ_TIMER0);
248 }
249
250 if (gba->audio.enable) {
251 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 0) {
252 GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
253 }
254
255 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 0) {
256 GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
257 }
258 }
259
260 nextTimer = &gba->timers[1];
261 if (nextTimer->countUp) {
262 ++gba->memory.io[REG_TM1CNT_LO >> 1];
263 if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
264 nextTimer->nextEvent = 0;
265 }
266 }
267 }
268 nextEvent = timer->nextEvent;
269 }
270
271 timer = &gba->timers[1];
272 if (timer->enable) {
273 timer->nextEvent -= cycles;
274 timer->lastEvent -= cycles;
275 if (timer->nextEvent <= 0) {
276 timer->lastEvent = timer->nextEvent;
277 timer->nextEvent += timer->overflowInterval;
278 gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
279 timer->oldReload = timer->reload;
280
281 if (timer->doIrq) {
282 GBARaiseIRQ(gba, IRQ_TIMER1);
283 }
284
285 if (gba->audio.enable) {
286 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 1) {
287 GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
288 }
289
290 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 1) {
291 GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
292 }
293 }
294
295 if (timer->countUp) {
296 timer->nextEvent = INT_MAX;
297 }
298
299 nextTimer = &gba->timers[2];
300 if (nextTimer->countUp) {
301 ++gba->memory.io[REG_TM2CNT_LO >> 1];
302 if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
303 nextTimer->nextEvent = 0;
304 }
305 }
306 }
307 if (timer->nextEvent < nextEvent) {
308 nextEvent = timer->nextEvent;
309 }
310 }
311
312 timer = &gba->timers[2];
313 if (timer->enable) {
314 timer->nextEvent -= cycles;
315 timer->lastEvent -= cycles;
316 if (timer->nextEvent <= 0) {
317 timer->lastEvent = timer->nextEvent;
318 timer->nextEvent += timer->overflowInterval;
319 gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
320 timer->oldReload = timer->reload;
321
322 if (timer->doIrq) {
323 GBARaiseIRQ(gba, IRQ_TIMER2);
324 }
325
326 if (timer->countUp) {
327 timer->nextEvent = INT_MAX;
328 }
329
330 nextTimer = &gba->timers[3];
331 if (nextTimer->countUp) {
332 ++gba->memory.io[REG_TM3CNT_LO >> 1];
333 if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
334 nextTimer->nextEvent = 0;
335 }
336 }
337 }
338 if (timer->nextEvent < nextEvent) {
339 nextEvent = timer->nextEvent;
340 }
341 }
342
343 timer = &gba->timers[3];
344 if (timer->enable) {
345 timer->nextEvent -= cycles;
346 timer->lastEvent -= cycles;
347 if (timer->nextEvent <= 0) {
348 timer->lastEvent = timer->nextEvent;
349 timer->nextEvent += timer->overflowInterval;
350 gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
351 timer->oldReload = timer->reload;
352
353 if (timer->doIrq) {
354 GBARaiseIRQ(gba, IRQ_TIMER3);
355 }
356
357 if (timer->countUp) {
358 timer->nextEvent = INT_MAX;
359 }
360 }
361 if (timer->nextEvent < nextEvent) {
362 nextEvent = timer->nextEvent;
363 }
364 }
365 }
366 return nextEvent;
367}
368
369void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
370 debugger->setSoftwareBreakpoint = _setSoftwareBreakpoint;
371 debugger->clearSoftwareBreakpoint = _clearSoftwareBreakpoint;
372 gba->debugger = debugger;
373 gba->cpu->components[GBA_COMPONENT_DEBUGGER] = &debugger->d;
374 ARMHotplugAttach(gba->cpu, GBA_COMPONENT_DEBUGGER);
375}
376
377void GBADetachDebugger(struct GBA* gba) {
378 gba->debugger = 0;
379 ARMHotplugDetach(gba->cpu, GBA_COMPONENT_DEBUGGER);
380 gba->cpu->components[GBA_COMPONENT_DEBUGGER] = 0;
381}
382
383void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname) {
384 GBAUnloadROM(gba);
385 gba->romVf = vf;
386 gba->pristineRomSize = vf->size(vf);
387 vf->seek(vf, 0, SEEK_SET);
388 if (gba->pristineRomSize > SIZE_CART0) {
389 gba->pristineRomSize = SIZE_CART0;
390 }
391 gba->pristineRom = vf->map(vf, gba->pristineRomSize, MAP_READ);
392 if (!gba->pristineRom) {
393 GBALog(gba, GBA_LOG_WARN, "Couldn't map ROM");
394 return;
395 }
396 gba->yankedRomSize = 0;
397 gba->memory.rom = gba->pristineRom;
398 gba->activeFile = fname;
399 gba->memory.romSize = gba->pristineRomSize;
400 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
401 GBASavedataInit(&gba->memory.savedata, sav);
402 GBAHardwareInit(&gba->memory.hw, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
403 // TODO: error check
404}
405
406void GBAYankROM(struct GBA* gba) {
407 gba->yankedRomSize = gba->memory.romSize;
408 gba->memory.romSize = 0;
409 GBARaiseIRQ(gba, IRQ_GAMEPAK);
410}
411
412void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
413 gba->biosVf = vf;
414 uint32_t* bios = vf->map(vf, SIZE_BIOS, MAP_READ);
415 if (!bios) {
416 GBALog(gba, GBA_LOG_WARN, "Couldn't map BIOS");
417 return;
418 }
419 gba->memory.bios = bios;
420 gba->memory.fullBios = 1;
421 uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
422 GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
423 if (checksum == GBA_BIOS_CHECKSUM) {
424 GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
425 } else if (checksum == GBA_DS_BIOS_CHECKSUM) {
426 GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
427 } else {
428 GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
429 }
430 gba->biosChecksum = checksum;
431 if (gba->memory.activeRegion == REGION_BIOS) {
432 gba->cpu->memory.activeRegion = gba->memory.bios;
433 }
434 // TODO: error check
435}
436
437void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
438 size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
439 if (!patchedSize || patchedSize > SIZE_CART0) {
440 return;
441 }
442 gba->memory.rom = anonymousMemoryMap(SIZE_CART0);
443 if (!patch->applyPatch(patch, gba->pristineRom, gba->pristineRomSize, gba->memory.rom, patchedSize)) {
444 mappedMemoryFree(gba->memory.rom, patchedSize);
445 gba->memory.rom = gba->pristineRom;
446 return;
447 }
448 gba->memory.romSize = patchedSize;
449 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
450}
451
452void GBATimerUpdateRegister(struct GBA* gba, int timer) {
453 struct GBATimer* currentTimer = &gba->timers[timer];
454 if (currentTimer->enable && !currentTimer->countUp) {
455 int32_t prefetchSkew = 0;
456 if (gba->memory.lastPrefetchedPc - gba->memory.lastPrefetchedLoads * WORD_SIZE_THUMB >= (uint32_t) gba->cpu->gprs[ARM_PC]) {
457 prefetchSkew = (gba->memory.lastPrefetchedPc - gba->cpu->gprs[ARM_PC]) * (gba->cpu->memory.activeSeqCycles16 + 1) / WORD_SIZE_THUMB;
458 }
459 // Reading this takes two cycles (1N+1I), so let's remove them preemptively
460 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent - 2 + prefetchSkew) >> currentTimer->prescaleBits);
461 }
462}
463
464void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
465 gba->timers[timer].reload = reload;
466 gba->timers[timer].overflowInterval = (0x10000 - gba->timers[timer].reload) << gba->timers[timer].prescaleBits;
467}
468
469void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
470 struct GBATimer* currentTimer = &gba->timers[timer];
471 GBATimerUpdateRegister(gba, timer);
472
473 int oldPrescale = currentTimer->prescaleBits;
474 switch (control & 0x0003) {
475 case 0x0000:
476 currentTimer->prescaleBits = 0;
477 break;
478 case 0x0001:
479 currentTimer->prescaleBits = 6;
480 break;
481 case 0x0002:
482 currentTimer->prescaleBits = 8;
483 break;
484 case 0x0003:
485 currentTimer->prescaleBits = 10;
486 break;
487 }
488 currentTimer->countUp = !!(control & 0x0004);
489 currentTimer->doIrq = !!(control & 0x0040);
490 currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
491 int wasEnabled = currentTimer->enable;
492 currentTimer->enable = !!(control & 0x0080);
493 if (!wasEnabled && currentTimer->enable) {
494 if (!currentTimer->countUp) {
495 currentTimer->nextEvent = gba->cpu->cycles + currentTimer->overflowInterval;
496 } else {
497 currentTimer->nextEvent = INT_MAX;
498 }
499 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
500 currentTimer->oldReload = currentTimer->reload;
501 currentTimer->lastEvent = gba->cpu->cycles;
502 gba->timersEnabled |= 1 << timer;
503 } else if (wasEnabled && !currentTimer->enable) {
504 if (!currentTimer->countUp) {
505 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> oldPrescale);
506 }
507 gba->timersEnabled &= ~(1 << timer);
508 } else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
509 // FIXME: this might be before present
510 currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
511 }
512
513 if (currentTimer->nextEvent < gba->cpu->nextEvent) {
514 gba->cpu->nextEvent = currentTimer->nextEvent;
515 }
516};
517
518void GBAWriteIE(struct GBA* gba, uint16_t value) {
519 if (value & (1 << IRQ_KEYPAD)) {
520 GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
521 }
522
523 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
524 ARMRaiseIRQ(gba->cpu);
525 }
526}
527
528void GBAWriteIME(struct GBA* gba, uint16_t value) {
529 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
530 ARMRaiseIRQ(gba->cpu);
531 }
532}
533
534void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
535 gba->memory.io[REG_IF >> 1] |= 1 << irq;
536 gba->cpu->halted = 0;
537
538 if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
539 ARMRaiseIRQ(gba->cpu);
540 }
541}
542
543void GBATestIRQ(struct ARMCore* cpu) {
544 struct GBA* gba = (struct GBA*) cpu->master;
545 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
546 gba->springIRQ = 1;
547 gba->cpu->nextEvent = 0;
548 }
549}
550
551void GBAHalt(struct GBA* gba) {
552 gba->cpu->nextEvent = 0;
553 gba->cpu->halted = 1;
554}
555
556static void _GBAVLog(struct GBA* gba, enum GBALogLevel level, const char* format, va_list args) {
557 struct GBAThread* threadContext = GBAThreadGetContext();
558 enum GBALogLevel logLevel = GBA_LOG_ALL;
559
560 if (gba) {
561 logLevel = gba->logLevel;
562 }
563
564 if (threadContext) {
565 logLevel = threadContext->logLevel;
566 gba = threadContext->gba;
567 }
568
569 if (!(level & logLevel) && level != GBA_LOG_FATAL) {
570 return;
571 }
572
573 if (level == GBA_LOG_FATAL && gba) {
574 gba->cpu->nextEvent = 0;
575 }
576
577 if (threadContext) {
578 if (level == GBA_LOG_FATAL) {
579 MutexLock(&threadContext->stateMutex);
580 threadContext->state = THREAD_CRASHED;
581 MutexUnlock(&threadContext->stateMutex);
582 }
583 }
584 if (gba && gba->logHandler) {
585 gba->logHandler(threadContext, level, format, args);
586 return;
587 }
588
589 vprintf(format, args);
590 printf("\n");
591
592 if (level == GBA_LOG_FATAL && !threadContext) {
593 abort();
594 }
595}
596
597void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
598 va_list args;
599 va_start(args, format);
600 _GBAVLog(gba, level, format, args);
601 va_end(args);
602}
603
604void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...) {
605 struct GBA* gba = 0;
606 if (debugger->cpu) {
607 gba = (struct GBA*) debugger->cpu->master;
608 }
609
610 enum GBALogLevel gbaLevel;
611 switch (level) {
612 default: // Avoids compiler warning
613 case DEBUGGER_LOG_DEBUG:
614 gbaLevel = GBA_LOG_DEBUG;
615 break;
616 case DEBUGGER_LOG_INFO:
617 gbaLevel = GBA_LOG_INFO;
618 break;
619 case DEBUGGER_LOG_WARN:
620 gbaLevel = GBA_LOG_WARN;
621 break;
622 case DEBUGGER_LOG_ERROR:
623 gbaLevel = GBA_LOG_ERROR;
624 break;
625 }
626 va_list args;
627 va_start(args, format);
628 _GBAVLog(gba, gbaLevel, format, args);
629 va_end(args);
630}
631
632bool GBAIsROM(struct VFile* vf) {
633 if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
634 return false;
635 }
636 uint8_t signature[sizeof(GBA_ROM_MAGIC)];
637 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
638 return false;
639 }
640 return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
641}
642
643bool GBAIsBIOS(struct VFile* vf) {
644 if (vf->seek(vf, 0, SEEK_SET) < 0) {
645 return false;
646 }
647 uint32_t interruptTable[7];
648 if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
649 return false;
650 }
651 int i;
652 for (i = 0; i < 7; ++i) {
653 if ((interruptTable[i] & 0xFFFF0000) != 0xEA000000) {
654 return false;
655 }
656 }
657 return true;
658}
659
660void GBAGetGameCode(struct GBA* gba, char* out) {
661 if (!gba->memory.rom) {
662 out[0] = '\0';
663 return;
664 }
665 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->id, 4);
666}
667
668void GBAGetGameTitle(struct GBA* gba, char* out) {
669 if (!gba->memory.rom) {
670 strncpy(out, "(BIOS)", 12);
671 return;
672 }
673 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
674}
675
676void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
677 struct GBA* gba = (struct GBA*) cpu->master;
678 enum GBALogLevel level = GBA_LOG_ERROR;
679 if (gba->debugger) {
680 level = GBA_LOG_STUB;
681 struct DebuggerEntryInfo info = {
682 .address = _ARMPCAddress(cpu),
683 .opcode = opcode
684 };
685 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP, &info);
686 }
687 GBALog(gba, level, "Stub opcode: %08x", opcode);
688}
689
690void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
691 struct GBA* gba = (struct GBA*) cpu->master;
692 if (!gba->yankedRomSize) {
693 GBALog(gba, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
694 }
695 if (gba->debugger) {
696 struct DebuggerEntryInfo info = {
697 .address = _ARMPCAddress(cpu),
698 .opcode = opcode
699 };
700 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP, &info);
701 } else {
702 ARMRaiseUndefined(cpu);
703 }
704}
705
706void GBABreakpoint(struct ARMCore* cpu, int immediate) {
707 struct GBA* gba = (struct GBA*) cpu->master;
708 if (immediate >= GBA_COMPONENT_MAX) {
709 return;
710 }
711 switch (immediate) {
712 case GBA_COMPONENT_DEBUGGER:
713 if (gba->debugger) {
714 struct DebuggerEntryInfo info = {
715 .address = _ARMPCAddress(cpu)
716 };
717 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_BREAKPOINT, &info);
718 }
719 break;
720 case GBA_COMPONENT_CHEAT_DEVICE:
721 if (gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE]) {
722 struct GBACheatDevice* device = (struct GBACheatDevice*) gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE];
723 struct GBACheatHook* hook = 0;
724 size_t i;
725 for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) {
726 struct GBACheatSet* cheats = *GBACheatSetsGetPointer(&device->cheats, i);
727 if (cheats->hook && cheats->hook->address == _ARMPCAddress(cpu)) {
728 GBACheatRefresh(device, cheats);
729 hook = cheats->hook;
730 }
731 }
732 if (hook) {
733 ARMRunFake(cpu, hook->patchedOpcode);
734 }
735 }
736 break;
737 default:
738 break;
739 }
740}
741
742void GBAFrameStarted(struct GBA* gba) {
743 UNUSED(gba);
744
745 struct GBAThread* thread = GBAThreadGetContext();
746 if (!thread) {
747 return;
748 }
749
750 if (thread->rewindBuffer) {
751 --thread->rewindBufferNext;
752 if (thread->rewindBufferNext <= 0) {
753 thread->rewindBufferNext = thread->rewindBufferInterval;
754 GBARecordFrame(thread);
755 }
756 }
757}
758
759void GBAFrameEnded(struct GBA* gba) {
760 GBASavedataClean(&gba->memory.savedata, gba->video.frameCounter);
761
762 if (gba->rr) {
763 gba->rr->nextFrame(gba->rr);
764 }
765
766 if (gba->cpu->components && gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE]) {
767 struct GBACheatDevice* device = (struct GBACheatDevice*) gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE];
768 size_t i;
769 for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) {
770 struct GBACheatSet* cheats = *GBACheatSetsGetPointer(&device->cheats, i);
771 if (!cheats->hook) {
772 GBACheatRefresh(device, cheats);
773 }
774 }
775 }
776
777 if (gba->stream) {
778 gba->stream->postVideoFrame(gba->stream, gba->video.renderer);
779 }
780
781 GBAHardwarePlayerUpdate(gba);
782
783 struct GBAThread* thread = GBAThreadGetContext();
784 if (!thread) {
785 return;
786 }
787
788 if (thread->frameCallback) {
789 thread->frameCallback(thread);
790 }
791}
792
793void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
794 size_t immediate;
795 for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) {
796 if (gba->cpu->components[immediate] == component) {
797 break;
798 }
799 }
800 if (immediate == gba->cpu->numComponents) {
801 return;
802 }
803 if (mode == MODE_ARM) {
804 int32_t value;
805 int32_t old;
806 value = 0xE1200070;
807 value |= immediate & 0xF;
808 value |= (immediate & 0xFFF0) << 4;
809 GBAPatch32(gba->cpu, address, value, &old);
810 *opcode = old;
811 } else {
812 int16_t value;
813 int16_t old;
814 value = 0xBE00;
815 value |= immediate & 0xFF;
816 GBAPatch16(gba->cpu, address, value, &old);
817 *opcode = (uint16_t) old;
818 }
819}
820
821void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
822 if (mode == MODE_ARM) {
823 GBAPatch32(gba->cpu, address, opcode, 0);
824 } else {
825 GBAPatch16(gba->cpu, address, opcode, 0);
826 }
827}
828
829static bool _setSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
830 GBASetBreakpoint((struct GBA*) debugger->cpu->master, &debugger->d, address, mode, opcode);
831 return true;
832}
833
834static bool _clearSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
835 GBAClearBreakpoint((struct GBA*) debugger->cpu->master, address, mode, opcode);
836 return true;
837}