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