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 "core/cheats.h"
12#include "util/crc32.h"
13#include "util/memory.h"
14#include "util/math.h"
15#include "util/patch.h"
16#include "util/vfs.h"
17
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(void* cpu, struct mCPUComponent* 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 GBStop(struct LR35902Core* cpu);
31
32#ifdef _3DS
33extern uint32_t* romBuffer;
34extern size_t romBufferSize;
35#endif
36
37void GBCreate(struct GB* gb) {
38 gb->d.id = GB_COMPONENT_MAGIC;
39 gb->d.init = GBInit;
40 gb->d.deinit = 0;
41}
42
43static void GBInit(void* cpu, struct mCPUComponent* component) {
44 struct GB* gb = (struct GB*) component;
45 gb->cpu = cpu;
46 gb->sync = NULL;
47
48 GBInterruptHandlerInit(&gb->cpu->irqh);
49 GBMemoryInit(gb);
50
51 gb->video.p = gb;
52 GBVideoInit(&gb->video);
53
54 gb->audio.p = gb;
55 GBAudioInit(&gb->audio, 2048, &gb->memory.io[REG_NR52], GB_AUDIO_DMG); // TODO: Remove magic constant
56
57 gb->timer.p = gb;
58
59 gb->biosVf = 0;
60 gb->romVf = 0;
61 gb->sramVf = 0;
62
63 gb->pristineRom = 0;
64 gb->pristineRomSize = 0;
65 gb->yankedRomSize = 0;
66
67 gb->stream = NULL;
68
69 gb->eiPending = INT_MAX;
70 gb->doubleSpeed = 0;
71}
72
73bool GBLoadROM(struct GB* gb, struct VFile* vf) {
74 GBUnloadROM(gb);
75 gb->romVf = vf;
76 gb->pristineRomSize = vf->size(vf);
77 vf->seek(vf, 0, SEEK_SET);
78#ifdef _3DS
79 gb->pristineRom = 0;
80 if (gb->pristineRomSize <= romBufferSize) {
81 gb->pristineRom = romBuffer;
82 vf->read(vf, romBuffer, gb->pristineRomSize);
83 }
84#else
85 gb->pristineRom = vf->map(vf, gb->pristineRomSize, MAP_READ);
86#endif
87 if (!gb->pristineRom) {
88 return false;
89 }
90 gb->yankedRomSize = 0;
91 gb->memory.rom = gb->pristineRom;
92 gb->memory.romBase = gb->memory.rom;
93 gb->memory.romSize = gb->pristineRomSize;
94 gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
95
96 // TODO: error check
97 return true;
98}
99
100bool GBLoadSave(struct GB* gb, struct VFile* vf) {
101 gb->sramVf = vf;
102 if (vf) {
103 // TODO: Do this in bank-switching code
104 if (vf->size(vf) < 0x20000) {
105 vf->truncate(vf, 0x20000);
106 }
107 gb->memory.sram = vf->map(vf, 0x20000, MAP_WRITE);
108 }
109 return gb->memory.sram;
110}
111
112void GBUnloadROM(struct GB* gb) {
113 // TODO: Share with GBAUnloadROM
114 if (gb->memory.rom && gb->memory.romBase != gb->memory.rom) {
115 free(gb->memory.romBase);
116 }
117 if (gb->memory.rom && gb->pristineRom != gb->memory.rom) {
118 if (gb->yankedRomSize) {
119 gb->yankedRomSize = 0;
120 }
121 mappedMemoryFree(gb->memory.rom, GB_SIZE_CART_MAX);
122 }
123 gb->memory.rom = 0;
124
125 if (gb->romVf) {
126#ifndef _3DS
127 gb->romVf->unmap(gb->romVf, gb->pristineRom, gb->pristineRomSize);
128#endif
129 gb->romVf->close(gb->romVf);
130 gb->pristineRom = 0;
131 gb->romVf = 0;
132 }
133
134 if (gb->sramVf) {
135 gb->sramVf->unmap(gb->sramVf, gb->memory.sram, 0x8000);
136 gb->sramVf = 0;
137 } else if (gb->memory.sram) {
138 mappedMemoryFree(gb->memory.sram, 0x8000);
139 }
140 gb->memory.sram = 0;
141}
142
143void GBLoadBIOS(struct GB* gb, struct VFile* vf) {
144 gb->biosVf = vf;
145}
146
147void GBApplyPatch(struct GB* gb, struct Patch* patch) {
148 size_t patchedSize = patch->outputSize(patch, gb->memory.romSize);
149 if (!patchedSize) {
150 return;
151 }
152 if (patchedSize > GB_SIZE_CART_MAX) {
153 patchedSize = GB_SIZE_CART_MAX;
154 }
155 gb->memory.rom = anonymousMemoryMap(GB_SIZE_CART_MAX);
156 if (!patch->applyPatch(patch, gb->pristineRom, gb->pristineRomSize, gb->memory.rom, patchedSize)) {
157 mappedMemoryFree(gb->memory.rom, patchedSize);
158 gb->memory.rom = gb->pristineRom;
159 return;
160 }
161 gb->memory.romSize = patchedSize;
162 gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
163}
164
165void GBDestroy(struct GB* gb) {
166 GBUnloadROM(gb);
167
168 GBMemoryDeinit(gb);
169}
170
171void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh) {
172 irqh->reset = GBReset;
173 irqh->processEvents = GBProcessEvents;
174 irqh->setInterrupts = GBSetInterrupts;
175 irqh->hitIllegal = GBIllegal;
176 irqh->stop = GBStop;
177 irqh->halt = GBHalt;
178}
179
180void GBReset(struct LR35902Core* cpu) {
181 struct GB* gb = (struct GB*) cpu->master;
182
183 if (gb->biosVf) {
184 gb->biosVf->seek(gb->biosVf, 0, SEEK_SET);
185 gb->memory.romBase = malloc(GB_SIZE_CART_BANK0);
186 ssize_t size = gb->biosVf->read(gb->biosVf, gb->memory.romBase, GB_SIZE_CART_BANK0);
187 uint32_t biosCrc = doCrc32(gb->memory.romBase, size);
188 switch (biosCrc) {
189 case 0x59C8598E:
190 gb->model = GB_MODEL_DMG;
191 gb->audio.style = GB_AUDIO_DMG;
192 break;
193 case 0x41884E46:
194 gb->model = GB_MODEL_CGB;
195 gb->audio.style = GB_AUDIO_CGB;
196 break;
197 default:
198 free(gb->memory.romBase);
199 gb->memory.romBase = gb->memory.rom;
200 gb->biosVf = NULL;
201 break;
202 }
203
204 memcpy(&gb->memory.romBase[size], &gb->memory.rom[size], GB_SIZE_CART_BANK0 - size);
205 if (size > 0x100) {
206 memcpy(&gb->memory.romBase[0x100], &gb->memory.rom[0x100], sizeof(struct GBCartridge));
207 }
208
209 cpu->a = 0;
210 cpu->f.packed = 0;
211 cpu->c = 0;
212 cpu->e = 0;
213 cpu->h = 0;
214 cpu->l = 0;
215 cpu->sp = 0;
216 cpu->pc = 0;
217 }
218 if (!gb->biosVf) {
219 const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
220 if (cart->cgb & 0x80) {
221 gb->model = GB_MODEL_CGB;
222 gb->audio.style = GB_AUDIO_CGB;
223 cpu->a = 0x11;
224 cpu->f.packed = 0x80;
225 cpu->c = 0;
226 cpu->e = 0x08;
227 cpu->h = 0;
228 cpu->l = 0x7C;
229 } else {
230 // TODO: SGB
231 gb->model = GB_MODEL_DMG;
232 gb->audio.style = GB_AUDIO_DMG;
233 cpu->a = 1;
234 cpu->f.packed = 0xB0;
235 cpu->c = 0x13;
236 cpu->e = 0xD8;
237 cpu->h = 1;
238 cpu->l = 0x4D;
239 }
240
241 cpu->sp = 0xFFFE;
242 cpu->pc = 0x100;
243 }
244
245 cpu->b = 0;
246 cpu->d = 0;
247
248 cpu->memory.setActiveRegion(cpu, cpu->pc);
249
250 if (gb->yankedRomSize) {
251 gb->memory.romSize = gb->yankedRomSize;
252 gb->yankedRomSize = 0;
253 }
254 GBMemoryReset(gb);
255 GBVideoReset(&gb->video);
256 GBTimerReset(&gb->timer);
257 GBIOReset(gb);
258 GBAudioReset(&gb->audio);
259}
260
261void GBUpdateIRQs(struct GB* gb) {
262 int irqs = gb->memory.ie & gb->memory.io[REG_IF];
263 if (!irqs) {
264 return;
265 }
266 gb->cpu->halted = false;
267
268 if (!gb->memory.ime) {
269 return;
270 }
271
272 if (irqs & (1 << GB_IRQ_VBLANK)) {
273 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_VBLANK);
274 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_VBLANK);
275 return;
276 }
277 if (irqs & (1 << GB_IRQ_LCDSTAT)) {
278 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_LCDSTAT);
279 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_LCDSTAT);
280 return;
281 }
282 if (irqs & (1 << GB_IRQ_TIMER)) {
283 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_TIMER);
284 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_TIMER);
285 return;
286 }
287 if (irqs & (1 << GB_IRQ_SIO)) {
288 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_SIO);
289 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_SIO);
290 return;
291 }
292 if (irqs & (1 << GB_IRQ_KEYPAD)) {
293 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_KEYPAD);
294 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_KEYPAD);
295 }
296}
297
298void GBProcessEvents(struct LR35902Core* cpu) {
299 struct GB* gb = (struct GB*) cpu->master;
300 do {
301 int32_t cycles = cpu->nextEvent;
302 int32_t nextEvent = INT_MAX;
303 int32_t testEvent;
304
305 if (gb->eiPending != INT_MAX) {
306 gb->eiPending -= cycles;
307 if (gb->eiPending <= 0) {
308 gb->memory.ime = true;
309 GBUpdateIRQs(gb);
310 gb->eiPending = INT_MAX;
311 }
312 }
313
314 testEvent = GBVideoProcessEvents(&gb->video, cycles >> gb->doubleSpeed);
315 if (testEvent != INT_MAX) {
316 testEvent <<= gb->doubleSpeed;
317 if (testEvent < nextEvent) {
318 nextEvent = testEvent;
319 }
320 }
321
322 testEvent = GBAudioProcessEvents(&gb->audio, cycles >> gb->doubleSpeed);
323 if (testEvent != INT_MAX) {
324 testEvent <<= gb->doubleSpeed;
325 if (testEvent < nextEvent) {
326 nextEvent = testEvent;
327 }
328 }
329
330 testEvent = GBTimerProcessEvents(&gb->timer, cycles);
331 if (testEvent < nextEvent) {
332 nextEvent = testEvent;
333 }
334
335 testEvent = GBMemoryProcessEvents(gb, cycles);
336 if (testEvent < nextEvent) {
337 nextEvent = testEvent;
338 }
339
340 cpu->cycles -= cycles;
341 cpu->nextEvent = nextEvent;
342
343 if (cpu->halted) {
344 cpu->cycles = cpu->nextEvent;
345 }
346 } while (cpu->cycles >= cpu->nextEvent);
347}
348
349void GBSetInterrupts(struct LR35902Core* cpu, bool enable) {
350 struct GB* gb = (struct GB*) cpu->master;
351 if (!enable) {
352 gb->memory.ime = enable;
353 gb->eiPending = INT_MAX;
354 GBUpdateIRQs(gb);
355 } else {
356 if (cpu->nextEvent > cpu->cycles + 4) {
357 cpu->nextEvent = cpu->cycles + 4;
358 }
359 gb->eiPending = cpu->cycles + 4;
360 }
361}
362
363void GBHalt(struct LR35902Core* cpu) {
364 cpu->cycles = cpu->nextEvent;
365 cpu->halted = true;
366}
367
368void GBStop(struct LR35902Core* cpu) {
369 struct GB* gb = (struct GB*) cpu->master;
370 if (gb->memory.io[REG_KEY1] & 1) {
371 gb->doubleSpeed ^= 1;
372 gb->memory.io[REG_KEY1] &= 1;
373 gb->memory.io[REG_KEY1] |= gb->doubleSpeed << 7;
374 }
375 // TODO: Actually stop
376}
377
378void GBIllegal(struct LR35902Core* cpu) {
379 // TODO
380 mLOG(GB, GAME_ERROR, "Hit illegal opcode at address %04X:%02X\n", cpu->pc, cpu->bus);
381}
382
383bool GBIsROM(struct VFile* vf) {
384 vf->seek(vf, 0x104, SEEK_SET);
385 uint8_t header[4];
386 static const uint8_t knownHeader[4] = { 0xCE, 0xED, 0x66, 0x66};
387
388 if (vf->read(vf, &header, sizeof(header)) < (ssize_t) sizeof(header)) {
389 return false;
390 }
391 if (memcmp(header, knownHeader, sizeof(header))) {
392 return false;
393 }
394 return true;
395}
396
397void GBGetGameTitle(struct GB* gb, char* out) {
398 const struct GBCartridge* cart = NULL;
399 if (gb->memory.rom) {
400 cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
401 }
402 if (gb->pristineRom) {
403 cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
404 }
405 if (!cart) {
406 return;
407 }
408 if (cart->oldLicensee != 0x33) {
409 memcpy(out, cart->titleLong, 16);
410 } else {
411 memcpy(out, cart->titleShort, 11);
412 }
413}
414
415void GBGetGameCode(struct GB* gb, char* out) {
416 memset(out, 0, 8);
417 const struct GBCartridge* cart = NULL;
418 if (gb->memory.rom) {
419 cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
420 }
421 if (gb->pristineRom) {
422 cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
423 }
424 if (!cart) {
425 return;
426 }
427 if (cart->cgb == 0xC0) {
428 memcpy(out, "CGB-????", 8);
429 } else {
430 memcpy(out, "DMG-????", 8);
431 }
432 if (cart->oldLicensee == 0x33) {
433 memcpy(&out[4], cart->maker, 4);
434 }
435}
436
437void GBFrameEnded(struct GB* gb) {
438 if (gb->cpu->components && gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
439 struct mCheatDevice* device = (struct mCheatDevice*) gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
440 size_t i;
441 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
442 struct mCheatSet* cheats = *mCheatSetsGetPointer(&device->cheats, i);
443 mCheatRefresh(device, cheats);
444 }
445 }
446}