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