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