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
7#include "debugger.h"
8
9#include <limits.h>
10#include <stdarg.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/mman.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 // RockMan EXE 4.5 - Real Operation
72 { 'J4RB', SAVEDATA_FLASH512, GPIO_RTC },
73
74 // Super Mario Advance 4
75 { 'J4XA', SAVEDATA_FLASH1M, GPIO_NONE },
76 { 'E4XA', SAVEDATA_FLASH1M, GPIO_NONE },
77 { 'P4XA', SAVEDATA_FLASH1M, GPIO_NONE },
78
79 // Wario Ware Twisted
80 { 'JWZR', SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
81 { 'EWZR', SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
82 { 'PWZR', SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
83
84 { 0, 0, 0 }
85};
86
87static void GBAProcessEvents(struct ARMBoard* board);
88static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
89static void GBAHitStub(struct ARMBoard* board, uint32_t opcode);
90
91static void _checkOverrides(struct GBA* gba, uint32_t code);
92
93void GBAInit(struct GBA* gba) {
94 gba->debugger = 0;
95 gba->savefile = 0;
96
97 ARMInit(&gba->cpu);
98
99 gba->memory.p = gba;
100 GBAMemoryInit(&gba->memory);
101 ARMAssociateMemory(&gba->cpu, &gba->memory.d);
102
103 gba->board.p = gba;
104 GBABoardInit(&gba->board);
105 ARMAssociateBoard(&gba->cpu, &gba->board.d);
106
107 gba->video.p = gba;
108 GBAVideoInit(&gba->video);
109
110 gba->audio.p = gba;
111 GBAAudioInit(&gba->audio);
112
113 GBAIOInit(gba);
114
115 gba->timersEnabled = 0;
116 memset(gba->timers, 0, sizeof(gba->timers));
117
118 gba->halted = 0;
119 gba->springIRQ = 0;
120 gba->keySource = 0;
121 gba->rotationSource = 0;
122 gba->rumble = 0;
123
124 gba->logLevel = GBA_LOG_INFO | GBA_LOG_WARN | GBA_LOG_ERROR;
125
126 ARMReset(&gba->cpu);
127}
128
129void GBADeinit(struct GBA* gba) {
130 GBAMemoryDeinit(&gba->memory);
131 GBAVideoDeinit(&gba->video);
132 GBAAudioDeinit(&gba->audio);
133}
134
135void GBABoardInit(struct GBABoard* board) {
136 board->d.reset = GBABoardReset;
137 board->d.processEvents = GBAProcessEvents;
138 board->d.swi16 = GBASwi16;
139 board->d.swi32 = GBASwi32;
140 board->d.readCPSR = GBATestIRQ;
141 board->d.hitStub = GBAHitStub;
142}
143
144void GBABoardReset(struct ARMBoard* board) {
145 struct ARMCore* cpu = board->cpu;
146 ARMSetPrivilegeMode(cpu, MODE_IRQ);
147 cpu->gprs[ARM_SP] = SP_BASE_IRQ;
148 ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
149 cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
150 ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
151 cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
152}
153
154static void GBAProcessEvents(struct ARMBoard* board) {
155 struct GBABoard* gbaBoard = (struct GBABoard*) board;
156 do {
157 int32_t cycles = board->cpu->cycles;
158 int32_t nextEvent = INT_MAX;
159 int32_t testEvent;
160
161 if (gbaBoard->p->springIRQ) {
162 ARMRaiseIRQ(&gbaBoard->p->cpu);
163 gbaBoard->p->springIRQ = 0;
164 }
165
166 testEvent = GBAVideoProcessEvents(&gbaBoard->p->video, cycles);
167 if (testEvent < nextEvent) {
168 nextEvent = testEvent;
169 }
170
171 testEvent = GBAAudioProcessEvents(&gbaBoard->p->audio, cycles);
172 if (testEvent < nextEvent) {
173 nextEvent = testEvent;
174 }
175
176 testEvent = GBAMemoryProcessEvents(&gbaBoard->p->memory, cycles);
177 if (testEvent < nextEvent) {
178 nextEvent = testEvent;
179 }
180
181 testEvent = GBATimersProcessEvents(gbaBoard->p, cycles);
182 if (testEvent < nextEvent) {
183 nextEvent = testEvent;
184 }
185
186 board->cpu->cycles = 0;
187 board->cpu->nextEvent = nextEvent;
188
189 if (gbaBoard->p->halted) {
190 gbaBoard->p->halted = !(gbaBoard->p->memory.io[REG_IF >> 1] & gbaBoard->p->memory.io[REG_IE >> 1]);
191 board->cpu->cycles = nextEvent;
192 if (nextEvent == INT_MAX) {
193 break;
194 }
195 }
196 } while (gbaBoard->p->halted);
197}
198
199static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
200 int32_t nextEvent = INT_MAX;
201 if (gba->timersEnabled) {
202 struct GBATimer* timer;
203 struct GBATimer* nextTimer;
204
205 timer = &gba->timers[0];
206 if (timer->enable) {
207 timer->nextEvent -= cycles;
208 timer->lastEvent -= cycles;
209 if (timer->nextEvent <= 0) {
210 timer->lastEvent = timer->nextEvent;
211 timer->nextEvent += timer->overflowInterval;
212 gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
213 timer->oldReload = timer->reload;
214
215 if (timer->doIrq) {
216 GBARaiseIRQ(gba, IRQ_TIMER0);
217 }
218
219 if (gba->audio.enable) {
220 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 0) {
221 GBAAudioSampleFIFO(&gba->audio, 0);
222 }
223
224 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 0) {
225 GBAAudioSampleFIFO(&gba->audio, 1);
226 }
227 }
228
229 nextTimer = &gba->timers[1];
230 if (nextTimer->countUp) {
231 ++gba->memory.io[REG_TM1CNT_LO >> 1];
232 if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
233 nextTimer->nextEvent = 0;
234 }
235 }
236 }
237 nextEvent = timer->nextEvent;
238 }
239
240 timer = &gba->timers[1];
241 if (timer->enable) {
242 timer->nextEvent -= cycles;
243 timer->lastEvent -= cycles;
244 if (timer->nextEvent <= 0) {
245 timer->lastEvent = timer->nextEvent;
246 timer->nextEvent += timer->overflowInterval;
247 gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
248 timer->oldReload = timer->reload;
249
250 if (timer->doIrq) {
251 GBARaiseIRQ(gba, IRQ_TIMER1);
252 }
253
254 if (gba->audio.enable) {
255 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 1) {
256 GBAAudioSampleFIFO(&gba->audio, 0);
257 }
258
259 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 1) {
260 GBAAudioSampleFIFO(&gba->audio, 1);
261 }
262 }
263
264 if (timer->countUp) {
265 timer->nextEvent = INT_MAX;
266 }
267
268 nextTimer = &gba->timers[2];
269 if (nextTimer->countUp) {
270 ++gba->memory.io[REG_TM2CNT_LO >> 1];
271 if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
272 nextTimer->nextEvent = 0;
273 }
274 }
275 }
276 if (timer->nextEvent < nextEvent) {
277 nextEvent = timer->nextEvent;
278 }
279 }
280
281 timer = &gba->timers[2];
282 if (timer->enable) {
283 timer->nextEvent -= cycles;
284 timer->lastEvent -= cycles;
285 nextEvent = timer->nextEvent;
286 if (timer->nextEvent <= 0) {
287 timer->lastEvent = timer->nextEvent;
288 timer->nextEvent += timer->overflowInterval;
289 gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
290 timer->oldReload = timer->reload;
291
292 if (timer->doIrq) {
293 GBARaiseIRQ(gba, IRQ_TIMER2);
294 }
295
296 if (timer->countUp) {
297 timer->nextEvent = INT_MAX;
298 }
299
300 nextTimer = &gba->timers[3];
301 if (nextTimer->countUp) {
302 ++gba->memory.io[REG_TM3CNT_LO >> 1];
303 if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
304 nextTimer->nextEvent = 0;
305 }
306 }
307 }
308 if (timer->nextEvent < nextEvent) {
309 nextEvent = timer->nextEvent;
310 }
311 }
312
313 timer = &gba->timers[3];
314 if (timer->enable) {
315 timer->nextEvent -= cycles;
316 timer->lastEvent -= cycles;
317 nextEvent = timer->nextEvent;
318 if (timer->nextEvent <= 0) {
319 timer->lastEvent = timer->nextEvent;
320 timer->nextEvent += timer->overflowInterval;
321 gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
322 timer->oldReload = timer->reload;
323
324 if (timer->doIrq) {
325 GBARaiseIRQ(gba, IRQ_TIMER3);
326 }
327
328 if (timer->countUp) {
329 timer->nextEvent = INT_MAX;
330 }
331 }
332 if (timer->nextEvent < nextEvent) {
333 nextEvent = timer->nextEvent;
334 }
335 }
336 }
337 return nextEvent;
338}
339
340#ifdef USE_DEBUGGER
341void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
342 ARMDebuggerInit(debugger, &gba->cpu);
343 gba->debugger = debugger;
344}
345#endif
346
347void GBALoadROM(struct GBA* gba, int fd, const char* fname) {
348 struct stat info;
349 gba->memory.rom = mmap(0, SIZE_CART0, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
350 gba->activeFile = fname;
351 fstat(fd, &info);
352 gba->memory.romSize = info.st_size;
353 if (gba->savefile) {
354 GBASavedataInit(&gba->memory.savedata, gba->savefile);
355 }
356 GBAGPIOInit(&gba->memory.gpio, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
357 _checkOverrides(gba, ((struct GBACartridge*) gba->memory.rom)->id);
358 // TODO: error check
359}
360
361void GBALoadBIOS(struct GBA* gba, int fd) {
362 gba->memory.bios = mmap(0, SIZE_BIOS, PROT_READ, MAP_SHARED, fd, 0);
363 gba->memory.fullBios = 1;
364 if ((gba->cpu.gprs[ARM_PC] >> BASE_OFFSET) == BASE_BIOS) {
365 gba->memory.d.setActiveRegion(&gba->memory.d, gba->cpu.gprs[ARM_PC]);
366 }
367 // TODO: error check
368}
369
370void GBATimerUpdateRegister(struct GBA* gba, int timer) {
371 struct GBATimer* currentTimer = &gba->timers[timer];
372 if (currentTimer->enable && !currentTimer->countUp) {
373 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu.cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
374 }
375}
376
377void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
378 gba->timers[timer].reload = reload;
379}
380
381void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
382 struct GBATimer* currentTimer = &gba->timers[timer];
383 GBATimerUpdateRegister(gba, timer);
384
385 int oldPrescale = currentTimer->prescaleBits;
386 switch (control & 0x0003) {
387 case 0x0000:
388 currentTimer->prescaleBits = 0;
389 break;
390 case 0x0001:
391 currentTimer->prescaleBits = 6;
392 break;
393 case 0x0002:
394 currentTimer->prescaleBits = 8;
395 break;
396 case 0x0003:
397 currentTimer->prescaleBits = 10;
398 break;
399 }
400 currentTimer->countUp = !!(control & 0x0004);
401 currentTimer->doIrq = !!(control & 0x0040);
402 currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
403 int wasEnabled = currentTimer->enable;
404 currentTimer->enable = !!(control & 0x0080);
405 if (!wasEnabled && currentTimer->enable) {
406 if (!currentTimer->countUp) {
407 currentTimer->nextEvent = gba->cpu.cycles + currentTimer->overflowInterval;
408 } else {
409 currentTimer->nextEvent = INT_MAX;
410 }
411 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
412 currentTimer->oldReload = currentTimer->reload;
413 gba->timersEnabled |= 1 << timer;
414 } else if (wasEnabled && !currentTimer->enable) {
415 if (!currentTimer->countUp) {
416 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu.cycles - currentTimer->lastEvent) >> oldPrescale);
417 }
418 gba->timersEnabled &= ~(1 << timer);
419 } else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
420 // FIXME: this might be before present
421 currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
422 }
423
424 if (currentTimer->nextEvent < gba->cpu.nextEvent) {
425 gba->cpu.nextEvent = currentTimer->nextEvent;
426 }
427};
428
429void GBAWriteIE(struct GBA* gba, uint16_t value) {
430 if (value & (1 << IRQ_SIO)) {
431 GBALog(gba, GBA_LOG_STUB, "SIO interrupts not implemented");
432 }
433
434 if (value & (1 << IRQ_KEYPAD)) {
435 GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
436 }
437
438 if (value & (1 << IRQ_GAMEPAK)) {
439 GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
440 }
441
442 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
443 ARMRaiseIRQ(&gba->cpu);
444 }
445}
446
447void GBAWriteIME(struct GBA* gba, uint16_t value) {
448 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
449 ARMRaiseIRQ(&gba->cpu);
450 }
451}
452
453void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
454 gba->memory.io[REG_IF >> 1] |= 1 << irq;
455
456 if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
457 ARMRaiseIRQ(&gba->cpu);
458 }
459}
460
461void GBATestIRQ(struct ARMBoard* board) {
462 struct GBABoard* gbaBoard = (struct GBABoard*) board;
463 struct GBA* gba = gbaBoard->p;
464 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
465 gba->springIRQ = 1;
466 gba->cpu.nextEvent = 0;
467 }
468}
469
470void GBAHalt(struct GBA* gba) {
471 gba->cpu.cycles = gba->cpu.nextEvent;
472 gba->halted = 1;
473}
474
475void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
476 if (!gba) {
477 struct GBAThread* threadContext = GBAThreadGetContext();
478 if (threadContext) {
479 gba = threadContext->gba;
480 }
481 }
482 if (gba && !(level & gba->logLevel)) {
483 return;
484 }
485 va_list args;
486 va_start(args, format);
487 vprintf(format, args);
488 va_end(args);
489 printf("\n");
490}
491
492void GBAHitStub(struct ARMBoard* board, uint32_t opcode) {
493 struct GBABoard* gbaBoard = (struct GBABoard*) board;
494 GBALog(gbaBoard->p, GBA_LOG_STUB, "Stub opcode: %08x", opcode);
495#ifdef USE_DEBUGGER
496 if (!gbaBoard->p->debugger) {
497 abort();
498 } else {
499 ARMDebuggerEnter(gbaBoard->p->debugger);
500 }
501#else
502 abort();
503#endif
504
505}
506
507void _checkOverrides(struct GBA* gba, uint32_t id) {
508 int i;
509 for (i = 0; _overrides[i].id; ++i) {
510 if (_overrides[i].id == id) {
511 switch (_overrides[i].type) {
512 case SAVEDATA_FLASH512:
513 case SAVEDATA_FLASH1M:
514 gba->memory.savedata.type = _overrides[i].type;
515 GBASavedataInitFlash(&gba->memory.savedata);
516 break;
517 case SAVEDATA_EEPROM:
518 GBASavedataInitEEPROM(&gba->memory.savedata);
519 break;
520 case SAVEDATA_SRAM:
521 GBASavedataInitSRAM(&gba->memory.savedata);
522 break;
523 case SAVEDATA_NONE:
524 break;
525 }
526
527 if (_overrides[i].gpio & GPIO_RTC) {
528 GBAGPIOInitRTC(&gba->memory.gpio);
529 }
530
531 if (_overrides[i].gpio & GPIO_GYRO) {
532 GBAGPIOInitGyro(&gba->memory.gpio);
533 }
534
535 if (_overrides[i].gpio & GPIO_RUMBLE) {
536 GBAGPIOInitRumble(&gba->memory.gpio);
537 }
538 return;
539 }
540 }
541}