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