all repos — mgba @ 388dbc08513eaabfa99bce533f10df32e8627e23

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) {
235	struct stat info;
236	gba->memory.rom = mmap(0, SIZE_CART0, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
237	fstat(fd, &info);
238	gba->memory.romSize = info.st_size;
239	// TODO: error check
240}
241
242void GBATimerUpdateRegister(struct GBA* gba, int timer) {
243	struct GBATimer* currentTimer = &gba->timers[timer];
244	if (currentTimer->enable && !currentTimer->countUp) {
245		gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu.cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
246	}
247}
248
249void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
250	gba->timers[timer].reload = reload;
251}
252
253void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
254	struct GBATimer* currentTimer = &gba->timers[timer];
255	GBATimerUpdateRegister(gba, timer);
256
257	int oldPrescale = currentTimer->prescaleBits;
258	switch (control & 0x0003) {
259	case 0x0000:
260		currentTimer->prescaleBits = 0;
261		break;
262	case 0x0001:
263		currentTimer->prescaleBits = 6;
264		break;
265	case 0x0002:
266		currentTimer->prescaleBits = 8;
267		break;
268	case 0x0003:
269		currentTimer->prescaleBits = 10;
270		break;
271	}
272	currentTimer->countUp = !!(control & 0x0004);
273	currentTimer->doIrq = !!(control & 0x0040);
274	currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
275	int wasEnabled = currentTimer->enable;
276	currentTimer->enable = !!(control & 0x0080);
277	if (!wasEnabled && currentTimer->enable) {
278		if (!currentTimer->countUp) {
279			currentTimer->nextEvent = gba->cpu.cycles + currentTimer->overflowInterval;
280		} else {
281			currentTimer->nextEvent = INT_MAX;
282		}
283		gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
284		currentTimer->oldReload = currentTimer->reload;
285		gba->timersEnabled |= 1 << timer;
286	} else if (wasEnabled && !currentTimer->enable) {
287		if (!currentTimer->countUp) {
288			gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu.cycles - currentTimer->lastEvent) >> oldPrescale);
289		}
290		gba->timersEnabled &= ~(1 << timer);
291	} else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
292		// FIXME: this might be before present
293		currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
294	}
295
296	if (currentTimer->nextEvent < gba->cpu.nextEvent) {
297		gba->cpu.nextEvent = currentTimer->nextEvent;
298	}
299};
300
301void GBAWriteIE(struct GBA* gba, uint16_t value) {
302	if (value & (1 << IRQ_SIO)) {
303		GBALog(gba, GBA_LOG_STUB, "SIO interrupts not implemented");
304	}
305
306	if (value & (1 << IRQ_KEYPAD)) {
307		GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
308	}
309
310	if (value & (1 << IRQ_GAMEPAK)) {
311		GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
312	}
313
314	if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
315		ARMRaiseIRQ(&gba->cpu);
316	}
317}
318
319void GBAWriteIME(struct GBA* gba, uint16_t value) {
320	if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
321		ARMRaiseIRQ(&gba->cpu);
322	}
323}
324
325void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
326	gba->memory.io[REG_IF >> 1] |= 1 << irq;
327
328	if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
329		ARMRaiseIRQ(&gba->cpu);
330	}
331}
332
333int GBATestIRQ(struct GBA* gba) {
334	if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
335		gba->springIRQ = 1;
336		gba->cpu.nextEvent = gba->cpu.cycles;
337		return 1;
338	}
339	return 0;
340}
341
342int GBAWaitForIRQ(struct GBA* gba) {
343	int irqs = gba->memory.io[REG_IF >> 1];
344	int newIRQs = 0;
345	gba->memory.io[REG_IF >> 1] = 0;
346	while (1) {
347		if (gba->cpu.nextEvent == INT_MAX) {
348			break;
349		} else {
350			gba->cpu.cycles = gba->cpu.nextEvent;
351			GBAProcessEvents(&gba->board.d);
352			if (gba->memory.io[REG_IF >> 1]) {
353				newIRQs = gba->memory.io[REG_IF >> 1];
354				break;
355			}
356		}
357	}
358	gba->memory.io[REG_IF >> 1] = newIRQs | irqs;
359	return newIRQs;
360}
361
362int GBAHalt(struct GBA* gba) {
363	return GBAWaitForIRQ(gba);
364}
365
366void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
367	if (gba && level < gba->logLevel) {
368		return;
369	}
370	va_list args;
371	va_start(args, format);
372	vprintf(format, args);
373	va_end(args);
374	printf("\n");
375}
376
377void GBAHitStub(struct ARMBoard* board, uint32_t opcode) {
378	struct GBABoard* gbaBoard = (struct GBABoard*) board;
379	GBALog(gbaBoard->p, GBA_LOG_STUB, "Stub opcode: %08x", opcode);
380	if (!gbaBoard->p->debugger) {
381		abort();
382	} else {
383		ARMDebuggerEnter(gbaBoard->p->debugger);
384	}
385}