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