all repos — mgba @ dd1f1bc79e71700a279acd4c43effb235e6aa6ce

mGBA Game Boy Advance Emulator

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);
100
101static void _checkOverrides(struct GBA* gba, uint32_t code);
102
103void GBAInit(struct GBA* gba) {
104	gba->debugger = 0;
105	gba->savefile = 0;
106
107	ARMInit(&gba->cpu);
108
109	gba->memory.p = gba;
110	GBAMemoryInit(&gba->memory);
111	ARMAssociateMemory(&gba->cpu, &gba->memory.d);
112
113	gba->board.p = gba;
114	GBABoardInit(&gba->board);
115	ARMAssociateBoard(&gba->cpu, &gba->board.d);
116
117	gba->video.p = gba;
118	GBAVideoInit(&gba->video);
119
120	gba->audio.p = gba;
121	GBAAudioInit(&gba->audio);
122
123	GBAIOInit(gba);
124
125	gba->timersEnabled = 0;
126	memset(gba->timers, 0, sizeof(gba->timers));
127
128	gba->springIRQ = 0;
129	gba->keySource = 0;
130	gba->rotationSource = 0;
131	gba->rumble = 0;
132
133	gba->logLevel = GBA_LOG_INFO | GBA_LOG_WARN | GBA_LOG_ERROR;
134
135	ARMReset(&gba->cpu);
136}
137
138void GBADeinit(struct GBA* gba) {
139	GBAMemoryDeinit(&gba->memory);
140	GBAVideoDeinit(&gba->video);
141	GBAAudioDeinit(&gba->audio);
142}
143
144void GBABoardInit(struct GBABoard* board) {
145	board->d.reset = GBABoardReset;
146	board->d.processEvents = GBAProcessEvents;
147	board->d.swi16 = GBASwi16;
148	board->d.swi32 = GBASwi32;
149	board->d.readCPSR = GBATestIRQ;
150	board->d.hitStub = GBAHitStub;
151}
152
153void GBABoardReset(struct ARMBoard* board) {
154	struct ARMCore* cpu = board->cpu;
155	ARMSetPrivilegeMode(cpu, MODE_IRQ);
156	cpu->gprs[ARM_SP] = SP_BASE_IRQ;
157	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
158	cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
159	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
160	cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
161}
162
163static void GBAProcessEvents(struct ARMBoard* board) {
164	struct GBABoard* gbaBoard = (struct GBABoard*) board;
165	int32_t cycles = board->cpu->cycles;
166	int32_t nextEvent = INT_MAX;
167	int32_t testEvent;
168
169	if (gbaBoard->p->springIRQ) {
170		ARMRaiseIRQ(&gbaBoard->p->cpu);
171		gbaBoard->p->springIRQ = 0;
172	}
173
174	testEvent = GBAVideoProcessEvents(&gbaBoard->p->video, cycles);
175	if (testEvent < nextEvent) {
176		nextEvent = testEvent;
177	}
178
179	testEvent = GBAAudioProcessEvents(&gbaBoard->p->audio, cycles);
180	if (testEvent < nextEvent) {
181		nextEvent = testEvent;
182	}
183
184	testEvent = GBAMemoryProcessEvents(&gbaBoard->p->memory, 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	board->cpu->cycles = 0;
195	board->cpu->nextEvent = nextEvent;
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 = fileMemoryMap(fd, SIZE_CART0, MEMORY_READ);
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 = fileMemoryMap(fd, SIZE_BIOS, MEMORY_READ);
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
469int GBAWaitForIRQ(struct GBA* gba) {
470	int irqs = gba->memory.io[REG_IF >> 1];
471	int newIRQs = 0;
472	gba->memory.io[REG_IF >> 1] = 0;
473	while (1) {
474		if (gba->cpu.nextEvent == INT_MAX) {
475			break;
476		} else {
477			gba->cpu.cycles = gba->cpu.nextEvent;
478			GBAProcessEvents(&gba->board.d);
479			if (gba->memory.io[REG_IF >> 1]) {
480				newIRQs = gba->memory.io[REG_IF >> 1];
481				break;
482			}
483		}
484	}
485	gba->memory.io[REG_IF >> 1] = newIRQs | irqs;
486	return newIRQs;
487}
488
489int GBAHalt(struct GBA* gba) {
490	return GBAWaitForIRQ(gba);
491}
492
493void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
494	if (!gba) {
495		struct GBAThread* threadContext = GBAThreadGetContext();
496		if (threadContext) {
497			gba = threadContext->gba;
498		}
499	}
500	if (gba && !(level & gba->logLevel)) {
501		return;
502	}
503	va_list args;
504	va_start(args, format);
505	vprintf(format, args);
506	va_end(args);
507	printf("\n");
508}
509
510void GBAHitStub(struct ARMBoard* board, uint32_t opcode) {
511	struct GBABoard* gbaBoard = (struct GBABoard*) board;
512	GBALog(gbaBoard->p, GBA_LOG_STUB, "Stub opcode: %08x", opcode);
513#ifdef USE_DEBUGGER
514	if (!gbaBoard->p->debugger) {
515		abort();
516	} else {
517		ARMDebuggerEnter(gbaBoard->p->debugger);
518	}
519#else
520	abort();
521#endif
522
523}
524
525void _checkOverrides(struct GBA* gba, uint32_t id) {
526	int i;
527	for (i = 0; _overrides[i].id; ++i) {
528		if (_overrides[i].id == id) {
529			switch (_overrides[i].type) {
530				case SAVEDATA_FLASH512:
531				case SAVEDATA_FLASH1M:
532					gba->memory.savedata.type = _overrides[i].type;
533					GBASavedataInitFlash(&gba->memory.savedata);
534					break;
535				case SAVEDATA_EEPROM:
536					GBASavedataInitEEPROM(&gba->memory.savedata);
537					break;
538				case SAVEDATA_SRAM:
539					GBASavedataInitSRAM(&gba->memory.savedata);
540					break;
541				case SAVEDATA_NONE:
542					break;
543			}
544
545			if (_overrides[i].gpio & GPIO_RTC) {
546				GBAGPIOInitRTC(&gba->memory.gpio);
547			}
548
549			if (_overrides[i].gpio & GPIO_GYRO) {
550				GBAGPIOInitGyro(&gba->memory.gpio);
551			}
552
553			if (_overrides[i].gpio & GPIO_RUMBLE) {
554				GBAGPIOInitRumble(&gba->memory.gpio);
555			}
556			return;
557		}
558	}
559}