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