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