src/gba/gba.c (view raw)
1#include "gba.h"
2
3#include "gba-bios.h"
4#include "gba-io.h"
5#include "gba-rr.h"
6#include "gba-sio.h"
7#include "gba-thread.h"
8
9#include "util/crc32.h"
10#include "util/memory.h"
11#include "util/patch.h"
12#include "util/vfs.h"
13
14const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;
15const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;
16
17static const uint64_t GBA_ROM_MAGIC = 0x21A29A6951AEFF24;
18
19enum {
20 SP_BASE_SYSTEM = 0x03FFFF00,
21 SP_BASE_IRQ = 0x03FFFFA0,
22 SP_BASE_SUPERVISOR = 0x03FFFFE0
23};
24
25struct GBACartridgeOverride {
26 const char id[4];
27 enum SavedataType type;
28 int gpio;
29 uint32_t busyLoop;
30};
31
32static const struct GBACartridgeOverride _overrides[] = {
33 // Boktai: The Sun is in Your Hand
34 { "U3IE", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR, -1 },
35 { "U3IP", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR, -1 },
36
37 // Boktai 2: Solar Boy Django
38 { "U32E", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR, -1 },
39 { "U32P", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR, -1 },
40
41 // Drill Dozer
42 { "V49J", SAVEDATA_SRAM, GPIO_RUMBLE, -1 },
43 { "V49E", SAVEDATA_SRAM, GPIO_RUMBLE, -1 },
44
45 // Mega Man Battle Network
46 { "AREE", SAVEDATA_SRAM, GPIO_NONE, 0x8000338 },
47
48 // Pokemon Ruby
49 { "AXVJ", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
50 { "AXVE", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
51 { "AXVP", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
52 { "AXVI", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
53 { "AXVS", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
54 { "AXVD", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
55 { "AXVF", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
56
57 // Pokemon Sapphire
58 { "AXPJ", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
59 { "AXPE", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
60 { "AXPP", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
61 { "AXPI", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
62 { "AXPS", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
63 { "AXPD", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
64 { "AXPF", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
65
66 // Pokemon Emerald
67 { "BPEJ", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
68 { "BPEE", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
69 { "BPEP", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
70 { "BPEI", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
71 { "BPES", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
72 { "BPED", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
73 { "BPEF", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
74
75 // Pokemon FireRed
76 { "BPRJ", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
77 { "BPRE", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
78 { "BPRP", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
79
80 // Pokemon LeafGreen
81 { "BPGJ", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
82 { "BPGE", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
83 { "BPGP", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
84
85 // RockMan EXE 4.5 - Real Operation
86 { "BR4J", SAVEDATA_FLASH512, GPIO_RTC, -1 },
87
88 // Super Mario Advance 4
89 { "AX4J", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
90 { "AX4E", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
91 { "AX4P", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
92
93 // Wario Ware Twisted
94 { "RWZJ", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO, -1 },
95 { "RWZE", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO, -1 },
96 { "RWZP", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO, -1 },
97
98 { { 0, 0, 0, 0 }, 0, 0, -1 }
99};
100
101static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component);
102static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh);
103static void GBAProcessEvents(struct ARMCore* cpu);
104static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
105static void GBAHitStub(struct ARMCore* cpu, uint32_t opcode);
106static void GBAIllegal(struct ARMCore* cpu, uint32_t opcode);
107
108static void _checkOverrides(struct GBA* gba, uint32_t code);
109
110void GBACreate(struct GBA* gba) {
111 gba->d.id = GBA_COMPONENT_MAGIC;
112 gba->d.init = GBAInit;
113 gba->d.deinit = 0;
114}
115
116static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component) {
117 struct GBA* gba = (struct GBA*) component;
118 gba->cpu = cpu;
119 gba->debugger = 0;
120
121 GBAInterruptHandlerInit(&cpu->irqh);
122 GBAMemoryInit(gba);
123
124 gba->video.p = gba;
125 GBAVideoInit(&gba->video);
126
127 gba->audio.p = gba;
128 GBAAudioInit(&gba->audio, GBA_AUDIO_SAMPLES);
129
130 GBAIOInit(gba);
131
132 gba->sio.p = gba;
133 GBASIOInit(&gba->sio);
134
135 gba->timersEnabled = 0;
136 memset(gba->timers, 0, sizeof(gba->timers));
137
138 gba->springIRQ = 0;
139 gba->keySource = 0;
140 gba->rotationSource = 0;
141 gba->rumble = 0;
142 gba->rr = 0;
143
144 gba->romVf = 0;
145 gba->biosVf = 0;
146
147 gba->logLevel = GBA_LOG_INFO | GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
148
149 gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
150}
151
152void GBADestroy(struct GBA* gba) {
153 if (gba->pristineRom == gba->memory.rom) {
154 gba->memory.rom = 0;
155 }
156
157 if (gba->romVf) {
158 gba->romVf->unmap(gba->romVf, gba->pristineRom, gba->pristineRomSize);
159 }
160
161 if (gba->biosVf) {
162 gba->biosVf->unmap(gba->biosVf, gba->memory.bios, SIZE_BIOS);
163 }
164
165 GBAMemoryDeinit(gba);
166 GBAVideoDeinit(&gba->video);
167 GBAAudioDeinit(&gba->audio);
168 GBARRContextDestroy(gba);
169}
170
171void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {
172 irqh->reset = GBAReset;
173 irqh->processEvents = GBAProcessEvents;
174 irqh->swi16 = GBASwi16;
175 irqh->swi32 = GBASwi32;
176 irqh->hitIllegal = GBAIllegal;
177 irqh->readCPSR = GBATestIRQ;
178 irqh->hitStub = GBAHitStub;
179}
180
181void GBAReset(struct ARMCore* cpu) {
182 ARMSetPrivilegeMode(cpu, MODE_IRQ);
183 cpu->gprs[ARM_SP] = SP_BASE_IRQ;
184 ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
185 cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
186 ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
187 cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
188
189 struct GBA* gba = (struct GBA*) cpu->master;
190 GBAMemoryReset(gba);
191 GBAVideoReset(&gba->video);
192 GBAAudioReset(&gba->audio);
193 GBAIOInit(gba);
194}
195
196static void GBAProcessEvents(struct ARMCore* cpu) {
197 do {
198 struct GBA* gba = (struct GBA*) cpu->master;
199 int32_t cycles = cpu->cycles;
200 int32_t nextEvent = INT_MAX;
201 int32_t testEvent;
202
203 if (gba->springIRQ) {
204 ARMRaiseIRQ(cpu);
205 gba->springIRQ = 0;
206 }
207
208 testEvent = GBAVideoProcessEvents(&gba->video, cycles);
209 if (testEvent < nextEvent) {
210 nextEvent = testEvent;
211 }
212
213 testEvent = GBAAudioProcessEvents(&gba->audio, cycles);
214 if (testEvent < nextEvent) {
215 nextEvent = testEvent;
216 }
217
218 testEvent = GBATimersProcessEvents(gba, cycles);
219 if (testEvent < nextEvent) {
220 nextEvent = testEvent;
221 }
222
223 testEvent = GBAMemoryRunDMAs(gba, cycles);
224 if (testEvent < nextEvent) {
225 nextEvent = testEvent;
226 }
227
228 testEvent = GBASIOProcessEvents(&gba->sio, cycles);
229 if (testEvent < nextEvent) {
230 nextEvent = testEvent;
231 }
232
233 cpu->cycles -= cycles;
234 cpu->nextEvent = nextEvent;
235
236 if (cpu->halted) {
237 cpu->cycles = cpu->nextEvent;
238 }
239 } while (cpu->cycles >= cpu->nextEvent);
240}
241
242static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
243 int32_t nextEvent = INT_MAX;
244 if (gba->timersEnabled) {
245 struct GBATimer* timer;
246 struct GBATimer* nextTimer;
247
248 timer = &gba->timers[0];
249 if (timer->enable) {
250 timer->nextEvent -= cycles;
251 timer->lastEvent -= cycles;
252 if (timer->nextEvent <= 0) {
253 timer->lastEvent = timer->nextEvent;
254 timer->nextEvent += timer->overflowInterval;
255 gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
256 timer->oldReload = timer->reload;
257
258 if (timer->doIrq) {
259 GBARaiseIRQ(gba, IRQ_TIMER0);
260 }
261
262 if (gba->audio.enable) {
263 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 0) {
264 GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
265 }
266
267 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 0) {
268 GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
269 }
270 }
271
272 nextTimer = &gba->timers[1];
273 if (nextTimer->countUp) {
274 ++gba->memory.io[REG_TM1CNT_LO >> 1];
275 if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
276 nextTimer->nextEvent = 0;
277 }
278 }
279 }
280 nextEvent = timer->nextEvent;
281 }
282
283 timer = &gba->timers[1];
284 if (timer->enable) {
285 timer->nextEvent -= cycles;
286 timer->lastEvent -= cycles;
287 if (timer->nextEvent <= 0) {
288 timer->lastEvent = timer->nextEvent;
289 timer->nextEvent += timer->overflowInterval;
290 gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
291 timer->oldReload = timer->reload;
292
293 if (timer->doIrq) {
294 GBARaiseIRQ(gba, IRQ_TIMER1);
295 }
296
297 if (gba->audio.enable) {
298 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 1) {
299 GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
300 }
301
302 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 1) {
303 GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
304 }
305 }
306
307 if (timer->countUp) {
308 timer->nextEvent = INT_MAX;
309 }
310
311 nextTimer = &gba->timers[2];
312 if (nextTimer->countUp) {
313 ++gba->memory.io[REG_TM2CNT_LO >> 1];
314 if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
315 nextTimer->nextEvent = 0;
316 }
317 }
318 }
319 if (timer->nextEvent < nextEvent) {
320 nextEvent = timer->nextEvent;
321 }
322 }
323
324 timer = &gba->timers[2];
325 if (timer->enable) {
326 timer->nextEvent -= cycles;
327 timer->lastEvent -= cycles;
328 nextEvent = timer->nextEvent;
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 nextEvent = timer->nextEvent;
361 if (timer->nextEvent <= 0) {
362 timer->lastEvent = timer->nextEvent;
363 timer->nextEvent += timer->overflowInterval;
364 gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
365 timer->oldReload = timer->reload;
366
367 if (timer->doIrq) {
368 GBARaiseIRQ(gba, IRQ_TIMER3);
369 }
370
371 if (timer->countUp) {
372 timer->nextEvent = INT_MAX;
373 }
374 }
375 if (timer->nextEvent < nextEvent) {
376 nextEvent = timer->nextEvent;
377 }
378 }
379 }
380 return nextEvent;
381}
382
383void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
384 gba->debugger = debugger;
385}
386
387void GBADetachDebugger(struct GBA* gba) {
388 gba->debugger = 0;
389}
390
391void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname) {
392 gba->romVf = vf;
393 gba->pristineRomSize = vf->seek(vf, 0, SEEK_END);
394 vf->seek(vf, 0, SEEK_SET);
395 gba->pristineRom = vf->map(vf, SIZE_CART0, MAP_READ);
396 gba->memory.rom = gba->pristineRom;
397 gba->activeFile = fname;
398 gba->memory.romSize = gba->pristineRomSize;
399 gba->romCrc32 = crc32(gba->memory.rom, gba->memory.romSize);
400 GBASavedataInit(&gba->memory.savedata, sav);
401 GBAGPIOInit(&gba->memory.gpio, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
402 _checkOverrides(gba, ((struct GBACartridge*) gba->memory.rom)->id);
403 // TODO: error check
404}
405
406void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
407 gba->biosVf = vf;
408 gba->memory.bios = vf->map(vf, SIZE_BIOS, MAP_READ);
409 gba->memory.fullBios = 1;
410 uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
411 GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
412 if (checksum == GBA_BIOS_CHECKSUM) {
413 GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
414 } else if (checksum == GBA_DS_BIOS_CHECKSUM) {
415 GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
416 } else {
417 GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
418 }
419 gba->biosChecksum = checksum;
420 if ((gba->cpu->gprs[ARM_PC] >> BASE_OFFSET) == BASE_BIOS) {
421 gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
422 }
423 // TODO: error check
424}
425
426void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
427 size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
428 if (!patchedSize) {
429 return;
430 }
431 gba->memory.rom = anonymousMemoryMap(patchedSize);
432 memcpy(gba->memory.rom, gba->pristineRom, gba->memory.romSize > patchedSize ? patchedSize : gba->memory.romSize);
433 if (!patch->applyPatch(patch, gba->memory.rom, patchedSize)) {
434 mappedMemoryFree(gba->memory.rom, patchedSize);
435 gba->memory.rom = gba->pristineRom;
436 return;
437 }
438 gba->memory.romSize = patchedSize;
439 gba->romCrc32 = crc32(gba->memory.rom, gba->memory.romSize);
440}
441
442void GBATimerUpdateRegister(struct GBA* gba, int timer) {
443 struct GBATimer* currentTimer = &gba->timers[timer];
444 if (currentTimer->enable && !currentTimer->countUp) {
445 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
446 }
447}
448
449void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
450 gba->timers[timer].reload = reload;
451}
452
453void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
454 struct GBATimer* currentTimer = &gba->timers[timer];
455 GBATimerUpdateRegister(gba, timer);
456
457 int oldPrescale = currentTimer->prescaleBits;
458 switch (control & 0x0003) {
459 case 0x0000:
460 currentTimer->prescaleBits = 0;
461 break;
462 case 0x0001:
463 currentTimer->prescaleBits = 6;
464 break;
465 case 0x0002:
466 currentTimer->prescaleBits = 8;
467 break;
468 case 0x0003:
469 currentTimer->prescaleBits = 10;
470 break;
471 }
472 currentTimer->countUp = !!(control & 0x0004);
473 currentTimer->doIrq = !!(control & 0x0040);
474 currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
475 int wasEnabled = currentTimer->enable;
476 currentTimer->enable = !!(control & 0x0080);
477 if (!wasEnabled && currentTimer->enable) {
478 if (!currentTimer->countUp) {
479 currentTimer->nextEvent = gba->cpu->cycles + currentTimer->overflowInterval;
480 } else {
481 currentTimer->nextEvent = INT_MAX;
482 }
483 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
484 currentTimer->oldReload = currentTimer->reload;
485 currentTimer->lastEvent = 0;
486 gba->timersEnabled |= 1 << timer;
487 } else if (wasEnabled && !currentTimer->enable) {
488 if (!currentTimer->countUp) {
489 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> oldPrescale);
490 }
491 gba->timersEnabled &= ~(1 << timer);
492 } else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
493 // FIXME: this might be before present
494 currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
495 }
496
497 if (currentTimer->nextEvent < gba->cpu->nextEvent) {
498 gba->cpu->nextEvent = currentTimer->nextEvent;
499 }
500};
501
502void GBAWriteIE(struct GBA* gba, uint16_t value) {
503 if (value & (1 << IRQ_KEYPAD)) {
504 GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
505 }
506
507 if (value & (1 << IRQ_GAMEPAK)) {
508 GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
509 }
510
511 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
512 ARMRaiseIRQ(gba->cpu);
513 }
514}
515
516void GBAWriteIME(struct GBA* gba, uint16_t value) {
517 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
518 ARMRaiseIRQ(gba->cpu);
519 }
520}
521
522void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
523 gba->memory.io[REG_IF >> 1] |= 1 << irq;
524 gba->cpu->halted = 0;
525
526 if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
527 ARMRaiseIRQ(gba->cpu);
528 }
529}
530
531void GBATestIRQ(struct ARMCore* cpu) {
532 struct GBA* gba = (struct GBA*) cpu->master;
533 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
534 gba->springIRQ = 1;
535 gba->cpu->nextEvent = 0;
536 }
537}
538
539void GBAHalt(struct GBA* gba) {
540 gba->cpu->nextEvent = 0;
541 gba->cpu->halted = 1;
542}
543
544static void _GBAVLog(struct GBA* gba, enum GBALogLevel level, const char* format, va_list args) {
545 struct GBAThread* threadContext = GBAThreadGetContext();
546 if (threadContext) {
547 if (!gba) {
548 gba = threadContext->gba;
549 }
550 if (threadContext->logHandler) {
551 threadContext->logHandler(threadContext, level, format, args);
552 return;
553 }
554 }
555
556 if (gba && !(level & gba->logLevel) && level != GBA_LOG_FATAL) {
557 return;
558 }
559
560 vprintf(format, args);
561 printf("\n");
562
563 if (level == GBA_LOG_FATAL) {
564 abort();
565 }
566}
567
568void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
569 va_list args;
570 va_start(args, format);
571 _GBAVLog(gba, level, format, args);
572 va_end(args);
573}
574
575void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...) {
576 struct GBA* gba = 0;
577 if (debugger->cpu) {
578 gba = (struct GBA*) debugger->cpu->master;
579 }
580
581 enum GBALogLevel gbaLevel;
582 switch (level) {
583 default: // Avoids compiler warning
584 case DEBUGGER_LOG_DEBUG:
585 gbaLevel = GBA_LOG_DEBUG;
586 break;
587 case DEBUGGER_LOG_INFO:
588 gbaLevel = GBA_LOG_INFO;
589 break;
590 case DEBUGGER_LOG_WARN:
591 gbaLevel = GBA_LOG_WARN;
592 break;
593 case DEBUGGER_LOG_ERROR:
594 gbaLevel = GBA_LOG_ERROR;
595 break;
596 }
597 va_list args;
598 va_start(args, format);
599 _GBAVLog(gba, gbaLevel, format, args);
600 va_end(args);
601}
602
603bool GBAIsROM(struct VFile* vf) {
604 if (vf->seek(vf, 4, SEEK_SET) < 0) {
605 return false;
606 }
607 uint64_t signature;
608 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
609 return false;
610 }
611 return signature == GBA_ROM_MAGIC;
612}
613
614void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
615 struct GBA* gba = (struct GBA*) cpu->master;
616 enum GBALogLevel level = GBA_LOG_FATAL;
617 if (gba->debugger) {
618 level = GBA_LOG_STUB;
619 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
620 }
621 GBALog(gba, level, "Stub opcode: %08x", opcode);
622}
623
624void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
625 struct GBA* gba = (struct GBA*) cpu->master;
626 GBALog(gba, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
627 if (gba->debugger) {
628 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
629 }
630}
631
632void _checkOverrides(struct GBA* gba, uint32_t id) {
633 int i;
634 gba->busyLoop = -1;
635 for (i = 0; _overrides[i].id[0]; ++i) {
636 const uint32_t* overrideId = (const uint32_t*) _overrides[i].id;
637 if (*overrideId == id) {
638 switch (_overrides[i].type) {
639 case SAVEDATA_FLASH512:
640 case SAVEDATA_FLASH1M:
641 gba->memory.savedata.type = _overrides[i].type;
642 GBASavedataInitFlash(&gba->memory.savedata);
643 break;
644 case SAVEDATA_EEPROM:
645 GBASavedataInitEEPROM(&gba->memory.savedata);
646 break;
647 case SAVEDATA_SRAM:
648 GBASavedataInitSRAM(&gba->memory.savedata);
649 break;
650 case SAVEDATA_NONE:
651 break;
652 }
653
654 if (_overrides[i].gpio & GPIO_RTC) {
655 GBAGPIOInitRTC(&gba->memory.gpio);
656 }
657
658 if (_overrides[i].gpio & GPIO_GYRO) {
659 GBAGPIOInitGyro(&gba->memory.gpio);
660 }
661
662 if (_overrides[i].gpio & GPIO_RUMBLE) {
663 GBAGPIOInitRumble(&gba->memory.gpio);
664 }
665
666 gba->busyLoop = _overrides[i].busyLoop;
667 return;
668 }
669 }
670}