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