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