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