src/gb/gb.c (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "gb.h"
7
8#include "gb/io.h"
9
10#include "core/core.h"
11#include "util/crc32.h"
12#include "util/memory.h"
13#include "util/math.h"
14#include "util/patch.h"
15#include "util/vfs.h"
16
17const uint32_t DMG_LR35902_FREQUENCY = 0x400000;
18const uint32_t CGB_LR35902_FREQUENCY = 0x800000;
19const uint32_t SGB_LR35902_FREQUENCY = 0x418B1E;
20
21const uint32_t GB_COMPONENT_MAGIC = 0x400000;
22
23mLOG_DEFINE_CATEGORY(GB, "GB");
24
25static void GBInit(struct LR35902Core* cpu, struct LR35902Component* component);
26static void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh);
27static void GBProcessEvents(struct LR35902Core* cpu);
28static void GBSetInterrupts(struct LR35902Core* cpu, bool enable);
29static void GBIllegal(struct LR35902Core* cpu);
30static void GBHitStub(struct LR35902Core* cpu);
31
32void GBCreate(struct GB* gb) {
33 gb->d.id = GB_COMPONENT_MAGIC;
34 gb->d.init = GBInit;
35 gb->d.deinit = 0;
36}
37
38static void GBInit(struct LR35902Core* cpu, struct LR35902Component* component) {
39 struct GB* gb = (struct GB*) component;
40 gb->cpu = cpu;
41
42 GBInterruptHandlerInit(&cpu->irqh);
43 GBMemoryInit(gb);
44
45 gb->video.p = gb;
46 GBVideoInit(&gb->video);
47
48 gb->audio.p = gb;
49 GBAudioInit(&gb->audio, 2048); // TODO: Remove magic constant
50
51 gb->timer.p = gb;
52
53 gb->romVf = 0;
54 gb->sramVf = 0;
55
56 gb->pristineRom = 0;
57 gb->pristineRomSize = 0;
58 gb->yankedRomSize = 0;
59
60 gb->diPending = false;
61}
62
63bool GBLoadROM(struct GB* gb, struct VFile* vf, struct VFile* sav, const char* fname) {
64 GBUnloadROM(gb);
65 gb->romVf = vf;
66 gb->pristineRomSize = vf->size(vf);
67 vf->seek(vf, 0, SEEK_SET);
68#ifdef _3DS
69 gb->pristineRom = 0;
70 if (gb->pristineRomSize <= romBufferSize) {
71 gb->pristineRom = romBuffer;
72 vf->read(vf, romBuffer, gb->pristineRomSize);
73 }
74#else
75 gb->pristineRom = vf->map(vf, gb->pristineRomSize, MAP_READ);
76#endif
77 if (!gb->pristineRom) {
78 return false;
79 }
80 gb->yankedRomSize = 0;
81 gb->memory.rom = gb->pristineRom;
82 gb->activeFile = fname;
83 gb->memory.romSize = gb->pristineRomSize;
84 gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
85 gb->sramVf = sav;
86 if (sav) {
87 // TODO: Do this in bank-switching code
88 if (sav->size(sav) < 0x20000) {
89 sav->truncate(sav, 0x20000);
90 }
91 gb->memory.sram = sav->map(sav, 0x20000, MAP_WRITE);
92 } else {
93 gb->memory.sram = anonymousMemoryMap(0x20000);
94 }
95 return true;
96 // TODO: error check
97}
98
99void GBUnloadROM(struct GB* gb) {
100 // TODO: Share with GBAUnloadROM
101 if (gb->memory.rom && gb->pristineRom != gb->memory.rom) {
102 if (gb->yankedRomSize) {
103 gb->yankedRomSize = 0;
104 }
105 mappedMemoryFree(gb->memory.rom, 0x400000);
106 }
107 gb->memory.rom = 0;
108
109 if (gb->romVf) {
110#ifndef _3DS
111 gb->romVf->unmap(gb->romVf, gb->pristineRom, gb->pristineRomSize);
112#endif
113 gb->pristineRom = 0;
114 gb->romVf = 0;
115 }
116
117 if (gb->sramVf) {
118 gb->sramVf->unmap(gb->sramVf, gb->memory.sram, 0x8000);
119 } else if (gb->memory.sram) {
120 mappedMemoryFree(gb->memory.sram, 0x8000);
121 }
122 gb->memory.sram = 0;
123}
124
125void GBDestroy(struct GB* gb) {
126 GBUnloadROM(gb);
127
128 GBMemoryDeinit(gb);
129}
130
131void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh) {
132 irqh->reset = GBReset;
133 irqh->processEvents = GBProcessEvents;
134 irqh->setInterrupts = GBSetInterrupts;
135 irqh->hitIllegal = GBIllegal;
136 irqh->hitStub = GBHitStub;
137 irqh->halt = GBHalt;
138}
139
140void GBReset(struct LR35902Core* cpu) {
141 cpu->a = 1;
142 cpu->f.packed = 0xB0;
143 cpu->b = 0;
144 cpu->c = 0x13;
145 cpu->d = 0;
146 cpu->e = 0xD8;
147 cpu->h = 1;
148 cpu->l = 0x4D;
149 cpu->sp = 0xFFFE;
150 cpu->pc = 0x100;
151
152 struct GB* gb = (struct GB*) cpu->master;
153
154 if (gb->yankedRomSize) {
155 gb->memory.romSize = gb->yankedRomSize;
156 gb->yankedRomSize = 0;
157 }
158 GBMemoryReset(gb);
159 GBVideoReset(&gb->video);
160 GBTimerReset(&gb->timer);
161 GBIOReset(gb);
162 GBAudioReset(&gb->audio);
163}
164
165void GBUpdateIRQs(struct GB* gb) {
166 int irqs = gb->memory.ie & gb->memory.io[REG_IF];
167 if (!irqs) {
168 return;
169 }
170 gb->cpu->halted = false;
171
172 if (!gb->memory.ime) {
173 return;
174 }
175
176 if (irqs & (1 << GB_IRQ_VBLANK)) {
177 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_VBLANK);
178 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_VBLANK);
179 return;
180 }
181 if (irqs & (1 << GB_IRQ_LCDSTAT)) {
182 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_LCDSTAT);
183 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_LCDSTAT);
184 return;
185 }
186 if (irqs & (1 << GB_IRQ_TIMER)) {
187 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_TIMER);
188 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_TIMER);
189 return;
190 }
191 if (irqs & (1 << GB_IRQ_SIO)) {
192 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_SIO);
193 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_SIO);
194 return;
195 }
196 if (irqs & (1 << GB_IRQ_KEYPAD)) {
197 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_KEYPAD);
198 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_KEYPAD);
199 }
200}
201
202void GBProcessEvents(struct LR35902Core* cpu) {
203 struct GB* gb = (struct GB*) cpu->master;
204 do {
205 int32_t cycles = cpu->nextEvent;
206 int32_t nextEvent = INT_MAX;
207 int32_t testEvent;
208
209 if (gb->diPending) {
210 gb->memory.ime = false;
211 gb->diPending = false;
212 }
213
214 testEvent = GBVideoProcessEvents(&gb->video, cycles);
215 if (testEvent < nextEvent) {
216 nextEvent = testEvent;
217 }
218
219 testEvent = GBAudioProcessEvents(&gb->audio, cycles);
220 if (testEvent < nextEvent) {
221 nextEvent = testEvent;
222 }
223
224 testEvent = GBTimerProcessEvents(&gb->timer, cycles);
225 if (testEvent < nextEvent) {
226 nextEvent = testEvent;
227 }
228
229 testEvent = GBMemoryProcessEvents(gb, cycles);
230 if (testEvent < nextEvent) {
231 nextEvent = testEvent;
232 }
233
234 cpu->cycles -= cycles;
235 cpu->nextEvent = nextEvent;
236
237 if (cpu->halted) {
238 cpu->cycles = cpu->nextEvent;
239 }
240 } while (cpu->cycles >= cpu->nextEvent);
241}
242
243void GBSetInterrupts(struct LR35902Core* cpu, bool enable) {
244 struct GB* gb = (struct GB*) cpu->master;
245 if (enable) {
246 gb->memory.ime = enable;
247 GBUpdateIRQs(gb);
248 } else {
249 if (cpu->nextEvent > 4) {
250 cpu->nextEvent = 4;
251 }
252 gb->diPending = true;
253 }
254}
255
256void GBHalt(struct LR35902Core* cpu) {
257 cpu->cycles = cpu->nextEvent;
258 cpu->halted = true;
259}
260
261void GBIllegal(struct LR35902Core* cpu) {
262 // TODO
263 mLOG(GB, GAME_ERROR, "Hit illegal opcode at address %04X:%02X\n", cpu->pc, cpu->bus);
264}
265
266void GBHitStub(struct LR35902Core* cpu) {
267 // TODO
268 mLOG(GB, STUB, "Hit stub at address %04X:%02X\n", cpu->pc, cpu->bus);
269}
270
271bool GBIsROM(struct VFile* vf) {
272 vf->seek(vf, 0x104, SEEK_SET);
273 uint8_t header[4];
274 static const uint8_t knownHeader[4] = { 0xCE, 0xED, 0x66, 0x66};
275
276 if (vf->read(vf, &header, sizeof(header)) < (ssize_t) sizeof(header)) {
277 return false;
278 }
279 if (memcmp(header, knownHeader, sizeof(header))) {
280 return false;
281 }
282 return true;
283}