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