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 if (gb->sramVf == gb->sramRealVf) {
131 return;
132 }
133 GBSramDeinit(gb);
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 } else {
340 nextEvent = gb->eiPending;
341 }
342 }
343
344 testEvent = GBVideoProcessEvents(&gb->video, cycles >> gb->doubleSpeed);
345 if (testEvent != INT_MAX) {
346 testEvent <<= gb->doubleSpeed;
347 if (testEvent < nextEvent) {
348 nextEvent = testEvent;
349 }
350 }
351
352 testEvent = GBAudioProcessEvents(&gb->audio, cycles >> gb->doubleSpeed);
353 if (testEvent != INT_MAX) {
354 testEvent <<= gb->doubleSpeed;
355 if (testEvent < nextEvent) {
356 nextEvent = testEvent;
357 }
358 }
359
360 testEvent = GBTimerProcessEvents(&gb->timer, cycles);
361 if (testEvent < nextEvent) {
362 nextEvent = testEvent;
363 }
364
365 testEvent = GBSIOProcessEvents(&gb->sio, cycles);
366 if (testEvent < nextEvent) {
367 nextEvent = testEvent;
368 }
369
370 testEvent = GBMemoryProcessEvents(gb, cycles);
371 if (testEvent < nextEvent) {
372 nextEvent = testEvent;
373 }
374
375 cpu->cycles -= cycles;
376 cpu->nextEvent = nextEvent;
377
378 if (cpu->halted) {
379 cpu->cycles = cpu->nextEvent;
380 }
381 } while (cpu->cycles >= cpu->nextEvent);
382}
383
384void GBSetInterrupts(struct LR35902Core* cpu, bool enable) {
385 struct GB* gb = (struct GB*) cpu->master;
386 if (!enable) {
387 gb->memory.ime = enable;
388 gb->eiPending = INT_MAX;
389 GBUpdateIRQs(gb);
390 } else {
391 if (cpu->nextEvent > cpu->cycles + 4) {
392 cpu->nextEvent = cpu->cycles + 4;
393 }
394 gb->eiPending = cpu->cycles + 4;
395 }
396}
397
398void GBHalt(struct LR35902Core* cpu) {
399 if (!cpu->irqPending) {
400 cpu->cycles = cpu->nextEvent;
401 cpu->halted = true;
402 }
403}
404
405void GBStop(struct LR35902Core* cpu) {
406 struct GB* gb = (struct GB*) cpu->master;
407 if (gb->memory.io[REG_KEY1] & 1) {
408 gb->doubleSpeed ^= 1;
409 gb->memory.io[REG_KEY1] &= 1;
410 gb->memory.io[REG_KEY1] |= gb->doubleSpeed << 7;
411 }
412 // TODO: Actually stop
413}
414
415void GBIllegal(struct LR35902Core* cpu) {
416 // TODO
417 mLOG(GB, GAME_ERROR, "Hit illegal opcode at address %04X:%02X\n", cpu->pc, cpu->bus);
418}
419
420bool GBIsROM(struct VFile* vf) {
421 vf->seek(vf, 0x104, SEEK_SET);
422 uint8_t header[4];
423 static const uint8_t knownHeader[4] = { 0xCE, 0xED, 0x66, 0x66};
424
425 if (vf->read(vf, &header, sizeof(header)) < (ssize_t) sizeof(header)) {
426 return false;
427 }
428 if (memcmp(header, knownHeader, sizeof(header))) {
429 return false;
430 }
431 return true;
432}
433
434void GBGetGameTitle(struct GB* gb, char* out) {
435 const struct GBCartridge* cart = NULL;
436 if (gb->memory.rom) {
437 cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
438 }
439 if (gb->pristineRom) {
440 cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
441 }
442 if (!cart) {
443 return;
444 }
445 if (cart->oldLicensee != 0x33) {
446 memcpy(out, cart->titleLong, 16);
447 } else {
448 memcpy(out, cart->titleShort, 11);
449 }
450}
451
452void GBGetGameCode(struct GB* gb, char* out) {
453 memset(out, 0, 8);
454 const struct GBCartridge* cart = NULL;
455 if (gb->memory.rom) {
456 cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
457 }
458 if (gb->pristineRom) {
459 cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
460 }
461 if (!cart) {
462 return;
463 }
464 if (cart->cgb == 0xC0) {
465 memcpy(out, "CGB-????", 8);
466 } else {
467 memcpy(out, "DMG-????", 8);
468 }
469 if (cart->oldLicensee == 0x33) {
470 memcpy(&out[4], cart->maker, 4);
471 }
472}
473
474void GBFrameEnded(struct GB* gb) {
475 if (gb->cpu->components && gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
476 struct mCheatDevice* device = (struct mCheatDevice*) gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
477 size_t i;
478 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
479 struct mCheatSet* cheats = *mCheatSetsGetPointer(&device->cheats, i);
480 mCheatRefresh(device, cheats);
481 }
482 }
483}