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->yankedRomSize = 0;
380 gba->memory.rom = gba->pristineRom;
381 gba->activeFile = fname;
382 gba->memory.romSize = gba->pristineRomSize;
383 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
384 GBASavedataInit(&gba->memory.savedata, sav);
385 GBAHardwareInit(&gba->memory.hw, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
386 // TODO: error check
387}
388
389void GBAYankROM(struct GBA* gba) {
390 gba->yankedRomSize = gba->memory.romSize;
391 gba->memory.romSize = 0;
392}
393
394void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
395 gba->biosVf = vf;
396 uint32_t* bios = vf->map(vf, SIZE_BIOS, MAP_READ);
397 if (!bios) {
398 GBALog(gba, GBA_LOG_WARN, "Couldn't map BIOS");
399 return;
400 }
401 gba->memory.bios = bios;
402 gba->memory.fullBios = 1;
403 uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
404 GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
405 if (checksum == GBA_BIOS_CHECKSUM) {
406 GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
407 } else if (checksum == GBA_DS_BIOS_CHECKSUM) {
408 GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
409 } else {
410 GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
411 }
412 gba->biosChecksum = checksum;
413 if (gba->memory.activeRegion == REGION_BIOS) {
414 gba->cpu->memory.activeRegion = gba->memory.bios;
415 }
416 // TODO: error check
417}
418
419void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
420 size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
421 if (!patchedSize) {
422 return;
423 }
424 gba->memory.rom = anonymousMemoryMap(patchedSize);
425 if (!patch->applyPatch(patch, gba->pristineRom, gba->pristineRomSize, gba->memory.rom, patchedSize)) {
426 mappedMemoryFree(gba->memory.rom, patchedSize);
427 gba->memory.rom = gba->pristineRom;
428 return;
429 }
430 gba->memory.romSize = patchedSize;
431 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
432}
433
434void GBATimerUpdateRegister(struct GBA* gba, int timer) {
435 struct GBATimer* currentTimer = &gba->timers[timer];
436 if (currentTimer->enable && !currentTimer->countUp) {
437 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
438 }
439}
440
441void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
442 gba->timers[timer].reload = reload;
443 gba->timers[timer].overflowInterval = (0x10000 - gba->timers[timer].reload) << gba->timers[timer].prescaleBits;
444}
445
446void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
447 struct GBATimer* currentTimer = &gba->timers[timer];
448 GBATimerUpdateRegister(gba, timer);
449
450 int oldPrescale = currentTimer->prescaleBits;
451 switch (control & 0x0003) {
452 case 0x0000:
453 currentTimer->prescaleBits = 0;
454 break;
455 case 0x0001:
456 currentTimer->prescaleBits = 6;
457 break;
458 case 0x0002:
459 currentTimer->prescaleBits = 8;
460 break;
461 case 0x0003:
462 currentTimer->prescaleBits = 10;
463 break;
464 }
465 currentTimer->countUp = !!(control & 0x0004);
466 currentTimer->doIrq = !!(control & 0x0040);
467 currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
468 int wasEnabled = currentTimer->enable;
469 currentTimer->enable = !!(control & 0x0080);
470 if (!wasEnabled && currentTimer->enable) {
471 if (!currentTimer->countUp) {
472 currentTimer->nextEvent = gba->cpu->cycles + currentTimer->overflowInterval;
473 } else {
474 currentTimer->nextEvent = INT_MAX;
475 }
476 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
477 currentTimer->oldReload = currentTimer->reload;
478 currentTimer->lastEvent = gba->cpu->cycles;
479 gba->timersEnabled |= 1 << timer;
480 } else if (wasEnabled && !currentTimer->enable) {
481 if (!currentTimer->countUp) {
482 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> oldPrescale);
483 }
484 gba->timersEnabled &= ~(1 << timer);
485 } else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
486 // FIXME: this might be before present
487 currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
488 }
489
490 if (currentTimer->nextEvent < gba->cpu->nextEvent) {
491 gba->cpu->nextEvent = currentTimer->nextEvent;
492 }
493};
494
495void GBAWriteIE(struct GBA* gba, uint16_t value) {
496 if (value & (1 << IRQ_KEYPAD)) {
497 GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
498 }
499
500 if (value & (1 << IRQ_GAMEPAK)) {
501 GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
502 }
503
504 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
505 ARMRaiseIRQ(gba->cpu);
506 }
507}
508
509void GBAWriteIME(struct GBA* gba, uint16_t value) {
510 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
511 ARMRaiseIRQ(gba->cpu);
512 }
513}
514
515void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
516 gba->memory.io[REG_IF >> 1] |= 1 << irq;
517 gba->cpu->halted = 0;
518
519 if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
520 ARMRaiseIRQ(gba->cpu);
521 }
522}
523
524void GBATestIRQ(struct ARMCore* cpu) {
525 struct GBA* gba = (struct GBA*) cpu->master;
526 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
527 gba->springIRQ = 1;
528 gba->cpu->nextEvent = 0;
529 }
530}
531
532void GBAHalt(struct GBA* gba) {
533 gba->cpu->nextEvent = 0;
534 gba->cpu->halted = 1;
535}
536
537static void _GBAVLog(struct GBA* gba, enum GBALogLevel level, const char* format, va_list args) {
538 struct GBAThread* threadContext = GBAThreadGetContext();
539 enum GBALogLevel logLevel = GBA_LOG_ALL;
540
541 if (gba) {
542 logLevel = gba->logLevel;
543 }
544
545 if (threadContext) {
546 logLevel = threadContext->logLevel;
547 gba = threadContext->gba;
548 }
549
550 if (!(level & logLevel) && level != GBA_LOG_FATAL) {
551 return;
552 }
553
554 if (level == GBA_LOG_FATAL && gba) {
555 gba->cpu->nextEvent = 0;
556 }
557
558 if (threadContext) {
559 if (level == GBA_LOG_FATAL) {
560 MutexLock(&threadContext->stateMutex);
561 threadContext->state = THREAD_CRASHED;
562 MutexUnlock(&threadContext->stateMutex);
563 }
564 }
565 if (gba && gba->logHandler) {
566 gba->logHandler(threadContext, level, format, args);
567 return;
568 }
569
570 vprintf(format, args);
571 printf("\n");
572
573 if (level == GBA_LOG_FATAL && !threadContext) {
574 abort();
575 }
576}
577
578void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
579 va_list args;
580 va_start(args, format);
581 _GBAVLog(gba, level, format, args);
582 va_end(args);
583}
584
585void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...) {
586 struct GBA* gba = 0;
587 if (debugger->cpu) {
588 gba = (struct GBA*) debugger->cpu->master;
589 }
590
591 enum GBALogLevel gbaLevel;
592 switch (level) {
593 default: // Avoids compiler warning
594 case DEBUGGER_LOG_DEBUG:
595 gbaLevel = GBA_LOG_DEBUG;
596 break;
597 case DEBUGGER_LOG_INFO:
598 gbaLevel = GBA_LOG_INFO;
599 break;
600 case DEBUGGER_LOG_WARN:
601 gbaLevel = GBA_LOG_WARN;
602 break;
603 case DEBUGGER_LOG_ERROR:
604 gbaLevel = GBA_LOG_ERROR;
605 break;
606 }
607 va_list args;
608 va_start(args, format);
609 _GBAVLog(gba, gbaLevel, format, args);
610 va_end(args);
611}
612
613bool GBAIsROM(struct VFile* vf) {
614 if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
615 return false;
616 }
617 uint8_t signature[sizeof(GBA_ROM_MAGIC)];
618 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
619 return false;
620 }
621 return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
622}
623
624bool GBAIsBIOS(struct VFile* vf) {
625 if (vf->seek(vf, 0, SEEK_SET) < 0) {
626 return false;
627 }
628 uint32_t interruptTable[7];
629 if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
630 return false;
631 }
632 int i;
633 for (i = 0; i < 7; ++i) {
634 if ((interruptTable[i] & 0xFFFF0000) != 0xEA000000) {
635 return false;
636 }
637 }
638 return true;
639}
640
641void GBAGetGameCode(struct GBA* gba, char* out) {
642 if (!gba->memory.rom) {
643 out[0] = '\0';
644 return;
645 }
646 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->id, 4);
647}
648
649void GBAGetGameTitle(struct GBA* gba, char* out) {
650 if (!gba->memory.rom) {
651 strncpy(out, "(BIOS)", 12);
652 return;
653 }
654 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
655}
656
657void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
658 struct GBA* gba = (struct GBA*) cpu->master;
659 enum GBALogLevel level = GBA_LOG_ERROR;
660 if (gba->debugger) {
661 level = GBA_LOG_STUB;
662 struct DebuggerEntryInfo info = {
663 .address = _ARMPCAddress(cpu),
664 .opcode = opcode
665 };
666 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP, &info);
667 }
668 GBALog(gba, level, "Stub opcode: %08x", opcode);
669}
670
671void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
672 struct GBA* gba = (struct GBA*) cpu->master;
673 GBALog(gba, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
674 if (gba->debugger) {
675 struct DebuggerEntryInfo info = {
676 .address = _ARMPCAddress(cpu),
677 .opcode = opcode
678 };
679 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP, &info);
680 } else {
681 ARMRaiseUndefined(cpu);
682 }
683}
684
685void GBABreakpoint(struct ARMCore* cpu, int immediate) {
686 struct GBA* gba = (struct GBA*) cpu->master;
687 if (immediate >= GBA_COMPONENT_MAX) {
688 return;
689 }
690 switch (immediate) {
691 case GBA_COMPONENT_DEBUGGER:
692 if (gba->debugger) {
693 struct DebuggerEntryInfo info = {
694 .address = _ARMPCAddress(cpu)
695 };
696 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_BREAKPOINT, &info);
697 }
698 break;
699 case GBA_COMPONENT_CHEAT_DEVICE:
700 if (gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE]) {
701 struct GBACheatDevice* device = (struct GBACheatDevice*) gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE];
702 struct GBACheatHook* hook = 0;
703 size_t i;
704 for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) {
705 struct GBACheatSet* cheats = *GBACheatSetsGetPointer(&device->cheats, i);
706 if (cheats->hook && cheats->hook->address == _ARMPCAddress(cpu)) {
707 GBACheatRefresh(device, cheats);
708 hook = cheats->hook;
709 }
710 }
711 if (hook) {
712 ARMRunFake(cpu, hook->patchedOpcode);
713 }
714 }
715 break;
716 default:
717 break;
718 }
719}
720
721void GBAFrameStarted(struct GBA* gba) {
722 UNUSED(gba);
723
724 struct GBAThread* thread = GBAThreadGetContext();
725 if (!thread) {
726 return;
727 }
728
729 if (thread->rewindBuffer) {
730 --thread->rewindBufferNext;
731 if (thread->rewindBufferNext <= 0) {
732 thread->rewindBufferNext = thread->rewindBufferInterval;
733 GBARecordFrame(thread);
734 }
735 }
736}
737
738void GBAFrameEnded(struct GBA* gba) {
739 if (gba->rr) {
740 gba->rr->nextFrame(gba->rr);
741 }
742
743 if (gba->cpu->components && gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE]) {
744 struct GBACheatDevice* device = (struct GBACheatDevice*) gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE];
745 size_t i;
746 for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) {
747 struct GBACheatSet* cheats = *GBACheatSetsGetPointer(&device->cheats, i);
748 if (!cheats->hook) {
749 GBACheatRefresh(device, cheats);
750 }
751 }
752 }
753
754 if (gba->stream) {
755 gba->stream->postVideoFrame(gba->stream, gba->video.renderer);
756 }
757
758 struct GBAThread* thread = GBAThreadGetContext();
759 if (!thread) {
760 return;
761 }
762
763 if (thread->frameCallback) {
764 thread->frameCallback(thread);
765 }
766}
767
768void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
769 size_t immediate;
770 for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) {
771 if (gba->cpu->components[immediate] == component) {
772 break;
773 }
774 }
775 if (immediate == gba->cpu->numComponents) {
776 return;
777 }
778 if (mode == MODE_ARM) {
779 int32_t value;
780 int32_t old;
781 value = 0xE1200070;
782 value |= immediate & 0xF;
783 value |= (immediate & 0xFFF0) << 4;
784 GBAPatch32(gba->cpu, address, value, &old);
785 *opcode = old;
786 } else {
787 int16_t value;
788 int16_t old;
789 value = 0xBE00;
790 value |= immediate & 0xFF;
791 GBAPatch16(gba->cpu, address, value, &old);
792 *opcode = (uint16_t) old;
793 }
794}
795
796void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
797 if (mode == MODE_ARM) {
798 GBAPatch32(gba->cpu, address, opcode, 0);
799 } else {
800 GBAPatch16(gba->cpu, address, opcode, 0);
801 }
802}
803
804static bool _setSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
805 GBASetBreakpoint((struct GBA*) debugger->cpu->master, &debugger->d, address, mode, opcode);
806 return true;
807}
808
809static bool _clearSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
810 GBAClearBreakpoint((struct GBA*) debugger->cpu->master, address, mode, opcode);
811 return true;
812}