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 gba->cpu->components[GBA_COMPONENT_DEBUGGER] = &debugger->d;
438 ARMHotplugAttach(gba->cpu, GBA_COMPONENT_DEBUGGER);
439}
440
441void GBADetachDebugger(struct GBA* gba) {
442 gba->debugger = 0;
443 ARMHotplugDetach(gba->cpu, GBA_COMPONENT_DEBUGGER);
444 gba->cpu->components[GBA_COMPONENT_DEBUGGER] = 0;
445}
446
447void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname) {
448 gba->romVf = vf;
449 gba->pristineRomSize = vf->seek(vf, 0, SEEK_END);
450 vf->seek(vf, 0, SEEK_SET);
451 if (gba->pristineRomSize > SIZE_CART0) {
452 gba->pristineRomSize = SIZE_CART0;
453 }
454 gba->pristineRom = vf->map(vf, gba->pristineRomSize, MAP_READ);
455 if (!gba->pristineRom) {
456 GBALog(gba, GBA_LOG_WARN, "Couldn't map ROM");
457 return;
458 }
459 gba->memory.rom = gba->pristineRom;
460 gba->activeFile = fname;
461 gba->memory.romSize = gba->pristineRomSize;
462 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
463 GBASavedataInit(&gba->memory.savedata, sav);
464 GBAGPIOInit(&gba->memory.gpio, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
465 _checkOverrides(gba, ((struct GBACartridge*) gba->memory.rom)->id);
466 // TODO: error check
467}
468
469void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
470 gba->biosVf = vf;
471 uint32_t* bios = vf->map(vf, SIZE_BIOS, MAP_READ);
472 if (!bios) {
473 GBALog(gba, GBA_LOG_WARN, "Couldn't map BIOS");
474 return;
475 }
476 gba->memory.bios = bios;
477 gba->memory.fullBios = 1;
478 uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
479 GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
480 if (checksum == GBA_BIOS_CHECKSUM) {
481 GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
482 } else if (checksum == GBA_DS_BIOS_CHECKSUM) {
483 GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
484 } else {
485 GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
486 }
487 gba->biosChecksum = checksum;
488 if (gba->memory.activeRegion == REGION_BIOS) {
489 gba->cpu->memory.activeRegion = gba->memory.bios;
490 }
491 // TODO: error check
492}
493
494void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
495 size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
496 if (!patchedSize) {
497 return;
498 }
499 gba->memory.rom = anonymousMemoryMap(patchedSize);
500 memcpy(gba->memory.rom, gba->pristineRom, gba->memory.romSize > patchedSize ? patchedSize : gba->memory.romSize);
501 if (!patch->applyPatch(patch, gba->memory.rom, patchedSize)) {
502 mappedMemoryFree(gba->memory.rom, patchedSize);
503 gba->memory.rom = gba->pristineRom;
504 return;
505 }
506 gba->memory.romSize = patchedSize;
507 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
508}
509
510void GBATimerUpdateRegister(struct GBA* gba, int timer) {
511 struct GBATimer* currentTimer = &gba->timers[timer];
512 if (currentTimer->enable && !currentTimer->countUp) {
513 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
514 }
515}
516
517void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
518 gba->timers[timer].reload = reload;
519}
520
521void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
522 struct GBATimer* currentTimer = &gba->timers[timer];
523 GBATimerUpdateRegister(gba, timer);
524
525 int oldPrescale = currentTimer->prescaleBits;
526 switch (control & 0x0003) {
527 case 0x0000:
528 currentTimer->prescaleBits = 0;
529 break;
530 case 0x0001:
531 currentTimer->prescaleBits = 6;
532 break;
533 case 0x0002:
534 currentTimer->prescaleBits = 8;
535 break;
536 case 0x0003:
537 currentTimer->prescaleBits = 10;
538 break;
539 }
540 currentTimer->countUp = !!(control & 0x0004);
541 currentTimer->doIrq = !!(control & 0x0040);
542 currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
543 int wasEnabled = currentTimer->enable;
544 currentTimer->enable = !!(control & 0x0080);
545 if (!wasEnabled && currentTimer->enable) {
546 if (!currentTimer->countUp) {
547 currentTimer->nextEvent = gba->cpu->cycles + currentTimer->overflowInterval;
548 } else {
549 currentTimer->nextEvent = INT_MAX;
550 }
551 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
552 currentTimer->oldReload = currentTimer->reload;
553 currentTimer->lastEvent = 0;
554 gba->timersEnabled |= 1 << timer;
555 } else if (wasEnabled && !currentTimer->enable) {
556 if (!currentTimer->countUp) {
557 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> oldPrescale);
558 }
559 gba->timersEnabled &= ~(1 << timer);
560 } else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
561 // FIXME: this might be before present
562 currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
563 }
564
565 if (currentTimer->nextEvent < gba->cpu->nextEvent) {
566 gba->cpu->nextEvent = currentTimer->nextEvent;
567 }
568};
569
570void GBAWriteIE(struct GBA* gba, uint16_t value) {
571 if (value & (1 << IRQ_KEYPAD)) {
572 GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
573 }
574
575 if (value & (1 << IRQ_GAMEPAK)) {
576 GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
577 }
578
579 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
580 ARMRaiseIRQ(gba->cpu);
581 }
582}
583
584void GBAWriteIME(struct GBA* gba, uint16_t value) {
585 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
586 ARMRaiseIRQ(gba->cpu);
587 }
588}
589
590void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
591 gba->memory.io[REG_IF >> 1] |= 1 << irq;
592 gba->cpu->halted = 0;
593
594 if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
595 ARMRaiseIRQ(gba->cpu);
596 }
597}
598
599void GBATestIRQ(struct ARMCore* cpu) {
600 struct GBA* gba = (struct GBA*) cpu->master;
601 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
602 gba->springIRQ = 1;
603 gba->cpu->nextEvent = 0;
604 }
605}
606
607void GBAHalt(struct GBA* gba) {
608 gba->cpu->nextEvent = 0;
609 gba->cpu->halted = 1;
610}
611
612static void _GBAVLog(struct GBA* gba, enum GBALogLevel level, const char* format, va_list args) {
613 struct GBAThread* threadContext = GBAThreadGetContext();
614 enum GBALogLevel logLevel = -1;
615
616 if (gba) {
617 logLevel = gba->logLevel;
618 }
619
620 if (threadContext) {
621 logLevel = threadContext->logLevel;
622 }
623
624 if (!(level & logLevel) && level != GBA_LOG_FATAL) {
625 return;
626 }
627
628 if (threadContext && threadContext->logHandler) {
629 threadContext->logHandler(threadContext, level, format, args);
630 return;
631 }
632
633 vprintf(format, args);
634 printf("\n");
635
636 if (level == GBA_LOG_FATAL) {
637 abort();
638 }
639}
640
641void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
642 va_list args;
643 va_start(args, format);
644 _GBAVLog(gba, level, format, args);
645 va_end(args);
646}
647
648void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...) {
649 struct GBA* gba = 0;
650 if (debugger->cpu) {
651 gba = (struct GBA*) debugger->cpu->master;
652 }
653
654 enum GBALogLevel gbaLevel;
655 switch (level) {
656 default: // Avoids compiler warning
657 case DEBUGGER_LOG_DEBUG:
658 gbaLevel = GBA_LOG_DEBUG;
659 break;
660 case DEBUGGER_LOG_INFO:
661 gbaLevel = GBA_LOG_INFO;
662 break;
663 case DEBUGGER_LOG_WARN:
664 gbaLevel = GBA_LOG_WARN;
665 break;
666 case DEBUGGER_LOG_ERROR:
667 gbaLevel = GBA_LOG_ERROR;
668 break;
669 }
670 va_list args;
671 va_start(args, format);
672 _GBAVLog(gba, gbaLevel, format, args);
673 va_end(args);
674}
675
676bool GBAIsROM(struct VFile* vf) {
677 if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
678 return false;
679 }
680 uint8_t signature[sizeof(GBA_ROM_MAGIC)];
681 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
682 return false;
683 }
684 return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
685}
686
687bool GBAIsBIOS(struct VFile* vf) {
688 if (vf->seek(vf, 0, SEEK_SET) < 0) {
689 return false;
690 }
691 uint32_t interruptTable[7];
692 if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
693 return false;
694 }
695 int i;
696 for (i = 0; i < 7; ++i) {
697 if ((interruptTable[i] & 0xFFFF0000) != 0xEA000000) {
698 return false;
699 }
700 }
701 return true;
702}
703
704void GBAGetGameCode(struct GBA* gba, char* out) {
705 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->id, 4);
706}
707
708void GBAGetGameTitle(struct GBA* gba, char* out) {
709 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
710}
711
712void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
713 struct GBA* gba = (struct GBA*) cpu->master;
714 enum GBALogLevel level = GBA_LOG_FATAL;
715 if (gba->debugger) {
716 level = GBA_LOG_STUB;
717 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
718 }
719 GBALog(gba, level, "Stub opcode: %08x", opcode);
720}
721
722void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
723 struct GBA* gba = (struct GBA*) cpu->master;
724 GBALog(gba, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
725 if (gba->debugger) {
726 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
727 }
728}
729
730void _checkOverrides(struct GBA* gba, uint32_t id) {
731 int i;
732 gba->busyLoop = -1;
733 if ((id & 0xFF) == 'F') {
734 GBALog(gba, GBA_LOG_DEBUG, "Found Classic NES Series game, using EEPROM saves");
735 GBASavedataInitEEPROM(&gba->memory.savedata);
736 return;
737 }
738 for (i = 0; _overrides[i].id[0]; ++i) {
739 const uint32_t* overrideId = (const uint32_t*) _overrides[i].id;
740 if (*overrideId == id) {
741 GBALog(gba, GBA_LOG_DEBUG, "Found override for game %s!", _overrides[i].id);
742 GBASavedataForceType(&gba->memory.savedata, _overrides[i].type);
743
744 if (_overrides[i].gpio & GPIO_RTC) {
745 GBAGPIOInitRTC(&gba->memory.gpio);
746 }
747
748 if (_overrides[i].gpio & GPIO_GYRO) {
749 GBAGPIOInitGyro(&gba->memory.gpio);
750 }
751
752 if (_overrides[i].gpio & GPIO_RUMBLE) {
753 GBAGPIOInitRumble(&gba->memory.gpio);
754 }
755
756 if (_overrides[i].gpio & GPIO_LIGHT_SENSOR) {
757 GBAGPIOInitLightSensor(&gba->memory.gpio);
758 }
759
760 if (_overrides[i].gpio & GPIO_TILT) {
761 GBAGPIOInitTilt(&gba->memory.gpio);
762 }
763
764 gba->busyLoop = _overrides[i].busyLoop;
765 return;
766 }
767 }
768}