all repos — mgba @ 6e1ae2321eab2562bd7db11dd096a9bd1e7cd1a2

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