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