all repos — mgba @ f48a69e8e5a3aa8965140e15e643841c70eb6c1e

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