all repos — mgba @ e7be40e80ccb6dbeee0b79c97f349859b1b4afb8

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