all repos — mgba @ 5ceec845601788b9d9d3862555380793457fdc44

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/audio-mixer.h>
  16#include <mgba/internal/gba/extra/cli.h>
  17#include <mgba/internal/gba/overrides.h>
  18#ifndef DISABLE_THREADING
  19#include <mgba/feature/thread-proxy.h>
  20#endif
  21#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
  22#include <mgba/internal/gba/renderers/gl.h>
  23#endif
  24#include <mgba/internal/gba/renderers/proxy.h>
  25#include <mgba/internal/gba/renderers/video-software.h>
  26#include <mgba/internal/gba/savedata.h>
  27#include <mgba/internal/gba/serialize.h>
  28#ifdef USE_ELF
  29#include <mgba-util/elf-read.h>
  30#endif
  31#include <mgba-util/memory.h>
  32#include <mgba-util/patch.h>
  33#include <mgba-util/vfs.h>
  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_WORM | 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_WORM | 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_WORM | 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_WORM | 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_WORM | 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_WORM | 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_WORM | 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_WORM | 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_WORM | 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_WORM | 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_WORM | 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_WORM | 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_WORM | 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_WORM | 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_WORM | 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;
 127
 128#define CPU_COMPONENT_AUDIO_MIXER CPU_COMPONENT_MISC_1
 129
 130struct GBACore {
 131	struct mCore d;
 132	struct GBAVideoSoftwareRenderer renderer;
 133#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 134	struct GBAVideoGLRenderer glRenderer;
 135#endif
 136#ifndef MINIMAL_CORE
 137	struct GBAVideoProxyRenderer vlProxy;
 138	struct GBAVideoProxyRenderer proxyRenderer;
 139	struct mVideoLogContext* logContext;
 140#endif
 141	struct mCoreCallbacks logCallbacks;
 142#ifndef DISABLE_THREADING
 143	struct mVideoThreadProxy threadProxy;
 144#endif
 145	int keys;
 146	struct mCPUComponent* components[CPU_COMPONENT_MAX];
 147	const struct Configuration* overrides;
 148	struct mDebuggerPlatform* debuggerPlatform;
 149	struct mCheatDevice* cheatDevice;
 150	struct GBAAudioMixer* audioMixer;
 151};
 152
 153static bool _GBACoreInit(struct mCore* core) {
 154	struct GBACore* gbacore = (struct GBACore*) core;
 155
 156	struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
 157	struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
 158	if (!cpu || !gba) {
 159		free(cpu);
 160		free(gba);
 161		return false;
 162	}
 163	core->cpu = cpu;
 164	core->board = gba;
 165	core->timing = &gba->timing;
 166	core->debugger = NULL;
 167	core->symbolTable = NULL;
 168	core->videoLogger = NULL;
 169	gbacore->overrides = NULL;
 170	gbacore->debuggerPlatform = NULL;
 171	gbacore->cheatDevice = NULL;
 172#ifndef MINIMAL_CORE
 173	gbacore->logContext = NULL;
 174#endif
 175	gbacore->audioMixer = NULL;
 176
 177	GBACreate(gba);
 178	// TODO: Restore cheats
 179	memset(gbacore->components, 0, sizeof(gbacore->components));
 180	ARMSetComponents(cpu, &gba->d, CPU_COMPONENT_MAX, gbacore->components);
 181	ARMInit(cpu);
 182	mRTCGenericSourceInit(&core->rtc, core);
 183	gba->rtcSource = &core->rtc.d;
 184
 185	GBAVideoSoftwareRendererCreate(&gbacore->renderer);
 186	gbacore->renderer.outputBuffer = NULL;
 187
 188#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 189	GBAVideoGLRendererCreate(&gbacore->glRenderer);
 190	gbacore->glRenderer.outputTex = -1;
 191#endif
 192
 193#ifndef DISABLE_THREADING
 194	mVideoThreadProxyCreate(&gbacore->threadProxy);
 195#endif
 196#ifndef MINIMAL_CORE
 197	gbacore->vlProxy.logger = NULL;
 198	gbacore->proxyRenderer.logger = NULL;
 199#endif
 200
 201	gbacore->keys = 0;
 202	gba->keySource = &gbacore->keys;
 203
 204#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 205	mDirectorySetInit(&core->dirs);
 206#endif
 207	
 208	return true;
 209}
 210
 211static void _GBACoreDeinit(struct mCore* core) {
 212	ARMDeinit(core->cpu);
 213	GBADestroy(core->board);
 214	mappedMemoryFree(core->cpu, sizeof(struct ARMCore));
 215	mappedMemoryFree(core->board, sizeof(struct GBA));
 216#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 217	mDirectorySetDeinit(&core->dirs);
 218#endif
 219
 220	struct GBACore* gbacore = (struct GBACore*) core;
 221	free(gbacore->debuggerPlatform);
 222	if (gbacore->cheatDevice) {
 223		mCheatDeviceDestroy(gbacore->cheatDevice);
 224	}
 225	free(gbacore->cheatDevice);
 226	free(gbacore->audioMixer);
 227	mCoreConfigFreeOpts(&core->opts);
 228	free(core);
 229}
 230
 231static enum mPlatform _GBACorePlatform(const struct mCore* core) {
 232	UNUSED(core);
 233	return PLATFORM_GBA;
 234}
 235
 236static bool _GBACoreSupportsFeature(const struct mCore* core, enum mCoreFeature feature) {
 237	UNUSED(core);
 238	switch (feature) {
 239	case mCORE_FEATURE_OPENGL:
 240#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 241		return true;
 242#else
 243		return false;
 244#endif
 245	default:
 246		return false;
 247	}
 248}
 249
 250static void _GBACoreSetSync(struct mCore* core, struct mCoreSync* sync) {
 251	struct GBA* gba = core->board;
 252	gba->sync = sync;
 253}
 254
 255static void _GBACoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
 256	struct GBA* gba = core->board;
 257	if (core->opts.mute) {
 258		gba->audio.masterVolume = 0;
 259	} else {
 260		gba->audio.masterVolume = core->opts.volume;
 261	}
 262	gba->video.frameskip = core->opts.frameskip;
 263
 264#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 265	struct GBACore* gbacore = (struct GBACore*) core;
 266	gbacore->overrides = mCoreConfigGetOverridesConst(config);
 267#endif
 268
 269	const char* idleOptimization = mCoreConfigGetValue(config, "idleOptimization");
 270	if (idleOptimization) {
 271		if (strcasecmp(idleOptimization, "ignore") == 0) {
 272			gba->idleOptimization = IDLE_LOOP_IGNORE;
 273		} else if (strcasecmp(idleOptimization, "remove") == 0) {
 274			gba->idleOptimization = IDLE_LOOP_REMOVE;
 275		} else if (strcasecmp(idleOptimization, "detect") == 0) {
 276			if (gba->idleLoop == IDLE_LOOP_NONE) {
 277				gba->idleOptimization = IDLE_LOOP_DETECT;
 278			} else {
 279				gba->idleOptimization = IDLE_LOOP_REMOVE;
 280			}
 281		}
 282	}
 283
 284	int fakeBool = 0;
 285	mCoreConfigGetIntValue(config, "allowOpposingDirections", &fakeBool);
 286	gba->allowOpposingDirections = fakeBool;
 287
 288	mCoreConfigCopyValue(&core->config, config, "allowOpposingDirections");
 289	mCoreConfigCopyValue(&core->config, config, "gba.bios");
 290	mCoreConfigCopyValue(&core->config, config, "gba.audioHle");
 291
 292#ifndef DISABLE_THREADING
 293	mCoreConfigCopyValue(&core->config, config, "threadedVideo");
 294#endif
 295	mCoreConfigCopyValue(&core->config, config, "hwaccelVideo");
 296	mCoreConfigCopyValue(&core->config, config, "videoScale");
 297}
 298
 299static void _GBACoreReloadConfigOption(struct mCore* core, const char* option, const struct mCoreConfig* config) {
 300	struct GBA* gba = core->board;
 301	if (!config) {
 302		config = &core->config;
 303	}
 304
 305	if (!option) {
 306		// Reload options from opts
 307		if (core->opts.mute) {
 308			gba->audio.masterVolume = 0;
 309		} else {
 310			gba->audio.masterVolume = core->opts.volume;
 311		}
 312		gba->video.frameskip = core->opts.frameskip;
 313		return;
 314	}
 315
 316	int fakeBool;
 317	if (strcmp("mute", option) == 0) {
 318		if (mCoreConfigGetIntValue(config, "mute", &fakeBool)) {
 319			core->opts.mute = fakeBool;
 320
 321			if (core->opts.mute) {
 322				gba->audio.masterVolume = 0;
 323			} else {
 324				gba->audio.masterVolume = core->opts.volume;
 325			}
 326		}
 327		return;
 328	}
 329	if (strcmp("volume", option) == 0) {
 330		if (mCoreConfigGetIntValue(config, "volume", &core->opts.volume) && !core->opts.mute) {
 331			gba->audio.masterVolume = core->opts.volume;
 332		}
 333		return;
 334	}
 335	if (strcmp("frameskip", option) == 0) {
 336		if (mCoreConfigGetIntValue(config, "frameskip", &core->opts.frameskip)) {
 337			gba->video.frameskip = core->opts.frameskip;
 338		}
 339		return;
 340	}
 341	if (strcmp("allowOpposingDirections", option) == 0) {
 342		if (config != &core->config) {
 343			mCoreConfigCopyValue(&core->config, config, "allowOpposingDirections");
 344		}
 345		if (mCoreConfigGetIntValue(config, "allowOpposingDirections", &fakeBool)) {
 346			gba->allowOpposingDirections = fakeBool;
 347		}
 348		return;
 349	}
 350
 351	struct GBACore* gbacore = (struct GBACore*) core;
 352#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 353	if (strcmp("videoScale", option) == 0) {
 354		if (config != &core->config) {
 355			mCoreConfigCopyValue(&core->config, config, "videoScale");
 356		}
 357		if (gbacore->glRenderer.outputTex != (unsigned) -1 && mCoreConfigGetIntValue(&core->config, "hwaccelVideo", &fakeBool) && fakeBool) {
 358			int scale;
 359			mCoreConfigGetIntValue(config, "videoScale", &scale);
 360			GBAVideoGLRendererSetScale(&gbacore->glRenderer, scale);
 361		}
 362		return;
 363	}
 364#endif
 365	if (strcmp("hwaccelVideo", option) == 0) {
 366		struct GBAVideoRenderer* renderer = NULL;
 367		if (gbacore->renderer.outputBuffer) {
 368			renderer = &gbacore->renderer.d;
 369		}
 370#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 371		if (gbacore->glRenderer.outputTex != (unsigned) -1 && mCoreConfigGetIntValue(&core->config, "hwaccelVideo", &fakeBool) && fakeBool) {
 372			mCoreConfigGetIntValue(&core->config, "videoScale", &gbacore->glRenderer.scale);
 373			renderer = &gbacore->glRenderer.d;
 374		} else {
 375			gbacore->glRenderer.scale = 1;
 376		}
 377#endif
 378#ifndef MINIMAL_CORE
 379		if (renderer && core->videoLogger) {
 380			gbacore->proxyRenderer.logger = core->videoLogger;
 381			GBAVideoProxyRendererCreate(&gbacore->proxyRenderer, renderer);
 382			renderer = &gbacore->proxyRenderer.d;
 383		}
 384#endif
 385		if (renderer) {
 386			GBAVideoAssociateRenderer(&gba->video, renderer);
 387		}
 388	}
 389}
 390
 391static void _GBACoreDesiredVideoDimensions(const struct mCore* core, unsigned* width, unsigned* height) {
 392#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 393	const struct GBACore* gbacore = (const struct GBACore*) core;
 394	int scale = gbacore->glRenderer.scale;
 395#else
 396	UNUSED(core);
 397	int scale = 1;
 398#endif
 399
 400	*width = GBA_VIDEO_HORIZONTAL_PIXELS * scale;
 401	*height = GBA_VIDEO_VERTICAL_PIXELS * scale;
 402}
 403
 404static void _GBACoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
 405	struct GBACore* gbacore = (struct GBACore*) core;
 406	gbacore->renderer.outputBuffer = buffer;
 407	gbacore->renderer.outputBufferStride = stride;
 408	memset(gbacore->renderer.scanlineDirty, 0xFFFFFFFF, sizeof(gbacore->renderer.scanlineDirty));
 409}
 410
 411static void _GBACoreSetVideoGLTex(struct mCore* core, unsigned texid) {
 412#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 413	struct GBACore* gbacore = (struct GBACore*) core;
 414	gbacore->glRenderer.outputTex = texid;
 415#else
 416	UNUSED(core);
 417	UNUSED(texid);
 418#endif
 419}
 420
 421static void _GBACoreGetPixels(struct mCore* core, const void** buffer, size_t* stride) {
 422	struct GBA* gba = core->board;
 423	gba->video.renderer->getPixels(gba->video.renderer, stride, buffer);
 424}
 425
 426static void _GBACorePutPixels(struct mCore* core, const void* buffer, size_t stride) {
 427	struct GBA* gba = core->board;
 428	gba->video.renderer->putPixels(gba->video.renderer, stride, buffer);
 429}
 430
 431static struct blip_t* _GBACoreGetAudioChannel(struct mCore* core, int ch) {
 432	struct GBA* gba = core->board;
 433	switch (ch) {
 434	case 0:
 435		return gba->audio.psg.left;
 436	case 1:
 437		return gba->audio.psg.right;
 438	default:
 439		return NULL;
 440	}
 441}
 442
 443static void _GBACoreSetAudioBufferSize(struct mCore* core, size_t samples) {
 444	struct GBA* gba = core->board;
 445	GBAAudioResizeBuffer(&gba->audio, samples);
 446}
 447
 448static size_t _GBACoreGetAudioBufferSize(struct mCore* core) {
 449	struct GBA* gba = core->board;
 450	return gba->audio.samples;
 451}
 452
 453static void _GBACoreAddCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) {
 454	struct GBA* gba = core->board;
 455	*mCoreCallbacksListAppend(&gba->coreCallbacks) = *coreCallbacks;
 456}
 457
 458static void _GBACoreClearCoreCallbacks(struct mCore* core) {
 459	struct GBA* gba = core->board;
 460	mCoreCallbacksListClear(&gba->coreCallbacks);
 461}
 462
 463static void _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
 464	struct GBA* gba = core->board;
 465	gba->stream = stream;
 466	if (stream && stream->videoDimensionsChanged) {
 467		unsigned width, height;
 468		core->desiredVideoDimensions(core, &width, &height);
 469		stream->videoDimensionsChanged(stream, width, height);
 470	}
 471}
 472
 473static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
 474#ifdef USE_ELF
 475	struct ELF* elf = ELFOpen(vf);
 476	if (elf) {
 477		GBALoadNull(core->board);
 478		bool success = mCoreLoadELF(core, elf);
 479		ELFClose(elf);
 480		return success;
 481	}
 482#endif
 483	if (GBAIsMB(vf)) {
 484		return GBALoadMB(core->board, vf);
 485	}
 486	return GBALoadROM(core->board, vf);
 487}
 488
 489static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
 490	UNUSED(type);
 491	if (!GBAIsBIOS(vf)) {
 492		return false;
 493	}
 494	GBALoadBIOS(core->board, vf);
 495	return true;
 496}
 497
 498static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
 499	return GBALoadSave(core->board, vf);
 500}
 501
 502static bool _GBACoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
 503	struct GBA* gba = core->board;
 504	GBASavedataMask(&gba->memory.savedata, vf, false);
 505	return true; // TODO: Return a real value
 506}
 507
 508static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
 509	if (!vf) {
 510		return false;
 511	}
 512	struct Patch patch;
 513	if (!loadPatch(vf, &patch)) {
 514		return false;
 515	}
 516	GBAApplyPatch(core->board, &patch);
 517	return true;
 518}
 519
 520static void _GBACoreUnloadROM(struct mCore* core) {
 521	struct GBACore* gbacore = (struct GBACore*) core;
 522	struct ARMCore* cpu = core->cpu;
 523	if (gbacore->cheatDevice) {
 524		ARMHotplugDetach(cpu, CPU_COMPONENT_CHEAT_DEVICE);
 525		cpu->components[CPU_COMPONENT_CHEAT_DEVICE] = NULL;
 526		mCheatDeviceDestroy(gbacore->cheatDevice);
 527		gbacore->cheatDevice = NULL;
 528	}
 529	return GBAUnloadROM(core->board);
 530}
 531
 532static void _GBACoreChecksum(const struct mCore* core, void* data, enum mCoreChecksumType type) {
 533	struct GBA* gba = (struct GBA*) core->board;
 534	switch (type) {
 535	case CHECKSUM_CRC32:
 536		memcpy(data, &gba->romCrc32, sizeof(gba->romCrc32));
 537		break;
 538	}
 539	return;
 540}
 541
 542static void _GBACoreReset(struct mCore* core) {
 543	struct GBACore* gbacore = (struct GBACore*) core;
 544	struct GBA* gba = (struct GBA*) core->board;
 545	if (gbacore->renderer.outputBuffer
 546#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 547	    || gbacore->glRenderer.outputTex != (unsigned) -1
 548#endif
 549	) {
 550		struct GBAVideoRenderer* renderer = NULL;
 551		if (gbacore->renderer.outputBuffer) {
 552			renderer = &gbacore->renderer.d;
 553		}
 554		int fakeBool ATTRIBUTE_UNUSED;
 555#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 556		if (gbacore->glRenderer.outputTex != (unsigned) -1 && mCoreConfigGetIntValue(&core->config, "hwaccelVideo", &fakeBool) && fakeBool) {
 557			mCoreConfigGetIntValue(&core->config, "videoScale", &gbacore->glRenderer.scale);
 558			renderer = &gbacore->glRenderer.d;
 559		} else {
 560			gbacore->glRenderer.scale = 1;
 561		}
 562#endif
 563#ifndef DISABLE_THREADING
 564		if (mCoreConfigGetIntValue(&core->config, "threadedVideo", &fakeBool) && fakeBool) {
 565			if (!core->videoLogger) {
 566				core->videoLogger = &gbacore->threadProxy.d;
 567			}
 568		}
 569#endif
 570#ifndef MINIMAL_CORE
 571		if (renderer && core->videoLogger) {
 572			gbacore->proxyRenderer.logger = core->videoLogger;
 573			GBAVideoProxyRendererCreate(&gbacore->proxyRenderer, renderer);
 574			renderer = &gbacore->proxyRenderer.d;
 575		}
 576#endif
 577		if (renderer) {
 578			GBAVideoAssociateRenderer(&gba->video, renderer);
 579		}
 580	}
 581
 582#ifndef MINIMAL_CORE
 583	int useAudioMixer;
 584	if (!gbacore->audioMixer && mCoreConfigGetIntValue(&core->config, "gba.audioHle", &useAudioMixer) && useAudioMixer) {
 585		gbacore->audioMixer = malloc(sizeof(*gbacore->audioMixer));
 586		GBAAudioMixerCreate(gbacore->audioMixer);
 587		((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_AUDIO_MIXER] = &gbacore->audioMixer->d;
 588		ARMHotplugAttach(core->cpu, CPU_COMPONENT_AUDIO_MIXER);
 589	}
 590#endif
 591
 592	GBAOverrideApplyDefaults(gba, gbacore->overrides);
 593
 594#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 595	if (!gba->biosVf && core->opts.useBios) {
 596		struct VFile* bios = NULL;
 597		bool found = false;
 598		if (core->opts.bios) {
 599			bios = VFileOpen(core->opts.bios, O_RDONLY);
 600			if (bios && GBAIsBIOS(bios)) {
 601				found = true;
 602			} else if (bios) {
 603				bios->close(bios);
 604				bios = NULL;
 605			}
 606		}
 607		if (!found) {
 608			const char* configPath = mCoreConfigGetValue(&core->config, "gba.bios");
 609			if (configPath) {
 610				bios = VFileOpen(configPath, O_RDONLY);
 611			}
 612			if (bios && GBAIsBIOS(bios)) {
 613				found = true;
 614			} else if (bios) {
 615				bios->close(bios);
 616				bios = NULL;
 617			}
 618		}
 619		if (!found) {
 620			char path[PATH_MAX];
 621			mCoreConfigDirectory(path, PATH_MAX);
 622			strncat(path, PATH_SEP "gba_bios.bin", PATH_MAX - strlen(path));
 623			bios = VFileOpen(path, O_RDONLY);
 624			if (bios && GBAIsBIOS(bios)) {
 625				found = true;
 626			} else if (bios) {
 627				bios->close(bios);
 628				bios = NULL;
 629			}
 630		}
 631		if (found && bios) {
 632			GBALoadBIOS(gba, bios);
 633		}
 634	}
 635#endif
 636
 637	ARMReset(core->cpu);
 638	if ((core->opts.skipBios && (gba->romVf || gba->memory.rom)) || (gba->romVf && GBAIsMB(gba->romVf))) {
 639		GBASkipBIOS(core->board);
 640	}
 641}
 642
 643static void _GBACoreRunFrame(struct mCore* core) {
 644	struct GBA* gba = core->board;
 645	int32_t frameCounter = gba->video.frameCounter;
 646	uint32_t startCycle = mTimingCurrentTime(&gba->timing);
 647	while (gba->video.frameCounter == frameCounter && mTimingCurrentTime(&gba->timing) - startCycle < VIDEO_TOTAL_LENGTH + VIDEO_HORIZONTAL_LENGTH) {
 648		ARMRunLoop(core->cpu);
 649	}
 650}
 651
 652static void _GBACoreRunLoop(struct mCore* core) {
 653	ARMRunLoop(core->cpu);
 654}
 655
 656static void _GBACoreStep(struct mCore* core) {
 657	ARMRun(core->cpu);
 658}
 659
 660static size_t _GBACoreStateSize(struct mCore* core) {
 661	UNUSED(core);
 662	return sizeof(struct GBASerializedState);
 663}
 664
 665static bool _GBACoreLoadState(struct mCore* core, const void* state) {
 666	return GBADeserialize(core->board, state);
 667}
 668
 669static bool _GBACoreSaveState(struct mCore* core, void* state) {
 670	GBASerialize(core->board, state);
 671	return true;
 672}
 673
 674static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
 675	struct GBACore* gbacore = (struct GBACore*) core;
 676	gbacore->keys = keys;
 677	GBATestKeypadIRQ(core->board);
 678}
 679
 680static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
 681	struct GBACore* gbacore = (struct GBACore*) core;
 682	gbacore->keys |= keys;
 683	GBATestKeypadIRQ(core->board);
 684}
 685
 686static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
 687	struct GBACore* gbacore = (struct GBACore*) core;
 688	gbacore->keys &= ~keys;
 689}
 690
 691static int32_t _GBACoreFrameCounter(const struct mCore* core) {
 692	const struct GBA* gba = core->board;
 693	return gba->video.frameCounter;
 694}
 695
 696static int32_t _GBACoreFrameCycles(const struct mCore* core) {
 697	UNUSED(core);
 698	return VIDEO_TOTAL_LENGTH;
 699}
 700
 701static int32_t _GBACoreFrequency(const struct mCore* core) {
 702	UNUSED(core);
 703	return GBA_ARM7TDMI_FREQUENCY;
 704}
 705
 706static void _GBACoreGetGameTitle(const struct mCore* core, char* title) {
 707	GBAGetGameTitle(core->board, title);
 708}
 709
 710static void _GBACoreGetGameCode(const struct mCore* core, char* title) {
 711	GBAGetGameCode(core->board, title);
 712}
 713
 714static void _GBACoreSetPeripheral(struct mCore* core, int type, void* periph) {
 715	struct GBA* gba = core->board;
 716	switch (type) {
 717	case mPERIPH_ROTATION:
 718		gba->rotationSource = periph;
 719		break;
 720	case mPERIPH_RUMBLE:
 721		gba->rumble = periph;
 722		break;
 723	case mPERIPH_GBA_LUMINANCE:
 724		gba->luminanceSource = periph;
 725		break;
 726	case mPERIPH_GBA_BATTLECHIP_GATE:
 727		GBASIOSetDriver(&gba->sio, periph, SIO_MULTI);
 728		GBASIOSetDriver(&gba->sio, periph, SIO_NORMAL_32);
 729		break;
 730	default:
 731		return;
 732	}
 733}
 734
 735static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) {
 736	struct ARMCore* cpu = core->cpu;
 737	return cpu->memory.load8(cpu, address, 0);
 738}
 739
 740static uint32_t _GBACoreBusRead16(struct mCore* core, uint32_t address) {
 741	struct ARMCore* cpu = core->cpu;
 742	return cpu->memory.load16(cpu, address, 0);
 743
 744}
 745
 746static uint32_t _GBACoreBusRead32(struct mCore* core, uint32_t address) {
 747	struct ARMCore* cpu = core->cpu;
 748	return cpu->memory.load32(cpu, address, 0);
 749}
 750
 751static void _GBACoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
 752	struct ARMCore* cpu = core->cpu;
 753	cpu->memory.store8(cpu, address, value, 0);
 754}
 755
 756static void _GBACoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
 757	struct ARMCore* cpu = core->cpu;
 758	cpu->memory.store16(cpu, address, value, 0);
 759}
 760
 761static void _GBACoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
 762	struct ARMCore* cpu = core->cpu;
 763	cpu->memory.store32(cpu, address, value, 0);
 764}
 765
 766static uint32_t _GBACoreRawRead8(struct mCore* core, uint32_t address, int segment) {
 767	UNUSED(segment);
 768	struct ARMCore* cpu = core->cpu;
 769	return GBAView8(cpu, address);
 770}
 771
 772static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address, int segment) {
 773	UNUSED(segment);
 774	struct ARMCore* cpu = core->cpu;
 775	return GBAView16(cpu, address);
 776}
 777
 778static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address, int segment) {
 779	UNUSED(segment);
 780	struct ARMCore* cpu = core->cpu;
 781	return GBAView32(cpu, address);
 782}
 783
 784static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
 785	UNUSED(segment);
 786	struct ARMCore* cpu = core->cpu;
 787	GBAPatch8(cpu, address, value, NULL);
 788}
 789
 790static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
 791	UNUSED(segment);
 792	struct ARMCore* cpu = core->cpu;
 793	GBAPatch16(cpu, address, value, NULL);
 794}
 795
 796static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
 797	UNUSED(segment);
 798	struct ARMCore* cpu = core->cpu;
 799	GBAPatch32(cpu, address, value, NULL);
 800}
 801
 802size_t _GBAListMemoryBlocks(const struct mCore* core, const struct mCoreMemoryBlock** blocks) {
 803	const struct GBA* gba = core->board;
 804	switch (gba->memory.savedata.type) {
 805	case SAVEDATA_SRAM:
 806		*blocks = _GBAMemoryBlocksSRAM;
 807		return sizeof(_GBAMemoryBlocksSRAM) / sizeof(*_GBAMemoryBlocksSRAM);
 808	case SAVEDATA_FLASH512:
 809		*blocks = _GBAMemoryBlocksFlash512;
 810		return sizeof(_GBAMemoryBlocksFlash512) / sizeof(*_GBAMemoryBlocksFlash512);
 811	case SAVEDATA_FLASH1M:
 812		*blocks = _GBAMemoryBlocksFlash1M;
 813		return sizeof(_GBAMemoryBlocksFlash1M) / sizeof(*_GBAMemoryBlocksFlash1M);
 814	case SAVEDATA_EEPROM:
 815		*blocks = _GBAMemoryBlocksEEPROM;
 816		return sizeof(_GBAMemoryBlocksEEPROM) / sizeof(*_GBAMemoryBlocksEEPROM);
 817	default:
 818		*blocks = _GBAMemoryBlocks;
 819		return sizeof(_GBAMemoryBlocks) / sizeof(*_GBAMemoryBlocks);
 820	}
 821}
 822
 823void* _GBAGetMemoryBlock(struct mCore* core, size_t id, size_t* sizeOut) {
 824	struct GBA* gba = core->board;
 825	switch (id) {
 826	default:
 827		return NULL;
 828	case REGION_BIOS:
 829		*sizeOut = SIZE_BIOS;
 830		return gba->memory.bios;
 831	case REGION_WORKING_RAM:
 832		*sizeOut = SIZE_WORKING_RAM;
 833		return gba->memory.wram;
 834	case REGION_WORKING_IRAM:
 835		*sizeOut = SIZE_WORKING_IRAM;
 836		return gba->memory.iwram;
 837	case REGION_PALETTE_RAM:
 838		*sizeOut = SIZE_PALETTE_RAM;
 839		return gba->video.palette;
 840	case REGION_VRAM:
 841		*sizeOut = SIZE_VRAM;
 842		return gba->video.vram;
 843	case REGION_OAM:
 844		*sizeOut = SIZE_OAM;
 845		return gba->video.oam.raw;
 846	case REGION_CART0:
 847	case REGION_CART1:
 848	case REGION_CART2:
 849		*sizeOut = gba->memory.romSize;
 850		return gba->memory.rom;
 851	case REGION_CART_SRAM:
 852		if (gba->memory.savedata.type == SAVEDATA_FLASH1M) {
 853			*sizeOut = SIZE_CART_FLASH1M;
 854			return gba->memory.savedata.currentBank;
 855		}
 856		// Fall through
 857	case REGION_CART_SRAM_MIRROR:
 858		*sizeOut = GBASavedataSize(&gba->memory.savedata);
 859		return gba->memory.savedata.data;
 860	}
 861}
 862
 863#ifdef USE_DEBUGGERS
 864static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
 865	UNUSED(core);
 866	switch (type) {
 867	case DEBUGGER_CUSTOM:
 868	case DEBUGGER_CLI:
 869	case DEBUGGER_GDB:
 870		return true;
 871	default:
 872		return false;
 873	}
 874}
 875
 876static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
 877	struct GBACore* gbacore = (struct GBACore*) core;
 878	if (!gbacore->debuggerPlatform) {
 879		gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
 880	}
 881	return gbacore->debuggerPlatform;
 882}
 883
 884static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
 885	return &GBACLIDebuggerCreate(core)->d;
 886}
 887
 888static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
 889	if (core->debugger) {
 890		GBADetachDebugger(core->board);
 891	}
 892	GBAAttachDebugger(core->board, debugger);
 893	core->debugger = debugger;
 894}
 895
 896static void _GBACoreDetachDebugger(struct mCore* core) {
 897	GBADetachDebugger(core->board);
 898	core->debugger = NULL;
 899}
 900
 901static void _GBACoreLoadSymbols(struct mCore* core, struct VFile* vf) {
 902	bool closeAfter = false;
 903	core->symbolTable = mDebuggerSymbolTableCreate();
 904#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 905#ifdef USE_ELF
 906	if (!vf) {
 907		closeAfter = true;
 908		vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".elf", O_RDONLY);
 909	}
 910#endif
 911	if (!vf) {
 912		closeAfter = true;
 913		vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".sym", O_RDONLY);
 914	}
 915#endif
 916	if (!vf) {
 917		return;
 918	}
 919#ifdef USE_ELF
 920	struct ELF* elf = ELFOpen(vf);
 921	if (elf) {
 922#ifdef USE_DEBUGGERS
 923		mCoreLoadELFSymbols(core->symbolTable, elf);
 924#endif
 925		ELFClose(elf);
 926	} else
 927#endif
 928	{
 929		mDebuggerLoadARMIPSSymbols(core->symbolTable, vf);
 930	}
 931	if (closeAfter) {
 932		vf->close(vf);
 933	}
 934}
 935
 936static bool _GBACoreLookupIdentifier(struct mCore* core, const char* name, int32_t* value, int* segment) {
 937	UNUSED(core);
 938	*segment = -1;
 939	int i;
 940	for (i = 0; i < REG_MAX; i += 2) {
 941		const char* reg = GBAIORegisterNames[i >> 1];
 942		if (reg && strcasecmp(reg, name) == 0) {
 943			*value = BASE_IO | i;
 944			return true;
 945		}
 946	}
 947	return false;
 948}
 949#endif
 950
 951static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
 952	struct GBACore* gbacore = (struct GBACore*) core;
 953	if (!gbacore->cheatDevice) {
 954		gbacore->cheatDevice = GBACheatDeviceCreate();
 955		((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
 956		ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
 957		gbacore->cheatDevice->p = core;
 958	}
 959	return gbacore->cheatDevice;
 960}
 961
 962static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
 963	struct GBA* gba = core->board;
 964	size_t size = GBASavedataSize(&gba->memory.savedata);
 965	if (!size) {
 966		*sram = NULL;
 967		return 0;
 968	}
 969	*sram = malloc(size);
 970	struct VFile* vf = VFileFromMemory(*sram, size);
 971	if (!vf) {
 972		free(*sram);
 973		*sram = NULL;
 974		return 0;
 975	}
 976	bool success = GBASavedataClone(&gba->memory.savedata, vf);
 977	vf->close(vf);
 978	if (!success) {
 979		free(*sram);
 980		*sram = NULL;
 981		return 0;
 982	}
 983	return size;
 984}
 985
 986static bool _GBACoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
 987	struct VFile* vf = VFileMemChunk(sram, size);
 988	if (!vf) {
 989		return false;
 990	}
 991	struct GBA* gba = core->board;
 992	bool success = true;
 993	if (writeback) {
 994		success = GBASavedataLoad(&gba->memory.savedata, vf);
 995		vf->close(vf);
 996	} else {
 997		GBASavedataMask(&gba->memory.savedata, vf, true);
 998	}
 999	return success;
1000}
1001
1002static size_t _GBACoreListVideoLayers(const struct mCore* core, const struct mCoreChannelInfo** info) {
1003	UNUSED(core);
1004	if (info) {
1005		*info = _GBAVideoLayers;
1006	}
1007	return sizeof(_GBAVideoLayers) / sizeof(*_GBAVideoLayers);
1008}
1009
1010static size_t _GBACoreListAudioChannels(const struct mCore* core, const struct mCoreChannelInfo** info) {
1011	UNUSED(core);
1012	if (info) {
1013		*info = _GBAAudioChannels;
1014	}
1015	return sizeof(_GBAAudioChannels) / sizeof(*_GBAAudioChannels);
1016}
1017
1018static void _GBACoreEnableVideoLayer(struct mCore* core, size_t id, bool enable) {
1019	struct GBA* gba = core->board;
1020	switch (id) {
1021	case 0:
1022	case 1:
1023	case 2:
1024	case 3:
1025		gba->video.renderer->disableBG[id] = !enable;
1026		break;
1027	case 4:
1028		gba->video.renderer->disableOBJ = !enable;
1029		break;
1030	default:
1031		break;
1032	}
1033}
1034
1035static void _GBACoreEnableAudioChannel(struct mCore* core, size_t id, bool enable) {
1036	struct GBA* gba = core->board;
1037	switch (id) {
1038	case 0:
1039	case 1:
1040	case 2:
1041	case 3:
1042		gba->audio.psg.forceDisableCh[id] = !enable;
1043		break;
1044	case 4:
1045		gba->audio.forceDisableChA = !enable;
1046		break;
1047	case 5:
1048		gba->audio.forceDisableChB = !enable;
1049		break;
1050	default:
1051		break;
1052	}
1053}
1054
1055static void _GBACoreAdjustVideoLayer(struct mCore* core, size_t id, int32_t x, int32_t y) {
1056	struct GBACore* gbacore = (struct GBACore*) core;
1057	switch (id) {
1058	case 0:
1059	case 1:
1060	case 2:
1061	case 3:
1062		gbacore->renderer.bg[id].offsetX = x;
1063		gbacore->renderer.bg[id].offsetY = y;
1064		break;
1065	case 4:
1066		gbacore->renderer.objOffsetX = x;
1067		gbacore->renderer.objOffsetY = y;
1068		gbacore->renderer.oamDirty = 1;
1069		break;
1070	default:
1071		return;
1072	}
1073	memset(gbacore->renderer.scanlineDirty, 0xFFFFFFFF, sizeof(gbacore->renderer.scanlineDirty));
1074}
1075
1076#ifndef MINIMAL_CORE
1077static void _GBACoreStartVideoLog(struct mCore* core, struct mVideoLogContext* context) {
1078	struct GBACore* gbacore = (struct GBACore*) core;
1079	struct GBA* gba = core->board;
1080	gbacore->logContext = context;
1081
1082	struct GBASerializedState* state = mVideoLogContextInitialState(context, NULL);
1083	state->id = 0;
1084	state->cpu.gprs[ARM_PC] = BASE_WORKING_RAM;
1085
1086	int channelId = mVideoLoggerAddChannel(context);
1087	gbacore->vlProxy.logger = malloc(sizeof(struct mVideoLogger));
1088	mVideoLoggerRendererCreate(gbacore->vlProxy.logger, false);
1089	mVideoLoggerAttachChannel(gbacore->vlProxy.logger, context, channelId);
1090	gbacore->vlProxy.logger->block = false;
1091
1092	GBAVideoProxyRendererCreate(&gbacore->vlProxy, gba->video.renderer);
1093	GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1094}
1095
1096static void _GBACoreEndVideoLog(struct mCore* core) {
1097	struct GBACore* gbacore = (struct GBACore*) core;
1098	struct GBA* gba = core->board;
1099	if (gbacore->vlProxy.logger) {
1100		GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1101		free(gbacore->vlProxy.logger);
1102		gbacore->vlProxy.logger = NULL;
1103	}
1104}
1105#endif
1106
1107struct mCore* GBACoreCreate(void) {
1108	struct GBACore* gbacore = malloc(sizeof(*gbacore));
1109	struct mCore* core = &gbacore->d;
1110	memset(&core->opts, 0, sizeof(core->opts));
1111	core->cpu = NULL;
1112	core->board = NULL;
1113	core->debugger = NULL;
1114	core->init = _GBACoreInit;
1115	core->deinit = _GBACoreDeinit;
1116	core->platform = _GBACorePlatform;
1117	core->supportsFeature = _GBACoreSupportsFeature;
1118	core->setSync = _GBACoreSetSync;
1119	core->loadConfig = _GBACoreLoadConfig;
1120	core->reloadConfigOption = _GBACoreReloadConfigOption;
1121	core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
1122	core->setVideoBuffer = _GBACoreSetVideoBuffer;
1123	core->setVideoGLTex = _GBACoreSetVideoGLTex;
1124	core->getPixels = _GBACoreGetPixels;
1125	core->putPixels = _GBACorePutPixels;
1126	core->getAudioChannel = _GBACoreGetAudioChannel;
1127	core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
1128	core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
1129	core->addCoreCallbacks = _GBACoreAddCoreCallbacks;
1130	core->clearCoreCallbacks = _GBACoreClearCoreCallbacks;
1131	core->setAVStream = _GBACoreSetAVStream;
1132	core->isROM = GBAIsROM;
1133	core->loadROM = _GBACoreLoadROM;
1134	core->loadBIOS = _GBACoreLoadBIOS;
1135	core->loadSave = _GBACoreLoadSave;
1136	core->loadTemporarySave = _GBACoreLoadTemporarySave;
1137	core->loadPatch = _GBACoreLoadPatch;
1138	core->unloadROM = _GBACoreUnloadROM;
1139	core->checksum = _GBACoreChecksum;
1140	core->reset = _GBACoreReset;
1141	core->runFrame = _GBACoreRunFrame;
1142	core->runLoop = _GBACoreRunLoop;
1143	core->step = _GBACoreStep;
1144	core->stateSize = _GBACoreStateSize;
1145	core->loadState = _GBACoreLoadState;
1146	core->saveState = _GBACoreSaveState;
1147	core->setKeys = _GBACoreSetKeys;
1148	core->addKeys = _GBACoreAddKeys;
1149	core->clearKeys = _GBACoreClearKeys;
1150	core->frameCounter = _GBACoreFrameCounter;
1151	core->frameCycles = _GBACoreFrameCycles;
1152	core->frequency = _GBACoreFrequency;
1153	core->getGameTitle = _GBACoreGetGameTitle;
1154	core->getGameCode = _GBACoreGetGameCode;
1155	core->setPeripheral = _GBACoreSetPeripheral;
1156	core->busRead8 = _GBACoreBusRead8;
1157	core->busRead16 = _GBACoreBusRead16;
1158	core->busRead32 = _GBACoreBusRead32;
1159	core->busWrite8 = _GBACoreBusWrite8;
1160	core->busWrite16 = _GBACoreBusWrite16;
1161	core->busWrite32 = _GBACoreBusWrite32;
1162	core->rawRead8 = _GBACoreRawRead8;
1163	core->rawRead16 = _GBACoreRawRead16;
1164	core->rawRead32 = _GBACoreRawRead32;
1165	core->rawWrite8 = _GBACoreRawWrite8;
1166	core->rawWrite16 = _GBACoreRawWrite16;
1167	core->rawWrite32 = _GBACoreRawWrite32;
1168	core->listMemoryBlocks = _GBAListMemoryBlocks;
1169	core->getMemoryBlock = _GBAGetMemoryBlock;
1170#ifdef USE_DEBUGGERS
1171	core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
1172	core->debuggerPlatform = _GBACoreDebuggerPlatform;
1173	core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
1174	core->attachDebugger = _GBACoreAttachDebugger;
1175	core->detachDebugger = _GBACoreDetachDebugger;
1176	core->loadSymbols = _GBACoreLoadSymbols;
1177	core->lookupIdentifier = _GBACoreLookupIdentifier;
1178#endif
1179	core->cheatDevice = _GBACoreCheatDevice;
1180	core->savedataClone = _GBACoreSavedataClone;
1181	core->savedataRestore = _GBACoreSavedataRestore;
1182	core->listVideoLayers = _GBACoreListVideoLayers;
1183	core->listAudioChannels = _GBACoreListAudioChannels;
1184	core->enableVideoLayer = _GBACoreEnableVideoLayer;
1185	core->enableAudioChannel = _GBACoreEnableAudioChannel;
1186	core->adjustVideoLayer = _GBACoreAdjustVideoLayer;
1187#ifndef MINIMAL_CORE
1188	core->startVideoLog = _GBACoreStartVideoLog;
1189	core->endVideoLog = _GBACoreEndVideoLog;
1190#endif
1191	return core;
1192}
1193
1194#ifndef MINIMAL_CORE
1195static void _GBAVLPStartFrameCallback(void *context) {
1196	struct mCore* core = context;
1197	struct GBACore* gbacore = (struct GBACore*) core;
1198	struct GBA* gba = core->board;
1199
1200	if (!mVideoLoggerRendererRun(gbacore->vlProxy.logger, true)) {
1201		GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1202		mVideoLogContextRewind(gbacore->logContext, core);
1203		GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1204		gba->earlyExit = true;
1205	}
1206}
1207
1208static bool _GBAVLPInit(struct mCore* core) {
1209	struct GBACore* gbacore = (struct GBACore*) core;
1210	if (!_GBACoreInit(core)) {
1211		return false;
1212	}
1213	gbacore->vlProxy.logger = malloc(sizeof(struct mVideoLogger));
1214	mVideoLoggerRendererCreate(gbacore->vlProxy.logger, true);
1215	GBAVideoProxyRendererCreate(&gbacore->vlProxy, NULL);
1216	memset(&gbacore->logCallbacks, 0, sizeof(gbacore->logCallbacks));
1217	gbacore->logCallbacks.videoFrameStarted = _GBAVLPStartFrameCallback;
1218	gbacore->logCallbacks.context = core;
1219	core->addCoreCallbacks(core, &gbacore->logCallbacks);
1220	core->videoLogger = gbacore->vlProxy.logger;
1221	return true;
1222}
1223
1224static void _GBAVLPDeinit(struct mCore* core) {
1225	struct GBACore* gbacore = (struct GBACore*) core;
1226	if (gbacore->logContext) {
1227		mVideoLogContextDestroy(core, gbacore->logContext);
1228	}
1229	_GBACoreDeinit(core);
1230}
1231
1232static void _GBAVLPReset(struct mCore* core) {
1233	struct GBACore* gbacore = (struct GBACore*) core;
1234	struct GBA* gba = (struct GBA*) core->board;
1235	if (gba->video.renderer == &gbacore->vlProxy.d) {
1236		GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1237	} else if (gbacore->renderer.outputBuffer) {
1238		struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
1239		GBAVideoAssociateRenderer(&gba->video, renderer);
1240	}
1241
1242	ARMReset(core->cpu);
1243	mVideoLogContextRewind(gbacore->logContext, core);
1244	GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1245
1246	// Make sure CPU loop never spins
1247	GBAHalt(gba);
1248	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
1249	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
1250}
1251
1252static bool _GBAVLPLoadROM(struct mCore* core, struct VFile* vf) {
1253	struct GBACore* gbacore = (struct GBACore*) core;
1254	gbacore->logContext = mVideoLogContextCreate(NULL);
1255	if (!mVideoLogContextLoad(gbacore->logContext, vf)) {
1256		mVideoLogContextDestroy(core, gbacore->logContext);
1257		gbacore->logContext = NULL;
1258		return false;
1259	}
1260	mVideoLoggerAttachChannel(gbacore->vlProxy.logger, gbacore->logContext, 0);
1261	return true;
1262}
1263
1264static bool _GBAVLPLoadState(struct mCore* core, const void* state) {
1265	struct GBA* gba = (struct GBA*) core->board;
1266
1267	gba->timing.root = NULL;
1268	gba->cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
1269	gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
1270
1271	// Make sure CPU loop never spins
1272	GBAHalt(gba);
1273	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
1274	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
1275	GBAVideoDeserialize(&gba->video, state);
1276	GBAIODeserialize(gba, state);
1277	GBAAudioReset(&gba->audio);
1278
1279	return true;
1280}
1281
1282static bool _returnTrue(struct VFile* vf) {
1283	UNUSED(vf);
1284	return true;
1285}
1286
1287struct mCore* GBAVideoLogPlayerCreate(void) {
1288	struct mCore* core = GBACoreCreate();
1289	core->init = _GBAVLPInit;
1290	core->deinit = _GBAVLPDeinit;
1291	core->reset = _GBAVLPReset;
1292	core->loadROM = _GBAVLPLoadROM;
1293	core->loadState = _GBAVLPLoadState;
1294	core->isROM = _returnTrue;
1295	return core;
1296}
1297#else
1298struct mCore* GBAVideoLogPlayerCreate(void) {
1299	return false;
1300}
1301#endif