all repos — mgba @ d7f2f5ba06fdf8e3d6feff7d7962323b813a92ad

mGBA Game Boy Advance Emulator

src/gb/core.c (view raw)

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