all repos — mgba @ cbc27f08f9d4ea827c97f3e824f319f4104482ee

mGBA Game Boy Advance Emulator

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