all repos — mgba @ 9b2e5a5541870e92c1b8cb235fd57d88a74b58dc

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