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