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 CGB_LR35902_FREQUENCY = 0x800000;
18const uint32_t SGB_LR35902_FREQUENCY = 0x418B1E;
19
20const uint32_t GB_COMPONENT_MAGIC = 0x400000;
21
22mLOG_DEFINE_CATEGORY(GB, "GB");
23
24static void GBInit(void* cpu, struct mCPUComponent* component);
25static void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh);
26static void GBProcessEvents(struct LR35902Core* cpu);
27static void GBSetInterrupts(struct LR35902Core* cpu, bool enable);
28static void GBIllegal(struct LR35902Core* cpu);
29static void GBHitStub(struct LR35902Core* cpu);
30
31#ifdef _3DS
32extern uint32_t* romBuffer;
33extern size_t romBufferSize;
34#endif
35
36void GBCreate(struct GB* gb) {
37 gb->d.id = GB_COMPONENT_MAGIC;
38 gb->d.init = GBInit;
39 gb->d.deinit = 0;
40}
41
42static void GBInit(void* cpu, struct mCPUComponent* component) {
43 struct GB* gb = (struct GB*) component;
44 gb->cpu = cpu;
45
46 GBInterruptHandlerInit(&gb->cpu->irqh);
47 GBMemoryInit(gb);
48
49 gb->video.p = gb;
50 GBVideoInit(&gb->video);
51
52 gb->audio.p = gb;
53 GBAudioInit(&gb->audio, 2048, &gb->memory.io[REG_NR52], GB_AUDIO_DMG); // TODO: Remove magic constant
54
55 gb->timer.p = gb;
56
57 gb->romVf = 0;
58 gb->sramVf = 0;
59
60 gb->pristineRom = 0;
61 gb->pristineRomSize = 0;
62 gb->yankedRomSize = 0;
63
64 gb->eiPending = false;
65}
66
67bool GBLoadROM(struct GB* gb, struct VFile* vf) {
68 GBUnloadROM(gb);
69 gb->romVf = vf;
70 gb->pristineRomSize = vf->size(vf);
71 vf->seek(vf, 0, SEEK_SET);
72#ifdef _3DS
73 gb->pristineRom = 0;
74 if (gb->pristineRomSize <= romBufferSize) {
75 gb->pristineRom = romBuffer;
76 vf->read(vf, romBuffer, gb->pristineRomSize);
77 }
78#else
79 gb->pristineRom = vf->map(vf, gb->pristineRomSize, MAP_READ);
80#endif
81 if (!gb->pristineRom) {
82 return false;
83 }
84 gb->yankedRomSize = 0;
85 gb->memory.rom = gb->pristineRom;
86 gb->memory.romSize = gb->pristineRomSize;
87 gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
88
89 // TODO: error check
90 return true;
91}
92
93bool GBLoadSave(struct GB* gb, struct VFile* vf) {
94 gb->sramVf = vf;
95 if (vf) {
96 // TODO: Do this in bank-switching code
97 if (vf->size(vf) < 0x20000) {
98 vf->truncate(vf, 0x20000);
99 }
100 gb->memory.sram = vf->map(vf, 0x20000, MAP_WRITE);
101 } else {
102 gb->memory.sram = anonymousMemoryMap(0x20000);
103 }
104 return gb->memory.sram;
105}
106
107void GBUnloadROM(struct GB* gb) {
108 // TODO: Share with GBAUnloadROM
109 if (gb->memory.rom && gb->pristineRom != gb->memory.rom) {
110 if (gb->yankedRomSize) {
111 gb->yankedRomSize = 0;
112 }
113 mappedMemoryFree(gb->memory.rom, 0x400000);
114 }
115 gb->memory.rom = 0;
116
117 if (gb->romVf) {
118#ifndef _3DS
119 gb->romVf->unmap(gb->romVf, gb->pristineRom, gb->pristineRomSize);
120#endif
121 gb->pristineRom = 0;
122 gb->romVf = 0;
123 }
124
125 if (gb->sramVf) {
126 gb->sramVf->unmap(gb->sramVf, gb->memory.sram, 0x8000);
127 } else if (gb->memory.sram) {
128 mappedMemoryFree(gb->memory.sram, 0x8000);
129 }
130 gb->memory.sram = 0;
131}
132
133void GBApplyPatch(struct GB* gb, struct Patch* patch) {
134 size_t patchedSize = patch->outputSize(patch, gb->memory.romSize);
135 if (!patchedSize) {
136 return;
137 }
138 if (patchedSize > 0x400000) {
139 patchedSize = 0x400000;
140 }
141 gb->memory.rom = anonymousMemoryMap(0x400000);
142 if (!patch->applyPatch(patch, gb->pristineRom, gb->pristineRomSize, gb->memory.rom, patchedSize)) {
143 mappedMemoryFree(gb->memory.rom, patchedSize);
144 gb->memory.rom = gb->pristineRom;
145 return;
146 }
147 gb->memory.romSize = patchedSize;
148 gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
149}
150
151void GBDestroy(struct GB* gb) {
152 GBUnloadROM(gb);
153
154 GBMemoryDeinit(gb);
155}
156
157void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh) {
158 irqh->reset = GBReset;
159 irqh->processEvents = GBProcessEvents;
160 irqh->setInterrupts = GBSetInterrupts;
161 irqh->hitIllegal = GBIllegal;
162 irqh->hitStub = GBHitStub;
163 irqh->halt = GBHalt;
164}
165
166void GBReset(struct LR35902Core* cpu) {
167 cpu->a = 1;
168 cpu->f.packed = 0xB0;
169 cpu->b = 0;
170 cpu->c = 0x13;
171 cpu->d = 0;
172 cpu->e = 0xD8;
173 cpu->h = 1;
174 cpu->l = 0x4D;
175 cpu->sp = 0xFFFE;
176 cpu->pc = 0x100;
177
178 struct GB* gb = (struct GB*) cpu->master;
179
180 if (gb->yankedRomSize) {
181 gb->memory.romSize = gb->yankedRomSize;
182 gb->yankedRomSize = 0;
183 }
184 GBMemoryReset(gb);
185 GBVideoReset(&gb->video);
186 GBTimerReset(&gb->timer);
187 GBIOReset(gb);
188 GBAudioReset(&gb->audio);
189}
190
191void GBUpdateIRQs(struct GB* gb) {
192 int irqs = gb->memory.ie & gb->memory.io[REG_IF];
193 if (!irqs) {
194 return;
195 }
196 gb->cpu->halted = false;
197
198 if (!gb->memory.ime) {
199 return;
200 }
201
202 if (irqs & (1 << GB_IRQ_VBLANK)) {
203 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_VBLANK);
204 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_VBLANK);
205 return;
206 }
207 if (irqs & (1 << GB_IRQ_LCDSTAT)) {
208 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_LCDSTAT);
209 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_LCDSTAT);
210 return;
211 }
212 if (irqs & (1 << GB_IRQ_TIMER)) {
213 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_TIMER);
214 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_TIMER);
215 return;
216 }
217 if (irqs & (1 << GB_IRQ_SIO)) {
218 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_SIO);
219 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_SIO);
220 return;
221 }
222 if (irqs & (1 << GB_IRQ_KEYPAD)) {
223 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_KEYPAD);
224 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_KEYPAD);
225 }
226}
227
228void GBProcessEvents(struct LR35902Core* cpu) {
229 struct GB* gb = (struct GB*) cpu->master;
230 do {
231 int32_t cycles = cpu->nextEvent;
232 int32_t nextEvent = INT_MAX;
233 int32_t testEvent;
234
235 if (gb->eiPending) {
236 gb->eiPending -= cycles;
237 if (gb->eiPending <= 0) {
238 gb->memory.ime = true;
239 GBUpdateIRQs(gb);
240 gb->eiPending = 0;
241 }
242 }
243
244 testEvent = GBVideoProcessEvents(&gb->video, cycles);
245 if (testEvent < nextEvent) {
246 nextEvent = testEvent;
247 }
248
249 testEvent = GBAudioProcessEvents(&gb->audio, cycles);
250 if (testEvent < nextEvent) {
251 nextEvent = testEvent;
252 }
253
254 testEvent = GBTimerProcessEvents(&gb->timer, cycles);
255 if (testEvent < nextEvent) {
256 nextEvent = testEvent;
257 }
258
259 testEvent = GBMemoryProcessEvents(gb, cycles);
260 if (testEvent < nextEvent) {
261 nextEvent = testEvent;
262 }
263
264 cpu->cycles -= cycles;
265 cpu->nextEvent = nextEvent;
266
267 if (cpu->halted) {
268 cpu->cycles = cpu->nextEvent;
269 }
270 } while (cpu->cycles >= cpu->nextEvent);
271}
272
273void GBSetInterrupts(struct LR35902Core* cpu, bool enable) {
274 struct GB* gb = (struct GB*) cpu->master;
275 if (!enable) {
276 gb->memory.ime = enable;
277 gb->eiPending = 0;
278 GBUpdateIRQs(gb);
279 } else {
280 if (cpu->nextEvent > cpu->cycles + 4) {
281 cpu->nextEvent = cpu->cycles + 4;
282 }
283 gb->eiPending = cpu->cycles + 4;
284 }
285}
286
287void GBHalt(struct LR35902Core* cpu) {
288 cpu->cycles = cpu->nextEvent;
289 cpu->halted = true;
290}
291
292void GBIllegal(struct LR35902Core* cpu) {
293 // TODO
294 mLOG(GB, GAME_ERROR, "Hit illegal opcode at address %04X:%02X\n", cpu->pc, cpu->bus);
295}
296
297void GBHitStub(struct LR35902Core* cpu) {
298 // TODO
299 mLOG(GB, STUB, "Hit stub at address %04X:%02X\n", cpu->pc, cpu->bus);
300}
301
302bool GBIsROM(struct VFile* vf) {
303 vf->seek(vf, 0x104, SEEK_SET);
304 uint8_t header[4];
305 static const uint8_t knownHeader[4] = { 0xCE, 0xED, 0x66, 0x66};
306
307 if (vf->read(vf, &header, sizeof(header)) < (ssize_t) sizeof(header)) {
308 return false;
309 }
310 if (memcmp(header, knownHeader, sizeof(header))) {
311 return false;
312 }
313 return true;
314}
315
316void GBGetGameTitle(struct GB* gb, char* out) {
317 const struct GBCartridge* cart = NULL;
318 if (gb->memory.rom) {
319 cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
320 }
321 if (gb->pristineRom) {
322 cart = (const struct GBCartridge*) &gb->pristineRom[0x100];
323 }
324 if (!cart) {
325 return;
326 }
327 if (cart->oldLicensee != 0x33) {
328 memcpy(out, cart->titleLong, 16);
329 } else {
330 memcpy(out, cart->titleShort, 11);
331 }
332}