all repos — mgba @ 9ec3b15f47bf54defaa3d3e17731cc3cf9d23335

mGBA Game Boy Advance Emulator

src/gb/core.c (view raw)

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