all repos — mgba @ 57815667178964641dda561eb5378cedb19d607d

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