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