src/gb/core.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 "core.h"
7
8#include "core/core.h"
9#include "gb/cheats.h"
10#include "gb/cli.h"
11#include "gb/gb.h"
12#include "gb/mbc.h"
13#include "gb/overrides.h"
14#include "gb/renderers/software.h"
15#include "gb/serialize.h"
16#include "lr35902/debugger/debugger.h"
17#include "util/crc32.h"
18#include "util/memory.h"
19#include "util/patch.h"
20#include "util/vfs.h"
21
22struct GBCore {
23 struct mCore d;
24 struct GBVideoSoftwareRenderer renderer;
25 uint8_t keys;
26 struct mCPUComponent* components[CPU_COMPONENT_MAX];
27 const struct Configuration* overrides;
28 struct mDebuggerPlatform* debuggerPlatform;
29 struct mCheatDevice* cheatDevice;
30};
31
32static bool _GBCoreInit(struct mCore* core) {
33 struct GBCore* gbcore = (struct GBCore*) core;
34
35 struct LR35902Core* cpu = anonymousMemoryMap(sizeof(struct LR35902Core));
36 struct GB* gb = anonymousMemoryMap(sizeof(struct GB));
37 if (!cpu || !gb) {
38 free(cpu);
39 free(gb);
40 return false;
41 }
42 core->cpu = cpu;
43 core->board = gb;
44 gbcore->overrides = NULL;
45 gbcore->debuggerPlatform = NULL;
46 gbcore->cheatDevice = NULL;
47
48 GBCreate(gb);
49 memset(gbcore->components, 0, sizeof(gbcore->components));
50 LR35902SetComponents(cpu, &gb->d, CPU_COMPONENT_MAX, gbcore->components);
51 LR35902Init(cpu);
52
53 GBVideoSoftwareRendererCreate(&gbcore->renderer);
54 gbcore->renderer.outputBuffer = NULL;
55
56 gbcore->keys = 0;
57 gb->keySource = &gbcore->keys;
58
59#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
60 mDirectorySetInit(&core->dirs);
61#endif
62
63 return true;
64}
65
66static void _GBCoreDeinit(struct mCore* core) {
67 LR35902Deinit(core->cpu);
68 GBDestroy(core->board);
69 mappedMemoryFree(core->cpu, sizeof(struct LR35902Core));
70 mappedMemoryFree(core->board, sizeof(struct GB));
71#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
72 mDirectorySetDeinit(&core->dirs);
73#endif
74
75 struct GBCore* gbcore = (struct GBCore*) core;
76 free(gbcore->debuggerPlatform);
77 if (gbcore->cheatDevice) {
78 mCheatDeviceDestroy(gbcore->cheatDevice);
79 }
80 free(gbcore->cheatDevice);
81 mCoreConfigFreeOpts(&core->opts);
82 free(core);
83}
84
85static enum mPlatform _GBCorePlatform(const struct mCore* core) {
86 UNUSED(core);
87 return PLATFORM_GB;
88}
89
90static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
91 struct GB* gb = core->board;
92 gb->sync = sync;
93}
94
95static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
96 UNUSED(config);
97
98 struct GB* gb = core->board;
99 if (core->opts.mute) {
100 gb->audio.masterVolume = 0;
101 } else {
102 gb->audio.masterVolume = core->opts.volume;
103 }
104 gb->video.frameskip = core->opts.frameskip;
105
106#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
107 struct GBCore* gbcore = (struct GBCore*) core;
108 gbcore->overrides = mCoreConfigGetOverridesConst(config);
109#endif
110}
111
112static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
113 UNUSED(core);
114 *width = GB_VIDEO_HORIZONTAL_PIXELS;
115 *height = GB_VIDEO_VERTICAL_PIXELS;
116}
117
118static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
119 struct GBCore* gbcore = (struct GBCore*) core;
120 gbcore->renderer.outputBuffer = buffer;
121 gbcore->renderer.outputBufferStride = stride;
122}
123
124static void _GBCoreGetPixels(struct mCore* core, const void** buffer, size_t* stride) {
125 struct GBCore* gbcore = (struct GBCore*) core;
126 gbcore->renderer.d.getPixels(&gbcore->renderer.d, stride, buffer);
127}
128
129static void _GBCorePutPixels(struct mCore* core, const void* buffer, size_t stride) {
130 struct GBCore* gbcore = (struct GBCore*) core;
131 gbcore->renderer.d.putPixels(&gbcore->renderer.d, stride, buffer);
132}
133
134static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
135 struct GB* gb = core->board;
136 switch (ch) {
137 case 0:
138 return gb->audio.left;
139 case 1:
140 return gb->audio.right;
141 default:
142 return NULL;
143 }
144}
145
146static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
147 struct GB* gb = core->board;
148 GBAudioResizeBuffer(&gb->audio, samples);
149}
150
151static size_t _GBCoreGetAudioBufferSize(struct mCore* core) {
152 struct GB* gb = core->board;
153 return gb->audio.samples;
154}
155
156static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
157 struct GB* gb = core->board;
158 gb->stream = stream;
159 if (stream && stream->videoDimensionsChanged) {
160 stream->videoDimensionsChanged(stream, GB_VIDEO_HORIZONTAL_PIXELS, GB_VIDEO_VERTICAL_PIXELS);
161 }
162}
163
164static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
165 return GBLoadROM(core->board, vf);
166}
167
168static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
169 UNUSED(type);
170 GBLoadBIOS(core->board, vf);
171 return true;
172}
173
174static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
175 return GBLoadSave(core->board, vf);
176}
177
178static bool _GBCoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
179 struct GB* gb = core->board;
180 GBSavedataMask(gb, vf, false);
181 return true; // TODO: Return a real value
182}
183
184static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
185 if (!vf) {
186 return false;
187 }
188 struct Patch patch;
189 if (!loadPatch(vf, &patch)) {
190 return false;
191 }
192 GBApplyPatch(core->board, &patch);
193 return true;
194}
195
196static void _GBCoreUnloadROM(struct mCore* core) {
197 struct GBCore* gbcore = (struct GBCore*) core;
198 struct LR35902Core* cpu = core->cpu;
199 if (gbcore->cheatDevice) {
200 LR35902HotplugDetach(cpu, CPU_COMPONENT_CHEAT_DEVICE);
201 cpu->components[CPU_COMPONENT_CHEAT_DEVICE] = NULL;
202 mCheatDeviceDestroy(gbcore->cheatDevice);
203 gbcore->cheatDevice = NULL;
204 }
205 return GBUnloadROM(core->board);
206}
207
208static void _GBCoreReset(struct mCore* core) {
209 struct GBCore* gbcore = (struct GBCore*) core;
210 struct GB* gb = (struct GB*) core->board;
211 if (gbcore->renderer.outputBuffer) {
212 GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
213 }
214
215 struct GBCartridgeOverride override;
216 const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
217 if (cart) {
218 override.headerCrc32 = doCrc32(cart, sizeof(*cart));
219 if (GBOverrideFind(gbcore->overrides, &override)) {
220 GBOverrideApply(gb, &override);
221 }
222 }
223
224#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
225 struct VFile* bios = NULL;
226 if (core->opts.useBios) {
227 bool found = false;
228 if (core->opts.bios) {
229 bios = VFileOpen(core->opts.bios, O_RDONLY);
230 if (bios && GBIsBIOS(bios)) {
231 found = true;
232 } else if (bios) {
233 bios->close(bios);
234 bios = NULL;
235 }
236 }
237 if (!found) {
238 char path[PATH_MAX];
239 GBDetectModel(gb);
240 mCoreConfigDirectory(path, PATH_MAX);
241 switch (gb->model) {
242 case GB_MODEL_DMG:
243 case GB_MODEL_SGB: // TODO
244 strncat(path, PATH_SEP "gb_bios.bin", PATH_MAX - strlen(path));
245 break;
246 case GB_MODEL_CGB:
247 case GB_MODEL_AGB:
248 strncat(path, PATH_SEP "gbc_bios.bin", PATH_MAX - strlen(path));
249 break;
250 default:
251 break;
252 };
253 bios = VFileOpen(path, O_RDONLY);
254 }
255 }
256 if (bios) {
257 GBLoadBIOS(gb, bios);
258 }
259#endif
260
261 LR35902Reset(core->cpu);
262}
263
264static void _GBCoreRunFrame(struct mCore* core) {
265 struct GB* gb = core->board;
266 int32_t frameCounter = gb->video.frameCounter;
267 while (gb->video.frameCounter == frameCounter) {
268 LR35902Run(core->cpu);
269 }
270}
271
272static void _GBCoreRunLoop(struct mCore* core) {
273 LR35902Run(core->cpu);
274}
275
276static void _GBCoreStep(struct mCore* core) {
277 struct LR35902Core* cpu = core->cpu;
278 do {
279 LR35902Tick(cpu);
280 } while (cpu->executionState != LR35902_CORE_FETCH);
281}
282
283static size_t _GBCoreStateSize(struct mCore* core) {
284 UNUSED(core);
285 return sizeof(struct GBSerializedState);
286}
287
288static bool _GBCoreLoadState(struct mCore* core, const void* state) {
289 return GBDeserialize(core->board, state);
290}
291
292static bool _GBCoreSaveState(struct mCore* core, void* state) {
293 struct LR35902Core* cpu = core->cpu;
294 while (cpu->executionState != LR35902_CORE_FETCH) {
295 LR35902Tick(cpu);
296 }
297 GBSerialize(core->board, state);
298 return true;
299}
300
301static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
302 struct GBCore* gbcore = (struct GBCore*) core;
303 gbcore->keys = keys;
304}
305
306static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
307 struct GBCore* gbcore = (struct GBCore*) core;
308 gbcore->keys |= keys;
309}
310
311static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
312 struct GBCore* gbcore = (struct GBCore*) core;
313 gbcore->keys &= ~keys;
314}
315
316static int32_t _GBCoreFrameCounter(const struct mCore* core) {
317 const struct GB* gb = core->board;
318 return gb->video.frameCounter;
319}
320
321static int32_t _GBCoreFrameCycles(const struct mCore* core) {
322 UNUSED(core);
323 return GB_VIDEO_TOTAL_LENGTH;
324}
325
326static int32_t _GBCoreFrequency(const struct mCore* core) {
327 UNUSED(core);
328 // TODO: GB differences
329 return DMG_LR35902_FREQUENCY;
330}
331
332static void _GBCoreGetGameTitle(const struct mCore* core, char* title) {
333 GBGetGameTitle(core->board, title);
334}
335
336static void _GBCoreGetGameCode(const struct mCore* core, char* title) {
337 GBGetGameCode(core->board, title);
338}
339
340static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
341 struct GB* gb = core->board;
342 gb->memory.rtc = rtc;
343}
344
345static void _GBCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
346 struct GB* gb = core->board;
347 gb->memory.rotation = rotation;
348}
349
350static void _GBCoreSetRumble(struct mCore* core, struct mRumble* rumble) {
351 struct GB* gb = core->board;
352 gb->memory.rumble = rumble;
353}
354
355static uint32_t _GBCoreBusRead8(struct mCore* core, uint32_t address) {
356 struct LR35902Core* cpu = core->cpu;
357 return cpu->memory.load8(cpu, address);
358}
359
360static uint32_t _GBCoreBusRead16(struct mCore* core, uint32_t address) {
361 struct LR35902Core* cpu = core->cpu;
362 return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8);
363}
364
365static uint32_t _GBCoreBusRead32(struct mCore* core, uint32_t address) {
366 struct LR35902Core* cpu = core->cpu;
367 return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8) |
368 (cpu->memory.load8(cpu, address + 2) << 16) | (cpu->memory.load8(cpu, address + 3) << 24);
369}
370
371static void _GBCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
372 struct LR35902Core* cpu = core->cpu;
373 cpu->memory.store8(cpu, address, value);
374}
375
376static void _GBCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
377 struct LR35902Core* cpu = core->cpu;
378 cpu->memory.store8(cpu, address, value);
379 cpu->memory.store8(cpu, address + 1, value >> 8);
380}
381
382static void _GBCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
383 struct LR35902Core* cpu = core->cpu;
384 cpu->memory.store8(cpu, address, value);
385 cpu->memory.store8(cpu, address + 1, value >> 8);
386 cpu->memory.store8(cpu, address + 2, value >> 16);
387 cpu->memory.store8(cpu, address + 3, value >> 24);
388}
389
390static uint32_t _GBCoreRawRead8(struct mCore* core, uint32_t address, int segment) {
391 struct LR35902Core* cpu = core->cpu;
392 return GBView8(cpu, address, segment);
393}
394
395static uint32_t _GBCoreRawRead16(struct mCore* core, uint32_t address, int segment) {
396 struct LR35902Core* cpu = core->cpu;
397 return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8);
398}
399
400static uint32_t _GBCoreRawRead32(struct mCore* core, uint32_t address, int segment) {
401 struct LR35902Core* cpu = core->cpu;
402 return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8) |
403 (GBView8(cpu, address + 2, segment) << 16) | (GBView8(cpu, address + 3, segment) << 24);
404}
405
406static void _GBCoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
407 struct LR35902Core* cpu = core->cpu;
408 GBPatch8(cpu, address, value, NULL, segment);
409}
410
411static void _GBCoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
412 struct LR35902Core* cpu = core->cpu;
413 GBPatch8(cpu, address, value, NULL, segment);
414 GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
415}
416
417static void _GBCoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
418 struct LR35902Core* cpu = core->cpu;
419 GBPatch8(cpu, address, value, NULL, segment);
420 GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
421 GBPatch8(cpu, address + 2, value >> 16, NULL, segment);
422 GBPatch8(cpu, address + 3, value >> 24, NULL, segment);
423}
424
425static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
426 UNUSED(core);
427 switch (type) {
428#ifdef USE_CLI_DEBUGGER
429 case DEBUGGER_CLI:
430 return true;
431#endif
432 default:
433 return false;
434 }
435}
436
437static struct mDebuggerPlatform* _GBCoreDebuggerPlatform(struct mCore* core) {
438 struct GBCore* gbcore = (struct GBCore*) core;
439 if (!gbcore->debuggerPlatform) {
440 gbcore->debuggerPlatform = LR35902DebuggerPlatformCreate();
441 }
442 return gbcore->debuggerPlatform;
443}
444
445static struct CLIDebuggerSystem* _GBCoreCliDebuggerSystem(struct mCore* core) {
446#ifdef USE_CLI_DEBUGGER
447 return GBCLIDebuggerCreate(core);
448#else
449 UNUSED(core);
450 return NULL;
451#endif
452}
453
454static void _GBCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
455 struct LR35902Core* cpu = core->cpu;
456 if (core->debugger) {
457 LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
458 }
459 cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
460 LR35902HotplugAttach(cpu, CPU_COMPONENT_DEBUGGER);
461 core->debugger = debugger;
462}
463
464static void _GBCoreDetachDebugger(struct mCore* core) {
465 struct LR35902Core* cpu = core->cpu;
466 if (core->debugger) {
467 LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
468 }
469 cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
470 core->debugger = NULL;
471}
472
473static struct mCheatDevice* _GBCoreCheatDevice(struct mCore* core) {
474 struct GBCore* gbcore = (struct GBCore*) core;
475 if (!gbcore->cheatDevice) {
476 gbcore->cheatDevice = GBCheatDeviceCreate();
477 ((struct LR35902Core*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbcore->cheatDevice->d;
478 LR35902HotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
479 gbcore->cheatDevice->p = core;
480 }
481 return gbcore->cheatDevice;
482}
483
484static size_t _GBCoreSavedataClone(struct mCore* core, void** sram) {
485 struct GB* gb = core->board;
486 struct VFile* vf = gb->sramVf;
487 if (vf) {
488 *sram = malloc(vf->size(vf));
489 vf->seek(vf, 0, SEEK_SET);
490 return vf->read(vf, *sram, vf->size(vf));
491 }
492 *sram = malloc(gb->sramSize);
493 memcpy(*sram, gb->memory.sram, gb->sramSize);
494 return gb->sramSize;
495}
496
497static bool _GBCoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
498 struct GB* gb = core->board;
499 if (!writeback) {
500 struct VFile* vf = VFileMemChunk(sram, size);
501 GBSavedataMask(gb, vf, true);
502 return true;
503 }
504 struct VFile* vf = gb->sramVf;
505 if (vf) {
506 vf->seek(vf, 0, SEEK_SET);
507 return vf->write(vf, sram, size) > 0;
508 }
509 if (size > 0x20000) {
510 size = 0x20000;
511 }
512 GBResizeSram(gb, size);
513 memcpy(gb->memory.sram, sram, size);
514 return true;
515}
516
517struct mCore* GBCoreCreate(void) {
518 struct GBCore* gbcore = malloc(sizeof(*gbcore));
519 struct mCore* core = &gbcore->d;
520 memset(&core->opts, 0, sizeof(core->opts));
521 core->cpu = NULL;
522 core->board = NULL;
523 core->debugger = NULL;
524 core->init = _GBCoreInit;
525 core->deinit = _GBCoreDeinit;
526 core->platform = _GBCorePlatform;
527 core->setSync = _GBCoreSetSync;
528 core->loadConfig = _GBCoreLoadConfig;
529 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
530 core->setVideoBuffer = _GBCoreSetVideoBuffer;
531 core->getPixels = _GBCoreGetPixels;
532 core->putPixels = _GBCorePutPixels;
533 core->getAudioChannel = _GBCoreGetAudioChannel;
534 core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
535 core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
536 core->setAVStream = _GBCoreSetAVStream;
537 core->isROM = GBIsROM;
538 core->loadROM = _GBCoreLoadROM;
539 core->loadBIOS = _GBCoreLoadBIOS;
540 core->loadSave = _GBCoreLoadSave;
541 core->loadTemporarySave = _GBCoreLoadTemporarySave;
542 core->loadPatch = _GBCoreLoadPatch;
543 core->unloadROM = _GBCoreUnloadROM;
544 core->reset = _GBCoreReset;
545 core->runFrame = _GBCoreRunFrame;
546 core->runLoop = _GBCoreRunLoop;
547 core->step = _GBCoreStep;
548 core->stateSize = _GBCoreStateSize;
549 core->loadState = _GBCoreLoadState;
550 core->saveState = _GBCoreSaveState;
551 core->setKeys = _GBCoreSetKeys;
552 core->addKeys = _GBCoreAddKeys;
553 core->clearKeys = _GBCoreClearKeys;
554 core->frameCounter = _GBCoreFrameCounter;
555 core->frameCycles = _GBCoreFrameCycles;
556 core->frequency = _GBCoreFrequency;
557 core->getGameTitle = _GBCoreGetGameTitle;
558 core->getGameCode = _GBCoreGetGameCode;
559 core->setRTC = _GBCoreSetRTC;
560 core->setRotation = _GBCoreSetRotation;
561 core->setRumble = _GBCoreSetRumble;
562 core->busRead8 = _GBCoreBusRead8;
563 core->busRead16 = _GBCoreBusRead16;
564 core->busRead32 = _GBCoreBusRead32;
565 core->busWrite8 = _GBCoreBusWrite8;
566 core->busWrite16 = _GBCoreBusWrite16;
567 core->busWrite32 = _GBCoreBusWrite32;
568 core->rawRead8 = _GBCoreRawRead8;
569 core->rawRead16 = _GBCoreRawRead16;
570 core->rawRead32 = _GBCoreRawRead32;
571 core->rawWrite8 = _GBCoreRawWrite8;
572 core->rawWrite16 = _GBCoreRawWrite16;
573 core->rawWrite32 = _GBCoreRawWrite32;
574 core->supportsDebuggerType = _GBCoreSupportsDebuggerType;
575 core->debuggerPlatform = _GBCoreDebuggerPlatform;
576 core->cliDebuggerSystem = _GBCoreCliDebuggerSystem;
577 core->attachDebugger = _GBCoreAttachDebugger;
578 core->detachDebugger = _GBCoreDetachDebugger;
579 core->cheatDevice = _GBCoreCheatDevice;
580 core->savedataClone = _GBCoreSavedataClone;
581 core->savedataRestore = _GBCoreSavedataRestore;
582 return core;
583}