all repos — mgba @ 49a9da3e5ca5e414d58a2f15920d1349c01124c4

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