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