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