all repos — mgba @ b5af2b584adfa9b11ab0675872ec8993347507a9

mGBA Game Boy Advance Emulator

src/gb/core.c (view raw)

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