all repos — mgba @ 9cedf74b77dc0ea1341343cc285ace153ff5ed23

mGBA Game Boy Advance Emulator

src/gba/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/gba/core.h>
   7
   8#include <mgba/core/core.h>
   9#include <mgba/core/log.h>
  10#include <mgba/internal/arm/debugger/debugger.h>
  11#include <mgba/internal/gba/cheats.h>
  12#include <mgba/internal/gba/gba.h>
  13#include <mgba/internal/gba/io.h>
  14#include <mgba/internal/gba/extra/cli.h>
  15#include <mgba/internal/gba/overrides.h>
  16#ifndef DISABLE_THREADING
  17#include <mgba/feature/thread-proxy.h>
  18#endif
  19#include <mgba/internal/gba/renderers/proxy.h>
  20#include <mgba/internal/gba/renderers/video-software.h>
  21#include <mgba/internal/gba/savedata.h>
  22#include <mgba/internal/gba/serialize.h>
  23#include <mgba-util/memory.h>
  24#include <mgba-util/patch.h>
  25#include <mgba-util/vfs.h>
  26
  27static const struct mCoreChannelInfo _GBAVideoLayers[] = {
  28	{ 0, "bg0", "Background 0", NULL },
  29	{ 1, "bg1", "Background 1", NULL },
  30	{ 2, "bg2", "Background 2", NULL },
  31	{ 3, "bg3", "Background 3", NULL },
  32	{ 4, "obj", "Objects", NULL },
  33};
  34
  35static const struct mCoreChannelInfo _GBAAudioChannels[] = {
  36	{ 0, "ch1", "PSG Channel 1", "Square/Sweep" },
  37	{ 1, "ch2", "PSG Channel 2", "Square" },
  38	{ 2, "ch3", "PSG Channel 3", "PCM" },
  39	{ 3, "ch4", "PSG Channel 4", "Noise" },
  40	{ 4, "chA", "FIFO Channel A", NULL },
  41	{ 5, "chB", "FIFO Channel B", NULL },
  42};
  43
  44static const struct mCoreMemoryBlock _GBAMemoryBlocks[] = {
  45	{ -1, "mem", "All", "All", 0, 0x10000000, 0x10000000, mCORE_MEMORY_VIRTUAL },
  46	{ REGION_BIOS, "bios", "BIOS", "BIOS (16kiB)", BASE_BIOS, SIZE_BIOS, SIZE_BIOS, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  47	{ REGION_WORKING_RAM, "wram", "EWRAM", "Working RAM (256kiB)", BASE_WORKING_RAM, BASE_WORKING_RAM + SIZE_WORKING_RAM, SIZE_WORKING_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  48	{ REGION_WORKING_IRAM, "iwram", "IWRAM", "Internal Working RAM (32kiB)", BASE_WORKING_IRAM, BASE_WORKING_IRAM + SIZE_WORKING_IRAM, SIZE_WORKING_IRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  49	{ REGION_IO, "io", "MMIO", "Memory-Mapped I/O", BASE_IO, BASE_IO + SIZE_IO, SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  50	{ REGION_PALETTE_RAM, "palette", "Palette", "Palette RAM (1kiB)", BASE_PALETTE_RAM, BASE_PALETTE_RAM + SIZE_PALETTE_RAM, SIZE_PALETTE_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  51	{ REGION_VRAM, "vram", "VRAM", "Video RAM (96kiB)", BASE_VRAM, BASE_VRAM + SIZE_VRAM, SIZE_VRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  52	{ REGION_OAM, "oam", "OAM", "OBJ Attribute Memory (1kiB)", BASE_OAM, BASE_OAM + SIZE_OAM, SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  53	{ REGION_CART0, "cart0", "ROM", "Game Pak (32MiB)", BASE_CART0, BASE_CART0 + SIZE_CART0, SIZE_CART0, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  54	{ REGION_CART1, "cart1", "ROM WS1", "Game Pak (Waitstate 1)", BASE_CART1, BASE_CART1 + SIZE_CART1, SIZE_CART1, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  55	{ REGION_CART2, "cart2", "ROM WS2", "Game Pak (Waitstate 2)", BASE_CART2, BASE_CART2 + SIZE_CART2, SIZE_CART2, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  56};
  57
  58static const struct mCoreMemoryBlock _GBAMemoryBlocksSRAM[] = {
  59	{ -1, "mem", "All", "All", 0, 0x10000000, 0x10000000, mCORE_MEMORY_VIRTUAL },
  60	{ REGION_BIOS, "bios", "BIOS", "BIOS (16kiB)", BASE_BIOS, SIZE_BIOS, SIZE_BIOS, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  61	{ REGION_WORKING_RAM, "wram", "EWRAM", "Working RAM (256kiB)", BASE_WORKING_RAM, BASE_WORKING_RAM + SIZE_WORKING_RAM, SIZE_WORKING_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  62	{ REGION_WORKING_IRAM, "iwram", "IWRAM", "Internal Working RAM (32kiB)", BASE_WORKING_IRAM, BASE_WORKING_IRAM + SIZE_WORKING_IRAM, SIZE_WORKING_IRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  63	{ REGION_IO, "io", "MMIO", "Memory-Mapped I/O", BASE_IO, BASE_IO + SIZE_IO, SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  64	{ REGION_PALETTE_RAM, "palette", "Palette", "Palette RAM (1kiB)", BASE_PALETTE_RAM, BASE_PALETTE_RAM + SIZE_PALETTE_RAM, SIZE_PALETTE_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  65	{ REGION_VRAM, "vram", "VRAM", "Video RAM (96kiB)", BASE_VRAM, BASE_VRAM + SIZE_VRAM, SIZE_VRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  66	{ REGION_OAM, "oam", "OAM", "OBJ Attribute Memory (1kiB)", BASE_OAM, BASE_OAM + SIZE_OAM, SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  67	{ REGION_CART0, "cart0", "ROM", "Game Pak (32MiB)", BASE_CART0, BASE_CART0 + SIZE_CART0, SIZE_CART0, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  68	{ REGION_CART1, "cart1", "ROM WS1", "Game Pak (Waitstate 1)", BASE_CART1, BASE_CART1 + SIZE_CART1, SIZE_CART1, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  69	{ REGION_CART2, "cart2", "ROM WS2", "Game Pak (Waitstate 2)", BASE_CART2, BASE_CART2 + SIZE_CART2, SIZE_CART2, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  70	{ REGION_CART_SRAM, "sram", "SRAM", "Static RAM (64kiB)", BASE_CART_SRAM, BASE_CART_SRAM + SIZE_CART_SRAM, SIZE_CART_SRAM, true },
  71};
  72
  73static const struct mCoreMemoryBlock _GBAMemoryBlocksFlash512[] = {
  74	{ -1, "mem", "All", "All", 0, 0x10000000, 0x10000000, mCORE_MEMORY_VIRTUAL },
  75	{ REGION_BIOS, "bios", "BIOS", "BIOS (16kiB)", BASE_BIOS, SIZE_BIOS, SIZE_BIOS, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  76	{ REGION_WORKING_RAM, "wram", "EWRAM", "Working RAM (256kiB)", BASE_WORKING_RAM, BASE_WORKING_RAM + SIZE_WORKING_RAM, SIZE_WORKING_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  77	{ REGION_WORKING_IRAM, "iwram", "IWRAM", "Internal Working RAM (32kiB)", BASE_WORKING_IRAM, BASE_WORKING_IRAM + SIZE_WORKING_IRAM, SIZE_WORKING_IRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  78	{ REGION_IO, "io", "MMIO", "Memory-Mapped I/O", BASE_IO, BASE_IO + SIZE_IO, SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  79	{ REGION_PALETTE_RAM, "palette", "Palette", "Palette RAM (1kiB)", BASE_PALETTE_RAM, BASE_PALETTE_RAM + SIZE_PALETTE_RAM, SIZE_PALETTE_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  80	{ REGION_VRAM, "vram", "VRAM", "Video RAM (96kiB)", BASE_VRAM, BASE_VRAM + SIZE_VRAM, SIZE_VRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  81	{ REGION_OAM, "oam", "OAM", "OBJ Attribute Memory (1kiB)", BASE_OAM, BASE_OAM + SIZE_OAM, SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  82	{ REGION_CART0, "cart0", "ROM", "Game Pak (32MiB)", BASE_CART0, BASE_CART0 + SIZE_CART0, SIZE_CART0, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  83	{ REGION_CART1, "cart1", "ROM WS1", "Game Pak (Waitstate 1)", BASE_CART1, BASE_CART1 + SIZE_CART1, SIZE_CART1, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  84	{ REGION_CART2, "cart2", "ROM WS2", "Game Pak (Waitstate 2)", BASE_CART2, BASE_CART2 + SIZE_CART2, SIZE_CART2, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  85	{ REGION_CART_SRAM, "sram", "Flash", "Flash Memory (64kiB)", BASE_CART_SRAM, BASE_CART_SRAM + SIZE_CART_FLASH512, SIZE_CART_FLASH512, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  86};
  87
  88static const struct mCoreMemoryBlock _GBAMemoryBlocksFlash1M[] = {
  89	{ -1, "mem", "All", "All", 0, 0x10000000, 0x10000000, mCORE_MEMORY_VIRTUAL },
  90	{ REGION_BIOS, "bios", "BIOS", "BIOS (16kiB)", BASE_BIOS, SIZE_BIOS, SIZE_BIOS, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  91	{ REGION_WORKING_RAM, "wram", "EWRAM", "Working RAM (256kiB)", BASE_WORKING_RAM, BASE_WORKING_RAM + SIZE_WORKING_RAM, SIZE_WORKING_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  92	{ REGION_WORKING_IRAM, "iwram", "IWRAM", "Internal Working RAM (32kiB)", BASE_WORKING_IRAM, BASE_WORKING_IRAM + SIZE_WORKING_IRAM, SIZE_WORKING_IRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  93	{ REGION_IO, "io", "MMIO", "Memory-Mapped I/O", BASE_IO, BASE_IO + SIZE_IO, SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  94	{ REGION_PALETTE_RAM, "palette", "Palette", "Palette RAM (1kiB)", BASE_PALETTE_RAM, BASE_PALETTE_RAM + SIZE_PALETTE_RAM, SIZE_PALETTE_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  95	{ REGION_VRAM, "vram", "VRAM", "Video RAM (96kiB)", BASE_VRAM, BASE_VRAM + SIZE_VRAM, SIZE_VRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  96	{ REGION_OAM, "oam", "OAM", "OBJ Attribute Memory (1kiB)", BASE_OAM, BASE_OAM + SIZE_OAM, SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  97	{ REGION_CART0, "cart0", "ROM", "Game Pak (32MiB)", BASE_CART0, BASE_CART0 + SIZE_CART0, SIZE_CART0, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  98	{ REGION_CART1, "cart1", "ROM WS1", "Game Pak (Waitstate 1)", BASE_CART1, BASE_CART1 + SIZE_CART1, SIZE_CART1, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  99	{ REGION_CART2, "cart2", "ROM WS2", "Game Pak (Waitstate 2)", BASE_CART2, BASE_CART2 + SIZE_CART2, SIZE_CART2, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
 100	{ REGION_CART_SRAM, "sram", "Flash", "Flash Memory (64kiB)", BASE_CART_SRAM, BASE_CART_SRAM + SIZE_CART_FLASH512, SIZE_CART_FLASH1M, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED, 1 },
 101};
 102
 103static const struct mCoreMemoryBlock _GBAMemoryBlocksEEPROM[] = {
 104	{ -1, "mem", "All", "All", 0, 0x10000000, 0x10000000, mCORE_MEMORY_VIRTUAL },
 105	{ REGION_BIOS, "bios", "BIOS", "BIOS (16kiB)", BASE_BIOS, SIZE_BIOS, SIZE_BIOS, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
 106	{ REGION_WORKING_RAM, "wram", "EWRAM", "Working RAM (256kiB)", BASE_WORKING_RAM, BASE_WORKING_RAM + SIZE_WORKING_RAM, SIZE_WORKING_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 107	{ REGION_WORKING_IRAM, "iwram", "IWRAM", "Internal Working RAM (32kiB)", BASE_WORKING_IRAM, BASE_WORKING_IRAM + SIZE_WORKING_IRAM, SIZE_WORKING_IRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 108	{ REGION_IO, "io", "MMIO", "Memory-Mapped I/O", BASE_IO, BASE_IO + SIZE_IO, SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 109	{ REGION_PALETTE_RAM, "palette", "Palette", "Palette RAM (1kiB)", BASE_PALETTE_RAM, BASE_PALETTE_RAM + SIZE_PALETTE_RAM, SIZE_PALETTE_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 110	{ REGION_VRAM, "vram", "VRAM", "Video RAM (96kiB)", BASE_VRAM, BASE_VRAM + SIZE_VRAM, SIZE_VRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 111	{ REGION_OAM, "oam", "OAM", "OBJ Attribute Memory (1kiB)", BASE_OAM, BASE_OAM + SIZE_OAM, SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 112	{ REGION_CART0, "cart0", "ROM", "Game Pak (32MiB)", BASE_CART0, BASE_CART0 + SIZE_CART0, SIZE_CART0, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
 113	{ REGION_CART1, "cart1", "ROM WS1", "Game Pak (Waitstate 1)", BASE_CART1, BASE_CART1 + SIZE_CART1, SIZE_CART1, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
 114	{ REGION_CART2, "cart2", "ROM WS2", "Game Pak (Waitstate 2)", BASE_CART2, BASE_CART2 + SIZE_CART2, SIZE_CART2, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
 115	{ REGION_CART_SRAM_MIRROR, "eeprom", "EEPROM", "EEPROM (8kiB)", 0, SIZE_CART_EEPROM, SIZE_CART_EEPROM, mCORE_MEMORY_RW },
 116};
 117
 118struct mVideoLogContext;
 119struct GBACore {
 120	struct mCore d;
 121	struct GBAVideoSoftwareRenderer renderer;
 122	struct GBAVideoProxyRenderer proxyRenderer;
 123	struct mVideoLogContext* logContext;
 124	struct mCoreCallbacks logCallbacks;
 125#ifndef DISABLE_THREADING
 126	struct mVideoThreadProxy threadProxy;
 127	int threadedVideo;
 128#endif
 129	int keys;
 130	struct mCPUComponent* components[CPU_COMPONENT_MAX];
 131	const struct Configuration* overrides;
 132	struct mDebuggerPlatform* debuggerPlatform;
 133	struct mCheatDevice* cheatDevice;
 134};
 135
 136static bool _GBACoreInit(struct mCore* core) {
 137	struct GBACore* gbacore = (struct GBACore*) core;
 138
 139	struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
 140	struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
 141	if (!cpu || !gba) {
 142		free(cpu);
 143		free(gba);
 144		return false;
 145	}
 146	core->cpu = cpu;
 147	core->board = gba;
 148	core->debugger = NULL;
 149	core->symbolTable = NULL;
 150	gbacore->overrides = NULL;
 151	gbacore->debuggerPlatform = NULL;
 152	gbacore->cheatDevice = NULL;
 153	gbacore->logContext = NULL;
 154
 155	GBACreate(gba);
 156	// TODO: Restore cheats
 157	memset(gbacore->components, 0, sizeof(gbacore->components));
 158	ARMSetComponents(cpu, &gba->d, CPU_COMPONENT_MAX, gbacore->components);
 159	ARMInit(cpu);
 160	mRTCGenericSourceInit(&core->rtc, core);
 161	gba->rtcSource = &core->rtc.d;
 162
 163	GBAVideoSoftwareRendererCreate(&gbacore->renderer);
 164	gbacore->renderer.outputBuffer = NULL;
 165
 166#ifndef DISABLE_THREADING
 167	gbacore->threadedVideo = false;
 168	mVideoThreadProxyCreate(&gbacore->threadProxy);
 169#endif
 170	gbacore->proxyRenderer.logger = NULL;
 171
 172	gbacore->keys = 0;
 173	gba->keySource = &gbacore->keys;
 174
 175#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 176	mDirectorySetInit(&core->dirs);
 177#endif
 178	
 179	return true;
 180}
 181
 182static void _GBACoreDeinit(struct mCore* core) {
 183	ARMDeinit(core->cpu);
 184	GBADestroy(core->board);
 185	mappedMemoryFree(core->cpu, sizeof(struct ARMCore));
 186	mappedMemoryFree(core->board, sizeof(struct GBA));
 187#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 188	mDirectorySetDeinit(&core->dirs);
 189#endif
 190
 191	struct GBACore* gbacore = (struct GBACore*) core;
 192	free(gbacore->debuggerPlatform);
 193	if (gbacore->cheatDevice) {
 194		mCheatDeviceDestroy(gbacore->cheatDevice);
 195	}
 196	free(gbacore->cheatDevice);
 197	mCoreConfigFreeOpts(&core->opts);
 198	free(core);
 199}
 200
 201static enum mPlatform _GBACorePlatform(const struct mCore* core) {
 202	UNUSED(core);
 203	return PLATFORM_GBA;
 204}
 205
 206static void _GBACoreSetSync(struct mCore* core, struct mCoreSync* sync) {
 207	struct GBA* gba = core->board;
 208	gba->sync = sync;
 209}
 210
 211static void _GBACoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
 212	struct GBA* gba = core->board;
 213	if (core->opts.mute) {
 214		gba->audio.masterVolume = 0;
 215	} else {
 216		gba->audio.masterVolume = core->opts.volume;
 217	}
 218	gba->video.frameskip = core->opts.frameskip;
 219
 220#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 221	struct GBACore* gbacore = (struct GBACore*) core;
 222	gbacore->overrides = mCoreConfigGetOverridesConst(config);
 223#endif
 224
 225	const char* idleOptimization = mCoreConfigGetValue(config, "idleOptimization");
 226	if (idleOptimization) {
 227		if (strcasecmp(idleOptimization, "ignore") == 0) {
 228			gba->idleOptimization = IDLE_LOOP_IGNORE;
 229		} else if (strcasecmp(idleOptimization, "remove") == 0) {
 230			gba->idleOptimization = IDLE_LOOP_REMOVE;
 231		} else if (strcasecmp(idleOptimization, "detect") == 0) {
 232			if (gba->idleLoop == IDLE_LOOP_NONE) {
 233				gba->idleOptimization = IDLE_LOOP_DETECT;
 234			} else {
 235				gba->idleOptimization = IDLE_LOOP_REMOVE;
 236			}
 237		}
 238	}
 239
 240	mCoreConfigCopyValue(&core->config, config, "gba.bios");
 241
 242#ifndef DISABLE_THREADING
 243	mCoreConfigGetIntValue(config, "threadedVideo", &gbacore->threadedVideo);
 244#endif
 245}
 246
 247static void _GBACoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
 248	UNUSED(core);
 249	*width = VIDEO_HORIZONTAL_PIXELS;
 250	*height = VIDEO_VERTICAL_PIXELS;
 251}
 252
 253static void _GBACoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
 254	struct GBACore* gbacore = (struct GBACore*) core;
 255	gbacore->renderer.outputBuffer = buffer;
 256	gbacore->renderer.outputBufferStride = stride;
 257}
 258
 259static void _GBACoreGetPixels(struct mCore* core, const void** buffer, size_t* stride) {
 260	struct GBACore* gbacore = (struct GBACore*) core;
 261	gbacore->renderer.d.getPixels(&gbacore->renderer.d, stride, buffer);
 262}
 263
 264static void _GBACorePutPixels(struct mCore* core, const void* buffer, size_t stride) {
 265	struct GBACore* gbacore = (struct GBACore*) core;
 266	gbacore->renderer.d.putPixels(&gbacore->renderer.d, stride, buffer);
 267}
 268
 269static struct blip_t* _GBACoreGetAudioChannel(struct mCore* core, int ch) {
 270	struct GBA* gba = core->board;
 271	switch (ch) {
 272	case 0:
 273		return gba->audio.psg.left;
 274	case 1:
 275		return gba->audio.psg.right;
 276	default:
 277		return NULL;
 278	}
 279}
 280
 281static void _GBACoreSetAudioBufferSize(struct mCore* core, size_t samples) {
 282	struct GBA* gba = core->board;
 283	GBAAudioResizeBuffer(&gba->audio, samples);
 284}
 285
 286static size_t _GBACoreGetAudioBufferSize(struct mCore* core) {
 287	struct GBA* gba = core->board;
 288	return gba->audio.samples;
 289}
 290
 291static void _GBACoreAddCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) {
 292	struct GBA* gba = core->board;
 293	*mCoreCallbacksListAppend(&gba->coreCallbacks) = *coreCallbacks;
 294}
 295
 296static void _GBACoreClearCoreCallbacks(struct mCore* core) {
 297	struct GBA* gba = core->board;
 298	mCoreCallbacksListClear(&gba->coreCallbacks);
 299}
 300
 301static void _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
 302	struct GBA* gba = core->board;
 303	gba->stream = stream;
 304	if (stream && stream->videoDimensionsChanged) {
 305		stream->videoDimensionsChanged(stream, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
 306	}
 307}
 308
 309static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
 310	if (GBAIsMB(vf)) {
 311		return GBALoadMB(core->board, vf);
 312	}
 313	return GBALoadROM(core->board, vf);
 314}
 315
 316static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
 317	UNUSED(type);
 318	if (!GBAIsBIOS(vf)) {
 319		return false;
 320	}
 321	GBALoadBIOS(core->board, vf);
 322	return true;
 323}
 324
 325static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
 326	return GBALoadSave(core->board, vf);
 327}
 328
 329static bool _GBACoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
 330	struct GBA* gba = core->board;
 331	GBASavedataMask(&gba->memory.savedata, vf, false);
 332	return true; // TODO: Return a real value
 333}
 334
 335static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
 336	if (!vf) {
 337		return false;
 338	}
 339	struct Patch patch;
 340	if (!loadPatch(vf, &patch)) {
 341		return false;
 342	}
 343	GBAApplyPatch(core->board, &patch);
 344	return true;
 345}
 346
 347static void _GBACoreUnloadROM(struct mCore* core) {
 348	struct GBACore* gbacore = (struct GBACore*) core;
 349	struct ARMCore* cpu = core->cpu;
 350	if (gbacore->cheatDevice) {
 351		ARMHotplugDetach(cpu, CPU_COMPONENT_CHEAT_DEVICE);
 352		cpu->components[CPU_COMPONENT_CHEAT_DEVICE] = NULL;
 353		mCheatDeviceDestroy(gbacore->cheatDevice);
 354		gbacore->cheatDevice = NULL;
 355	}
 356	return GBAUnloadROM(core->board);
 357}
 358
 359static void _GBACoreChecksum(const struct mCore* core, void* data, enum mCoreChecksumType type) {
 360	struct GBA* gba = (struct GBA*) core->board;
 361	switch (type) {
 362	case CHECKSUM_CRC32:
 363		memcpy(data, &gba->romCrc32, sizeof(gba->romCrc32));
 364		break;
 365	}
 366	return;
 367}
 368
 369static void _GBACoreReset(struct mCore* core) {
 370	struct GBACore* gbacore = (struct GBACore*) core;
 371	struct GBA* gba = (struct GBA*) core->board;
 372	if (gbacore->renderer.outputBuffer) {
 373		struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
 374#ifndef DISABLE_THREADING
 375		if (gbacore->threadedVideo) {
 376			gbacore->proxyRenderer.logger = &gbacore->threadProxy.d;
 377			GBAVideoProxyRendererCreate(&gbacore->proxyRenderer, renderer);
 378			renderer = &gbacore->proxyRenderer.d;
 379		}
 380#endif
 381		GBAVideoAssociateRenderer(&gba->video, renderer);
 382	}
 383
 384	struct GBACartridgeOverride override;
 385	const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
 386	if (cart) {
 387		memcpy(override.id, &cart->id, sizeof(override.id));
 388
 389		if (!strncmp("pokemon red version", &((const char*) gba->memory.rom)[0x108], 20) && gba->romCrc32 != 0xDD88761C) {
 390			// Enable FLASH1M and RTC on Pokémon FireRed ROM hacks
 391			override.savetype = SAVEDATA_FLASH1M;
 392			override.hardware = HW_RTC;
 393			GBAOverrideApply(gba, &override);
 394		} else if (GBAOverrideFind(gbacore->overrides, &override)) {
 395			GBAOverrideApply(gba, &override);
 396		}
 397	}
 398#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 399	if (!gba->biosVf && core->opts.useBios) {
 400		struct VFile* bios = NULL;
 401		bool found = false;
 402		if (core->opts.bios) {
 403			bios = VFileOpen(core->opts.bios, O_RDONLY);
 404			if (bios && GBAIsBIOS(bios)) {
 405				found = true;
 406			} else if (bios) {
 407				bios->close(bios);
 408				bios = NULL;
 409			}
 410		}
 411		if (!found) {
 412			const char* configPath = mCoreConfigGetValue(&core->config, "gba.bios");
 413			if (configPath) {
 414				bios = VFileOpen(configPath, O_RDONLY);
 415			}
 416			if (bios && GBAIsBIOS(bios)) {
 417				found = true;
 418			} else if (bios) {
 419				bios->close(bios);
 420				bios = NULL;
 421			}
 422		}
 423		if (!found) {
 424			char path[PATH_MAX];
 425			mCoreConfigDirectory(path, PATH_MAX);
 426			strncat(path, PATH_SEP "gba_bios.bin", PATH_MAX - strlen(path));
 427			bios = VFileOpen(path, O_RDONLY);
 428			if (bios && GBAIsBIOS(bios)) {
 429				found = true;
 430			} else if (bios) {
 431				bios->close(bios);
 432				bios = NULL;
 433			}
 434		}
 435		if (bios) {
 436			GBALoadBIOS(gba, bios);
 437		}
 438	}
 439#endif
 440
 441	ARMReset(core->cpu);
 442	if (core->opts.skipBios && gba->isPristine) {
 443		GBASkipBIOS(core->board);
 444	}
 445}
 446
 447static void _GBACoreRunFrame(struct mCore* core) {
 448	struct GBA* gba = core->board;
 449	int32_t frameCounter = gba->video.frameCounter;
 450	while (gba->video.frameCounter == frameCounter) {
 451		ARMRunLoop(core->cpu);
 452	}
 453}
 454
 455static void _GBACoreRunLoop(struct mCore* core) {
 456	ARMRunLoop(core->cpu);
 457}
 458
 459static void _GBACoreStep(struct mCore* core) {
 460	ARMRun(core->cpu);
 461}
 462
 463static size_t _GBACoreStateSize(struct mCore* core) {
 464	UNUSED(core);
 465	return sizeof(struct GBASerializedState);
 466}
 467
 468static bool _GBACoreLoadState(struct mCore* core, const void* state) {
 469	return GBADeserialize(core->board, state);
 470}
 471
 472static bool _GBACoreSaveState(struct mCore* core, void* state) {
 473	GBASerialize(core->board, state);
 474	return true;
 475}
 476
 477static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
 478	struct GBACore* gbacore = (struct GBACore*) core;
 479	gbacore->keys = keys;
 480	GBATestKeypadIRQ(core->board);
 481}
 482
 483static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
 484	struct GBACore* gbacore = (struct GBACore*) core;
 485	gbacore->keys |= keys;
 486	GBATestKeypadIRQ(core->board);
 487}
 488
 489static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
 490	struct GBACore* gbacore = (struct GBACore*) core;
 491	gbacore->keys &= ~keys;
 492}
 493
 494static int32_t _GBACoreFrameCounter(const struct mCore* core) {
 495	const struct GBA* gba = core->board;
 496	return gba->video.frameCounter;
 497}
 498
 499static int32_t _GBACoreFrameCycles(const struct mCore* core) {
 500	UNUSED(core);
 501	return VIDEO_TOTAL_LENGTH;
 502}
 503
 504static int32_t _GBACoreFrequency(const struct mCore* core) {
 505	UNUSED(core);
 506	return GBA_ARM7TDMI_FREQUENCY;
 507}
 508
 509static void _GBACoreGetGameTitle(const struct mCore* core, char* title) {
 510	GBAGetGameTitle(core->board, title);
 511}
 512
 513static void _GBACoreGetGameCode(const struct mCore* core, char* title) {
 514	GBAGetGameCode(core->board, title);
 515}
 516
 517static void _GBACoreSetPeripheral(struct mCore* core, int type, void* periph) {
 518	struct GBA* gba = core->board;
 519	switch (type) {
 520	case mPERIPH_ROTATION:
 521		gba->rotationSource = periph;
 522		break;
 523	case mPERIPH_RUMBLE:
 524		gba->rumble = periph;
 525		break;
 526	case mPERIPH_GBA_LUMINANCE:
 527		gba->luminanceSource = periph;
 528		break;
 529	default:
 530		return;
 531	}
 532}
 533
 534static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) {
 535	struct ARMCore* cpu = core->cpu;
 536	return cpu->memory.load8(cpu, address, 0);
 537}
 538
 539static uint32_t _GBACoreBusRead16(struct mCore* core, uint32_t address) {
 540	struct ARMCore* cpu = core->cpu;
 541	return cpu->memory.load16(cpu, address, 0);
 542
 543}
 544
 545static uint32_t _GBACoreBusRead32(struct mCore* core, uint32_t address) {
 546	struct ARMCore* cpu = core->cpu;
 547	return cpu->memory.load32(cpu, address, 0);
 548}
 549
 550static void _GBACoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
 551	struct ARMCore* cpu = core->cpu;
 552	cpu->memory.store8(cpu, address, value, 0);
 553}
 554
 555static void _GBACoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
 556	struct ARMCore* cpu = core->cpu;
 557	cpu->memory.store16(cpu, address, value, 0);
 558}
 559
 560static void _GBACoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
 561	struct ARMCore* cpu = core->cpu;
 562	cpu->memory.store32(cpu, address, value, 0);
 563}
 564
 565static uint32_t _GBACoreRawRead8(struct mCore* core, uint32_t address, int segment) {
 566	UNUSED(segment);
 567	struct ARMCore* cpu = core->cpu;
 568	return GBAView8(cpu, address);
 569}
 570
 571static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address, int segment) {
 572	UNUSED(segment);
 573	struct ARMCore* cpu = core->cpu;
 574	return GBAView16(cpu, address);
 575}
 576
 577static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address, int segment) {
 578	UNUSED(segment);
 579	struct ARMCore* cpu = core->cpu;
 580	return GBAView32(cpu, address);
 581}
 582
 583static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
 584	UNUSED(segment);
 585	struct ARMCore* cpu = core->cpu;
 586	GBAPatch8(cpu, address, value, NULL);
 587}
 588
 589static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
 590	UNUSED(segment);
 591	struct ARMCore* cpu = core->cpu;
 592	GBAPatch16(cpu, address, value, NULL);
 593}
 594
 595static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
 596	UNUSED(segment);
 597	struct ARMCore* cpu = core->cpu;
 598	GBAPatch32(cpu, address, value, NULL);
 599}
 600
 601size_t _GBAListMemoryBlocks(const struct mCore* core, const struct mCoreMemoryBlock** blocks) {
 602	const struct GBA* gba = core->board;
 603	switch (gba->memory.savedata.type) {
 604	case SAVEDATA_SRAM:
 605		*blocks = _GBAMemoryBlocksSRAM;
 606		return sizeof(_GBAMemoryBlocksSRAM) / sizeof(*_GBAMemoryBlocksSRAM);
 607	case SAVEDATA_FLASH512:
 608		*blocks = _GBAMemoryBlocksFlash512;
 609		return sizeof(_GBAMemoryBlocksFlash512) / sizeof(*_GBAMemoryBlocksFlash512);
 610	case SAVEDATA_FLASH1M:
 611		*blocks = _GBAMemoryBlocksFlash1M;
 612		return sizeof(_GBAMemoryBlocksFlash1M) / sizeof(*_GBAMemoryBlocksFlash1M);
 613	case SAVEDATA_EEPROM:
 614		*blocks = _GBAMemoryBlocksEEPROM;
 615		return sizeof(_GBAMemoryBlocksEEPROM) / sizeof(*_GBAMemoryBlocksEEPROM);
 616	default:
 617		*blocks = _GBAMemoryBlocks;
 618		return sizeof(_GBAMemoryBlocks) / sizeof(*_GBAMemoryBlocks);
 619	}
 620}
 621
 622void* _GBAGetMemoryBlock(struct mCore* core, size_t id, size_t* sizeOut) {
 623	struct GBA* gba = core->board;
 624	switch (id) {
 625	default:
 626		return NULL;
 627	case REGION_BIOS:
 628		*sizeOut = SIZE_BIOS;
 629		return gba->memory.bios;
 630	case REGION_WORKING_RAM:
 631		*sizeOut = SIZE_WORKING_RAM;
 632		return gba->memory.wram;
 633	case REGION_WORKING_IRAM:
 634		*sizeOut = SIZE_WORKING_IRAM;
 635		return gba->memory.iwram;
 636	case REGION_PALETTE_RAM:
 637		*sizeOut = SIZE_PALETTE_RAM;
 638		return gba->video.palette;
 639	case REGION_VRAM:
 640		*sizeOut = SIZE_VRAM;
 641		return gba->video.vram;
 642	case REGION_OAM:
 643		*sizeOut = SIZE_OAM;
 644		return gba->video.oam.raw;
 645	case REGION_CART0:
 646	case REGION_CART1:
 647	case REGION_CART2:
 648		*sizeOut = gba->memory.romSize;
 649		return gba->memory.rom;
 650	case REGION_CART_SRAM:
 651		if (gba->memory.savedata.type == SAVEDATA_FLASH1M) {
 652			*sizeOut = SIZE_CART_FLASH1M;
 653			return gba->memory.savedata.currentBank;
 654		}
 655		// Fall through
 656	case REGION_CART_SRAM_MIRROR:
 657		*sizeOut = GBASavedataSize(&gba->memory.savedata);
 658		return gba->memory.savedata.data;
 659	}
 660}
 661
 662#ifdef USE_DEBUGGERS
 663static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
 664	UNUSED(core);
 665	switch (type) {
 666	case DEBUGGER_CLI:
 667		return true;
 668#ifdef USE_GDB_STUB
 669	case DEBUGGER_GDB:
 670		return true;
 671#endif
 672	default:
 673		return false;
 674	}
 675}
 676
 677static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
 678	struct GBACore* gbacore = (struct GBACore*) core;
 679	if (!gbacore->debuggerPlatform) {
 680		gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
 681	}
 682	return gbacore->debuggerPlatform;
 683}
 684
 685static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
 686	return &GBACLIDebuggerCreate(core)->d;
 687}
 688
 689static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
 690	if (core->debugger) {
 691		GBADetachDebugger(core->board);
 692	}
 693	GBAAttachDebugger(core->board, debugger);
 694	core->debugger = debugger;
 695}
 696
 697static void _GBACoreDetachDebugger(struct mCore* core) {
 698	GBADetachDebugger(core->board);
 699	core->debugger = NULL;
 700}
 701
 702static void _GBACoreLoadSymbols(struct mCore* core, struct VFile* vf) {
 703	// TODO
 704}
 705#endif
 706
 707static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
 708	struct GBACore* gbacore = (struct GBACore*) core;
 709	if (!gbacore->cheatDevice) {
 710		gbacore->cheatDevice = GBACheatDeviceCreate();
 711		((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
 712		ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
 713		gbacore->cheatDevice->p = core;
 714	}
 715	return gbacore->cheatDevice;
 716}
 717
 718static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
 719	struct GBA* gba = core->board;
 720	size_t size = GBASavedataSize(&gba->memory.savedata);
 721	if (!size) {
 722		*sram = NULL;
 723		return 0;
 724	}
 725	*sram = malloc(size);
 726	struct VFile* vf = VFileFromMemory(*sram, size);
 727	if (!vf) {
 728		free(*sram);
 729		*sram = NULL;
 730		return 0;
 731	}
 732	bool success = GBASavedataClone(&gba->memory.savedata, vf);
 733	vf->close(vf);
 734	if (!success) {
 735		free(*sram);
 736		*sram = NULL;
 737		return 0;
 738	}
 739	return size;
 740}
 741
 742static bool _GBACoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
 743	struct VFile* vf = VFileMemChunk(sram, size);
 744	if (!vf) {
 745		return false;
 746	}
 747	struct GBA* gba = core->board;
 748	bool success = true;
 749	if (writeback) {
 750		success = GBASavedataLoad(&gba->memory.savedata, vf);
 751		vf->close(vf);
 752	} else {
 753		GBASavedataMask(&gba->memory.savedata, vf, true);
 754	}
 755	return success;
 756}
 757
 758static size_t _GBACoreListVideoLayers(const struct mCore* core, const struct mCoreChannelInfo** info) {
 759	UNUSED(core);
 760	*info = _GBAVideoLayers;
 761	return sizeof(_GBAVideoLayers) / sizeof(*_GBAVideoLayers);
 762}
 763
 764static size_t _GBACoreListAudioChannels(const struct mCore* core, const struct mCoreChannelInfo** info) {
 765	UNUSED(core);
 766	*info = _GBAAudioChannels;
 767	return sizeof(_GBAAudioChannels) / sizeof(*_GBAAudioChannels);
 768}
 769
 770static void _GBACoreEnableVideoLayer(struct mCore* core, size_t id, bool enable) {
 771	struct GBA* gba = core->board;
 772	switch (id) {
 773	case 0:
 774	case 1:
 775	case 2:
 776	case 3:
 777		gba->video.renderer->disableBG[id] = !enable;
 778		break;
 779	case 4:
 780		gba->video.renderer->disableOBJ = !enable;
 781		break;
 782	default:
 783		break;
 784	}
 785}
 786
 787static void _GBACoreEnableAudioChannel(struct mCore* core, size_t id, bool enable) {
 788	struct GBA* gba = core->board;
 789	switch (id) {
 790	case 0:
 791	case 1:
 792	case 2:
 793	case 3:
 794		gba->audio.psg.forceDisableCh[id] = !enable;
 795		break;
 796	case 4:
 797		gba->audio.forceDisableChA = !enable;
 798	case 5:
 799		gba->audio.forceDisableChB = !enable;
 800		break;
 801	default:
 802		break;
 803	}
 804}
 805
 806#ifndef MINIMAL_CORE
 807static void _GBACoreStartVideoLog(struct mCore* core, struct mVideoLogContext* context) {
 808	struct GBACore* gbacore = (struct GBACore*) core;
 809	struct GBA* gba = core->board;
 810	gbacore->logContext = context;
 811
 812	struct GBASerializedState* state = mVideoLogContextInitialState(context, NULL);
 813	state->id = 0;
 814	state->cpu.gprs[ARM_PC] = BASE_WORKING_RAM;
 815
 816	int channelId = mVideoLoggerAddChannel(context);
 817	gbacore->proxyRenderer.logger = malloc(sizeof(struct mVideoLogger));
 818	mVideoLoggerRendererCreate(gbacore->proxyRenderer.logger, false);
 819	mVideoLoggerAttachChannel(gbacore->proxyRenderer.logger, context, channelId);
 820	gbacore->proxyRenderer.logger->block = false;
 821
 822	GBAVideoProxyRendererCreate(&gbacore->proxyRenderer, &gbacore->renderer.d);
 823	GBAVideoProxyRendererShim(&gba->video, &gbacore->proxyRenderer);
 824}
 825
 826static void _GBACoreEndVideoLog(struct mCore* core) {
 827	struct GBACore* gbacore = (struct GBACore*) core;
 828	struct GBA* gba = core->board;
 829	GBAVideoProxyRendererUnshim(&gba->video, &gbacore->proxyRenderer);
 830	free(gbacore->proxyRenderer.logger);
 831	gbacore->proxyRenderer.logger = NULL;
 832}
 833#endif
 834
 835struct mCore* GBACoreCreate(void) {
 836	struct GBACore* gbacore = malloc(sizeof(*gbacore));
 837	struct mCore* core = &gbacore->d;
 838	memset(&core->opts, 0, sizeof(core->opts));
 839	core->cpu = NULL;
 840	core->board = NULL;
 841	core->debugger = NULL;
 842	core->init = _GBACoreInit;
 843	core->deinit = _GBACoreDeinit;
 844	core->platform = _GBACorePlatform;
 845	core->setSync = _GBACoreSetSync;
 846	core->loadConfig = _GBACoreLoadConfig;
 847	core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
 848	core->setVideoBuffer = _GBACoreSetVideoBuffer;
 849	core->getPixels = _GBACoreGetPixels;
 850	core->putPixels = _GBACorePutPixels;
 851	core->getAudioChannel = _GBACoreGetAudioChannel;
 852	core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
 853	core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
 854	core->addCoreCallbacks = _GBACoreAddCoreCallbacks;
 855	core->clearCoreCallbacks = _GBACoreClearCoreCallbacks;
 856	core->setAVStream = _GBACoreSetAVStream;
 857	core->isROM = GBAIsROM;
 858	core->loadROM = _GBACoreLoadROM;
 859	core->loadBIOS = _GBACoreLoadBIOS;
 860	core->loadSave = _GBACoreLoadSave;
 861	core->loadTemporarySave = _GBACoreLoadTemporarySave;
 862	core->loadPatch = _GBACoreLoadPatch;
 863	core->unloadROM = _GBACoreUnloadROM;
 864	core->checksum = _GBACoreChecksum;
 865	core->reset = _GBACoreReset;
 866	core->runFrame = _GBACoreRunFrame;
 867	core->runLoop = _GBACoreRunLoop;
 868	core->step = _GBACoreStep;
 869	core->stateSize = _GBACoreStateSize;
 870	core->loadState = _GBACoreLoadState;
 871	core->saveState = _GBACoreSaveState;
 872	core->setKeys = _GBACoreSetKeys;
 873	core->addKeys = _GBACoreAddKeys;
 874	core->clearKeys = _GBACoreClearKeys;
 875	core->frameCounter = _GBACoreFrameCounter;
 876	core->frameCycles = _GBACoreFrameCycles;
 877	core->frequency = _GBACoreFrequency;
 878	core->getGameTitle = _GBACoreGetGameTitle;
 879	core->getGameCode = _GBACoreGetGameCode;
 880	core->setPeripheral = _GBACoreSetPeripheral;
 881	core->busRead8 = _GBACoreBusRead8;
 882	core->busRead16 = _GBACoreBusRead16;
 883	core->busRead32 = _GBACoreBusRead32;
 884	core->busWrite8 = _GBACoreBusWrite8;
 885	core->busWrite16 = _GBACoreBusWrite16;
 886	core->busWrite32 = _GBACoreBusWrite32;
 887	core->rawRead8 = _GBACoreRawRead8;
 888	core->rawRead16 = _GBACoreRawRead16;
 889	core->rawRead32 = _GBACoreRawRead32;
 890	core->rawWrite8 = _GBACoreRawWrite8;
 891	core->rawWrite16 = _GBACoreRawWrite16;
 892	core->rawWrite32 = _GBACoreRawWrite32;
 893	core->listMemoryBlocks = _GBAListMemoryBlocks;
 894	core->getMemoryBlock = _GBAGetMemoryBlock;
 895#ifdef USE_DEBUGGERS
 896	core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
 897	core->debuggerPlatform = _GBACoreDebuggerPlatform;
 898	core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
 899	core->attachDebugger = _GBACoreAttachDebugger;
 900	core->detachDebugger = _GBACoreDetachDebugger;
 901	core->loadSymbols = _GBACoreLoadSymbols;
 902#endif
 903	core->cheatDevice = _GBACoreCheatDevice;
 904	core->savedataClone = _GBACoreSavedataClone;
 905	core->savedataRestore = _GBACoreSavedataRestore;
 906	core->listVideoLayers = _GBACoreListVideoLayers;
 907	core->listAudioChannels = _GBACoreListAudioChannels;
 908	core->enableVideoLayer = _GBACoreEnableVideoLayer;
 909	core->enableAudioChannel = _GBACoreEnableAudioChannel;
 910#ifndef MINIMAL_CORE
 911	core->startVideoLog = _GBACoreStartVideoLog;
 912	core->endVideoLog = _GBACoreEndVideoLog;
 913#endif
 914	return core;
 915}
 916
 917#ifndef MINIMAL_CORE
 918static void _GBAVLPStartFrameCallback(void *context) {
 919	struct mCore* core = context;
 920	struct GBACore* gbacore = (struct GBACore*) core;
 921	struct GBA* gba = core->board;
 922
 923	if (!mVideoLoggerRendererRun(gbacore->proxyRenderer.logger, true)) {
 924		GBAVideoProxyRendererUnshim(&gba->video, &gbacore->proxyRenderer);
 925		mVideoLogContextRewind(gbacore->logContext, core);
 926		GBAVideoProxyRendererShim(&gba->video, &gbacore->proxyRenderer);
 927	}
 928}
 929
 930static bool _GBAVLPInit(struct mCore* core) {
 931	struct GBACore* gbacore = (struct GBACore*) core;
 932	if (!_GBACoreInit(core)) {
 933		return false;
 934	}
 935	gbacore->proxyRenderer.logger = malloc(sizeof(struct mVideoLogger));
 936	mVideoLoggerRendererCreate(gbacore->proxyRenderer.logger, true);
 937	GBAVideoProxyRendererCreate(&gbacore->proxyRenderer, NULL);
 938	memset(&gbacore->logCallbacks, 0, sizeof(gbacore->logCallbacks));
 939	gbacore->logCallbacks.videoFrameStarted = _GBAVLPStartFrameCallback;
 940	gbacore->logCallbacks.context = core;
 941	core->addCoreCallbacks(core, &gbacore->logCallbacks);
 942	return true;
 943}
 944
 945static void _GBAVLPDeinit(struct mCore* core) {
 946	struct GBACore* gbacore = (struct GBACore*) core;
 947	if (gbacore->logContext) {
 948		mVideoLogContextDestroy(core, gbacore->logContext);
 949	}
 950	_GBACoreDeinit(core);
 951}
 952
 953static void _GBAVLPReset(struct mCore* core) {
 954	struct GBACore* gbacore = (struct GBACore*) core;
 955	struct GBA* gba = (struct GBA*) core->board;
 956	if (gba->video.renderer == &gbacore->proxyRenderer.d) {
 957		GBAVideoProxyRendererUnshim(&gba->video, &gbacore->proxyRenderer);
 958	} else if (gbacore->renderer.outputBuffer) {
 959		struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
 960		GBAVideoAssociateRenderer(&gba->video, renderer);
 961	}
 962
 963	ARMReset(core->cpu);
 964	mVideoLogContextRewind(gbacore->logContext, core);
 965	GBAVideoProxyRendererShim(&gba->video, &gbacore->proxyRenderer);
 966
 967	// Make sure CPU loop never spins
 968	GBAHalt(gba);
 969	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
 970	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
 971}
 972
 973static bool _GBAVLPLoadROM(struct mCore* core, struct VFile* vf) {
 974	struct GBACore* gbacore = (struct GBACore*) core;
 975	gbacore->logContext = mVideoLogContextCreate(NULL);
 976	if (!mVideoLogContextLoad(gbacore->logContext, vf)) {
 977		mVideoLogContextDestroy(core, gbacore->logContext);
 978		gbacore->logContext = NULL;
 979		return false;
 980	}
 981	mVideoLoggerAttachChannel(gbacore->proxyRenderer.logger, gbacore->logContext, 0);
 982	return true;
 983}
 984
 985static bool _GBAVLPLoadState(struct mCore* core, const void* state) {
 986	struct GBA* gba = (struct GBA*) core->board;
 987
 988	gba->timing.root = NULL;
 989	gba->cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
 990	gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
 991
 992	// Make sure CPU loop never spins
 993	GBAHalt(gba);
 994	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
 995	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
 996	GBAVideoDeserialize(&gba->video, state);
 997	GBAIODeserialize(gba, state);
 998	GBAAudioReset(&gba->audio);
 999
1000	return true;
1001}
1002
1003static bool _returnTrue(struct VFile* vf) {
1004	UNUSED(vf);
1005	return true;
1006}
1007
1008struct mCore* GBAVideoLogPlayerCreate(void) {
1009	struct mCore* core = GBACoreCreate();
1010	core->init = _GBAVLPInit;
1011	core->deinit = _GBAVLPDeinit;
1012	core->reset = _GBAVLPReset;
1013	core->loadROM = _GBAVLPLoadROM;
1014	core->loadState = _GBAVLPLoadState;
1015	core->isROM = _returnTrue;
1016	return core;
1017}
1018#else
1019struct mCore* GBAVideoLogPlayerCreate(void) {
1020	return false;
1021}
1022#endif