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