all repos — mgba @ medusa

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