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