all repos — mgba @ 3439fa5bf41eb9a8f71e73938ff209804e54eb84

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