all repos — mgba @ 91eb813e564f9ddd1b60dfc371b2a6c793e46e14

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