src/gba/gba.c (view raw)
1#include "gba.h"
2
3#include "gba-bios.h"
4#include "gba-io.h"
5#include "gba-thread.h"
6#include "memory.h"
7
8#include "debugger.h"
9
10#include <limits.h>
11#include <stdarg.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <sys/stat.h>
16
17const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;
18
19enum {
20 SP_BASE_SYSTEM = 0x03FFFF00,
21 SP_BASE_IRQ = 0x03FFFFA0,
22 SP_BASE_SUPERVISOR = 0x03FFFFE0
23};
24
25struct GBACartridgeOverride {
26 uint32_t id;
27 enum SavedataType type;
28 int gpio;
29};
30
31static const struct GBACartridgeOverride _overrides[] = {
32 // Boktai: The Sun is in Your Hand
33 { 'EI3U', SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
34 { 'PI3U', SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
35
36 // Boktai 2: Solar Boy Django
37 { 'E23U', SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
38 { 'P23U', SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
39
40 // Drill Dozer
41 { 'J94V', SAVEDATA_SRAM, GPIO_RUMBLE },
42 { 'E94V', SAVEDATA_SRAM, GPIO_RUMBLE },
43
44 // Pokemon Ruby
45 { 'JVXA', SAVEDATA_FLASH1M, GPIO_RTC },
46 { 'EVXA', SAVEDATA_FLASH1M, GPIO_RTC },
47 { 'PVXA', SAVEDATA_FLASH1M, GPIO_RTC },
48 { 'IVXA', SAVEDATA_FLASH1M, GPIO_RTC },
49 { 'SVXA', SAVEDATA_FLASH1M, GPIO_RTC },
50 { 'DVXA', SAVEDATA_FLASH1M, GPIO_RTC },
51 { 'FVXA', SAVEDATA_FLASH1M, GPIO_RTC },
52
53 // Pokemon Sapphire
54 { 'JPXA', SAVEDATA_FLASH1M, GPIO_RTC },
55 { 'EPXA', SAVEDATA_FLASH1M, GPIO_RTC },
56 { 'PPXA', SAVEDATA_FLASH1M, GPIO_RTC },
57 { 'IPXA', SAVEDATA_FLASH1M, GPIO_RTC },
58 { 'SPXA', SAVEDATA_FLASH1M, GPIO_RTC },
59 { 'DPXA', SAVEDATA_FLASH1M, GPIO_RTC },
60 { 'FPXA', SAVEDATA_FLASH1M, GPIO_RTC },
61
62 // Pokemon Emerald
63 { 'JEPB', SAVEDATA_FLASH1M, GPIO_RTC },
64 { 'EEPB', SAVEDATA_FLASH1M, GPIO_RTC },
65 { 'PEPB', SAVEDATA_FLASH1M, GPIO_RTC },
66 { 'IEPB', SAVEDATA_FLASH1M, GPIO_RTC },
67 { 'SEPB', SAVEDATA_FLASH1M, GPIO_RTC },
68 { 'DEPB', SAVEDATA_FLASH1M, GPIO_RTC },
69 { 'FEPB', SAVEDATA_FLASH1M, GPIO_RTC },
70
71 // Pokemon FireRed
72 { 'JRPB', SAVEDATA_FLASH1M, GPIO_NONE },
73 { 'ERPB', SAVEDATA_FLASH1M, GPIO_NONE },
74 { 'PRPB', SAVEDATA_FLASH1M, GPIO_NONE },
75
76 // Pokemon LeafGreen
77 { 'JGPB', SAVEDATA_FLASH1M, GPIO_NONE },
78 { 'EGPB', SAVEDATA_FLASH1M, GPIO_NONE },
79 { 'PGPB', SAVEDATA_FLASH1M, GPIO_NONE },
80
81 // RockMan EXE 4.5 - Real Operation
82 { 'J4RB', SAVEDATA_FLASH512, GPIO_RTC },
83
84 // Super Mario Advance 4
85 { 'J4XA', SAVEDATA_FLASH1M, GPIO_NONE },
86 { 'E4XA', SAVEDATA_FLASH1M, GPIO_NONE },
87 { 'P4XA', SAVEDATA_FLASH1M, GPIO_NONE },
88
89 // Wario Ware Twisted
90 { 'JWZR', SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
91 { 'EWZR', SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
92 { 'PWZR', SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
93
94 { 0, 0, 0 }
95};
96
97static void GBAProcessEvents(struct ARMBoard* board);
98static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
99static void GBAHitStub(struct ARMBoard* board, uint32_t opcode);
100static void GBAIllegal(struct ARMBoard* board, uint32_t opcode);
101
102static void _checkOverrides(struct GBA* gba, uint32_t code);
103
104void GBAInit(struct GBA* gba) {
105 gba->debugger = 0;
106 gba->savefile = 0;
107
108 ARMInit(&gba->cpu);
109
110 gba->memory.p = gba;
111 GBAMemoryInit(&gba->memory);
112 ARMAssociateMemory(&gba->cpu, &gba->memory.d);
113
114 gba->board.p = gba;
115 GBABoardInit(&gba->board);
116 ARMAssociateBoard(&gba->cpu, &gba->board.d);
117
118 gba->video.p = gba;
119 GBAVideoInit(&gba->video);
120
121 gba->audio.p = gba;
122 GBAAudioInit(&gba->audio);
123
124 GBAIOInit(gba);
125
126 gba->timersEnabled = 0;
127 memset(gba->timers, 0, sizeof(gba->timers));
128
129 gba->springIRQ = 0;
130 gba->keySource = 0;
131 gba->rotationSource = 0;
132 gba->rumble = 0;
133
134 gba->logLevel = GBA_LOG_INFO | GBA_LOG_WARN | GBA_LOG_ERROR;
135
136 gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
137
138 ARMReset(&gba->cpu);
139}
140
141void GBADeinit(struct GBA* gba) {
142 GBAMemoryDeinit(&gba->memory);
143 GBAVideoDeinit(&gba->video);
144 GBAAudioDeinit(&gba->audio);
145}
146
147void GBABoardInit(struct GBABoard* board) {
148 board->d.reset = GBABoardReset;
149 board->d.processEvents = GBAProcessEvents;
150 board->d.swi16 = GBASwi16;
151 board->d.swi32 = GBASwi32;
152 board->d.hitIllegal = GBAIllegal;
153 board->d.readCPSR = GBATestIRQ;
154 board->d.hitStub = GBAHitStub;
155}
156
157void GBABoardReset(struct ARMBoard* board) {
158 struct ARMCore* cpu = board->cpu;
159 ARMSetPrivilegeMode(cpu, MODE_IRQ);
160 cpu->gprs[ARM_SP] = SP_BASE_IRQ;
161 ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
162 cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
163 ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
164 cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
165}
166
167static void GBAProcessEvents(struct ARMBoard* board) {
168 do {
169 struct GBABoard* gbaBoard = (struct GBABoard*) board;
170 int32_t cycles = board->cpu->cycles;
171 int32_t nextEvent = INT_MAX;
172 int32_t testEvent;
173
174 if (gbaBoard->p->springIRQ) {
175 ARMRaiseIRQ(&gbaBoard->p->cpu);
176 gbaBoard->p->springIRQ = 0;
177 }
178
179 testEvent = GBAVideoProcessEvents(&gbaBoard->p->video, cycles);
180 if (testEvent < nextEvent) {
181 nextEvent = testEvent;
182 }
183
184 testEvent = GBAAudioProcessEvents(&gbaBoard->p->audio, cycles);
185 if (testEvent < nextEvent) {
186 nextEvent = testEvent;
187 }
188
189 testEvent = GBATimersProcessEvents(gbaBoard->p, cycles);
190 if (testEvent < nextEvent) {
191 nextEvent = testEvent;
192 }
193
194 testEvent = GBAMemoryRunDMAs(&gbaBoard->p->memory, cycles);
195 if (testEvent < nextEvent) {
196 nextEvent = testEvent;
197 }
198
199 board->cpu->cycles -= cycles;
200 board->cpu->nextEvent = nextEvent;
201 } while (board->cpu->cycles >= board->cpu->nextEvent);
202}
203
204static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
205 int32_t nextEvent = INT_MAX;
206 if (gba->timersEnabled) {
207 struct GBATimer* timer;
208 struct GBATimer* nextTimer;
209
210 timer = &gba->timers[0];
211 if (timer->enable) {
212 timer->nextEvent -= cycles;
213 timer->lastEvent -= cycles;
214 if (timer->nextEvent <= 0) {
215 timer->lastEvent = timer->nextEvent;
216 timer->nextEvent += timer->overflowInterval;
217 gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
218 timer->oldReload = timer->reload;
219
220 if (timer->doIrq) {
221 GBARaiseIRQ(gba, IRQ_TIMER0);
222 }
223
224 if (gba->audio.enable) {
225 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 0) {
226 GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
227 }
228
229 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 0) {
230 GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
231 }
232 }
233
234 nextTimer = &gba->timers[1];
235 if (nextTimer->countUp) {
236 ++gba->memory.io[REG_TM1CNT_LO >> 1];
237 if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
238 nextTimer->nextEvent = 0;
239 }
240 }
241 }
242 nextEvent = timer->nextEvent;
243 }
244
245 timer = &gba->timers[1];
246 if (timer->enable) {
247 timer->nextEvent -= cycles;
248 timer->lastEvent -= cycles;
249 if (timer->nextEvent <= 0) {
250 timer->lastEvent = timer->nextEvent;
251 timer->nextEvent += timer->overflowInterval;
252 gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
253 timer->oldReload = timer->reload;
254
255 if (timer->doIrq) {
256 GBARaiseIRQ(gba, IRQ_TIMER1);
257 }
258
259 if (gba->audio.enable) {
260 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 1) {
261 GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
262 }
263
264 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 1) {
265 GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
266 }
267 }
268
269 if (timer->countUp) {
270 timer->nextEvent = INT_MAX;
271 }
272
273 nextTimer = &gba->timers[2];
274 if (nextTimer->countUp) {
275 ++gba->memory.io[REG_TM2CNT_LO >> 1];
276 if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
277 nextTimer->nextEvent = 0;
278 }
279 }
280 }
281 if (timer->nextEvent < nextEvent) {
282 nextEvent = timer->nextEvent;
283 }
284 }
285
286 timer = &gba->timers[2];
287 if (timer->enable) {
288 timer->nextEvent -= cycles;
289 timer->lastEvent -= cycles;
290 nextEvent = timer->nextEvent;
291 if (timer->nextEvent <= 0) {
292 timer->lastEvent = timer->nextEvent;
293 timer->nextEvent += timer->overflowInterval;
294 gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
295 timer->oldReload = timer->reload;
296
297 if (timer->doIrq) {
298 GBARaiseIRQ(gba, IRQ_TIMER2);
299 }
300
301 if (timer->countUp) {
302 timer->nextEvent = INT_MAX;
303 }
304
305 nextTimer = &gba->timers[3];
306 if (nextTimer->countUp) {
307 ++gba->memory.io[REG_TM3CNT_LO >> 1];
308 if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
309 nextTimer->nextEvent = 0;
310 }
311 }
312 }
313 if (timer->nextEvent < nextEvent) {
314 nextEvent = timer->nextEvent;
315 }
316 }
317
318 timer = &gba->timers[3];
319 if (timer->enable) {
320 timer->nextEvent -= cycles;
321 timer->lastEvent -= cycles;
322 nextEvent = timer->nextEvent;
323 if (timer->nextEvent <= 0) {
324 timer->lastEvent = timer->nextEvent;
325 timer->nextEvent += timer->overflowInterval;
326 gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
327 timer->oldReload = timer->reload;
328
329 if (timer->doIrq) {
330 GBARaiseIRQ(gba, IRQ_TIMER3);
331 }
332
333 if (timer->countUp) {
334 timer->nextEvent = INT_MAX;
335 }
336 }
337 if (timer->nextEvent < nextEvent) {
338 nextEvent = timer->nextEvent;
339 }
340 }
341 }
342 return nextEvent;
343}
344
345#ifdef USE_DEBUGGER
346void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
347 ARMDebuggerInit(debugger, &gba->cpu);
348 gba->debugger = debugger;
349}
350#endif
351
352void GBALoadROM(struct GBA* gba, int fd, const char* fname) {
353 struct stat info;
354 gba->memory.rom = fileMemoryMap(fd, SIZE_CART0, MEMORY_READ);
355 gba->activeFile = fname;
356 fstat(fd, &info);
357 gba->memory.romSize = info.st_size;
358 if (gba->savefile) {
359 GBASavedataInit(&gba->memory.savedata, gba->savefile);
360 }
361 GBAGPIOInit(&gba->memory.gpio, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
362 _checkOverrides(gba, ((struct GBACartridge*) gba->memory.rom)->id);
363 // TODO: error check
364}
365
366void GBALoadBIOS(struct GBA* gba, int fd) {
367 gba->memory.bios = fileMemoryMap(fd, SIZE_BIOS, MEMORY_READ);
368 gba->memory.fullBios = 1;
369 uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
370 GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
371 if (checksum == GBA_BIOS_CHECKSUM) {
372 GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
373 } else if (checksum == GBA_DS_BIOS_CHECKSUM) {
374 GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
375 } else {
376 GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
377 }
378 gba->biosChecksum = checksum;
379 if ((gba->cpu.gprs[ARM_PC] >> BASE_OFFSET) == BASE_BIOS) {
380 gba->memory.d.setActiveRegion(&gba->memory.d, gba->cpu.gprs[ARM_PC]);
381 }
382 // TODO: error check
383}
384
385void GBATimerUpdateRegister(struct GBA* gba, int timer) {
386 struct GBATimer* currentTimer = &gba->timers[timer];
387 if (currentTimer->enable && !currentTimer->countUp) {
388 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu.cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
389 }
390}
391
392void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
393 gba->timers[timer].reload = reload;
394}
395
396void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
397 struct GBATimer* currentTimer = &gba->timers[timer];
398 GBATimerUpdateRegister(gba, timer);
399
400 int oldPrescale = currentTimer->prescaleBits;
401 switch (control & 0x0003) {
402 case 0x0000:
403 currentTimer->prescaleBits = 0;
404 break;
405 case 0x0001:
406 currentTimer->prescaleBits = 6;
407 break;
408 case 0x0002:
409 currentTimer->prescaleBits = 8;
410 break;
411 case 0x0003:
412 currentTimer->prescaleBits = 10;
413 break;
414 }
415 currentTimer->countUp = !!(control & 0x0004);
416 currentTimer->doIrq = !!(control & 0x0040);
417 currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
418 int wasEnabled = currentTimer->enable;
419 currentTimer->enable = !!(control & 0x0080);
420 if (!wasEnabled && currentTimer->enable) {
421 if (!currentTimer->countUp) {
422 currentTimer->nextEvent = gba->cpu.cycles + currentTimer->overflowInterval;
423 } else {
424 currentTimer->nextEvent = INT_MAX;
425 }
426 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
427 currentTimer->oldReload = currentTimer->reload;
428 gba->timersEnabled |= 1 << timer;
429 } else if (wasEnabled && !currentTimer->enable) {
430 if (!currentTimer->countUp) {
431 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu.cycles - currentTimer->lastEvent) >> oldPrescale);
432 }
433 gba->timersEnabled &= ~(1 << timer);
434 } else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
435 // FIXME: this might be before present
436 currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
437 }
438
439 if (currentTimer->nextEvent < gba->cpu.nextEvent) {
440 gba->cpu.nextEvent = currentTimer->nextEvent;
441 }
442};
443
444void GBAWriteIE(struct GBA* gba, uint16_t value) {
445 if (value & (1 << IRQ_SIO)) {
446 GBALog(gba, GBA_LOG_STUB, "SIO interrupts not implemented");
447 }
448
449 if (value & (1 << IRQ_KEYPAD)) {
450 GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
451 }
452
453 if (value & (1 << IRQ_GAMEPAK)) {
454 GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
455 }
456
457 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
458 ARMRaiseIRQ(&gba->cpu);
459 }
460}
461
462void GBAWriteIME(struct GBA* gba, uint16_t value) {
463 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
464 ARMRaiseIRQ(&gba->cpu);
465 }
466}
467
468void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
469 gba->memory.io[REG_IF >> 1] |= 1 << irq;
470
471 if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
472 ARMRaiseIRQ(&gba->cpu);
473 }
474}
475
476void GBATestIRQ(struct ARMBoard* board) {
477 struct GBABoard* gbaBoard = (struct GBABoard*) board;
478 struct GBA* gba = gbaBoard->p;
479 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
480 gba->springIRQ = 1;
481 gba->cpu.nextEvent = 0;
482 }
483}
484
485int GBAWaitForIRQ(struct GBA* gba) {
486 int irqs = gba->memory.io[REG_IF >> 1];
487 int newIRQs = 0;
488 gba->memory.io[REG_IF >> 1] = 0;
489 while (1) {
490 if (gba->cpu.nextEvent == INT_MAX) {
491 break;
492 } else {
493 gba->cpu.cycles = gba->cpu.nextEvent;
494 GBAProcessEvents(&gba->board.d);
495 if (gba->memory.io[REG_IF >> 1]) {
496 newIRQs = gba->memory.io[REG_IF >> 1];
497 break;
498 }
499 }
500 }
501 gba->memory.io[REG_IF >> 1] = newIRQs | irqs;
502 return newIRQs;
503}
504
505int GBAHalt(struct GBA* gba) {
506 return GBAWaitForIRQ(gba);
507}
508
509void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
510 if (!gba) {
511 struct GBAThread* threadContext = GBAThreadGetContext();
512 if (threadContext) {
513 gba = threadContext->gba;
514 }
515 }
516 if (gba && !(level & gba->logLevel)) {
517 return;
518 }
519 va_list args;
520 va_start(args, format);
521 vprintf(format, args);
522 va_end(args);
523 printf("\n");
524}
525
526void GBAHitStub(struct ARMBoard* board, uint32_t opcode) {
527 struct GBABoard* gbaBoard = (struct GBABoard*) board;
528 GBALog(gbaBoard->p, GBA_LOG_STUB, "Stub opcode: %08x", opcode);
529#ifdef USE_DEBUGGER
530 if (!gbaBoard->p->debugger) {
531 abort();
532 } else {
533 ARMDebuggerEnter(gbaBoard->p->debugger);
534 }
535#else
536 abort();
537#endif
538}
539
540void GBAIllegal(struct ARMBoard* board, uint32_t opcode) {
541 struct GBABoard* gbaBoard = (struct GBABoard*) board;
542 GBALog(gbaBoard->p, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
543#ifdef USE_DEBUGGER
544 if (gbaBoard->p->debugger) {
545 ARMDebuggerEnter(gbaBoard->p->debugger);
546 }
547#endif
548}
549
550void _checkOverrides(struct GBA* gba, uint32_t id) {
551 int i;
552 for (i = 0; _overrides[i].id; ++i) {
553 if (_overrides[i].id == id) {
554 switch (_overrides[i].type) {
555 case SAVEDATA_FLASH512:
556 case SAVEDATA_FLASH1M:
557 gba->memory.savedata.type = _overrides[i].type;
558 GBASavedataInitFlash(&gba->memory.savedata);
559 break;
560 case SAVEDATA_EEPROM:
561 GBASavedataInitEEPROM(&gba->memory.savedata);
562 break;
563 case SAVEDATA_SRAM:
564 GBASavedataInitSRAM(&gba->memory.savedata);
565 break;
566 case SAVEDATA_NONE:
567 break;
568 }
569
570 if (_overrides[i].gpio & GPIO_RTC) {
571 GBAGPIOInitRTC(&gba->memory.gpio);
572 }
573
574 if (_overrides[i].gpio & GPIO_GYRO) {
575 GBAGPIOInitGyro(&gba->memory.gpio);
576 }
577
578 if (_overrides[i].gpio & GPIO_RUMBLE) {
579 GBAGPIOInitRumble(&gba->memory.gpio);
580 }
581 return;
582 }
583 }
584}