mgba/src/gba/gba.c

297 lines
6.8 KiB
C
Raw Normal View History

2013-04-05 09:17:22 +00:00
#include "gba.h"
2013-04-14 20:21:21 +00:00
#include "gba-bios.h"
2013-04-16 14:42:20 +00:00
#include "gba-io.h"
2013-04-14 20:21:21 +00:00
#include "debugger.h"
#include <limits.h>
2013-04-11 05:52:46 +00:00
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2013-04-05 09:17:22 +00:00
#include <sys/mman.h>
2013-04-14 11:08:06 +00:00
2013-04-14 20:21:21 +00:00
enum {
SP_BASE_SYSTEM = 0x03FFFF00,
SP_BASE_IRQ = 0x03FFFFA0,
SP_BASE_SUPERVISOR = 0x03FFFFE0
};
2013-04-14 18:57:39 +00:00
static void GBAProcessEvents(struct ARMBoard* board);
static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
2013-04-11 05:52:46 +00:00
static void GBAHitStub(struct ARMBoard* board, uint32_t opcode);
2013-04-10 05:20:35 +00:00
2013-04-05 09:17:22 +00:00
void GBAInit(struct GBA* gba) {
2013-04-06 11:34:19 +00:00
gba->errno = GBA_NO_ERROR;
gba->errstr = 0;
2013-04-05 09:17:22 +00:00
ARMInit(&gba->cpu);
2013-04-06 11:34:19 +00:00
2013-04-06 11:20:44 +00:00
gba->memory.p = gba;
2013-04-05 09:17:22 +00:00
GBAMemoryInit(&gba->memory);
2013-04-07 08:46:28 +00:00
ARMAssociateMemory(&gba->cpu, &gba->memory.d);
2013-04-07 20:25:45 +00:00
2013-04-13 20:56:29 +00:00
gba->board.p = gba;
2013-04-07 20:25:45 +00:00
GBABoardInit(&gba->board);
ARMAssociateBoard(&gba->cpu, &gba->board.d);
gba->video.p = gba;
GBAVideoInit(&gba->video);
2013-04-19 07:04:50 +00:00
GBAIOInit(gba);
memset(gba->timers, 0, sizeof(gba->timers));
gba->springIRQ = 0;
2013-04-07 20:25:45 +00:00
ARMReset(&gba->cpu);
2013-04-05 09:17:22 +00:00
}
void GBADeinit(struct GBA* gba) {
GBAMemoryDeinit(&gba->memory);
}
2013-04-07 20:25:45 +00:00
void GBABoardInit(struct GBABoard* board) {
board->d.reset = GBABoardReset;
board->d.processEvents = GBAProcessEvents;
2013-04-14 18:57:39 +00:00
board->d.swi16 = GBASwi16;
board->d.swi32 = GBASwi32;
2013-04-11 05:52:46 +00:00
board->d.hitStub = GBAHitStub;
2013-04-07 20:25:45 +00:00
}
void GBABoardReset(struct ARMBoard* board) {
struct ARMCore* cpu = board->cpu;
ARMSetPrivilegeMode(cpu, MODE_IRQ);
cpu->gprs[ARM_SP] = SP_BASE_IRQ;
ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
}
static void GBAProcessEvents(struct ARMBoard* board) {
struct GBABoard* gbaBoard = (struct GBABoard*) board;
int32_t cycles = board->cpu->cycles;
int32_t nextEvent = INT_MAX;
int32_t testEvent;
testEvent = GBAVideoProcessEvents(&gbaBoard->p->video, cycles);
if (testEvent < nextEvent) {
nextEvent = testEvent;
}
2013-04-17 06:13:52 +00:00
testEvent = GBAMemoryProcessEvents(&gbaBoard->p->memory, cycles);
if (testEvent < nextEvent) {
nextEvent = testEvent;
}
testEvent = GBATimersProcessEvents(gbaBoard->p, cycles);
if (testEvent < nextEvent) {
nextEvent = testEvent;
}
board->cpu->cycles = 0;
board->cpu->nextEvent = nextEvent;
}
static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
int32_t nextEvent = INT_MAX;
if (gba->timersEnabled) {
struct GBATimer* timer;
struct GBATimer* nextTimer;
timer = &gba->timers[0];
if (timer->enable) {
timer->nextEvent -= cycles;
if (timer->nextEvent <= 0) {
timer->nextEvent += timer->overflowInterval;
gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
timer->oldReload = timer->reload;
if (timer->doIrq) {
GBARaiseIRQ(gba, IRQ_TIMER0);
}
nextTimer = &gba->timers[1];
if (nextTimer->countUp) {
++gba->memory.io[REG_TM1CNT_LO >> 1];
if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
nextTimer->nextEvent = 0;
}
}
}
nextEvent = timer->nextEvent;
}
timer = &gba->timers[1];
if (timer->enable) {
timer->nextEvent -= cycles;
if (timer->nextEvent <= 0) {
timer->nextEvent += timer->overflowInterval;
gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
timer->oldReload = timer->reload;
if (timer->doIrq) {
GBARaiseIRQ(gba, IRQ_TIMER1);
}
if (timer->countUp) {
timer->nextEvent = INT_MAX;
}
nextTimer = &gba->timers[2];
if (nextTimer->countUp) {
++gba->memory.io[REG_TM2CNT_LO >> 1];
if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
nextTimer->nextEvent = 0;
}
}
}
if (timer->nextEvent < nextEvent) {
nextEvent = timer->nextEvent;
}
}
timer = &gba->timers[2];
if (timer->enable) {
timer->nextEvent -= cycles;
nextEvent = timer->nextEvent;
if (timer->nextEvent <= 0) {
timer->nextEvent += timer->overflowInterval;
gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
timer->oldReload = timer->reload;
if (timer->doIrq) {
GBARaiseIRQ(gba, IRQ_TIMER2);
}
if (timer->countUp) {
timer->nextEvent = INT_MAX;
}
nextTimer = &gba->timers[3];
if (nextTimer->countUp) {
++gba->memory.io[REG_TM3CNT_LO >> 1];
if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
nextTimer->nextEvent = 0;
}
}
}
if (timer->nextEvent < nextEvent) {
nextEvent = timer->nextEvent;
}
}
timer = &gba->timers[3];
if (timer->enable) {
timer->nextEvent -= cycles;
nextEvent = timer->nextEvent;
if (timer->nextEvent <= 0) {
timer->nextEvent += timer->overflowInterval;
gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
timer->oldReload = timer->reload;
if (timer->doIrq) {
GBARaiseIRQ(gba, IRQ_TIMER3);
}
if (timer->countUp) {
timer->nextEvent = INT_MAX;
}
}
if (timer->nextEvent < nextEvent) {
nextEvent = timer->nextEvent;
}
}
}
return nextEvent;
}
void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
ARMDebuggerInit(debugger, &gba->cpu);
gba->debugger = debugger;
}
2013-04-07 08:46:28 +00:00
void GBALoadROM(struct GBA* gba, int fd) {
gba->memory.rom = mmap(0, SIZE_CART0, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, 0);
// TODO: error check
}
2013-04-16 14:42:20 +00:00
void GBAWriteIE(struct GBA* gba, uint16_t value) {
if (value & (1 << IRQ_SIO)) {
GBALog(GBA_LOG_STUB, "SIO interrupts not implemented");
}
if (value & (1 << IRQ_KEYPAD)) {
GBALog(GBA_LOG_STUB, "Keypad interrupts not implemented");
}
if (value & (1 << IRQ_GAMEPAK)) {
GBALog(GBA_LOG_STUB, "Gamepak interrupts not implemented");
}
if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
2013-04-16 14:50:34 +00:00
ARMRaiseIRQ(&gba->cpu);
2013-04-16 14:42:20 +00:00
}
}
void GBAWriteIME(struct GBA* gba, uint16_t value) {
if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
2013-04-16 14:50:34 +00:00
ARMRaiseIRQ(&gba->cpu);
2013-04-16 14:42:20 +00:00
}
}
2013-04-16 14:18:25 +00:00
void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
2013-04-16 14:42:20 +00:00
gba->memory.io[REG_IF >> 1] |= 1 << irq;
if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
2013-04-16 14:50:34 +00:00
ARMRaiseIRQ(&gba->cpu);
2013-04-16 14:42:20 +00:00
}
2013-04-16 14:18:25 +00:00
}
int GBATestIRQ(struct GBA* gba) {
if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
gba->springIRQ = 1;
gba->cpu.nextEvent = gba->cpu.cycles;
return 1;
}
return 0;
}
int GBAWaitForIRQ(struct GBA* gba) {
while (1) {
if (gba->cpu.nextEvent == INT_MAX) {
return 0;
} else {
gba->cpu.cycles = gba->cpu.nextEvent;
GBAProcessEvents(&gba->board.d);
if (gba->memory.io[REG_IF >> 1]) {
return 1;
}
}
}
}
2013-04-17 06:14:16 +00:00
int GBAHalt(struct GBA* gba) {
return GBAWaitForIRQ(gba);
}
2013-04-11 05:52:46 +00:00
void GBALog(int level, const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
printf("\n");
}
void GBAHitStub(struct ARMBoard* board, uint32_t opcode) {
GBALog(GBA_LOG_STUB, "Stub opcode: %08x", opcode);
2013-04-13 20:56:29 +00:00
struct GBABoard* gbaBoard = (struct GBABoard*) board;
if (!gbaBoard->p->debugger) {
abort();
} else {
ARMDebuggerEnter(gbaBoard->p->debugger);
}
2013-04-11 05:52:46 +00:00
}