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