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