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