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