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};
30
31static const struct GBACartridgeOverride _overrides[] = {
32 // Boktai: The Sun is in Your Hand
33 { "U3IE", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
34 { "U3IP", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
35
36 // Boktai 2: Solar Boy Django
37 { "U32E", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
38 { "U32P", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
39
40 // Drill Dozer
41 { "V49J", SAVEDATA_SRAM, GPIO_RUMBLE },
42 { "V49E", SAVEDATA_SRAM, GPIO_RUMBLE },
43
44 // Pokemon Ruby
45 { "AXVJ", SAVEDATA_FLASH1M, GPIO_RTC },
46 { "AXVE", SAVEDATA_FLASH1M, GPIO_RTC },
47 { "AXVP", SAVEDATA_FLASH1M, GPIO_RTC },
48 { "AXVI", SAVEDATA_FLASH1M, GPIO_RTC },
49 { "AXVS", SAVEDATA_FLASH1M, GPIO_RTC },
50 { "AXVD", SAVEDATA_FLASH1M, GPIO_RTC },
51 { "AXVF", SAVEDATA_FLASH1M, GPIO_RTC },
52
53 // Pokemon Sapphire
54 { "AXPJ", SAVEDATA_FLASH1M, GPIO_RTC },
55 { "AXPE", SAVEDATA_FLASH1M, GPIO_RTC },
56 { "AXPP", SAVEDATA_FLASH1M, GPIO_RTC },
57 { "AXPI", SAVEDATA_FLASH1M, GPIO_RTC },
58 { "AXPS", SAVEDATA_FLASH1M, GPIO_RTC },
59 { "AXPD", SAVEDATA_FLASH1M, GPIO_RTC },
60 { "AXPF", SAVEDATA_FLASH1M, GPIO_RTC },
61
62 // Pokemon Emerald
63 { "BPEJ", SAVEDATA_FLASH1M, GPIO_RTC },
64 { "BPEE", SAVEDATA_FLASH1M, GPIO_RTC },
65 { "BPEP", SAVEDATA_FLASH1M, GPIO_RTC },
66 { "BPEI", SAVEDATA_FLASH1M, GPIO_RTC },
67 { "BPES", SAVEDATA_FLASH1M, GPIO_RTC },
68 { "BPED", SAVEDATA_FLASH1M, GPIO_RTC },
69 { "BPEF", SAVEDATA_FLASH1M, GPIO_RTC },
70
71 // Pokemon FireRed
72 { "BPRJ", SAVEDATA_FLASH1M, GPIO_NONE },
73 { "BPRE", SAVEDATA_FLASH1M, GPIO_NONE },
74 { "BPRP", SAVEDATA_FLASH1M, GPIO_NONE },
75
76 // Pokemon LeafGreen
77 { "BPGJ", SAVEDATA_FLASH1M, GPIO_NONE },
78 { "BPGE", SAVEDATA_FLASH1M, GPIO_NONE },
79 { "BPGP", SAVEDATA_FLASH1M, GPIO_NONE },
80
81 // RockMan EXE 4.5 - Real Operation
82 { "BR4J", SAVEDATA_FLASH512, GPIO_RTC },
83
84 // Super Mario Advance 4
85 { "AX4J", SAVEDATA_FLASH1M, GPIO_NONE },
86 { "AX4E", SAVEDATA_FLASH1M, GPIO_NONE },
87 { "AX4P", SAVEDATA_FLASH1M, GPIO_NONE },
88
89 // Wario Ware Twisted
90 { "RWZJ", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
91 { "RWZE", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
92 { "RWZP", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
93
94 { { 0, 0, 0, 0 }, 0, 0 }
95};
96
97static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component);
98static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh);
99static void GBAProcessEvents(struct ARMCore* cpu);
100static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
101static void GBAHitStub(struct ARMCore* cpu, uint32_t opcode);
102static void GBAIllegal(struct ARMCore* cpu, uint32_t opcode);
103
104static void _checkOverrides(struct GBA* gba, uint32_t code);
105
106void GBACreate(struct GBA* gba) {
107 gba->d.id = GBA_COMPONENT_MAGIC;
108 gba->d.init = GBAInit;
109 gba->d.deinit = 0;
110}
111
112static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component) {
113 struct GBA* gba = (struct GBA*) component;
114 gba->cpu = cpu;
115 gba->debugger = 0;
116
117 GBAInterruptHandlerInit(&cpu->irqh);
118 GBAMemoryInit(gba);
119
120 gba->video.p = gba;
121 GBAVideoInit(&gba->video);
122
123 gba->audio.p = gba;
124 GBAAudioInit(&gba->audio, GBA_AUDIO_SAMPLES);
125
126 GBAIOInit(gba);
127
128 gba->sio.p = gba;
129 GBASIOInit(&gba->sio);
130
131 gba->timersEnabled = 0;
132 memset(gba->timers, 0, sizeof(gba->timers));
133
134 gba->springIRQ = 0;
135 gba->keySource = 0;
136 gba->rotationSource = 0;
137 gba->rumble = 0;
138 gba->rr = 0;
139
140 gba->romVf = 0;
141 gba->biosVf = 0;
142
143 gba->logLevel = GBA_LOG_INFO | GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
144
145 gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
146}
147
148void GBADestroy(struct GBA* gba) {
149 if (gba->pristineRom == gba->memory.rom) {
150 gba->memory.rom = 0;
151 }
152
153 if (gba->romVf) {
154 gba->romVf->unmap(gba->romVf, gba->pristineRom, gba->pristineRomSize);
155 }
156
157 if (gba->biosVf) {
158 gba->biosVf->unmap(gba->biosVf, gba->memory.bios, SIZE_BIOS);
159 }
160
161 GBAMemoryDeinit(gba);
162 GBAVideoDeinit(&gba->video);
163 GBAAudioDeinit(&gba->audio);
164 GBARRContextDestroy(gba);
165}
166
167void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {
168 irqh->reset = GBAReset;
169 irqh->processEvents = GBAProcessEvents;
170 irqh->swi16 = GBASwi16;
171 irqh->swi32 = GBASwi32;
172 irqh->hitIllegal = GBAIllegal;
173 irqh->readCPSR = GBATestIRQ;
174 irqh->hitStub = GBAHitStub;
175}
176
177void GBAReset(struct ARMCore* cpu) {
178 ARMSetPrivilegeMode(cpu, MODE_IRQ);
179 cpu->gprs[ARM_SP] = SP_BASE_IRQ;
180 ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
181 cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
182 ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
183 cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
184
185 struct GBA* gba = (struct GBA*) cpu->master;
186 GBAMemoryReset(gba);
187 GBAVideoReset(&gba->video);
188 GBAAudioReset(&gba->audio);
189 GBAIOInit(gba);
190}
191
192static void GBAProcessEvents(struct ARMCore* cpu) {
193 do {
194 struct GBA* gba = (struct GBA*) cpu->master;
195 int32_t cycles = cpu->cycles;
196 int32_t nextEvent = INT_MAX;
197 int32_t testEvent;
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 if (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 nextEvent = timer->nextEvent;
325 if (timer->nextEvent <= 0) {
326 timer->lastEvent = timer->nextEvent;
327 timer->nextEvent += timer->overflowInterval;
328 gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
329 timer->oldReload = timer->reload;
330
331 if (timer->doIrq) {
332 GBARaiseIRQ(gba, IRQ_TIMER2);
333 }
334
335 if (timer->countUp) {
336 timer->nextEvent = INT_MAX;
337 }
338
339 nextTimer = &gba->timers[3];
340 if (nextTimer->countUp) {
341 ++gba->memory.io[REG_TM3CNT_LO >> 1];
342 if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
343 nextTimer->nextEvent = 0;
344 }
345 }
346 }
347 if (timer->nextEvent < nextEvent) {
348 nextEvent = timer->nextEvent;
349 }
350 }
351
352 timer = &gba->timers[3];
353 if (timer->enable) {
354 timer->nextEvent -= cycles;
355 timer->lastEvent -= cycles;
356 nextEvent = timer->nextEvent;
357 if (timer->nextEvent <= 0) {
358 timer->lastEvent = timer->nextEvent;
359 timer->nextEvent += timer->overflowInterval;
360 gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
361 timer->oldReload = timer->reload;
362
363 if (timer->doIrq) {
364 GBARaiseIRQ(gba, IRQ_TIMER3);
365 }
366
367 if (timer->countUp) {
368 timer->nextEvent = INT_MAX;
369 }
370 }
371 if (timer->nextEvent < nextEvent) {
372 nextEvent = timer->nextEvent;
373 }
374 }
375 }
376 return nextEvent;
377}
378
379void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
380 gba->debugger = debugger;
381}
382
383void GBADetachDebugger(struct GBA* gba) {
384 gba->debugger = 0;
385}
386
387void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname) {
388 gba->romVf = vf;
389 gba->pristineRomSize = vf->seek(vf, 0, SEEK_END);
390 vf->seek(vf, 0, SEEK_SET);
391 gba->pristineRom = vf->map(vf, SIZE_CART0, MAP_READ);
392 gba->memory.rom = gba->pristineRom;
393 gba->activeFile = fname;
394 gba->memory.romSize = gba->pristineRomSize;
395 gba->romCrc32 = crc32(gba->memory.rom, gba->memory.romSize);
396 GBASavedataInit(&gba->memory.savedata, sav);
397 GBAGPIOInit(&gba->memory.gpio, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
398 _checkOverrides(gba, ((struct GBACartridge*) gba->memory.rom)->id);
399 // TODO: error check
400}
401
402void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
403 gba->biosVf = vf;
404 gba->memory.bios = vf->map(vf, SIZE_BIOS, MAP_READ);
405 gba->memory.fullBios = 1;
406 uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
407 GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
408 if (checksum == GBA_BIOS_CHECKSUM) {
409 GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
410 } else if (checksum == GBA_DS_BIOS_CHECKSUM) {
411 GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
412 } else {
413 GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
414 }
415 gba->biosChecksum = checksum;
416 if ((gba->cpu->gprs[ARM_PC] >> BASE_OFFSET) == BASE_BIOS) {
417 gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
418 }
419 // TODO: error check
420}
421
422void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
423 size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
424 if (!patchedSize) {
425 return;
426 }
427 gba->memory.rom = anonymousMemoryMap(patchedSize);
428 memcpy(gba->memory.rom, gba->pristineRom, gba->memory.romSize > patchedSize ? patchedSize : gba->memory.romSize);
429 if (!patch->applyPatch(patch, gba->memory.rom, patchedSize)) {
430 mappedMemoryFree(gba->memory.rom, patchedSize);
431 gba->memory.rom = gba->pristineRom;
432 return;
433 }
434 gba->memory.romSize = patchedSize;
435 gba->romCrc32 = crc32(gba->memory.rom, gba->memory.romSize);
436}
437
438void GBATimerUpdateRegister(struct GBA* gba, int timer) {
439 struct GBATimer* currentTimer = &gba->timers[timer];
440 if (currentTimer->enable && !currentTimer->countUp) {
441 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
442 }
443}
444
445void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
446 gba->timers[timer].reload = reload;
447}
448
449void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
450 struct GBATimer* currentTimer = &gba->timers[timer];
451 GBATimerUpdateRegister(gba, timer);
452
453 int oldPrescale = currentTimer->prescaleBits;
454 switch (control & 0x0003) {
455 case 0x0000:
456 currentTimer->prescaleBits = 0;
457 break;
458 case 0x0001:
459 currentTimer->prescaleBits = 6;
460 break;
461 case 0x0002:
462 currentTimer->prescaleBits = 8;
463 break;
464 case 0x0003:
465 currentTimer->prescaleBits = 10;
466 break;
467 }
468 currentTimer->countUp = !!(control & 0x0004);
469 currentTimer->doIrq = !!(control & 0x0040);
470 currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
471 int wasEnabled = currentTimer->enable;
472 currentTimer->enable = !!(control & 0x0080);
473 if (!wasEnabled && currentTimer->enable) {
474 if (!currentTimer->countUp) {
475 currentTimer->nextEvent = gba->cpu->cycles + currentTimer->overflowInterval;
476 } else {
477 currentTimer->nextEvent = INT_MAX;
478 }
479 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
480 currentTimer->oldReload = currentTimer->reload;
481 currentTimer->lastEvent = 0;
482 gba->timersEnabled |= 1 << timer;
483 } else if (wasEnabled && !currentTimer->enable) {
484 if (!currentTimer->countUp) {
485 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> oldPrescale);
486 }
487 gba->timersEnabled &= ~(1 << timer);
488 } else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
489 // FIXME: this might be before present
490 currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
491 }
492
493 if (currentTimer->nextEvent < gba->cpu->nextEvent) {
494 gba->cpu->nextEvent = currentTimer->nextEvent;
495 }
496};
497
498void GBAWriteIE(struct GBA* gba, uint16_t value) {
499 if (value & (1 << IRQ_KEYPAD)) {
500 GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
501 }
502
503 if (value & (1 << IRQ_GAMEPAK)) {
504 GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
505 }
506
507 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
508 ARMRaiseIRQ(gba->cpu);
509 }
510}
511
512void GBAWriteIME(struct GBA* gba, uint16_t value) {
513 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
514 ARMRaiseIRQ(gba->cpu);
515 }
516}
517
518void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
519 gba->memory.io[REG_IF >> 1] |= 1 << irq;
520 gba->cpu->halted = 0;
521
522 if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
523 ARMRaiseIRQ(gba->cpu);
524 }
525}
526
527void GBATestIRQ(struct ARMCore* cpu) {
528 struct GBA* gba = (struct GBA*) cpu->master;
529 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
530 gba->springIRQ = 1;
531 gba->cpu->nextEvent = 0;
532 }
533}
534
535void GBAHalt(struct GBA* gba) {
536 gba->cpu->nextEvent = 0;
537 gba->cpu->halted = 1;
538}
539
540static void _GBAVLog(struct GBA* gba, enum GBALogLevel level, const char* format, va_list args) {
541 struct GBAThread* threadContext = GBAThreadGetContext();
542 if (threadContext) {
543 if (!gba) {
544 gba = threadContext->gba;
545 }
546 if (threadContext->logHandler) {
547 threadContext->logHandler(threadContext, level, format, args);
548 return;
549 }
550 }
551
552 if (gba && !(level & gba->logLevel) && level != GBA_LOG_FATAL) {
553 return;
554 }
555
556 vprintf(format, args);
557 printf("\n");
558
559 if (level == GBA_LOG_FATAL) {
560 abort();
561 }
562}
563
564void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
565 va_list args;
566 va_start(args, format);
567 _GBAVLog(gba, level, format, args);
568 va_end(args);
569}
570
571void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...) {
572 struct GBA* gba = 0;
573 if (debugger->cpu) {
574 gba = (struct GBA*) debugger->cpu->master;
575 }
576
577 enum GBALogLevel gbaLevel;
578 switch (level) {
579 case DEBUGGER_LOG_DEBUG:
580 gbaLevel = GBA_LOG_DEBUG;
581 break;
582 case DEBUGGER_LOG_INFO:
583 gbaLevel = GBA_LOG_INFO;
584 break;
585 case DEBUGGER_LOG_WARN:
586 gbaLevel = GBA_LOG_WARN;
587 break;
588 case DEBUGGER_LOG_ERROR:
589 gbaLevel = GBA_LOG_ERROR;
590 break;
591 }
592 va_list args;
593 va_start(args, format);
594 _GBAVLog(gba, gbaLevel, format, args);
595 va_end(args);
596}
597
598bool GBAIsROM(struct VFile* vf) {
599 if (vf->seek(vf, 4, SEEK_SET) < 0) {
600 return false;
601 }
602 uint64_t signature;
603 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
604 return false;
605 }
606 return signature == GBA_ROM_MAGIC;
607}
608
609void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
610 struct GBA* gba = (struct GBA*) cpu->master;
611 enum GBALogLevel level = GBA_LOG_FATAL;
612 if (gba->debugger) {
613 level = GBA_LOG_STUB;
614 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
615 }
616 GBALog(gba, level, "Stub opcode: %08x", opcode);
617}
618
619void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
620 struct GBA* gba = (struct GBA*) cpu->master;
621 GBALog(gba, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
622 if (gba->debugger) {
623 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
624 }
625}
626
627void _checkOverrides(struct GBA* gba, uint32_t id) {
628 int i;
629 for (i = 0; _overrides[i].id[0]; ++i) {
630 const uint32_t* overrideId = (const uint32_t*) _overrides[i].id;
631 if (*overrideId == id) {
632 switch (_overrides[i].type) {
633 case SAVEDATA_FLASH512:
634 case SAVEDATA_FLASH1M:
635 gba->memory.savedata.type = _overrides[i].type;
636 GBASavedataInitFlash(&gba->memory.savedata);
637 break;
638 case SAVEDATA_EEPROM:
639 GBASavedataInitEEPROM(&gba->memory.savedata);
640 break;
641 case SAVEDATA_SRAM:
642 GBASavedataInitSRAM(&gba->memory.savedata);
643 break;
644 case SAVEDATA_NONE:
645 break;
646 }
647
648 if (_overrides[i].gpio & GPIO_RTC) {
649 GBAGPIOInitRTC(&gba->memory.gpio);
650 }
651
652 if (_overrides[i].gpio & GPIO_GYRO) {
653 GBAGPIOInitGyro(&gba->memory.gpio);
654 }
655
656 if (_overrides[i].gpio & GPIO_RUMBLE) {
657 GBAGPIOInitRumble(&gba->memory.gpio);
658 }
659 return;
660 }
661 }
662}