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