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_WORM | 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_WORM | 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(const struct mCore* core, unsigned* width, unsigned* height) {
281 const 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 (found && 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 if (gb->sramSize) {
826 *sram = malloc(gb->sramSize);
827 memcpy(*sram, gb->memory.sram, gb->sramSize);
828 return gb->sramSize;
829 }
830 *sram = NULL;
831 return 0;
832}
833
834static bool _GBCoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
835 struct GB* gb = core->board;
836 if (!writeback) {
837 struct VFile* vf = VFileMemChunk(sram, size);
838 GBSavedataMask(gb, vf, true);
839 return true;
840 }
841 struct VFile* vf = gb->sramVf;
842 if (vf) {
843 vf->seek(vf, 0, SEEK_SET);
844 return vf->write(vf, sram, size) > 0;
845 }
846 if (size > 0x20000) {
847 size = 0x20000;
848 }
849 GBResizeSram(gb, size);
850 memcpy(gb->memory.sram, sram, size);
851 return true;
852}
853
854static size_t _GBCoreListVideoLayers(const struct mCore* core, const struct mCoreChannelInfo** info) {
855 UNUSED(core);
856 if (info) {
857 *info = _GBVideoLayers;
858 }
859 return sizeof(_GBVideoLayers) / sizeof(*_GBVideoLayers);
860}
861
862static size_t _GBCoreListAudioChannels(const struct mCore* core, const struct mCoreChannelInfo** info) {
863 UNUSED(core);
864 if (info) {
865 *info = _GBAudioChannels;
866 }
867 return sizeof(_GBAudioChannels) / sizeof(*_GBAudioChannels);
868}
869
870static void _GBCoreEnableVideoLayer(struct mCore* core, size_t id, bool enable) {
871 struct GB* gb = core->board;
872 switch (id) {
873 case 0:
874 gb->video.renderer->disableBG = !enable;
875 break;
876 case 1:
877 gb->video.renderer->disableWIN = !enable;
878 break;
879 case 2:
880 gb->video.renderer->disableOBJ = !enable;
881 break;
882 default:
883 break;
884 }
885}
886
887static void _GBCoreEnableAudioChannel(struct mCore* core, size_t id, bool enable) {
888 struct GB* gb = core->board;
889 switch (id) {
890 case 0:
891 case 1:
892 case 2:
893 case 3:
894 gb->audio.forceDisableCh[id] = !enable;
895 break;
896 default:
897 break;
898 }
899}
900
901static void _GBCoreAdjustVideoLayer(struct mCore* core, size_t id, int32_t x, int32_t y) {
902 struct GBCore* gbcore = (struct GBCore*) core;
903 switch (id) {
904 case 0:
905 gbcore->renderer.offsetScx = x;
906 gbcore->renderer.offsetScy = y;
907 break;
908 case 1:
909 gbcore->renderer.offsetWx = x;
910 gbcore->renderer.offsetWy = y;
911 break;
912 case 2:
913 gbcore->renderer.objOffsetX = x;
914 gbcore->renderer.objOffsetY = y;
915 break;
916 default:
917 return;
918 }
919}
920
921#ifndef MINIMAL_CORE
922static void _GBCoreStartVideoLog(struct mCore* core, struct mVideoLogContext* context) {
923 struct GBCore* gbcore = (struct GBCore*) core;
924 struct GB* gb = core->board;
925 gbcore->logContext = context;
926
927 int channelId = mVideoLoggerAddChannel(context);
928 gbcore->proxyRenderer.logger = malloc(sizeof(struct mVideoLogger));
929 mVideoLoggerRendererCreate(gbcore->proxyRenderer.logger, false);
930 mVideoLoggerAttachChannel(gbcore->proxyRenderer.logger, context, channelId);
931 gbcore->proxyRenderer.logger->block = false;
932
933 GBVideoProxyRendererCreate(&gbcore->proxyRenderer, &gbcore->renderer.d);
934 GBVideoProxyRendererShim(&gb->video, &gbcore->proxyRenderer);
935}
936
937static void _GBCoreEndVideoLog(struct mCore* core) {
938 struct GBCore* gbcore = (struct GBCore*) core;
939 struct GB* gb = core->board;
940 if (gbcore->proxyRenderer.logger) {
941 GBVideoProxyRendererUnshim(&gb->video, &gbcore->proxyRenderer);
942 free(gbcore->proxyRenderer.logger);
943 gbcore->proxyRenderer.logger = NULL;
944 }
945}
946#endif
947
948struct mCore* GBCoreCreate(void) {
949 struct GBCore* gbcore = malloc(sizeof(*gbcore));
950 struct mCore* core = &gbcore->d;
951 memset(&core->opts, 0, sizeof(core->opts));
952 core->cpu = NULL;
953 core->board = NULL;
954 core->debugger = NULL;
955 core->symbolTable = NULL;
956 core->init = _GBCoreInit;
957 core->deinit = _GBCoreDeinit;
958 core->platform = _GBCorePlatform;
959 core->supportsFeature = _GBCoreSupportsFeature;
960 core->setSync = _GBCoreSetSync;
961 core->loadConfig = _GBCoreLoadConfig;
962 core->reloadConfigOption = _GBCoreReloadConfigOption;
963 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
964 core->setVideoBuffer = _GBCoreSetVideoBuffer;
965 core->setVideoGLTex = _GBCoreSetVideoGLTex;
966 core->getPixels = _GBCoreGetPixels;
967 core->putPixels = _GBCorePutPixels;
968 core->getAudioChannel = _GBCoreGetAudioChannel;
969 core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
970 core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
971 core->setAVStream = _GBCoreSetAVStream;
972 core->addCoreCallbacks = _GBCoreAddCoreCallbacks;
973 core->clearCoreCallbacks = _GBCoreClearCoreCallbacks;
974 core->isROM = GBIsROM;
975 core->loadROM = _GBCoreLoadROM;
976 core->loadBIOS = _GBCoreLoadBIOS;
977 core->loadSave = _GBCoreLoadSave;
978 core->loadTemporarySave = _GBCoreLoadTemporarySave;
979 core->loadPatch = _GBCoreLoadPatch;
980 core->unloadROM = _GBCoreUnloadROM;
981 core->checksum = _GBCoreChecksum;
982 core->reset = _GBCoreReset;
983 core->runFrame = _GBCoreRunFrame;
984 core->runLoop = _GBCoreRunLoop;
985 core->step = _GBCoreStep;
986 core->stateSize = _GBCoreStateSize;
987 core->loadState = _GBCoreLoadState;
988 core->saveState = _GBCoreSaveState;
989 core->setKeys = _GBCoreSetKeys;
990 core->addKeys = _GBCoreAddKeys;
991 core->clearKeys = _GBCoreClearKeys;
992 core->frameCounter = _GBCoreFrameCounter;
993 core->frameCycles = _GBCoreFrameCycles;
994 core->frequency = _GBCoreFrequency;
995 core->getGameTitle = _GBCoreGetGameTitle;
996 core->getGameCode = _GBCoreGetGameCode;
997 core->setPeripheral = _GBCoreSetPeripheral;
998 core->busRead8 = _GBCoreBusRead8;
999 core->busRead16 = _GBCoreBusRead16;
1000 core->busRead32 = _GBCoreBusRead32;
1001 core->busWrite8 = _GBCoreBusWrite8;
1002 core->busWrite16 = _GBCoreBusWrite16;
1003 core->busWrite32 = _GBCoreBusWrite32;
1004 core->rawRead8 = _GBCoreRawRead8;
1005 core->rawRead16 = _GBCoreRawRead16;
1006 core->rawRead32 = _GBCoreRawRead32;
1007 core->rawWrite8 = _GBCoreRawWrite8;
1008 core->rawWrite16 = _GBCoreRawWrite16;
1009 core->rawWrite32 = _GBCoreRawWrite32;
1010 core->listMemoryBlocks = _GBListMemoryBlocks;
1011 core->getMemoryBlock = _GBGetMemoryBlock;
1012#ifdef USE_DEBUGGERS
1013 core->supportsDebuggerType = _GBCoreSupportsDebuggerType;
1014 core->debuggerPlatform = _GBCoreDebuggerPlatform;
1015 core->cliDebuggerSystem = _GBCoreCliDebuggerSystem;
1016 core->attachDebugger = _GBCoreAttachDebugger;
1017 core->detachDebugger = _GBCoreDetachDebugger;
1018 core->loadSymbols = _GBCoreLoadSymbols;
1019 core->lookupIdentifier = _GBCoreLookupIdentifier;
1020#endif
1021 core->cheatDevice = _GBCoreCheatDevice;
1022 core->savedataClone = _GBCoreSavedataClone;
1023 core->savedataRestore = _GBCoreSavedataRestore;
1024 core->listVideoLayers = _GBCoreListVideoLayers;
1025 core->listAudioChannels = _GBCoreListAudioChannels;
1026 core->enableVideoLayer = _GBCoreEnableVideoLayer;
1027 core->enableAudioChannel = _GBCoreEnableAudioChannel;
1028 core->adjustVideoLayer = _GBCoreAdjustVideoLayer;
1029#ifndef MINIMAL_CORE
1030 core->startVideoLog = _GBCoreStartVideoLog;
1031 core->endVideoLog = _GBCoreEndVideoLog;
1032#endif
1033 return core;
1034}
1035
1036#ifndef MINIMAL_CORE
1037static void _GBVLPStartFrameCallback(void *context) {
1038 struct mCore* core = context;
1039 struct GBCore* gbcore = (struct GBCore*) core;
1040 struct GB* gb = core->board;
1041
1042 if (!mVideoLoggerRendererRun(gbcore->proxyRenderer.logger, true)) {
1043 GBVideoProxyRendererUnshim(&gb->video, &gbcore->proxyRenderer);
1044 mVideoLogContextRewind(gbcore->logContext, core);
1045 GBVideoProxyRendererShim(&gb->video, &gbcore->proxyRenderer);
1046 gb->earlyExit = true;
1047 }
1048}
1049
1050static bool _GBVLPInit(struct mCore* core) {
1051 struct GBCore* gbcore = (struct GBCore*) core;
1052 if (!_GBCoreInit(core)) {
1053 return false;
1054 }
1055 gbcore->proxyRenderer.logger = malloc(sizeof(struct mVideoLogger));
1056 mVideoLoggerRendererCreate(gbcore->proxyRenderer.logger, true);
1057 GBVideoProxyRendererCreate(&gbcore->proxyRenderer, NULL);
1058 memset(&gbcore->logCallbacks, 0, sizeof(gbcore->logCallbacks));
1059 gbcore->logCallbacks.videoFrameStarted = _GBVLPStartFrameCallback;
1060 gbcore->logCallbacks.context = core;
1061 core->addCoreCallbacks(core, &gbcore->logCallbacks);
1062 core->videoLogger = gbcore->proxyRenderer.logger;
1063 return true;
1064}
1065
1066static void _GBVLPDeinit(struct mCore* core) {
1067 struct GBCore* gbcore = (struct GBCore*) core;
1068 if (gbcore->logContext) {
1069 mVideoLogContextDestroy(core, gbcore->logContext);
1070 }
1071 _GBCoreDeinit(core);
1072}
1073
1074static void _GBVLPReset(struct mCore* core) {
1075 struct GBCore* gbcore = (struct GBCore*) core;
1076 struct GB* gb = (struct GB*) core->board;
1077 if (gb->video.renderer == &gbcore->proxyRenderer.d) {
1078 GBVideoProxyRendererUnshim(&gb->video, &gbcore->proxyRenderer);
1079 } else if (gbcore->renderer.outputBuffer) {
1080 struct GBVideoRenderer* renderer = &gbcore->renderer.d;
1081 GBVideoAssociateRenderer(&gb->video, renderer);
1082 }
1083
1084 SM83Reset(core->cpu);
1085 mVideoLogContextRewind(gbcore->logContext, core);
1086 GBVideoProxyRendererShim(&gb->video, &gbcore->proxyRenderer);
1087
1088 // Make sure CPU loop never spins
1089 gb->memory.ie = 0;
1090 gb->memory.ime = false;
1091 GBHalt(gb->cpu);
1092}
1093
1094static bool _GBVLPLoadROM(struct mCore* core, struct VFile* vf) {
1095 struct GBCore* gbcore = (struct GBCore*) core;
1096 gbcore->logContext = mVideoLogContextCreate(NULL);
1097 if (!mVideoLogContextLoad(gbcore->logContext, vf)) {
1098 mVideoLogContextDestroy(core, gbcore->logContext);
1099 gbcore->logContext = NULL;
1100 return false;
1101 }
1102 mVideoLoggerAttachChannel(gbcore->proxyRenderer.logger, gbcore->logContext, 0);
1103 return true;
1104}
1105
1106static bool _GBVLPLoadState(struct mCore* core, const void* buffer) {
1107 struct GB* gb = (struct GB*) core->board;
1108 const struct GBSerializedState* state = buffer;
1109
1110 gb->timing.root = NULL;
1111 gb->model = state->model;
1112
1113 gb->cpu->pc = GB_BASE_HRAM;
1114 gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc);
1115
1116 GBVideoReset(&gb->video);
1117 GBVideoDeserialize(&gb->video, state);
1118 GBIODeserialize(gb, state);
1119 GBAudioReset(&gb->audio);
1120
1121 // Make sure CPU loop never spins
1122 gb->memory.ie = 0;
1123 gb->memory.ime = false;
1124 GBHalt(gb->cpu);
1125
1126 return true;
1127}
1128
1129static bool _returnTrue(struct VFile* vf) {
1130 UNUSED(vf);
1131 return true;
1132}
1133
1134struct mCore* GBVideoLogPlayerCreate(void) {
1135 struct mCore* core = GBCoreCreate();
1136 core->init = _GBVLPInit;
1137 core->deinit = _GBVLPDeinit;
1138 core->reset = _GBVLPReset;
1139 core->loadROM = _GBVLPLoadROM;
1140 core->loadState = _GBVLPLoadState;
1141 core->isROM = _returnTrue;
1142 return core;
1143}
1144#else
1145struct mCore* GBVideoLogPlayerCreate(void) {
1146 return false;
1147}
1148#endif