all repos — mgba @ 1a0e44c014ad34bea30d237d27015d82d02cda4b

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		stream->videoDimensionsChanged(stream, GB_VIDEO_HORIZONTAL_PIXELS, GB_VIDEO_VERTICAL_PIXELS);
 294	}
 295	if (stream && stream->videoFrameRateChanged) {
 296		stream->videoFrameRateChanged(stream, core->frameCycles(core), core->frequency(core));
 297	}
 298}
 299
 300static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
 301	return GBLoadROM(core->board, vf);
 302}
 303
 304static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
 305	UNUSED(type);
 306	GBLoadBIOS(core->board, vf);
 307	return true;
 308}
 309
 310static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
 311	return GBLoadSave(core->board, vf);
 312}
 313
 314static bool _GBCoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
 315	struct GB* gb = core->board;
 316	GBSavedataMask(gb, vf, false);
 317	return true; // TODO: Return a real value
 318}
 319
 320static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
 321	if (!vf) {
 322		return false;
 323	}
 324	struct Patch patch;
 325	if (!loadPatch(vf, &patch)) {
 326		return false;
 327	}
 328	GBApplyPatch(core->board, &patch);
 329	return true;
 330}
 331
 332static void _GBCoreUnloadROM(struct mCore* core) {
 333	struct GBCore* gbcore = (struct GBCore*) core;
 334	struct LR35902Core* cpu = core->cpu;
 335	if (gbcore->cheatDevice) {
 336		LR35902HotplugDetach(cpu, CPU_COMPONENT_CHEAT_DEVICE);
 337		cpu->components[CPU_COMPONENT_CHEAT_DEVICE] = NULL;
 338		mCheatDeviceDestroy(gbcore->cheatDevice);
 339		gbcore->cheatDevice = NULL;
 340	}
 341	return GBUnloadROM(core->board);
 342}
 343
 344static void _GBCoreChecksum(const struct mCore* core, void* data, enum mCoreChecksumType type) {
 345	struct GB* gb = (struct GB*) core->board;
 346	switch (type) {
 347	case CHECKSUM_CRC32:
 348		memcpy(data, &gb->romCrc32, sizeof(gb->romCrc32));
 349		break;
 350	}
 351	return;
 352}
 353
 354static void _GBCoreReset(struct mCore* core) {
 355	struct GBCore* gbcore = (struct GBCore*) core;
 356	struct GB* gb = (struct GB*) core->board;
 357	if (gbcore->renderer.outputBuffer) {
 358		GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
 359	}
 360
 361	if (gb->memory.rom) {
 362		struct GBCartridgeOverride override;
 363		const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
 364		override.headerCrc32 = doCrc32(cart, sizeof(*cart));
 365		if (GBOverrideFind(gbcore->overrides, &override)) {
 366			GBOverrideApply(gb, &override);
 367		}
 368	}
 369
 370	const char* modelGB = mCoreConfigGetValue(&core->config, "gb.model");
 371	const char* modelCGB = mCoreConfigGetValue(&core->config, "cgb.model");
 372	const char* modelSGB = mCoreConfigGetValue(&core->config, "sgb.model");
 373	if (modelGB || modelCGB || modelSGB) {
 374		GBDetectModel(gb);
 375		if (gb->model == GB_MODEL_DMG && modelGB) {
 376			gb->model = GBNameToModel(modelGB);
 377		} else if (gb->model == GB_MODEL_CGB && modelCGB) {
 378			gb->model = GBNameToModel(modelCGB);
 379		} else if (gb->model == GB_MODEL_SGB && modelSGB) {
 380			gb->model = GBNameToModel(modelSGB);
 381		}
 382	}
 383
 384	int fakeBool;
 385	if (mCoreConfigGetIntValue(&core->config, "sgb.borders", &fakeBool)) {
 386		gb->video.sgbBorders = fakeBool;
 387	}
 388
 389#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 390	if (!gb->biosVf && core->opts.useBios) {
 391		struct VFile* bios = NULL;
 392		bool found = false;
 393		if (core->opts.bios) {
 394			bios = VFileOpen(core->opts.bios, O_RDONLY);
 395			if (bios && GBIsBIOS(bios)) {
 396				found = true;
 397			} else if (bios) {
 398				bios->close(bios);
 399				bios = NULL;
 400			}
 401		}
 402		if (!found) {
 403			GBDetectModel(gb);
 404			const char* configPath = NULL;
 405
 406			switch (gb->model) {
 407			case GB_MODEL_DMG:
 408			case GB_MODEL_MGB: // TODO
 409				configPath = mCoreConfigGetValue(&core->config, "gb.bios");
 410				break;
 411			case GB_MODEL_SGB:
 412			case GB_MODEL_SGB2: // TODO
 413				configPath = mCoreConfigGetValue(&core->config, "sgb.bios");
 414				break;
 415			case GB_MODEL_CGB:
 416			case GB_MODEL_AGB:
 417				configPath = mCoreConfigGetValue(&core->config, "gbc.bios");
 418				break;
 419			default:
 420				break;
 421			};
 422			if (configPath) {
 423				bios = VFileOpen(configPath, O_RDONLY);
 424			}
 425			if (bios && GBIsBIOS(bios)) {
 426				found = true;
 427			} else if (bios) {
 428				bios->close(bios);
 429				bios = NULL;
 430			}
 431		}
 432		if (!found) {
 433			char path[PATH_MAX];
 434			mCoreConfigDirectory(path, PATH_MAX);
 435			switch (gb->model) {
 436			case GB_MODEL_DMG:
 437			case GB_MODEL_MGB: // TODO
 438				strncat(path, PATH_SEP "gb_bios.bin", PATH_MAX - strlen(path));
 439				break;
 440			case GB_MODEL_SGB:
 441			case GB_MODEL_SGB2: // TODO
 442				strncat(path, PATH_SEP "sgb_bios.bin", PATH_MAX - strlen(path));
 443				break;
 444			case GB_MODEL_CGB:
 445			case GB_MODEL_AGB:
 446				strncat(path, PATH_SEP "gbc_bios.bin", PATH_MAX - strlen(path));
 447				break;
 448			default:
 449				break;
 450			};
 451			bios = VFileOpen(path, O_RDONLY);
 452			if (bios && GBIsBIOS(bios)) {
 453				found = true;
 454			} else if (bios) {
 455				bios->close(bios);
 456				bios = NULL;
 457			}
 458		}
 459		if (bios) {
 460			GBLoadBIOS(gb, bios);
 461		}
 462	}
 463#endif
 464
 465	LR35902Reset(core->cpu);
 466
 467	if (core->opts.skipBios) {
 468		GBSkipBIOS(core->board);
 469	}
 470}
 471
 472static void _GBCoreRunFrame(struct mCore* core) {
 473	struct GB* gb = core->board;
 474	int32_t frameCounter = gb->video.frameCounter;
 475	while (gb->video.frameCounter == frameCounter) {
 476		LR35902Run(core->cpu);
 477	}
 478}
 479
 480static void _GBCoreRunLoop(struct mCore* core) {
 481	LR35902Run(core->cpu);
 482}
 483
 484static void _GBCoreStep(struct mCore* core) {
 485	struct LR35902Core* cpu = core->cpu;
 486	do {
 487		LR35902Tick(cpu);
 488	} while (cpu->executionState != LR35902_CORE_FETCH);
 489}
 490
 491static size_t _GBCoreStateSize(struct mCore* core) {
 492	UNUSED(core);
 493	return sizeof(struct GBSerializedState);
 494}
 495
 496static bool _GBCoreLoadState(struct mCore* core, const void* state) {
 497	return GBDeserialize(core->board, state);
 498}
 499
 500static bool _GBCoreSaveState(struct mCore* core, void* state) {
 501	struct LR35902Core* cpu = core->cpu;
 502	while (cpu->executionState != LR35902_CORE_FETCH) {
 503		LR35902Tick(cpu);
 504	}
 505	GBSerialize(core->board, state);
 506	return true;
 507}
 508
 509static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
 510	struct GBCore* gbcore = (struct GBCore*) core;
 511	gbcore->keys = keys;
 512	GBTestKeypadIRQ(core->board);
 513}
 514
 515static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
 516	struct GBCore* gbcore = (struct GBCore*) core;
 517	gbcore->keys |= keys;
 518	GBTestKeypadIRQ(core->board);
 519}
 520
 521static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
 522	struct GBCore* gbcore = (struct GBCore*) core;
 523	gbcore->keys &= ~keys;
 524}
 525
 526static void _GBCoreSetCursorLocation(struct mCore* core, int x, int y) {
 527	UNUSED(core);
 528	UNUSED(x);
 529	UNUSED(y);
 530}
 531
 532static void _GBCoreSetCursorDown(struct mCore* core, bool down) {
 533	UNUSED(core);
 534	UNUSED(down);
 535}
 536
 537static int32_t _GBCoreFrameCounter(const struct mCore* core) {
 538	const struct GB* gb = core->board;
 539	return gb->video.frameCounter;
 540}
 541
 542static int32_t _GBCoreFrameCycles(const  struct mCore* core) {
 543	UNUSED(core);
 544	return GB_VIDEO_TOTAL_LENGTH;
 545}
 546
 547static int32_t _GBCoreFrequency(const struct mCore* core) {
 548	UNUSED(core);
 549	// TODO: GB differences
 550	return DMG_LR35902_FREQUENCY;
 551}
 552
 553static void _GBCoreGetGameTitle(const struct mCore* core, char* title) {
 554	GBGetGameTitle(core->board, title);
 555}
 556
 557static void _GBCoreGetGameCode(const struct mCore* core, char* title) {
 558	GBGetGameCode(core->board, title);
 559}
 560
 561static void _GBCoreSetPeripheral(struct mCore* core, int type, void* periph) {
 562	struct GB* gb = core->board;
 563	switch (type) {
 564	case mPERIPH_ROTATION:
 565		gb->memory.rotation = periph;
 566		break;
 567	case mPERIPH_RUMBLE:
 568		gb->memory.rumble = periph;
 569		break;
 570	case mPERIPH_IMAGE_SOURCE:
 571		gb->memory.cam = periph;
 572		break;
 573	default:
 574		return;
 575	}
 576}
 577
 578static uint32_t _GBCoreBusRead8(struct mCore* core, uint32_t address) {
 579	struct LR35902Core* cpu = core->cpu;
 580	return cpu->memory.load8(cpu, address);
 581}
 582
 583static uint32_t _GBCoreBusRead16(struct mCore* core, uint32_t address) {
 584	struct LR35902Core* cpu = core->cpu;
 585	return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8);
 586}
 587
 588static uint32_t _GBCoreBusRead32(struct mCore* core, uint32_t address) {
 589	struct LR35902Core* cpu = core->cpu;
 590	return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8) |
 591	       (cpu->memory.load8(cpu, address + 2) << 16) | (cpu->memory.load8(cpu, address + 3) << 24);
 592}
 593
 594static void _GBCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
 595	struct LR35902Core* cpu = core->cpu;
 596	cpu->memory.store8(cpu, address, value);
 597}
 598
 599static void _GBCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
 600	struct LR35902Core* cpu = core->cpu;
 601	cpu->memory.store8(cpu, address, value);
 602	cpu->memory.store8(cpu, address + 1, value >> 8);
 603}
 604
 605static void _GBCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
 606	struct LR35902Core* cpu = core->cpu;
 607	cpu->memory.store8(cpu, address, value);
 608	cpu->memory.store8(cpu, address + 1, value >> 8);
 609	cpu->memory.store8(cpu, address + 2, value >> 16);
 610	cpu->memory.store8(cpu, address + 3, value >> 24);
 611}
 612
 613static uint32_t _GBCoreRawRead8(struct mCore* core, uint32_t address, int segment) {
 614	struct LR35902Core* cpu = core->cpu;
 615	return GBView8(cpu, address, segment);
 616}
 617
 618static uint32_t _GBCoreRawRead16(struct mCore* core, uint32_t address, int segment) {
 619	struct LR35902Core* cpu = core->cpu;
 620	return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8);
 621}
 622
 623static uint32_t _GBCoreRawRead32(struct mCore* core, uint32_t address, int segment) {
 624	struct LR35902Core* cpu = core->cpu;
 625	return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8) |
 626	       (GBView8(cpu, address + 2, segment) << 16) | (GBView8(cpu, address + 3, segment) << 24);
 627}
 628
 629static void _GBCoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
 630	struct LR35902Core* cpu = core->cpu;
 631	GBPatch8(cpu, address, value, NULL, segment);
 632}
 633
 634static void _GBCoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
 635	struct LR35902Core* cpu = core->cpu;
 636	GBPatch8(cpu, address, value, NULL, segment);
 637	GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
 638}
 639
 640static void _GBCoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
 641	struct LR35902Core* cpu = core->cpu;
 642	GBPatch8(cpu, address, value, NULL, segment);
 643	GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
 644	GBPatch8(cpu, address + 2, value >> 16, NULL, segment);
 645	GBPatch8(cpu, address + 3, value >> 24, NULL, segment);
 646}
 647
 648size_t _GBListMemoryBlocks(const struct mCore* core, const struct mCoreMemoryBlock** blocks) {
 649	const struct GB* gb = core->board;
 650	switch (gb->model) {
 651	case GB_MODEL_DMG:
 652	case GB_MODEL_MGB:
 653	case GB_MODEL_SGB:
 654	case GB_MODEL_SGB2:
 655	default:
 656		*blocks = _GBMemoryBlocks;
 657		return sizeof(_GBMemoryBlocks) / sizeof(*_GBMemoryBlocks);
 658	case GB_MODEL_CGB:
 659	case GB_MODEL_AGB:
 660		*blocks = _GBCMemoryBlocks;
 661		return sizeof(_GBCMemoryBlocks) / sizeof(*_GBCMemoryBlocks);
 662	}
 663}
 664
 665void* _GBGetMemoryBlock(struct mCore* core, size_t id, size_t* sizeOut) {
 666	struct GB* gb = core->board;
 667	bool isCgb = gb->model >= GB_MODEL_CGB;
 668	switch (id) {
 669	default:
 670		return NULL;
 671	case GB_REGION_CART_BANK0:
 672		*sizeOut = gb->memory.romSize;
 673		return gb->memory.rom;
 674	case GB_REGION_VRAM:
 675		*sizeOut = GB_SIZE_WORKING_RAM_BANK0 * (isCgb ? 1 : 2);
 676		return gb->video.vram;
 677	case GB_REGION_EXTERNAL_RAM:
 678		*sizeOut = gb->sramSize;
 679		return gb->memory.sram;
 680	case GB_REGION_WORKING_RAM_BANK0:
 681		*sizeOut = GB_SIZE_VRAM * (isCgb ? 8 : 2);
 682		return gb->memory.wram;
 683	case GB_BASE_OAM:
 684		*sizeOut = GB_SIZE_OAM;
 685		return gb->video.oam.raw;
 686	case GB_BASE_HRAM:
 687		*sizeOut = GB_SIZE_HRAM;
 688		return gb->memory.hram;
 689	}
 690}
 691
 692#ifdef USE_DEBUGGERS
 693static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
 694	UNUSED(core);
 695	switch (type) {
 696	case DEBUGGER_CLI:
 697		return true;
 698	default:
 699		return false;
 700	}
 701}
 702
 703static struct mDebuggerPlatform* _GBCoreDebuggerPlatform(struct mCore* core) {
 704	struct GBCore* gbcore = (struct GBCore*) core;
 705	struct GB* gb = core->board;
 706	if (!gbcore->debuggerPlatform) {
 707		struct LR35902Debugger* platform = (struct LR35902Debugger*) LR35902DebuggerPlatformCreate();
 708		if (gb->model >= GB_MODEL_CGB) {
 709			platform->segments = _GBCSegments;
 710		} else {
 711			platform->segments = _GBSegments;
 712		}
 713		gbcore->debuggerPlatform = &platform->d;
 714	}
 715	return gbcore->debuggerPlatform;
 716}
 717
 718static struct CLIDebuggerSystem* _GBCoreCliDebuggerSystem(struct mCore* core) {
 719	return GBCLIDebuggerCreate(core);
 720}
 721
 722static void _GBCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
 723	struct LR35902Core* cpu = core->cpu;
 724	if (core->debugger) {
 725		LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
 726	}
 727	cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
 728	LR35902HotplugAttach(cpu, CPU_COMPONENT_DEBUGGER);
 729	core->debugger = debugger;
 730}
 731
 732static void _GBCoreDetachDebugger(struct mCore* core) {
 733	struct LR35902Core* cpu = core->cpu;
 734	if (core->debugger) {
 735		LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
 736	}
 737	cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
 738	core->debugger = NULL;
 739}
 740
 741static void _GBCoreLoadSymbols(struct mCore* core, struct VFile* vf) {
 742	core->symbolTable = mDebuggerSymbolTableCreate();
 743#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 744	if (!vf) {
 745		vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".sym", O_RDONLY);
 746	}
 747#endif
 748	if (!vf) {
 749		return;
 750	}
 751	GBLoadSymbols(core->symbolTable, vf);
 752}
 753
 754static bool _GBCoreLookupIdentifier(struct mCore* core, const char* name, int32_t* value, int* segment) {
 755	UNUSED(core);
 756	*segment = -1;
 757	int i;
 758	for (i = 0; i < REG_MAX; ++i) {
 759		const char* reg = GBIORegisterNames[i];
 760		if (reg && strcasecmp(reg, name) == 0) {
 761			*value = GB_BASE_IO | i;
 762			return true;
 763		}
 764	}
 765	return false;
 766}
 767#endif
 768
 769static struct mCheatDevice* _GBCoreCheatDevice(struct mCore* core) {
 770	struct GBCore* gbcore = (struct GBCore*) core;
 771	if (!gbcore->cheatDevice) {
 772		gbcore->cheatDevice = GBCheatDeviceCreate();
 773		((struct LR35902Core*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbcore->cheatDevice->d;
 774		LR35902HotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
 775		gbcore->cheatDevice->p = core;
 776	}
 777	return gbcore->cheatDevice;
 778}
 779
 780static size_t _GBCoreSavedataClone(struct mCore* core, void** sram) {
 781	struct GB* gb = core->board;
 782	struct VFile* vf = gb->sramVf;
 783	if (vf) {
 784		*sram = malloc(vf->size(vf));
 785		vf->seek(vf, 0, SEEK_SET);
 786		return vf->read(vf, *sram, vf->size(vf));
 787	}
 788	*sram = malloc(gb->sramSize);
 789	memcpy(*sram, gb->memory.sram, gb->sramSize);
 790	return gb->sramSize;
 791}
 792
 793static bool _GBCoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
 794	struct GB* gb = core->board;
 795	if (!writeback) {
 796		struct VFile* vf = VFileMemChunk(sram, size);
 797		GBSavedataMask(gb, vf, true);
 798		return true;
 799	}
 800	struct VFile* vf = gb->sramVf;
 801	if (vf) {
 802		vf->seek(vf, 0, SEEK_SET);
 803		return vf->write(vf, sram, size) > 0;
 804	}
 805	if (size > 0x20000) {
 806		size = 0x20000;
 807	}
 808	GBResizeSram(gb, size);
 809	memcpy(gb->memory.sram, sram, size);
 810	return true;
 811}
 812
 813static size_t _GBCoreListVideoLayers(const struct mCore* core, const struct mCoreChannelInfo** info) {
 814	UNUSED(core);
 815	*info = _GBVideoLayers;
 816	return sizeof(_GBVideoLayers) / sizeof(*_GBVideoLayers);
 817}
 818
 819static size_t _GBCoreListAudioChannels(const struct mCore* core, const struct mCoreChannelInfo** info) {
 820	UNUSED(core);
 821	*info = _GBAudioChannels;
 822	return sizeof(_GBAudioChannels) / sizeof(*_GBAudioChannels);
 823}
 824
 825static void _GBCoreEnableVideoLayer(struct mCore* core, size_t id, bool enable) {
 826	struct GB* gb = core->board;
 827	switch (id) {
 828	case 0:
 829		gb->video.renderer->disableBG = !enable;
 830		break;
 831	case 1:
 832		gb->video.renderer->disableOBJ = !enable;
 833		break;
 834	case 2:
 835		gb->video.renderer->disableWIN = !enable;
 836		break;
 837	default:
 838		break;
 839	}
 840}
 841
 842static void _GBCoreEnableAudioChannel(struct mCore* core, size_t id, bool enable) {
 843	struct GB* gb = core->board;
 844	switch (id) {
 845	case 0:
 846	case 1:
 847	case 2:
 848	case 3:
 849		gb->audio.forceDisableCh[id] = !enable;
 850		break;
 851	default:
 852		break;
 853	}
 854}
 855
 856#ifndef MINIMAL_CORE
 857static void _GBCoreStartVideoLog(struct mCore* core, struct mVideoLogContext* context) {
 858	struct GBCore* gbcore = (struct GBCore*) core;
 859	struct GB* gb = core->board;
 860	gbcore->logContext = context;
 861
 862	int channelId = mVideoLoggerAddChannel(context);
 863	gbcore->proxyRenderer.logger = malloc(sizeof(struct mVideoLogger));
 864	mVideoLoggerRendererCreate(gbcore->proxyRenderer.logger, false);
 865	mVideoLoggerAttachChannel(gbcore->proxyRenderer.logger, context, channelId);
 866	gbcore->proxyRenderer.logger->block = false;
 867
 868	GBVideoProxyRendererCreate(&gbcore->proxyRenderer, &gbcore->renderer.d);
 869	GBVideoProxyRendererShim(&gb->video, &gbcore->proxyRenderer);
 870}
 871
 872static void _GBCoreEndVideoLog(struct mCore* core) {
 873	struct GBCore* gbcore = (struct GBCore*) core;
 874	struct GB* gb = core->board;
 875	GBVideoProxyRendererUnshim(&gb->video, &gbcore->proxyRenderer);
 876	free(gbcore->proxyRenderer.logger);
 877	gbcore->proxyRenderer.logger = NULL;
 878}
 879#endif
 880
 881struct mCore* GBCoreCreate(void) {
 882	struct GBCore* gbcore = malloc(sizeof(*gbcore));
 883	struct mCore* core = &gbcore->d;
 884	memset(&core->opts, 0, sizeof(core->opts));
 885	core->cpu = NULL;
 886	core->board = NULL;
 887	core->debugger = NULL;
 888	core->symbolTable = NULL;
 889	core->init = _GBCoreInit;
 890	core->deinit = _GBCoreDeinit;
 891	core->platform = _GBCorePlatform;
 892	core->setSync = _GBCoreSetSync;
 893	core->loadConfig = _GBCoreLoadConfig;
 894	core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
 895	core->setVideoBuffer = _GBCoreSetVideoBuffer;
 896	core->getPixels = _GBCoreGetPixels;
 897	core->putPixels = _GBCorePutPixels;
 898	core->getAudioChannel = _GBCoreGetAudioChannel;
 899	core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
 900	core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
 901	core->setAVStream = _GBCoreSetAVStream;
 902	core->addCoreCallbacks = _GBCoreAddCoreCallbacks;
 903	core->clearCoreCallbacks = _GBCoreClearCoreCallbacks;
 904	core->isROM = GBIsROM;
 905	core->loadROM = _GBCoreLoadROM;
 906	core->loadBIOS = _GBCoreLoadBIOS;
 907	core->loadSave = _GBCoreLoadSave;
 908	core->loadTemporarySave = _GBCoreLoadTemporarySave;
 909	core->loadPatch = _GBCoreLoadPatch;
 910	core->unloadROM = _GBCoreUnloadROM;
 911	core->checksum = _GBCoreChecksum;
 912	core->reset = _GBCoreReset;
 913	core->runFrame = _GBCoreRunFrame;
 914	core->runLoop = _GBCoreRunLoop;
 915	core->step = _GBCoreStep;
 916	core->stateSize = _GBCoreStateSize;
 917	core->loadState = _GBCoreLoadState;
 918	core->saveState = _GBCoreSaveState;
 919	core->setKeys = _GBCoreSetKeys;
 920	core->addKeys = _GBCoreAddKeys;
 921	core->clearKeys = _GBCoreClearKeys;
 922	core->setCursorLocation = _GBCoreSetCursorLocation;
 923	core->setCursorDown = _GBCoreSetCursorDown;
 924	core->frameCounter = _GBCoreFrameCounter;
 925	core->frameCycles = _GBCoreFrameCycles;
 926	core->frequency = _GBCoreFrequency;
 927	core->getGameTitle = _GBCoreGetGameTitle;
 928	core->getGameCode = _GBCoreGetGameCode;
 929	core->setPeripheral = _GBCoreSetPeripheral;
 930	core->busRead8 = _GBCoreBusRead8;
 931	core->busRead16 = _GBCoreBusRead16;
 932	core->busRead32 = _GBCoreBusRead32;
 933	core->busWrite8 = _GBCoreBusWrite8;
 934	core->busWrite16 = _GBCoreBusWrite16;
 935	core->busWrite32 = _GBCoreBusWrite32;
 936	core->rawRead8 = _GBCoreRawRead8;
 937	core->rawRead16 = _GBCoreRawRead16;
 938	core->rawRead32 = _GBCoreRawRead32;
 939	core->rawWrite8 = _GBCoreRawWrite8;
 940	core->rawWrite16 = _GBCoreRawWrite16;
 941	core->rawWrite32 = _GBCoreRawWrite32;
 942	core->listMemoryBlocks = _GBListMemoryBlocks;
 943	core->getMemoryBlock = _GBGetMemoryBlock;
 944#ifdef USE_DEBUGGERS
 945	core->supportsDebuggerType = _GBCoreSupportsDebuggerType;
 946	core->debuggerPlatform = _GBCoreDebuggerPlatform;
 947	core->cliDebuggerSystem = _GBCoreCliDebuggerSystem;
 948	core->attachDebugger = _GBCoreAttachDebugger;
 949	core->detachDebugger = _GBCoreDetachDebugger;
 950	core->loadSymbols = _GBCoreLoadSymbols;
 951	core->lookupIdentifier = _GBCoreLookupIdentifier;
 952#endif
 953	core->cheatDevice = _GBCoreCheatDevice;
 954	core->savedataClone = _GBCoreSavedataClone;
 955	core->savedataRestore = _GBCoreSavedataRestore;
 956	core->listVideoLayers = _GBCoreListVideoLayers;
 957	core->listAudioChannels = _GBCoreListAudioChannels;
 958	core->enableVideoLayer = _GBCoreEnableVideoLayer;
 959	core->enableAudioChannel = _GBCoreEnableAudioChannel;
 960#ifndef MINIMAL_CORE
 961	core->startVideoLog = _GBCoreStartVideoLog;
 962	core->endVideoLog = _GBCoreEndVideoLog;
 963#endif
 964	return core;
 965}
 966
 967#ifndef MINIMAL_CORE
 968static void _GBVLPStartFrameCallback(void *context) {
 969	struct mCore* core = context;
 970	struct GBCore* gbcore = (struct GBCore*) core;
 971	struct GB* gb = core->board;
 972
 973	if (!mVideoLoggerRendererRun(gbcore->proxyRenderer.logger, true)) {
 974		GBVideoProxyRendererUnshim(&gb->video, &gbcore->proxyRenderer);
 975		mVideoLogContextRewind(gbcore->logContext, core);
 976		GBVideoProxyRendererShim(&gb->video, &gbcore->proxyRenderer);
 977	}
 978}
 979
 980static bool _GBVLPInit(struct mCore* core) {
 981	struct GBCore* gbcore = (struct GBCore*) core;
 982	if (!_GBCoreInit(core)) {
 983		return false;
 984	}
 985	gbcore->proxyRenderer.logger = malloc(sizeof(struct mVideoLogger));
 986	mVideoLoggerRendererCreate(gbcore->proxyRenderer.logger, true);
 987	GBVideoProxyRendererCreate(&gbcore->proxyRenderer, NULL);
 988	memset(&gbcore->logCallbacks, 0, sizeof(gbcore->logCallbacks));
 989	gbcore->logCallbacks.videoFrameStarted = _GBVLPStartFrameCallback;
 990	gbcore->logCallbacks.context = core;
 991	core->addCoreCallbacks(core, &gbcore->logCallbacks);
 992	return true;
 993}
 994
 995static void _GBVLPDeinit(struct mCore* core) {
 996	struct GBCore* gbcore = (struct GBCore*) core;
 997	if (gbcore->logContext) {
 998		mVideoLogContextDestroy(core, gbcore->logContext);
 999	}
1000	_GBCoreDeinit(core);
1001}
1002
1003static void _GBVLPReset(struct mCore* core) {
1004	struct GBCore* gbcore = (struct GBCore*) core;
1005	struct GB* gb = (struct GB*) core->board;
1006	if (gb->video.renderer == &gbcore->proxyRenderer.d) {
1007		GBVideoProxyRendererUnshim(&gb->video, &gbcore->proxyRenderer);
1008	} else if (gbcore->renderer.outputBuffer) {
1009		struct GBVideoRenderer* renderer = &gbcore->renderer.d;
1010		GBVideoAssociateRenderer(&gb->video, renderer);
1011	}
1012
1013	LR35902Reset(core->cpu);
1014	mVideoLogContextRewind(gbcore->logContext, core);
1015	GBVideoProxyRendererShim(&gb->video, &gbcore->proxyRenderer);
1016
1017	// Make sure CPU loop never spins
1018	GBHalt(gb->cpu);
1019	gb->memory.ie = 0;
1020	gb->memory.ime = false;
1021}
1022
1023static bool _GBVLPLoadROM(struct mCore* core, struct VFile* vf) {
1024	struct GBCore* gbcore = (struct GBCore*) core;
1025	gbcore->logContext = mVideoLogContextCreate(NULL);
1026	if (!mVideoLogContextLoad(gbcore->logContext, vf)) {
1027		mVideoLogContextDestroy(core, gbcore->logContext);
1028		gbcore->logContext = NULL;
1029		return false;
1030	}
1031	mVideoLoggerAttachChannel(gbcore->proxyRenderer.logger, gbcore->logContext, 0);
1032	return true;
1033}
1034
1035static bool _GBVLPLoadState(struct mCore* core, const void* buffer) {
1036	struct GB* gb = (struct GB*) core->board;
1037	const struct GBSerializedState* state = buffer;
1038
1039	gb->timing.root = NULL;
1040	gb->model = state->model;
1041
1042	gb->cpu->pc = GB_BASE_HRAM;
1043	gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc);
1044
1045	GBVideoDeserialize(&gb->video, state);
1046	GBIODeserialize(gb, state);
1047	GBAudioReset(&gb->audio);
1048
1049	// Make sure CPU loop never spins
1050	GBHalt(gb->cpu);
1051	gb->memory.ie = 0;
1052	gb->memory.ime = false;
1053
1054	return true;
1055}
1056
1057static bool _returnTrue(struct VFile* vf) {
1058	UNUSED(vf);
1059	return true;
1060}
1061
1062struct mCore* GBVideoLogPlayerCreate(void) {
1063	struct mCore* core = GBCoreCreate();
1064	core->init = _GBVLPInit;
1065	core->deinit = _GBVLPDeinit;
1066	core->reset = _GBVLPReset;
1067	core->loadROM = _GBVLPLoadROM;
1068	core->loadState = _GBVLPLoadState;
1069	core->isROM = _returnTrue;
1070	return core;
1071}
1072#else
1073struct mCore* GBVideoLogPlayerCreate(void) {
1074	return false;
1075}
1076#endif