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 <mgba/gb/core.h>
7
8#include <mgba/core/core.h>
9#include <mgba/internal/debugger/symbols.h>
10#include <mgba/internal/gb/cheats.h>
11#include <mgba/internal/gb/debugger/debugger.h>
12#include <mgba/internal/gb/debugger/symbols.h>
13#include <mgba/internal/gb/extra/cli.h>
14#include <mgba/internal/gb/io.h>
15#include <mgba/internal/gb/gb.h>
16#include <mgba/internal/gb/mbc.h>
17#include <mgba/internal/gb/overrides.h>
18#include <mgba/internal/gb/renderers/software.h>
19#include <mgba/internal/gb/renderers/proxy.h>
20#include <mgba/internal/gb/serialize.h>
21#include <mgba/internal/sm83/sm83.h>
22#include <mgba/internal/sm83/debugger/debugger.h>
23#include <mgba-util/crc32.h>
24#include <mgba-util/memory.h>
25#include <mgba-util/patch.h>
26#include <mgba-util/vfs.h>
27
28static const struct mCoreChannelInfo _GBVideoLayers[] = {
29 { 0, "bg", "Background", NULL },
30 { 1, "bgwin", "Window", NULL },
31 { 2, "obj", "Objects", NULL },
32};
33
34static const struct mCoreChannelInfo _GBAudioChannels[] = {
35 { 0, "ch1", "Channel 1", "Square/Sweep" },
36 { 1, "ch2", "Channel 2", "Square" },
37 { 2, "ch3", "Channel 3", "PCM" },
38 { 3, "ch4", "Channel 4", "Noise" },
39};
40
41static const struct mCoreMemoryBlock _GBMemoryBlocks[] = {
42 { -1, "mem", "All", "All", 0, 0x10000, 0x10000, mCORE_MEMORY_VIRTUAL },
43 { GB_REGION_CART_BANK0, "cart0", "ROM Bank", "Game Pak (32kiB)", GB_BASE_CART_BANK0, GB_BASE_CART_BANK0 + GB_SIZE_CART_BANK0 * 2, 0x800000, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED, 511, GB_BASE_CART_BANK0 + GB_SIZE_CART_BANK0 },
44 { GB_REGION_VRAM, "vram", "VRAM", "Video RAM (8kiB)", GB_BASE_VRAM, GB_BASE_VRAM + GB_SIZE_VRAM, GB_SIZE_VRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
45 { GB_REGION_EXTERNAL_RAM, "sram", "SRAM", "External RAM (8kiB)", GB_BASE_EXTERNAL_RAM, GB_BASE_EXTERNAL_RAM + GB_SIZE_EXTERNAL_RAM, GB_SIZE_EXTERNAL_RAM * 4, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED, 3 },
46 { GB_REGION_WORKING_RAM_BANK0, "wram", "WRAM", "Working RAM (8kiB)", GB_BASE_WORKING_RAM_BANK0, GB_BASE_WORKING_RAM_BANK0 + GB_SIZE_WORKING_RAM_BANK0 * 2 , GB_SIZE_WORKING_RAM_BANK0 * 2, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
47 { GB_BASE_OAM, "oam", "OAM", "OBJ Attribute Memory", GB_BASE_OAM, GB_BASE_OAM + GB_SIZE_OAM, GB_SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
48 { GB_BASE_IO, "io", "MMIO", "Memory-Mapped I/O", GB_BASE_IO, GB_BASE_IO + GB_SIZE_IO, GB_SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
49 { GB_BASE_HRAM, "hram", "HRAM", "High RAM", GB_BASE_HRAM, GB_BASE_HRAM + GB_SIZE_HRAM, GB_SIZE_HRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
50};
51
52static const struct mCoreMemoryBlock _GBCMemoryBlocks[] = {
53 { -1, "mem", "All", "All", 0, 0x10000, 0x10000, mCORE_MEMORY_VIRTUAL },
54 { GB_REGION_CART_BANK0, "cart0", "ROM Bank", "Game Pak (32kiB)", GB_BASE_CART_BANK0, GB_BASE_CART_BANK0 + GB_SIZE_CART_BANK0 * 2, 0x800000, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED, 511, GB_BASE_CART_BANK0 + GB_SIZE_CART_BANK0 },
55 { GB_REGION_VRAM, "vram", "VRAM", "Video RAM (8kiB)", GB_BASE_VRAM, GB_BASE_VRAM + GB_SIZE_VRAM, GB_SIZE_VRAM * 2, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED, 1 },
56 { GB_REGION_EXTERNAL_RAM, "sram", "SRAM", "External RAM (8kiB)", GB_BASE_EXTERNAL_RAM, GB_BASE_EXTERNAL_RAM + GB_SIZE_EXTERNAL_RAM, GB_SIZE_EXTERNAL_RAM * 4, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED, 3 },
57 { GB_REGION_WORKING_RAM_BANK0, "wram", "WRAM", "Working RAM (8kiB)", GB_BASE_WORKING_RAM_BANK0, GB_BASE_WORKING_RAM_BANK0 + GB_SIZE_WORKING_RAM_BANK0 * 2, GB_SIZE_WORKING_RAM_BANK0 * 8, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED, 7, GB_BASE_WORKING_RAM_BANK0 + GB_SIZE_WORKING_RAM_BANK0 },
58 { GB_BASE_OAM, "oam", "OAM", "OBJ Attribute Memory", GB_BASE_OAM, GB_BASE_OAM + GB_SIZE_OAM, GB_SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
59 { GB_BASE_IO, "io", "MMIO", "Memory-Mapped I/O", GB_BASE_IO, GB_BASE_IO + GB_SIZE_IO, GB_SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
60 { GB_BASE_HRAM, "hram", "HRAM", "High RAM", GB_BASE_HRAM, GB_BASE_HRAM + GB_SIZE_HRAM, GB_SIZE_HRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
61};
62
63struct mVideoLogContext;
64struct GBCore {
65 struct mCore d;
66 struct GBVideoSoftwareRenderer renderer;
67 struct GBVideoProxyRenderer proxyRenderer;
68 struct mVideoLogContext* logContext;
69 struct mCoreCallbacks logCallbacks;
70 uint8_t keys;
71 struct mCPUComponent* components[CPU_COMPONENT_MAX];
72 const struct Configuration* overrides;
73 struct mDebuggerPlatform* debuggerPlatform;
74 struct mCheatDevice* cheatDevice;
75};
76
77static bool _GBCoreInit(struct mCore* core) {
78 struct GBCore* gbcore = (struct GBCore*) core;
79
80 struct SM83Core* cpu = anonymousMemoryMap(sizeof(struct SM83Core));
81 struct GB* gb = anonymousMemoryMap(sizeof(struct GB));
82 if (!cpu || !gb) {
83 free(cpu);
84 free(gb);
85 return false;
86 }
87 core->cpu = cpu;
88 core->board = gb;
89 core->timing = &gb->timing;
90 gbcore->overrides = NULL;
91 gbcore->debuggerPlatform = NULL;
92 gbcore->cheatDevice = NULL;
93
94 GBCreate(gb);
95 memset(gbcore->components, 0, sizeof(gbcore->components));
96 SM83SetComponents(cpu, &gb->d, CPU_COMPONENT_MAX, gbcore->components);
97 SM83Init(cpu);
98 mRTCGenericSourceInit(&core->rtc, core);
99 gb->memory.rtc = &core->rtc.d;
100
101 GBVideoSoftwareRendererCreate(&gbcore->renderer);
102 gbcore->renderer.outputBuffer = NULL;
103
104 gbcore->keys = 0;
105 gb->keySource = &gbcore->keys;
106
107#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
108 mDirectorySetInit(&core->dirs);
109#endif
110
111 return true;
112}
113
114static void _GBCoreDeinit(struct mCore* core) {
115 SM83Deinit(core->cpu);
116 GBDestroy(core->board);
117 mappedMemoryFree(core->cpu, sizeof(struct SM83Core));
118 mappedMemoryFree(core->board, sizeof(struct GB));
119#if defined USE_DEBUGGERS && (!defined(MINIMAL_CORE) || MINIMAL_CORE < 2)
120 mDirectorySetDeinit(&core->dirs);
121 if (core->symbolTable) {
122 mDebuggerSymbolTableDestroy(core->symbolTable);
123 }
124#endif
125
126 struct GBCore* gbcore = (struct GBCore*) core;
127 free(gbcore->debuggerPlatform);
128 if (gbcore->cheatDevice) {
129 mCheatDeviceDestroy(gbcore->cheatDevice);
130 }
131 free(gbcore->cheatDevice);
132 mCoreConfigFreeOpts(&core->opts);
133 free(core);
134}
135
136static enum mPlatform _GBCorePlatform(const struct mCore* core) {
137 UNUSED(core);
138 return PLATFORM_GB;
139}
140
141static bool _GBCoreSupportsFeature(const struct mCore* core, enum mCoreFeature feature) {
142 UNUSED(core);
143 switch (feature) {
144 default:
145 return false;
146 }
147}
148
149static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
150 struct GB* gb = core->board;
151 gb->sync = sync;
152}
153
154static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
155 UNUSED(config);
156
157 struct GB* gb = core->board;
158 if (core->opts.mute) {
159 gb->audio.masterVolume = 0;
160 } else {
161 gb->audio.masterVolume = core->opts.volume;
162 }
163 gb->video.frameskip = core->opts.frameskip;
164
165 int color;
166 if (mCoreConfigGetIntValue(config, "gb.pal[0]", &color)) {
167 GBVideoSetPalette(&gb->video, 0, color);
168 }
169 if (mCoreConfigGetIntValue(config, "gb.pal[1]", &color)) {
170 GBVideoSetPalette(&gb->video, 1, color);
171 }
172 if (mCoreConfigGetIntValue(config, "gb.pal[2]", &color)) {
173 GBVideoSetPalette(&gb->video, 2, color);
174 }
175 if (mCoreConfigGetIntValue(config, "gb.pal[3]", &color)) {
176 GBVideoSetPalette(&gb->video, 3, color);
177 }
178 if (mCoreConfigGetIntValue(config, "gb.pal[4]", &color)) {
179 GBVideoSetPalette(&gb->video, 4, color);
180 }
181 if (mCoreConfigGetIntValue(config, "gb.pal[5]", &color)) {
182 GBVideoSetPalette(&gb->video, 5, color);
183 }
184 if (mCoreConfigGetIntValue(config, "gb.pal[6]", &color)) {
185 GBVideoSetPalette(&gb->video, 6, color);
186 }
187 if (mCoreConfigGetIntValue(config, "gb.pal[7]", &color)) {
188 GBVideoSetPalette(&gb->video, 7, color);
189 }
190 if (mCoreConfigGetIntValue(config, "gb.pal[8]", &color)) {
191 GBVideoSetPalette(&gb->video, 8, color);
192 }
193 if (mCoreConfigGetIntValue(config, "gb.pal[9]", &color)) {
194 GBVideoSetPalette(&gb->video, 9, color);
195 }
196 if (mCoreConfigGetIntValue(config, "gb.pal[10]", &color)) {
197 GBVideoSetPalette(&gb->video, 10, color);
198 }
199 if (mCoreConfigGetIntValue(config, "gb.pal[11]", &color)) {
200 GBVideoSetPalette(&gb->video, 11, color);
201 }
202
203 mCoreConfigCopyValue(&core->config, config, "gb.bios");
204 mCoreConfigCopyValue(&core->config, config, "sgb.bios");
205 mCoreConfigCopyValue(&core->config, config, "gbc.bios");
206 mCoreConfigCopyValue(&core->config, config, "gb.model");
207 mCoreConfigCopyValue(&core->config, config, "sgb.model");
208 mCoreConfigCopyValue(&core->config, config, "cgb.model");
209 mCoreConfigCopyValue(&core->config, config, "useCgbColors");
210 mCoreConfigCopyValue(&core->config, config, "allowOpposingDirections");
211
212 int fakeBool = 0;
213 mCoreConfigGetIntValue(config, "allowOpposingDirections", &fakeBool);
214 gb->allowOpposingDirections = fakeBool;
215
216 if (mCoreConfigGetIntValue(config, "sgb.borders", &fakeBool)) {
217 gb->video.sgbBorders = fakeBool;
218 gb->video.renderer->enableSGBBorder(gb->video.renderer, fakeBool);
219 }
220
221#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
222 struct GBCore* gbcore = (struct GBCore*) core;
223 gbcore->overrides = mCoreConfigGetOverridesConst(config);
224#endif
225}
226
227static void _GBCoreReloadConfigOption(struct mCore* core, const char* option, const struct mCoreConfig* config) {
228 struct GB* gb = core->board;
229 if (!config) {
230 config = &core->config;
231 }
232
233 if (!option) {
234 // Reload options from opts
235 if (core->opts.mute) {
236 gb->audio.masterVolume = 0;
237 } else {
238 gb->audio.masterVolume = core->opts.volume;
239 }
240 gb->video.frameskip = core->opts.frameskip;
241 return;
242 }
243
244 int fakeBool;
245 if (strcmp("mute", option) == 0) {
246 if (mCoreConfigGetIntValue(config, "mute", &fakeBool)) {
247 core->opts.mute = fakeBool;
248
249 if (core->opts.mute) {
250 gb->audio.masterVolume = 0;
251 } else {
252 gb->audio.masterVolume = core->opts.volume;
253 }
254 }
255 return;
256 }
257 if (strcmp("volume", option) == 0) {
258 if (mCoreConfigGetIntValue(config, "volume", &core->opts.volume) && !core->opts.mute) {
259 gb->audio.masterVolume = core->opts.volume;
260 }
261 return;
262 }
263 if (strcmp("frameskip", option) == 0) {
264 if (mCoreConfigGetIntValue(config, "frameskip", &core->opts.frameskip)) {
265 gb->video.frameskip = core->opts.frameskip;
266 }
267 return;
268 }
269 if (strcmp("allowOpposingDirections", option) == 0) {
270 if (config != &core->config) {
271 mCoreConfigCopyValue(&core->config, config, "allowOpposingDirections");
272 }
273 if (mCoreConfigGetIntValue(config, "allowOpposingDirections", &fakeBool)) {
274 gb->allowOpposingDirections = fakeBool;
275 }
276 return;
277 }
278}
279
280static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
281 struct GB* gb = core->board;
282 if (gb && (!(gb->model & GB_MODEL_SGB) || !gb->video.sgbBorders)) {
283 *width = GB_VIDEO_HORIZONTAL_PIXELS;
284 *height = GB_VIDEO_VERTICAL_PIXELS;
285 } else {
286 *width = 256;
287 *height = 224;
288 }
289}
290
291static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
292 struct GBCore* gbcore = (struct GBCore*) core;
293 gbcore->renderer.outputBuffer = buffer;
294 gbcore->renderer.outputBufferStride = stride;
295}
296
297static void _GBCoreSetVideoGLTex(struct mCore* core, unsigned texid) {
298 UNUSED(core);
299 UNUSED(texid);
300}
301
302static void _GBCoreGetPixels(struct mCore* core, const void** buffer, size_t* stride) {
303 struct GBCore* gbcore = (struct GBCore*) core;
304 gbcore->renderer.d.getPixels(&gbcore->renderer.d, stride, buffer);
305}
306
307static void _GBCorePutPixels(struct mCore* core, const void* buffer, size_t stride) {
308 struct GBCore* gbcore = (struct GBCore*) core;
309 gbcore->renderer.d.putPixels(&gbcore->renderer.d, stride, buffer);
310}
311
312static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
313 struct GB* gb = core->board;
314 switch (ch) {
315 case 0:
316 return gb->audio.left;
317 case 1:
318 return gb->audio.right;
319 default:
320 return NULL;
321 }
322}
323
324static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
325 struct GB* gb = core->board;
326 GBAudioResizeBuffer(&gb->audio, samples);
327}
328
329static size_t _GBCoreGetAudioBufferSize(struct mCore* core) {
330 struct GB* gb = core->board;
331 return gb->audio.samples;
332}
333
334static void _GBCoreAddCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) {
335 struct GB* gb = core->board;
336 *mCoreCallbacksListAppend(&gb->coreCallbacks) = *coreCallbacks;
337}
338
339static void _GBCoreClearCoreCallbacks(struct mCore* core) {
340 struct GB* gb = core->board;
341 mCoreCallbacksListClear(&gb->coreCallbacks);
342}
343
344static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
345 struct GB* gb = core->board;
346 gb->stream = stream;
347 if (stream && stream->videoDimensionsChanged) {
348 unsigned width, height;
349 core->desiredVideoDimensions(core, &width, &height);
350 stream->videoDimensionsChanged(stream, width, height);
351 }
352}
353
354static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
355 return GBLoadROM(core->board, vf);
356}
357
358static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
359 UNUSED(type);
360 GBLoadBIOS(core->board, vf);
361 return true;
362}
363
364static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
365 return GBLoadSave(core->board, vf);
366}
367
368static bool _GBCoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
369 struct GB* gb = core->board;
370 GBSavedataMask(gb, vf, false);
371 return true; // TODO: Return a real value
372}
373
374static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
375 if (!vf) {
376 return false;
377 }
378 struct Patch patch;
379 if (!loadPatch(vf, &patch)) {
380 return false;
381 }
382 GBApplyPatch(core->board, &patch);
383 return true;
384}
385
386static void _GBCoreUnloadROM(struct mCore* core) {
387 struct GBCore* gbcore = (struct GBCore*) core;
388 struct SM83Core* cpu = core->cpu;
389 if (gbcore->cheatDevice) {
390 SM83HotplugDetach(cpu, CPU_COMPONENT_CHEAT_DEVICE);
391 cpu->components[CPU_COMPONENT_CHEAT_DEVICE] = NULL;
392 mCheatDeviceDestroy(gbcore->cheatDevice);
393 gbcore->cheatDevice = NULL;
394 }
395 return GBUnloadROM(core->board);
396}
397
398static void _GBCoreChecksum(const struct mCore* core, void* data, enum mCoreChecksumType type) {
399 struct GB* gb = (struct GB*) core->board;
400 switch (type) {
401 case CHECKSUM_CRC32:
402 memcpy(data, &gb->romCrc32, sizeof(gb->romCrc32));
403 break;
404 }
405 return;
406}
407
408static void _GBCoreReset(struct mCore* core) {
409 struct GBCore* gbcore = (struct GBCore*) core;
410 struct GB* gb = (struct GB*) core->board;
411 if (gbcore->renderer.outputBuffer) {
412 GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
413 }
414
415 if (gb->memory.rom) {
416 int doColorOverride = 0;
417 mCoreConfigGetIntValue(&core->config, "useCgbColors", &doColorOverride);
418
419 struct GBCartridgeOverride override;
420 const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
421 override.headerCrc32 = doCrc32(cart, sizeof(*cart));
422 if (GBOverrideFind(gbcore->overrides, &override) || (doColorOverride && GBOverrideColorFind(&override))) {
423 GBOverrideApply(gb, &override);
424 }
425 }
426
427 const char* modelGB = mCoreConfigGetValue(&core->config, "gb.model");
428 const char* modelCGB = mCoreConfigGetValue(&core->config, "cgb.model");
429 const char* modelSGB = mCoreConfigGetValue(&core->config, "sgb.model");
430 if (modelGB || modelCGB || modelSGB) {
431 GBDetectModel(gb);
432 if (gb->model == GB_MODEL_DMG && modelGB) {
433 gb->model = GBNameToModel(modelGB);
434 } else if ((gb->model & GB_MODEL_CGB) && modelCGB) {
435 gb->model = GBNameToModel(modelCGB);
436 } else if ((gb->model & GB_MODEL_SGB) && modelSGB) {
437 gb->model = GBNameToModel(modelSGB);
438 }
439 }
440
441#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
442 if (!gb->biosVf && core->opts.useBios) {
443 struct VFile* bios = NULL;
444 bool found = false;
445 if (core->opts.bios) {
446 bios = VFileOpen(core->opts.bios, O_RDONLY);
447 if (bios && GBIsBIOS(bios)) {
448 found = true;
449 } else if (bios) {
450 bios->close(bios);
451 bios = NULL;
452 }
453 }
454 if (!found) {
455 GBDetectModel(gb);
456 const char* configPath = NULL;
457
458 switch (gb->model) {
459 case GB_MODEL_DMG:
460 case GB_MODEL_MGB: // TODO
461 configPath = mCoreConfigGetValue(&core->config, "gb.bios");
462 break;
463 case GB_MODEL_SGB:
464 case GB_MODEL_SGB2: // TODO
465 configPath = mCoreConfigGetValue(&core->config, "sgb.bios");
466 break;
467 case GB_MODEL_CGB:
468 case GB_MODEL_AGB:
469 configPath = mCoreConfigGetValue(&core->config, "gbc.bios");
470 break;
471 default:
472 break;
473 };
474 if (configPath) {
475 bios = VFileOpen(configPath, O_RDONLY);
476 }
477 if (bios && GBIsBIOS(bios)) {
478 found = true;
479 } else if (bios) {
480 bios->close(bios);
481 bios = NULL;
482 }
483 }
484 if (!found) {
485 char path[PATH_MAX];
486 mCoreConfigDirectory(path, PATH_MAX);
487 switch (gb->model) {
488 case GB_MODEL_DMG:
489 case GB_MODEL_MGB: // TODO
490 strncat(path, PATH_SEP "gb_bios.bin", PATH_MAX - strlen(path));
491 break;
492 case GB_MODEL_SGB:
493 case GB_MODEL_SGB2: // TODO
494 strncat(path, PATH_SEP "sgb_bios.bin", PATH_MAX - strlen(path));
495 break;
496 case GB_MODEL_CGB:
497 case GB_MODEL_AGB:
498 strncat(path, PATH_SEP "gbc_bios.bin", PATH_MAX - strlen(path));
499 break;
500 default:
501 break;
502 };
503 bios = VFileOpen(path, O_RDONLY);
504 if (bios && GBIsBIOS(bios)) {
505 found = true;
506 } else if (bios) {
507 bios->close(bios);
508 bios = NULL;
509 }
510 }
511 if (bios) {
512 GBLoadBIOS(gb, bios);
513 }
514 }
515#endif
516
517 SM83Reset(core->cpu);
518
519 if (core->opts.skipBios) {
520 GBSkipBIOS(core->board);
521 }
522}
523
524static void _GBCoreRunFrame(struct mCore* core) {
525 struct GB* gb = core->board;
526 int32_t frameCounter = gb->video.frameCounter;
527 while (gb->video.frameCounter == frameCounter) {
528 SM83Run(core->cpu);
529 }
530}
531
532static void _GBCoreRunLoop(struct mCore* core) {
533 SM83Run(core->cpu);
534}
535
536static void _GBCoreStep(struct mCore* core) {
537 struct SM83Core* cpu = core->cpu;
538 do {
539 SM83Tick(cpu);
540 } while (cpu->executionState != SM83_CORE_FETCH);
541}
542
543static size_t _GBCoreStateSize(struct mCore* core) {
544 UNUSED(core);
545 return sizeof(struct GBSerializedState);
546}
547
548static bool _GBCoreLoadState(struct mCore* core, const void* state) {
549 return GBDeserialize(core->board, state);
550}
551
552static bool _GBCoreSaveState(struct mCore* core, void* state) {
553 struct SM83Core* cpu = core->cpu;
554 while (cpu->executionState != SM83_CORE_FETCH) {
555 SM83Tick(cpu);
556 }
557 GBSerialize(core->board, state);
558 return true;
559}
560
561static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
562 struct GBCore* gbcore = (struct GBCore*) core;
563 gbcore->keys = keys;
564 GBTestKeypadIRQ(core->board);
565}
566
567static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
568 struct GBCore* gbcore = (struct GBCore*) core;
569 gbcore->keys |= keys;
570 GBTestKeypadIRQ(core->board);
571}
572
573static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
574 struct GBCore* gbcore = (struct GBCore*) core;
575 gbcore->keys &= ~keys;
576}
577
578static int32_t _GBCoreFrameCounter(const struct mCore* core) {
579 const struct GB* gb = core->board;
580 return gb->video.frameCounter;
581}
582
583static int32_t _GBCoreFrameCycles(const struct mCore* core) {
584 UNUSED(core);
585 return GB_VIDEO_TOTAL_LENGTH;
586}
587
588static int32_t _GBCoreFrequency(const struct mCore* core) {
589 UNUSED(core);
590 // TODO: GB differences
591 return DMG_SM83_FREQUENCY;
592}
593
594static void _GBCoreGetGameTitle(const struct mCore* core, char* title) {
595 GBGetGameTitle(core->board, title);
596}
597
598static void _GBCoreGetGameCode(const struct mCore* core, char* title) {
599 GBGetGameCode(core->board, title);
600}
601
602static void _GBCoreSetPeripheral(struct mCore* core, int type, void* periph) {
603 struct GB* gb = core->board;
604 switch (type) {
605 case mPERIPH_ROTATION:
606 gb->memory.rotation = periph;
607 break;
608 case mPERIPH_RUMBLE:
609 gb->memory.rumble = periph;
610 break;
611 case mPERIPH_IMAGE_SOURCE:
612 gb->memory.cam = periph;
613 break;
614 default:
615 return;
616 }
617}
618
619static uint32_t _GBCoreBusRead8(struct mCore* core, uint32_t address) {
620 struct SM83Core* cpu = core->cpu;
621 return cpu->memory.load8(cpu, address);
622}
623
624static uint32_t _GBCoreBusRead16(struct mCore* core, uint32_t address) {
625 struct SM83Core* cpu = core->cpu;
626 return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8);
627}
628
629static uint32_t _GBCoreBusRead32(struct mCore* core, uint32_t address) {
630 struct SM83Core* cpu = core->cpu;
631 return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8) |
632 (cpu->memory.load8(cpu, address + 2) << 16) | (cpu->memory.load8(cpu, address + 3) << 24);
633}
634
635static void _GBCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
636 struct SM83Core* cpu = core->cpu;
637 cpu->memory.store8(cpu, address, value);
638}
639
640static void _GBCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
641 struct SM83Core* cpu = core->cpu;
642 cpu->memory.store8(cpu, address, value);
643 cpu->memory.store8(cpu, address + 1, value >> 8);
644}
645
646static void _GBCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
647 struct SM83Core* cpu = core->cpu;
648 cpu->memory.store8(cpu, address, value);
649 cpu->memory.store8(cpu, address + 1, value >> 8);
650 cpu->memory.store8(cpu, address + 2, value >> 16);
651 cpu->memory.store8(cpu, address + 3, value >> 24);
652}
653
654static uint32_t _GBCoreRawRead8(struct mCore* core, uint32_t address, int segment) {
655 struct SM83Core* cpu = core->cpu;
656 return GBView8(cpu, address, segment);
657}
658
659static uint32_t _GBCoreRawRead16(struct mCore* core, uint32_t address, int segment) {
660 struct SM83Core* cpu = core->cpu;
661 return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8);
662}
663
664static uint32_t _GBCoreRawRead32(struct mCore* core, uint32_t address, int segment) {
665 struct SM83Core* cpu = core->cpu;
666 return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8) |
667 (GBView8(cpu, address + 2, segment) << 16) | (GBView8(cpu, address + 3, segment) << 24);
668}
669
670static void _GBCoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
671 struct SM83Core* cpu = core->cpu;
672 GBPatch8(cpu, address, value, NULL, segment);
673}
674
675static void _GBCoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
676 struct SM83Core* cpu = core->cpu;
677 GBPatch8(cpu, address, value, NULL, segment);
678 GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
679}
680
681static void _GBCoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
682 struct SM83Core* cpu = core->cpu;
683 GBPatch8(cpu, address, value, NULL, segment);
684 GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
685 GBPatch8(cpu, address + 2, value >> 16, NULL, segment);
686 GBPatch8(cpu, address + 3, value >> 24, NULL, segment);
687}
688
689size_t _GBListMemoryBlocks(const struct mCore* core, const struct mCoreMemoryBlock** blocks) {
690 const struct GB* gb = core->board;
691 switch (gb->model) {
692 case GB_MODEL_DMG:
693 case GB_MODEL_MGB:
694 case GB_MODEL_SGB:
695 case GB_MODEL_SGB2:
696 default:
697 *blocks = _GBMemoryBlocks;
698 return sizeof(_GBMemoryBlocks) / sizeof(*_GBMemoryBlocks);
699 case GB_MODEL_CGB:
700 case GB_MODEL_AGB:
701 *blocks = _GBCMemoryBlocks;
702 return sizeof(_GBCMemoryBlocks) / sizeof(*_GBCMemoryBlocks);
703 }
704}
705
706void* _GBGetMemoryBlock(struct mCore* core, size_t id, size_t* sizeOut) {
707 struct GB* gb = core->board;
708 bool isCgb = gb->model >= GB_MODEL_CGB;
709 switch (id) {
710 default:
711 return NULL;
712 case GB_REGION_CART_BANK0:
713 *sizeOut = gb->memory.romSize;
714 return gb->memory.rom;
715 case GB_REGION_VRAM:
716 *sizeOut = GB_SIZE_WORKING_RAM_BANK0 * (isCgb ? 1 : 2);
717 return gb->video.vram;
718 case GB_REGION_EXTERNAL_RAM:
719 *sizeOut = gb->sramSize;
720 return gb->memory.sram;
721 case GB_REGION_WORKING_RAM_BANK0:
722 *sizeOut = GB_SIZE_VRAM * (isCgb ? 8 : 2);
723 return gb->memory.wram;
724 case GB_BASE_OAM:
725 *sizeOut = GB_SIZE_OAM;
726 return gb->video.oam.raw;
727 case GB_BASE_HRAM:
728 *sizeOut = GB_SIZE_HRAM;
729 return gb->memory.hram;
730 }
731}
732
733#ifdef USE_DEBUGGERS
734static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
735 UNUSED(core);
736 switch (type) {
737 case DEBUGGER_CUSTOM:
738 case DEBUGGER_CLI:
739 return true;
740 default:
741 return false;
742 }
743}
744
745static struct mDebuggerPlatform* _GBCoreDebuggerPlatform(struct mCore* core) {
746 struct GBCore* gbcore = (struct GBCore*) core;
747 struct GB* gb = core->board;
748 if (!gbcore->debuggerPlatform) {
749 struct SM83Debugger* platform = (struct SM83Debugger*) GBDebuggerCreate(gb);
750 gbcore->debuggerPlatform = &platform->d;
751 }
752 return gbcore->debuggerPlatform;
753}
754
755static struct CLIDebuggerSystem* _GBCoreCliDebuggerSystem(struct mCore* core) {
756 return GBCLIDebuggerCreate(core);
757}
758
759static void _GBCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
760 struct SM83Core* cpu = core->cpu;
761 if (core->debugger) {
762 SM83HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
763 }
764 cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
765 SM83HotplugAttach(cpu, CPU_COMPONENT_DEBUGGER);
766 core->debugger = debugger;
767}
768
769static void _GBCoreDetachDebugger(struct mCore* core) {
770 struct SM83Core* cpu = core->cpu;
771 if (core->debugger) {
772 SM83HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
773 }
774 cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
775 core->debugger = NULL;
776}
777
778static void _GBCoreLoadSymbols(struct mCore* core, struct VFile* vf) {
779 core->symbolTable = mDebuggerSymbolTableCreate();
780#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
781 if (!vf) {
782 vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".sym", O_RDONLY);
783 }
784#endif
785 if (!vf) {
786 return;
787 }
788 GBLoadSymbols(core->symbolTable, vf);
789}
790
791static bool _GBCoreLookupIdentifier(struct mCore* core, const char* name, int32_t* value, int* segment) {
792 UNUSED(core);
793 *segment = -1;
794 int i;
795 for (i = 0; i < REG_MAX; ++i) {
796 const char* reg = GBIORegisterNames[i];
797 if (reg && strcasecmp(reg, name) == 0) {
798 *value = GB_BASE_IO | i;
799 return true;
800 }
801 }
802 return false;
803}
804#endif
805
806static struct mCheatDevice* _GBCoreCheatDevice(struct mCore* core) {
807 struct GBCore* gbcore = (struct GBCore*) core;
808 if (!gbcore->cheatDevice) {
809 gbcore->cheatDevice = GBCheatDeviceCreate();
810 ((struct SM83Core*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbcore->cheatDevice->d;
811 SM83HotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
812 gbcore->cheatDevice->p = core;
813 }
814 return gbcore->cheatDevice;
815}
816
817static size_t _GBCoreSavedataClone(struct mCore* core, void** sram) {
818 struct GB* gb = core->board;
819 struct VFile* vf = gb->sramVf;
820 if (vf) {
821 *sram = malloc(vf->size(vf));
822 vf->seek(vf, 0, SEEK_SET);
823 return vf->read(vf, *sram, vf->size(vf));
824 }
825 *sram = malloc(gb->sramSize);
826 memcpy(*sram, gb->memory.sram, gb->sramSize);
827 return gb->sramSize;
828}
829
830static bool _GBCoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
831 struct GB* gb = core->board;
832 if (!writeback) {
833 struct VFile* vf = VFileMemChunk(sram, size);
834 GBSavedataMask(gb, vf, true);
835 return true;
836 }
837 struct VFile* vf = gb->sramVf;
838 if (vf) {
839 vf->seek(vf, 0, SEEK_SET);
840 return vf->write(vf, sram, size) > 0;
841 }
842 if (size > 0x20000) {
843 size = 0x20000;
844 }
845 GBResizeSram(gb, size);
846 memcpy(gb->memory.sram, sram, size);
847 return true;
848}
849
850static size_t _GBCoreListVideoLayers(const struct mCore* core, const struct mCoreChannelInfo** info) {
851 UNUSED(core);
852 if (info) {
853 *info = _GBVideoLayers;
854 }
855 return sizeof(_GBVideoLayers) / sizeof(*_GBVideoLayers);
856}
857
858static size_t _GBCoreListAudioChannels(const struct mCore* core, const struct mCoreChannelInfo** info) {
859 UNUSED(core);
860 if (info) {
861 *info = _GBAudioChannels;
862 }
863 return sizeof(_GBAudioChannels) / sizeof(*_GBAudioChannels);
864}
865
866static void _GBCoreEnableVideoLayer(struct mCore* core, size_t id, bool enable) {
867 struct GB* gb = core->board;
868 switch (id) {
869 case 0:
870 gb->video.renderer->disableBG = !enable;
871 break;
872 case 1:
873 gb->video.renderer->disableWIN = !enable;
874 break;
875 case 2:
876 gb->video.renderer->disableOBJ = !enable;
877 break;
878 default:
879 break;
880 }
881}
882
883static void _GBCoreEnableAudioChannel(struct mCore* core, size_t id, bool enable) {
884 struct GB* gb = core->board;
885 switch (id) {
886 case 0:
887 case 1:
888 case 2:
889 case 3:
890 gb->audio.forceDisableCh[id] = !enable;
891 break;
892 default:
893 break;
894 }
895}
896
897static void _GBCoreAdjustVideoLayer(struct mCore* core, size_t id, int32_t x, int32_t y) {
898 struct GBCore* gbcore = (struct GBCore*) core;
899 switch (id) {
900 case 0:
901 gbcore->renderer.offsetScx = x;
902 gbcore->renderer.offsetScy = y;
903 break;
904 case 1:
905 gbcore->renderer.offsetWx = x;
906 gbcore->renderer.offsetWy = y;
907 break;
908 case 2:
909 gbcore->renderer.objOffsetX = x;
910 gbcore->renderer.objOffsetY = y;
911 break;
912 default:
913 return;
914 }
915}
916
917#ifndef MINIMAL_CORE
918static void _GBCoreStartVideoLog(struct mCore* core, struct mVideoLogContext* context) {
919 struct GBCore* gbcore = (struct GBCore*) core;
920 struct GB* gb = core->board;
921 gbcore->logContext = context;
922
923 int channelId = mVideoLoggerAddChannel(context);
924 gbcore->proxyRenderer.logger = malloc(sizeof(struct mVideoLogger));
925 mVideoLoggerRendererCreate(gbcore->proxyRenderer.logger, false);
926 mVideoLoggerAttachChannel(gbcore->proxyRenderer.logger, context, channelId);
927 gbcore->proxyRenderer.logger->block = false;
928
929 GBVideoProxyRendererCreate(&gbcore->proxyRenderer, &gbcore->renderer.d);
930 GBVideoProxyRendererShim(&gb->video, &gbcore->proxyRenderer);
931}
932
933static void _GBCoreEndVideoLog(struct mCore* core) {
934 struct GBCore* gbcore = (struct GBCore*) core;
935 struct GB* gb = core->board;
936 if (gbcore->proxyRenderer.logger) {
937 GBVideoProxyRendererUnshim(&gb->video, &gbcore->proxyRenderer);
938 free(gbcore->proxyRenderer.logger);
939 gbcore->proxyRenderer.logger = NULL;
940 }
941}
942#endif
943
944struct mCore* GBCoreCreate(void) {
945 struct GBCore* gbcore = malloc(sizeof(*gbcore));
946 struct mCore* core = &gbcore->d;
947 memset(&core->opts, 0, sizeof(core->opts));
948 core->cpu = NULL;
949 core->board = NULL;
950 core->debugger = NULL;
951 core->symbolTable = NULL;
952 core->init = _GBCoreInit;
953 core->deinit = _GBCoreDeinit;
954 core->platform = _GBCorePlatform;
955 core->supportsFeature = _GBCoreSupportsFeature;
956 core->setSync = _GBCoreSetSync;
957 core->loadConfig = _GBCoreLoadConfig;
958 core->reloadConfigOption = _GBCoreReloadConfigOption;
959 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
960 core->setVideoBuffer = _GBCoreSetVideoBuffer;
961 core->setVideoGLTex = _GBCoreSetVideoGLTex;
962 core->getPixels = _GBCoreGetPixels;
963 core->putPixels = _GBCorePutPixels;
964 core->getAudioChannel = _GBCoreGetAudioChannel;
965 core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
966 core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
967 core->setAVStream = _GBCoreSetAVStream;
968 core->addCoreCallbacks = _GBCoreAddCoreCallbacks;
969 core->clearCoreCallbacks = _GBCoreClearCoreCallbacks;
970 core->isROM = GBIsROM;
971 core->loadROM = _GBCoreLoadROM;
972 core->loadBIOS = _GBCoreLoadBIOS;
973 core->loadSave = _GBCoreLoadSave;
974 core->loadTemporarySave = _GBCoreLoadTemporarySave;
975 core->loadPatch = _GBCoreLoadPatch;
976 core->unloadROM = _GBCoreUnloadROM;
977 core->checksum = _GBCoreChecksum;
978 core->reset = _GBCoreReset;
979 core->runFrame = _GBCoreRunFrame;
980 core->runLoop = _GBCoreRunLoop;
981 core->step = _GBCoreStep;
982 core->stateSize = _GBCoreStateSize;
983 core->loadState = _GBCoreLoadState;
984 core->saveState = _GBCoreSaveState;
985 core->setKeys = _GBCoreSetKeys;
986 core->addKeys = _GBCoreAddKeys;
987 core->clearKeys = _GBCoreClearKeys;
988 core->frameCounter = _GBCoreFrameCounter;
989 core->frameCycles = _GBCoreFrameCycles;
990 core->frequency = _GBCoreFrequency;
991 core->getGameTitle = _GBCoreGetGameTitle;
992 core->getGameCode = _GBCoreGetGameCode;
993 core->setPeripheral = _GBCoreSetPeripheral;
994 core->busRead8 = _GBCoreBusRead8;
995 core->busRead16 = _GBCoreBusRead16;
996 core->busRead32 = _GBCoreBusRead32;
997 core->busWrite8 = _GBCoreBusWrite8;
998 core->busWrite16 = _GBCoreBusWrite16;
999 core->busWrite32 = _GBCoreBusWrite32;
1000 core->rawRead8 = _GBCoreRawRead8;
1001 core->rawRead16 = _GBCoreRawRead16;
1002 core->rawRead32 = _GBCoreRawRead32;
1003 core->rawWrite8 = _GBCoreRawWrite8;
1004 core->rawWrite16 = _GBCoreRawWrite16;
1005 core->rawWrite32 = _GBCoreRawWrite32;
1006 core->listMemoryBlocks = _GBListMemoryBlocks;
1007 core->getMemoryBlock = _GBGetMemoryBlock;
1008#ifdef USE_DEBUGGERS
1009 core->supportsDebuggerType = _GBCoreSupportsDebuggerType;
1010 core->debuggerPlatform = _GBCoreDebuggerPlatform;
1011 core->cliDebuggerSystem = _GBCoreCliDebuggerSystem;
1012 core->attachDebugger = _GBCoreAttachDebugger;
1013 core->detachDebugger = _GBCoreDetachDebugger;
1014 core->loadSymbols = _GBCoreLoadSymbols;
1015 core->lookupIdentifier = _GBCoreLookupIdentifier;
1016#endif
1017 core->cheatDevice = _GBCoreCheatDevice;
1018 core->savedataClone = _GBCoreSavedataClone;
1019 core->savedataRestore = _GBCoreSavedataRestore;
1020 core->listVideoLayers = _GBCoreListVideoLayers;
1021 core->listAudioChannels = _GBCoreListAudioChannels;
1022 core->enableVideoLayer = _GBCoreEnableVideoLayer;
1023 core->enableAudioChannel = _GBCoreEnableAudioChannel;
1024 core->adjustVideoLayer = _GBCoreAdjustVideoLayer;
1025#ifndef MINIMAL_CORE
1026 core->startVideoLog = _GBCoreStartVideoLog;
1027 core->endVideoLog = _GBCoreEndVideoLog;
1028#endif
1029 return core;
1030}
1031
1032#ifndef MINIMAL_CORE
1033static void _GBVLPStartFrameCallback(void *context) {
1034 struct mCore* core = context;
1035 struct GBCore* gbcore = (struct GBCore*) core;
1036 struct GB* gb = core->board;
1037
1038 if (!mVideoLoggerRendererRun(gbcore->proxyRenderer.logger, true)) {
1039 GBVideoProxyRendererUnshim(&gb->video, &gbcore->proxyRenderer);
1040 mVideoLogContextRewind(gbcore->logContext, core);
1041 GBVideoProxyRendererShim(&gb->video, &gbcore->proxyRenderer);
1042 gb->earlyExit = true;
1043 }
1044}
1045
1046static bool _GBVLPInit(struct mCore* core) {
1047 struct GBCore* gbcore = (struct GBCore*) core;
1048 if (!_GBCoreInit(core)) {
1049 return false;
1050 }
1051 gbcore->proxyRenderer.logger = malloc(sizeof(struct mVideoLogger));
1052 mVideoLoggerRendererCreate(gbcore->proxyRenderer.logger, true);
1053 GBVideoProxyRendererCreate(&gbcore->proxyRenderer, NULL);
1054 memset(&gbcore->logCallbacks, 0, sizeof(gbcore->logCallbacks));
1055 gbcore->logCallbacks.videoFrameStarted = _GBVLPStartFrameCallback;
1056 gbcore->logCallbacks.context = core;
1057 core->addCoreCallbacks(core, &gbcore->logCallbacks);
1058 core->videoLogger = gbcore->proxyRenderer.logger;
1059 return true;
1060}
1061
1062static void _GBVLPDeinit(struct mCore* core) {
1063 struct GBCore* gbcore = (struct GBCore*) core;
1064 if (gbcore->logContext) {
1065 mVideoLogContextDestroy(core, gbcore->logContext);
1066 }
1067 _GBCoreDeinit(core);
1068}
1069
1070static void _GBVLPReset(struct mCore* core) {
1071 struct GBCore* gbcore = (struct GBCore*) core;
1072 struct GB* gb = (struct GB*) core->board;
1073 if (gb->video.renderer == &gbcore->proxyRenderer.d) {
1074 GBVideoProxyRendererUnshim(&gb->video, &gbcore->proxyRenderer);
1075 } else if (gbcore->renderer.outputBuffer) {
1076 struct GBVideoRenderer* renderer = &gbcore->renderer.d;
1077 GBVideoAssociateRenderer(&gb->video, renderer);
1078 }
1079
1080 SM83Reset(core->cpu);
1081 mVideoLogContextRewind(gbcore->logContext, core);
1082 GBVideoProxyRendererShim(&gb->video, &gbcore->proxyRenderer);
1083
1084 // Make sure CPU loop never spins
1085 GBHalt(gb->cpu);
1086 gb->memory.ie = 0;
1087 gb->memory.ime = false;
1088}
1089
1090static bool _GBVLPLoadROM(struct mCore* core, struct VFile* vf) {
1091 struct GBCore* gbcore = (struct GBCore*) core;
1092 gbcore->logContext = mVideoLogContextCreate(NULL);
1093 if (!mVideoLogContextLoad(gbcore->logContext, vf)) {
1094 mVideoLogContextDestroy(core, gbcore->logContext);
1095 gbcore->logContext = NULL;
1096 return false;
1097 }
1098 mVideoLoggerAttachChannel(gbcore->proxyRenderer.logger, gbcore->logContext, 0);
1099 return true;
1100}
1101
1102static bool _GBVLPLoadState(struct mCore* core, const void* buffer) {
1103 struct GB* gb = (struct GB*) core->board;
1104 const struct GBSerializedState* state = buffer;
1105
1106 gb->timing.root = NULL;
1107 gb->model = state->model;
1108
1109 gb->cpu->pc = GB_BASE_HRAM;
1110 gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc);
1111
1112 GBVideoReset(&gb->video);
1113 GBVideoDeserialize(&gb->video, state);
1114 GBIODeserialize(gb, state);
1115 GBAudioReset(&gb->audio);
1116
1117 // Make sure CPU loop never spins
1118 GBHalt(gb->cpu);
1119 gb->memory.ie = 0;
1120 gb->memory.ime = false;
1121
1122 return true;
1123}
1124
1125static bool _returnTrue(struct VFile* vf) {
1126 UNUSED(vf);
1127 return true;
1128}
1129
1130struct mCore* GBVideoLogPlayerCreate(void) {
1131 struct mCore* core = GBCoreCreate();
1132 core->init = _GBVLPInit;
1133 core->deinit = _GBVLPDeinit;
1134 core->reset = _GBVLPReset;
1135 core->loadROM = _GBVLPLoadROM;
1136 core->loadState = _GBVLPLoadState;
1137 core->isROM = _returnTrue;
1138 return core;
1139}
1140#else
1141struct mCore* GBVideoLogPlayerCreate(void) {
1142 return false;
1143}
1144#endif