all repos — mgba @ 633a87269ad02928d7d2509ce34eeb2cdd99eb3e

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