all repos — mgba @ f48a69e8e5a3aa8965140e15e643841c70eb6c1e

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