all repos — mgba @ 82c06bb42e7874bfc658a58d4a12be8230ac055d

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