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