all repos — mgba @ 54fffb7fff09bc5daca6f995b041b185f0cf2524

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 <sys/mman.h>
 13
 14enum {
 15	SP_BASE_SYSTEM = 0x03FFFF00,
 16	SP_BASE_IRQ = 0x03FFFFA0,
 17	SP_BASE_SUPERVISOR = 0x03FFFFE0
 18};
 19
 20static void GBAProcessEvents(struct ARMBoard* board);
 21static void GBAHitStub(struct ARMBoard* board, uint32_t opcode);
 22
 23void GBAInit(struct GBA* gba) {
 24	gba->errno = GBA_NO_ERROR;
 25	gba->errstr = 0;
 26
 27	ARMInit(&gba->cpu);
 28
 29	gba->memory.p = gba;
 30	GBAMemoryInit(&gba->memory);
 31	ARMAssociateMemory(&gba->cpu, &gba->memory.d);
 32
 33	gba->board.p = gba;
 34	GBABoardInit(&gba->board);
 35	ARMAssociateBoard(&gba->cpu, &gba->board.d);
 36
 37	gba->video.p = gba;
 38	GBAVideoInit(&gba->video);
 39
 40	gba->springIRQ = 0;
 41
 42	ARMReset(&gba->cpu);
 43}
 44
 45void GBADeinit(struct GBA* gba) {
 46	GBAMemoryDeinit(&gba->memory);
 47}
 48
 49void GBABoardInit(struct GBABoard* board) {
 50	board->d.reset = GBABoardReset;
 51	board->d.processEvents = GBAProcessEvents;
 52	board->d.swi16 = GBASwi16;
 53	board->d.swi32 = GBASwi32;
 54	board->d.hitStub = GBAHitStub;
 55}
 56
 57void GBABoardReset(struct ARMBoard* board) {
 58	struct ARMCore* cpu = board->cpu;
 59	ARMSetPrivilegeMode(cpu, MODE_IRQ);
 60	cpu->gprs[ARM_SP] = SP_BASE_IRQ;
 61	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
 62	cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
 63	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
 64	cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
 65}
 66
 67static void GBAProcessEvents(struct ARMBoard* board) {
 68	struct GBABoard* gbaBoard = (struct GBABoard*) board;
 69	int32_t cycles = board->cpu->cycles;
 70	int32_t nextEvent = INT_MAX;
 71	int32_t testEvent;
 72
 73	testEvent = GBAVideoProcessEvents(&gbaBoard->p->video, cycles);
 74	if (testEvent < nextEvent) {
 75		nextEvent = testEvent;
 76	}
 77
 78	testEvent = GBAMemoryProcessEvents(&gbaBoard->p->memory, cycles);
 79	if (testEvent < nextEvent) {
 80		nextEvent = testEvent;
 81	}
 82
 83	board->cpu->cycles = 0;
 84	board->cpu->nextEvent = nextEvent;
 85}
 86
 87void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
 88	ARMDebuggerInit(debugger, &gba->cpu);
 89	gba->debugger = debugger;
 90}
 91
 92void GBALoadROM(struct GBA* gba, int fd) {
 93	gba->memory.rom = mmap(0, SIZE_CART0, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, 0);
 94	// TODO: error check
 95}
 96
 97void GBAWriteIE(struct GBA* gba, uint16_t value) {
 98	if (value & ((1 << IRQ_TIMER0) | (1 << IRQ_TIMER1) | (1 << IRQ_TIMER2) | (1 << IRQ_TIMER3))) {
 99		GBALog(GBA_LOG_STUB, "Timer interrupts not implemented");
100	}
101
102	if (value & (1 << IRQ_SIO)) {
103		GBALog(GBA_LOG_STUB, "SIO interrupts not implemented");
104	}
105
106	if (value & (1 << IRQ_KEYPAD)) {
107		GBALog(GBA_LOG_STUB, "Keypad interrupts not implemented");
108	}
109
110	if (value & (1 << IRQ_GAMEPAK)) {
111		GBALog(GBA_LOG_STUB, "Gamepak interrupts not implemented");
112	}
113
114	if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
115		ARMRaiseIRQ(&gba->cpu);
116	}
117}
118
119void GBAWriteIME(struct GBA* gba, uint16_t value) {
120	if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
121		ARMRaiseIRQ(&gba->cpu);
122	}
123}
124
125void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
126	gba->memory.io[REG_IF >> 1] |= 1 << irq;
127
128	if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
129		ARMRaiseIRQ(&gba->cpu);
130	}
131}
132
133int GBATestIRQ(struct GBA* gba) {
134	if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
135		gba->springIRQ = 1;
136		gba->cpu.nextEvent = gba->cpu.cycles;
137		return 1;
138	}
139	return 0;
140}
141
142int GBAWaitForIRQ(struct GBA* gba) {
143	while (1) {
144		if (gba->cpu.nextEvent == INT_MAX) {
145			return 0;
146		} else {
147			gba->cpu.cycles = gba->cpu.nextEvent;
148			GBAProcessEvents(&gba->board.d);
149			if (gba->memory.io[REG_IF >> 1]) {
150				return 1;
151			}
152		}
153	}
154}
155
156int GBAHalt(struct GBA* gba) {
157	return GBAWaitForIRQ(gba);
158}
159
160void GBALog(int level, const char* format, ...) {
161	va_list args;
162	va_start(args, format);
163	vprintf(format, args);
164	va_end(args);
165	printf("\n");
166}
167
168void GBAHitStub(struct ARMBoard* board, uint32_t opcode) {
169	GBALog(GBA_LOG_STUB, "Stub opcode: %08x", opcode);
170	struct GBABoard* gbaBoard = (struct GBABoard*) board;
171	if (!gbaBoard->p->debugger) {
172		abort();
173	} else {
174		ARMDebuggerEnter(gbaBoard->p->debugger);
175	}
176}