all repos — mgba @ 34ddb09516c584e7a47cf61e0ad3fe00c7e13c5a

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
  6#include "debugger.h"
  7
  8#include <limits.h>
  9#include <stdarg.h>
 10#include <stdio.h>
 11#include <stdlib.h>
 12#include <string.h>
 13#include <sys/mman.h>
 14#include <sys/stat.h>
 15
 16enum {
 17	SP_BASE_SYSTEM = 0x03FFFF00,
 18	SP_BASE_IRQ = 0x03FFFFA0,
 19	SP_BASE_SUPERVISOR = 0x03FFFFE0
 20};
 21
 22static void GBAProcessEvents(struct ARMBoard* board);
 23static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
 24static void GBAHitStub(struct ARMBoard* board, uint32_t opcode);
 25
 26void GBAInit(struct GBA* gba) {
 27	gba->errno = GBA_NO_ERROR;
 28	gba->errstr = 0;
 29
 30	ARMInit(&gba->cpu);
 31
 32	gba->memory.p = gba;
 33	GBAMemoryInit(&gba->memory);
 34	ARMAssociateMemory(&gba->cpu, &gba->memory.d);
 35
 36	gba->board.p = gba;
 37	GBABoardInit(&gba->board);
 38	ARMAssociateBoard(&gba->cpu, &gba->board.d);
 39
 40	gba->video.p = gba;
 41	GBAVideoInit(&gba->video);
 42
 43	GBAIOInit(gba);
 44
 45	memset(gba->timers, 0, sizeof(gba->timers));
 46
 47	gba->springIRQ = 0;
 48	gba->keySource = 0;
 49
 50	gba->logLevel = GBA_LOG_INFO;
 51
 52	ARMReset(&gba->cpu);
 53}
 54
 55void GBADeinit(struct GBA* gba) {
 56	GBAMemoryDeinit(&gba->memory);
 57	GBAVideoDeinit(&gba->video);
 58}
 59
 60void GBABoardInit(struct GBABoard* board) {
 61	board->d.reset = GBABoardReset;
 62	board->d.processEvents = GBAProcessEvents;
 63	board->d.swi16 = GBASwi16;
 64	board->d.swi32 = GBASwi32;
 65	board->d.hitStub = GBAHitStub;
 66}
 67
 68void GBABoardReset(struct ARMBoard* board) {
 69	struct ARMCore* cpu = board->cpu;
 70	ARMSetPrivilegeMode(cpu, MODE_IRQ);
 71	cpu->gprs[ARM_SP] = SP_BASE_IRQ;
 72	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
 73	cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
 74	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
 75	cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
 76}
 77
 78static void GBAProcessEvents(struct ARMBoard* board) {
 79	struct GBABoard* gbaBoard = (struct GBABoard*) board;
 80	int32_t cycles = board->cpu->cycles;
 81	int32_t nextEvent = INT_MAX;
 82	int32_t testEvent;
 83
 84	if (gbaBoard->p->springIRQ) {
 85		ARMRaiseIRQ(&gbaBoard->p->cpu);
 86		gbaBoard->p->springIRQ = 0;
 87	}
 88
 89	testEvent = GBAVideoProcessEvents(&gbaBoard->p->video, cycles);
 90	if (testEvent < nextEvent) {
 91		nextEvent = testEvent;
 92	}
 93
 94	testEvent = GBAMemoryProcessEvents(&gbaBoard->p->memory, cycles);
 95	if (testEvent < nextEvent) {
 96		nextEvent = testEvent;
 97	}
 98
 99	testEvent = GBATimersProcessEvents(gbaBoard->p, cycles);
100	if (testEvent < nextEvent) {
101		nextEvent = testEvent;
102	}
103
104	board->cpu->cycles = 0;
105	board->cpu->nextEvent = nextEvent;
106}
107
108static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
109	int32_t nextEvent = INT_MAX;
110	if (gba->timersEnabled) {
111		struct GBATimer* timer;
112		struct GBATimer* nextTimer;
113
114		timer = &gba->timers[0];
115		if (timer->enable) {
116			timer->nextEvent -= cycles;
117			timer->lastEvent -= cycles;
118			if (timer->nextEvent <= 0) {
119				timer->lastEvent = timer->nextEvent;
120				timer->nextEvent += timer->overflowInterval;
121				gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
122				timer->oldReload = timer->reload;
123
124				if (timer->doIrq) {
125					GBARaiseIRQ(gba, IRQ_TIMER0);
126				}
127
128				nextTimer = &gba->timers[1];
129				if (nextTimer->countUp) {
130					++gba->memory.io[REG_TM1CNT_LO >> 1];
131					if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
132						nextTimer->nextEvent = 0;
133					}
134				}
135			}
136			nextEvent = timer->nextEvent;
137		}
138
139		timer = &gba->timers[1];
140		if (timer->enable) {
141			timer->nextEvent -= cycles;
142			timer->lastEvent -= cycles;
143			if (timer->nextEvent <= 0) {
144				timer->lastEvent = timer->nextEvent;
145				timer->nextEvent += timer->overflowInterval;
146				gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
147				timer->oldReload = timer->reload;
148
149				if (timer->doIrq) {
150					GBARaiseIRQ(gba, IRQ_TIMER1);
151				}
152
153				if (timer->countUp) {
154					timer->nextEvent = INT_MAX;
155				}
156
157				nextTimer = &gba->timers[2];
158				if (nextTimer->countUp) {
159					++gba->memory.io[REG_TM2CNT_LO >> 1];
160					if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
161						nextTimer->nextEvent = 0;
162					}
163				}
164			}
165			if (timer->nextEvent < nextEvent) {
166				nextEvent = timer->nextEvent;
167			}
168		}
169
170		timer = &gba->timers[2];
171		if (timer->enable) {
172			timer->nextEvent -= cycles;
173			timer->lastEvent -= cycles;
174			nextEvent = timer->nextEvent;
175			if (timer->nextEvent <= 0) {
176				timer->lastEvent = timer->nextEvent;
177				timer->nextEvent += timer->overflowInterval;
178				gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
179				timer->oldReload = timer->reload;
180
181				if (timer->doIrq) {
182					GBARaiseIRQ(gba, IRQ_TIMER2);
183				}
184
185				if (timer->countUp) {
186					timer->nextEvent = INT_MAX;
187				}
188
189				nextTimer = &gba->timers[3];
190				if (nextTimer->countUp) {
191					++gba->memory.io[REG_TM3CNT_LO >> 1];
192					if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
193						nextTimer->nextEvent = 0;
194					}
195				}
196			}
197			if (timer->nextEvent < nextEvent) {
198				nextEvent = timer->nextEvent;
199			}
200		}
201
202		timer = &gba->timers[3];
203		if (timer->enable) {
204			timer->nextEvent -= cycles;
205			timer->lastEvent -= cycles;
206			nextEvent = timer->nextEvent;
207			if (timer->nextEvent <= 0) {
208				timer->lastEvent = timer->nextEvent;
209				timer->nextEvent += timer->overflowInterval;
210				gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
211				timer->oldReload = timer->reload;
212
213				if (timer->doIrq) {
214					GBARaiseIRQ(gba, IRQ_TIMER3);
215				}
216
217				if (timer->countUp) {
218					timer->nextEvent = INT_MAX;
219				}
220			}
221			if (timer->nextEvent < nextEvent) {
222				nextEvent = timer->nextEvent;
223			}
224		}
225	}
226	return nextEvent;
227}
228
229void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
230	ARMDebuggerInit(debugger, &gba->cpu);
231	gba->debugger = debugger;
232}
233
234void GBALoadROM(struct GBA* gba, int fd, const char* fname) {
235	struct stat info;
236	gba->memory.rom = mmap(0, SIZE_CART0, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
237	gba->activeFile = fname;
238	fstat(fd, &info);
239	gba->memory.romSize = info.st_size;
240	// TODO: error check
241}
242
243void GBATimerUpdateRegister(struct GBA* gba, int timer) {
244	struct GBATimer* currentTimer = &gba->timers[timer];
245	if (currentTimer->enable && !currentTimer->countUp) {
246		gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu.cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
247	}
248}
249
250void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
251	gba->timers[timer].reload = reload;
252}
253
254void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
255	struct GBATimer* currentTimer = &gba->timers[timer];
256	GBATimerUpdateRegister(gba, timer);
257
258	int oldPrescale = currentTimer->prescaleBits;
259	switch (control & 0x0003) {
260	case 0x0000:
261		currentTimer->prescaleBits = 0;
262		break;
263	case 0x0001:
264		currentTimer->prescaleBits = 6;
265		break;
266	case 0x0002:
267		currentTimer->prescaleBits = 8;
268		break;
269	case 0x0003:
270		currentTimer->prescaleBits = 10;
271		break;
272	}
273	currentTimer->countUp = !!(control & 0x0004);
274	currentTimer->doIrq = !!(control & 0x0040);
275	currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
276	int wasEnabled = currentTimer->enable;
277	currentTimer->enable = !!(control & 0x0080);
278	if (!wasEnabled && currentTimer->enable) {
279		if (!currentTimer->countUp) {
280			currentTimer->nextEvent = gba->cpu.cycles + currentTimer->overflowInterval;
281		} else {
282			currentTimer->nextEvent = INT_MAX;
283		}
284		gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
285		currentTimer->oldReload = currentTimer->reload;
286		gba->timersEnabled |= 1 << timer;
287	} else if (wasEnabled && !currentTimer->enable) {
288		if (!currentTimer->countUp) {
289			gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu.cycles - currentTimer->lastEvent) >> oldPrescale);
290		}
291		gba->timersEnabled &= ~(1 << timer);
292	} else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
293		// FIXME: this might be before present
294		currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
295	}
296
297	if (currentTimer->nextEvent < gba->cpu.nextEvent) {
298		gba->cpu.nextEvent = currentTimer->nextEvent;
299	}
300};
301
302void GBAWriteIE(struct GBA* gba, uint16_t value) {
303	if (value & (1 << IRQ_SIO)) {
304		GBALog(gba, GBA_LOG_STUB, "SIO interrupts not implemented");
305	}
306
307	if (value & (1 << IRQ_KEYPAD)) {
308		GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
309	}
310
311	if (value & (1 << IRQ_GAMEPAK)) {
312		GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
313	}
314
315	if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
316		ARMRaiseIRQ(&gba->cpu);
317	}
318}
319
320void GBAWriteIME(struct GBA* gba, uint16_t value) {
321	if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
322		ARMRaiseIRQ(&gba->cpu);
323	}
324}
325
326void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
327	gba->memory.io[REG_IF >> 1] |= 1 << irq;
328
329	if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
330		ARMRaiseIRQ(&gba->cpu);
331	}
332}
333
334int GBATestIRQ(struct GBA* gba) {
335	if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
336		gba->springIRQ = 1;
337		gba->cpu.nextEvent = gba->cpu.cycles;
338		return 1;
339	}
340	return 0;
341}
342
343int GBAWaitForIRQ(struct GBA* gba) {
344	int irqs = gba->memory.io[REG_IF >> 1];
345	int newIRQs = 0;
346	gba->memory.io[REG_IF >> 1] = 0;
347	while (1) {
348		if (gba->cpu.nextEvent == INT_MAX) {
349			break;
350		} else {
351			gba->cpu.cycles = gba->cpu.nextEvent;
352			GBAProcessEvents(&gba->board.d);
353			if (gba->memory.io[REG_IF >> 1]) {
354				newIRQs = gba->memory.io[REG_IF >> 1];
355				break;
356			}
357		}
358	}
359	gba->memory.io[REG_IF >> 1] = newIRQs | irqs;
360	return newIRQs;
361}
362
363int GBAHalt(struct GBA* gba) {
364	return GBAWaitForIRQ(gba);
365}
366
367void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
368	if (gba && level < gba->logLevel) {
369		return;
370	}
371	va_list args;
372	va_start(args, format);
373	vprintf(format, args);
374	va_end(args);
375	printf("\n");
376}
377
378void GBAHitStub(struct ARMBoard* board, uint32_t opcode) {
379	struct GBABoard* gbaBoard = (struct GBABoard*) board;
380	GBALog(gbaBoard->p, GBA_LOG_STUB, "Stub opcode: %08x", opcode);
381	if (!gbaBoard->p->debugger) {
382		abort();
383	} else {
384		ARMDebuggerEnter(gbaBoard->p->debugger);
385	}
386}