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