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 _GBCoreSetCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) {
157 struct GB* gb = core->board;
158 gb->coreCallbacks = coreCallbacks;
159}
160
161static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
162 struct GB* gb = core->board;
163 gb->stream = stream;
164 if (stream && stream->videoDimensionsChanged) {
165 stream->videoDimensionsChanged(stream, GB_VIDEO_HORIZONTAL_PIXELS, GB_VIDEO_VERTICAL_PIXELS);
166 }
167}
168
169static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
170 return GBLoadROM(core->board, vf);
171}
172
173static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
174 UNUSED(type);
175 GBLoadBIOS(core->board, vf);
176 return true;
177}
178
179static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
180 return GBLoadSave(core->board, vf);
181}
182
183static bool _GBCoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
184 struct GB* gb = core->board;
185 GBSavedataMask(gb, vf, false);
186 return true; // TODO: Return a real value
187}
188
189static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
190 if (!vf) {
191 return false;
192 }
193 struct Patch patch;
194 if (!loadPatch(vf, &patch)) {
195 return false;
196 }
197 GBApplyPatch(core->board, &patch);
198 return true;
199}
200
201static void _GBCoreUnloadROM(struct mCore* core) {
202 struct GBCore* gbcore = (struct GBCore*) core;
203 struct LR35902Core* cpu = core->cpu;
204 if (gbcore->cheatDevice) {
205 LR35902HotplugDetach(cpu, CPU_COMPONENT_CHEAT_DEVICE);
206 cpu->components[CPU_COMPONENT_CHEAT_DEVICE] = NULL;
207 mCheatDeviceDestroy(gbcore->cheatDevice);
208 gbcore->cheatDevice = NULL;
209 }
210 return GBUnloadROM(core->board);
211}
212
213static void _GBCoreReset(struct mCore* core) {
214 struct GBCore* gbcore = (struct GBCore*) core;
215 struct GB* gb = (struct GB*) core->board;
216 if (gbcore->renderer.outputBuffer) {
217 GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
218 }
219
220 if (gb->memory.rom) {
221 struct GBCartridgeOverride override;
222 const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
223 override.headerCrc32 = doCrc32(cart, sizeof(*cart));
224 if (GBOverrideFind(gbcore->overrides, &override)) {
225 GBOverrideApply(gb, &override);
226 }
227 }
228
229#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
230 struct VFile* bios = NULL;
231 if (core->opts.useBios) {
232 bool found = false;
233 if (core->opts.bios) {
234 bios = VFileOpen(core->opts.bios, O_RDONLY);
235 if (bios && GBIsBIOS(bios)) {
236 found = true;
237 } else if (bios) {
238 bios->close(bios);
239 bios = NULL;
240 }
241 }
242 if (!found) {
243 char path[PATH_MAX];
244 GBDetectModel(gb);
245 mCoreConfigDirectory(path, PATH_MAX);
246 switch (gb->model) {
247 case GB_MODEL_DMG:
248 case GB_MODEL_SGB: // TODO
249 strncat(path, PATH_SEP "gb_bios.bin", PATH_MAX - strlen(path));
250 break;
251 case GB_MODEL_CGB:
252 case GB_MODEL_AGB:
253 strncat(path, PATH_SEP "gbc_bios.bin", PATH_MAX - strlen(path));
254 break;
255 default:
256 break;
257 };
258 bios = VFileOpen(path, O_RDONLY);
259 }
260 }
261 if (bios) {
262 GBLoadBIOS(gb, bios);
263 }
264#endif
265
266 LR35902Reset(core->cpu);
267}
268
269static void _GBCoreRunFrame(struct mCore* core) {
270 struct GB* gb = core->board;
271 int32_t frameCounter = gb->video.frameCounter;
272 while (gb->video.frameCounter == frameCounter) {
273 LR35902Run(core->cpu);
274 }
275}
276
277static void _GBCoreRunLoop(struct mCore* core) {
278 LR35902Run(core->cpu);
279}
280
281static void _GBCoreStep(struct mCore* core) {
282 struct LR35902Core* cpu = core->cpu;
283 do {
284 LR35902Tick(cpu);
285 } while (cpu->executionState != LR35902_CORE_FETCH);
286}
287
288static size_t _GBCoreStateSize(struct mCore* core) {
289 UNUSED(core);
290 return sizeof(struct GBSerializedState);
291}
292
293static bool _GBCoreLoadState(struct mCore* core, const void* state) {
294 return GBDeserialize(core->board, state);
295}
296
297static bool _GBCoreSaveState(struct mCore* core, void* state) {
298 struct LR35902Core* cpu = core->cpu;
299 while (cpu->executionState != LR35902_CORE_FETCH) {
300 LR35902Tick(cpu);
301 }
302 GBSerialize(core->board, state);
303 return true;
304}
305
306static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
307 struct GBCore* gbcore = (struct GBCore*) core;
308 gbcore->keys = keys;
309}
310
311static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
312 struct GBCore* gbcore = (struct GBCore*) core;
313 gbcore->keys |= keys;
314}
315
316static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
317 struct GBCore* gbcore = (struct GBCore*) core;
318 gbcore->keys &= ~keys;
319}
320
321static int32_t _GBCoreFrameCounter(const struct mCore* core) {
322 const struct GB* gb = core->board;
323 return gb->video.frameCounter;
324}
325
326static int32_t _GBCoreFrameCycles(const struct mCore* core) {
327 UNUSED(core);
328 return GB_VIDEO_TOTAL_LENGTH;
329}
330
331static int32_t _GBCoreFrequency(const struct mCore* core) {
332 UNUSED(core);
333 // TODO: GB differences
334 return DMG_LR35902_FREQUENCY;
335}
336
337static void _GBCoreGetGameTitle(const struct mCore* core, char* title) {
338 GBGetGameTitle(core->board, title);
339}
340
341static void _GBCoreGetGameCode(const struct mCore* core, char* title) {
342 GBGetGameCode(core->board, title);
343}
344
345static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
346 struct GB* gb = core->board;
347 gb->memory.rtc = rtc;
348}
349
350static void _GBCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
351 struct GB* gb = core->board;
352 gb->memory.rotation = rotation;
353}
354
355static void _GBCoreSetRumble(struct mCore* core, struct mRumble* rumble) {
356 struct GB* gb = core->board;
357 gb->memory.rumble = rumble;
358}
359
360static uint32_t _GBCoreBusRead8(struct mCore* core, uint32_t address) {
361 struct LR35902Core* cpu = core->cpu;
362 return cpu->memory.load8(cpu, address);
363}
364
365static uint32_t _GBCoreBusRead16(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}
369
370static uint32_t _GBCoreBusRead32(struct mCore* core, uint32_t address) {
371 struct LR35902Core* cpu = core->cpu;
372 return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8) |
373 (cpu->memory.load8(cpu, address + 2) << 16) | (cpu->memory.load8(cpu, address + 3) << 24);
374}
375
376static void _GBCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
377 struct LR35902Core* cpu = core->cpu;
378 cpu->memory.store8(cpu, address, value);
379}
380
381static void _GBCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
382 struct LR35902Core* cpu = core->cpu;
383 cpu->memory.store8(cpu, address, value);
384 cpu->memory.store8(cpu, address + 1, value >> 8);
385}
386
387static void _GBCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
388 struct LR35902Core* cpu = core->cpu;
389 cpu->memory.store8(cpu, address, value);
390 cpu->memory.store8(cpu, address + 1, value >> 8);
391 cpu->memory.store8(cpu, address + 2, value >> 16);
392 cpu->memory.store8(cpu, address + 3, value >> 24);
393}
394
395static uint32_t _GBCoreRawRead8(struct mCore* core, uint32_t address, int segment) {
396 struct LR35902Core* cpu = core->cpu;
397 return GBView8(cpu, address, segment);
398}
399
400static uint32_t _GBCoreRawRead16(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}
404
405static uint32_t _GBCoreRawRead32(struct mCore* core, uint32_t address, int segment) {
406 struct LR35902Core* cpu = core->cpu;
407 return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8) |
408 (GBView8(cpu, address + 2, segment) << 16) | (GBView8(cpu, address + 3, segment) << 24);
409}
410
411static void _GBCoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
412 struct LR35902Core* cpu = core->cpu;
413 GBPatch8(cpu, address, value, NULL, segment);
414}
415
416static void _GBCoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
417 struct LR35902Core* cpu = core->cpu;
418 GBPatch8(cpu, address, value, NULL, segment);
419 GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
420}
421
422static void _GBCoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
423 struct LR35902Core* cpu = core->cpu;
424 GBPatch8(cpu, address, value, NULL, segment);
425 GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
426 GBPatch8(cpu, address + 2, value >> 16, NULL, segment);
427 GBPatch8(cpu, address + 3, value >> 24, NULL, segment);
428}
429
430static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
431 UNUSED(core);
432 switch (type) {
433#ifdef USE_CLI_DEBUGGER
434 case DEBUGGER_CLI:
435 return true;
436#endif
437 default:
438 return false;
439 }
440}
441
442static struct mDebuggerPlatform* _GBCoreDebuggerPlatform(struct mCore* core) {
443 struct GBCore* gbcore = (struct GBCore*) core;
444 if (!gbcore->debuggerPlatform) {
445 gbcore->debuggerPlatform = LR35902DebuggerPlatformCreate();
446 }
447 return gbcore->debuggerPlatform;
448}
449
450static struct CLIDebuggerSystem* _GBCoreCliDebuggerSystem(struct mCore* core) {
451#ifdef USE_CLI_DEBUGGER
452 return GBCLIDebuggerCreate(core);
453#else
454 UNUSED(core);
455 return NULL;
456#endif
457}
458
459static void _GBCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
460 struct LR35902Core* cpu = core->cpu;
461 if (core->debugger) {
462 LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
463 }
464 cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
465 LR35902HotplugAttach(cpu, CPU_COMPONENT_DEBUGGER);
466 core->debugger = debugger;
467}
468
469static void _GBCoreDetachDebugger(struct mCore* core) {
470 struct LR35902Core* cpu = core->cpu;
471 if (core->debugger) {
472 LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
473 }
474 cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
475 core->debugger = NULL;
476}
477
478static struct mCheatDevice* _GBCoreCheatDevice(struct mCore* core) {
479 struct GBCore* gbcore = (struct GBCore*) core;
480 if (!gbcore->cheatDevice) {
481 gbcore->cheatDevice = GBCheatDeviceCreate();
482 ((struct LR35902Core*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbcore->cheatDevice->d;
483 LR35902HotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
484 gbcore->cheatDevice->p = core;
485 }
486 return gbcore->cheatDevice;
487}
488
489static size_t _GBCoreSavedataClone(struct mCore* core, void** sram) {
490 struct GB* gb = core->board;
491 struct VFile* vf = gb->sramVf;
492 if (vf) {
493 *sram = malloc(vf->size(vf));
494 vf->seek(vf, 0, SEEK_SET);
495 return vf->read(vf, *sram, vf->size(vf));
496 }
497 *sram = malloc(gb->sramSize);
498 memcpy(*sram, gb->memory.sram, gb->sramSize);
499 return gb->sramSize;
500}
501
502static bool _GBCoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
503 struct GB* gb = core->board;
504 if (!writeback) {
505 struct VFile* vf = VFileMemChunk(sram, size);
506 GBSavedataMask(gb, vf, true);
507 return true;
508 }
509 struct VFile* vf = gb->sramVf;
510 if (vf) {
511 vf->seek(vf, 0, SEEK_SET);
512 return vf->write(vf, sram, size) > 0;
513 }
514 if (size > 0x20000) {
515 size = 0x20000;
516 }
517 GBResizeSram(gb, size);
518 memcpy(gb->memory.sram, sram, size);
519 return true;
520}
521
522struct mCore* GBCoreCreate(void) {
523 struct GBCore* gbcore = malloc(sizeof(*gbcore));
524 struct mCore* core = &gbcore->d;
525 memset(&core->opts, 0, sizeof(core->opts));
526 core->cpu = NULL;
527 core->board = NULL;
528 core->debugger = NULL;
529 core->init = _GBCoreInit;
530 core->deinit = _GBCoreDeinit;
531 core->platform = _GBCorePlatform;
532 core->setSync = _GBCoreSetSync;
533 core->loadConfig = _GBCoreLoadConfig;
534 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
535 core->setVideoBuffer = _GBCoreSetVideoBuffer;
536 core->getPixels = _GBCoreGetPixels;
537 core->putPixels = _GBCorePutPixels;
538 core->getAudioChannel = _GBCoreGetAudioChannel;
539 core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
540 core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
541 core->setAVStream = _GBCoreSetAVStream;
542 core->setCoreCallbacks = _GBCoreSetCoreCallbacks;
543 core->isROM = GBIsROM;
544 core->loadROM = _GBCoreLoadROM;
545 core->loadBIOS = _GBCoreLoadBIOS;
546 core->loadSave = _GBCoreLoadSave;
547 core->loadTemporarySave = _GBCoreLoadTemporarySave;
548 core->loadPatch = _GBCoreLoadPatch;
549 core->unloadROM = _GBCoreUnloadROM;
550 core->reset = _GBCoreReset;
551 core->runFrame = _GBCoreRunFrame;
552 core->runLoop = _GBCoreRunLoop;
553 core->step = _GBCoreStep;
554 core->stateSize = _GBCoreStateSize;
555 core->loadState = _GBCoreLoadState;
556 core->saveState = _GBCoreSaveState;
557 core->setKeys = _GBCoreSetKeys;
558 core->addKeys = _GBCoreAddKeys;
559 core->clearKeys = _GBCoreClearKeys;
560 core->frameCounter = _GBCoreFrameCounter;
561 core->frameCycles = _GBCoreFrameCycles;
562 core->frequency = _GBCoreFrequency;
563 core->getGameTitle = _GBCoreGetGameTitle;
564 core->getGameCode = _GBCoreGetGameCode;
565 core->setRTC = _GBCoreSetRTC;
566 core->setRotation = _GBCoreSetRotation;
567 core->setRumble = _GBCoreSetRumble;
568 core->busRead8 = _GBCoreBusRead8;
569 core->busRead16 = _GBCoreBusRead16;
570 core->busRead32 = _GBCoreBusRead32;
571 core->busWrite8 = _GBCoreBusWrite8;
572 core->busWrite16 = _GBCoreBusWrite16;
573 core->busWrite32 = _GBCoreBusWrite32;
574 core->rawRead8 = _GBCoreRawRead8;
575 core->rawRead16 = _GBCoreRawRead16;
576 core->rawRead32 = _GBCoreRawRead32;
577 core->rawWrite8 = _GBCoreRawWrite8;
578 core->rawWrite16 = _GBCoreRawWrite16;
579 core->rawWrite32 = _GBCoreRawWrite32;
580 core->supportsDebuggerType = _GBCoreSupportsDebuggerType;
581 core->debuggerPlatform = _GBCoreDebuggerPlatform;
582 core->cliDebuggerSystem = _GBCoreCliDebuggerSystem;
583 core->attachDebugger = _GBCoreAttachDebugger;
584 core->detachDebugger = _GBCoreDetachDebugger;
585 core->cheatDevice = _GBCoreCheatDevice;
586 core->savedataClone = _GBCoreSavedataClone;
587 core->savedataRestore = _GBCoreSavedataRestore;
588 return core;
589}