all repos — mgba @ 00e8b9877f76420e1e85c34cc2a156d1a52b8a30

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