all repos — mgba @ ee6bbaf61c0890552b103e2a577eb94f9d453c77

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(struct mCore* core, unsigned* width, unsigned* height) {
 392#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 393	struct GBACore* gbacore = (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	while (gba->video.frameCounter == frameCounter) {
 647		ARMRunLoop(core->cpu);
 648	}
 649}
 650
 651static void _GBACoreRunLoop(struct mCore* core) {
 652	ARMRunLoop(core->cpu);
 653}
 654
 655static void _GBACoreStep(struct mCore* core) {
 656	ARMRun(core->cpu);
 657}
 658
 659static size_t _GBACoreStateSize(struct mCore* core) {
 660	UNUSED(core);
 661	return sizeof(struct GBASerializedState);
 662}
 663
 664static bool _GBACoreLoadState(struct mCore* core, const void* state) {
 665	return GBADeserialize(core->board, state);
 666}
 667
 668static bool _GBACoreSaveState(struct mCore* core, void* state) {
 669	GBASerialize(core->board, state);
 670	return true;
 671}
 672
 673static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
 674	struct GBACore* gbacore = (struct GBACore*) core;
 675	gbacore->keys = keys;
 676	GBATestKeypadIRQ(core->board);
 677}
 678
 679static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
 680	struct GBACore* gbacore = (struct GBACore*) core;
 681	gbacore->keys |= keys;
 682	GBATestKeypadIRQ(core->board);
 683}
 684
 685static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
 686	struct GBACore* gbacore = (struct GBACore*) core;
 687	gbacore->keys &= ~keys;
 688}
 689
 690static int32_t _GBACoreFrameCounter(const struct mCore* core) {
 691	const struct GBA* gba = core->board;
 692	return gba->video.frameCounter;
 693}
 694
 695static int32_t _GBACoreFrameCycles(const struct mCore* core) {
 696	UNUSED(core);
 697	return VIDEO_TOTAL_LENGTH;
 698}
 699
 700static int32_t _GBACoreFrequency(const struct mCore* core) {
 701	UNUSED(core);
 702	return GBA_ARM7TDMI_FREQUENCY;
 703}
 704
 705static void _GBACoreGetGameTitle(const struct mCore* core, char* title) {
 706	GBAGetGameTitle(core->board, title);
 707}
 708
 709static void _GBACoreGetGameCode(const struct mCore* core, char* title) {
 710	GBAGetGameCode(core->board, title);
 711}
 712
 713static void _GBACoreSetPeripheral(struct mCore* core, int type, void* periph) {
 714	struct GBA* gba = core->board;
 715	switch (type) {
 716	case mPERIPH_ROTATION:
 717		gba->rotationSource = periph;
 718		break;
 719	case mPERIPH_RUMBLE:
 720		gba->rumble = periph;
 721		break;
 722	case mPERIPH_GBA_LUMINANCE:
 723		gba->luminanceSource = periph;
 724		break;
 725	case mPERIPH_GBA_BATTLECHIP_GATE:
 726		GBASIOSetDriver(&gba->sio, periph, SIO_MULTI);
 727		GBASIOSetDriver(&gba->sio, periph, SIO_NORMAL_32);
 728		break;
 729	default:
 730		return;
 731	}
 732}
 733
 734static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) {
 735	struct ARMCore* cpu = core->cpu;
 736	return cpu->memory.load8(cpu, address, 0);
 737}
 738
 739static uint32_t _GBACoreBusRead16(struct mCore* core, uint32_t address) {
 740	struct ARMCore* cpu = core->cpu;
 741	return cpu->memory.load16(cpu, address, 0);
 742
 743}
 744
 745static uint32_t _GBACoreBusRead32(struct mCore* core, uint32_t address) {
 746	struct ARMCore* cpu = core->cpu;
 747	return cpu->memory.load32(cpu, address, 0);
 748}
 749
 750static void _GBACoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
 751	struct ARMCore* cpu = core->cpu;
 752	cpu->memory.store8(cpu, address, value, 0);
 753}
 754
 755static void _GBACoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
 756	struct ARMCore* cpu = core->cpu;
 757	cpu->memory.store16(cpu, address, value, 0);
 758}
 759
 760static void _GBACoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
 761	struct ARMCore* cpu = core->cpu;
 762	cpu->memory.store32(cpu, address, value, 0);
 763}
 764
 765static uint32_t _GBACoreRawRead8(struct mCore* core, uint32_t address, int segment) {
 766	UNUSED(segment);
 767	struct ARMCore* cpu = core->cpu;
 768	return GBAView8(cpu, address);
 769}
 770
 771static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address, int segment) {
 772	UNUSED(segment);
 773	struct ARMCore* cpu = core->cpu;
 774	return GBAView16(cpu, address);
 775}
 776
 777static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address, int segment) {
 778	UNUSED(segment);
 779	struct ARMCore* cpu = core->cpu;
 780	return GBAView32(cpu, address);
 781}
 782
 783static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
 784	UNUSED(segment);
 785	struct ARMCore* cpu = core->cpu;
 786	GBAPatch8(cpu, address, value, NULL);
 787}
 788
 789static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
 790	UNUSED(segment);
 791	struct ARMCore* cpu = core->cpu;
 792	GBAPatch16(cpu, address, value, NULL);
 793}
 794
 795static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
 796	UNUSED(segment);
 797	struct ARMCore* cpu = core->cpu;
 798	GBAPatch32(cpu, address, value, NULL);
 799}
 800
 801size_t _GBAListMemoryBlocks(const struct mCore* core, const struct mCoreMemoryBlock** blocks) {
 802	const struct GBA* gba = core->board;
 803	switch (gba->memory.savedata.type) {
 804	case SAVEDATA_SRAM:
 805		*blocks = _GBAMemoryBlocksSRAM;
 806		return sizeof(_GBAMemoryBlocksSRAM) / sizeof(*_GBAMemoryBlocksSRAM);
 807	case SAVEDATA_FLASH512:
 808		*blocks = _GBAMemoryBlocksFlash512;
 809		return sizeof(_GBAMemoryBlocksFlash512) / sizeof(*_GBAMemoryBlocksFlash512);
 810	case SAVEDATA_FLASH1M:
 811		*blocks = _GBAMemoryBlocksFlash1M;
 812		return sizeof(_GBAMemoryBlocksFlash1M) / sizeof(*_GBAMemoryBlocksFlash1M);
 813	case SAVEDATA_EEPROM:
 814		*blocks = _GBAMemoryBlocksEEPROM;
 815		return sizeof(_GBAMemoryBlocksEEPROM) / sizeof(*_GBAMemoryBlocksEEPROM);
 816	default:
 817		*blocks = _GBAMemoryBlocks;
 818		return sizeof(_GBAMemoryBlocks) / sizeof(*_GBAMemoryBlocks);
 819	}
 820}
 821
 822void* _GBAGetMemoryBlock(struct mCore* core, size_t id, size_t* sizeOut) {
 823	struct GBA* gba = core->board;
 824	switch (id) {
 825	default:
 826		return NULL;
 827	case REGION_BIOS:
 828		*sizeOut = SIZE_BIOS;
 829		return gba->memory.bios;
 830	case REGION_WORKING_RAM:
 831		*sizeOut = SIZE_WORKING_RAM;
 832		return gba->memory.wram;
 833	case REGION_WORKING_IRAM:
 834		*sizeOut = SIZE_WORKING_IRAM;
 835		return gba->memory.iwram;
 836	case REGION_PALETTE_RAM:
 837		*sizeOut = SIZE_PALETTE_RAM;
 838		return gba->video.palette;
 839	case REGION_VRAM:
 840		*sizeOut = SIZE_VRAM;
 841		return gba->video.vram;
 842	case REGION_OAM:
 843		*sizeOut = SIZE_OAM;
 844		return gba->video.oam.raw;
 845	case REGION_CART0:
 846	case REGION_CART1:
 847	case REGION_CART2:
 848		*sizeOut = gba->memory.romSize;
 849		return gba->memory.rom;
 850	case REGION_CART_SRAM:
 851		if (gba->memory.savedata.type == SAVEDATA_FLASH1M) {
 852			*sizeOut = SIZE_CART_FLASH1M;
 853			return gba->memory.savedata.currentBank;
 854		}
 855		// Fall through
 856	case REGION_CART_SRAM_MIRROR:
 857		*sizeOut = GBASavedataSize(&gba->memory.savedata);
 858		return gba->memory.savedata.data;
 859	}
 860}
 861
 862#ifdef USE_DEBUGGERS
 863static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
 864	UNUSED(core);
 865	switch (type) {
 866	case DEBUGGER_CUSTOM:
 867	case DEBUGGER_CLI:
 868	case DEBUGGER_GDB:
 869		return true;
 870	default:
 871		return false;
 872	}
 873}
 874
 875static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
 876	struct GBACore* gbacore = (struct GBACore*) core;
 877	if (!gbacore->debuggerPlatform) {
 878		gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
 879	}
 880	return gbacore->debuggerPlatform;
 881}
 882
 883static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
 884	return &GBACLIDebuggerCreate(core)->d;
 885}
 886
 887static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
 888	if (core->debugger) {
 889		GBADetachDebugger(core->board);
 890	}
 891	GBAAttachDebugger(core->board, debugger);
 892	core->debugger = debugger;
 893}
 894
 895static void _GBACoreDetachDebugger(struct mCore* core) {
 896	GBADetachDebugger(core->board);
 897	core->debugger = NULL;
 898}
 899
 900static void _GBACoreLoadSymbols(struct mCore* core, struct VFile* vf) {
 901	bool closeAfter = false;
 902	core->symbolTable = mDebuggerSymbolTableCreate();
 903#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 904#ifdef USE_ELF
 905	if (!vf) {
 906		closeAfter = true;
 907		vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".elf", O_RDONLY);
 908	}
 909#endif
 910	if (!vf) {
 911		closeAfter = true;
 912		vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".sym", O_RDONLY);
 913	}
 914#endif
 915	if (!vf) {
 916		return;
 917	}
 918#ifdef USE_ELF
 919	struct ELF* elf = ELFOpen(vf);
 920	if (elf) {
 921#ifdef USE_DEBUGGERS
 922		mCoreLoadELFSymbols(core->symbolTable, elf);
 923#endif
 924		ELFClose(elf);
 925	} else
 926#endif
 927	{
 928		mDebuggerLoadARMIPSSymbols(core->symbolTable, vf);
 929	}
 930	if (closeAfter) {
 931		vf->close(vf);
 932	}
 933}
 934
 935static bool _GBACoreLookupIdentifier(struct mCore* core, const char* name, int32_t* value, int* segment) {
 936	UNUSED(core);
 937	*segment = -1;
 938	int i;
 939	for (i = 0; i < REG_MAX; i += 2) {
 940		const char* reg = GBAIORegisterNames[i >> 1];
 941		if (reg && strcasecmp(reg, name) == 0) {
 942			*value = BASE_IO | i;
 943			return true;
 944		}
 945	}
 946	return false;
 947}
 948#endif
 949
 950static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
 951	struct GBACore* gbacore = (struct GBACore*) core;
 952	if (!gbacore->cheatDevice) {
 953		gbacore->cheatDevice = GBACheatDeviceCreate();
 954		((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
 955		ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
 956		gbacore->cheatDevice->p = core;
 957	}
 958	return gbacore->cheatDevice;
 959}
 960
 961static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
 962	struct GBA* gba = core->board;
 963	size_t size = GBASavedataSize(&gba->memory.savedata);
 964	if (!size) {
 965		*sram = NULL;
 966		return 0;
 967	}
 968	*sram = malloc(size);
 969	struct VFile* vf = VFileFromMemory(*sram, size);
 970	if (!vf) {
 971		free(*sram);
 972		*sram = NULL;
 973		return 0;
 974	}
 975	bool success = GBASavedataClone(&gba->memory.savedata, vf);
 976	vf->close(vf);
 977	if (!success) {
 978		free(*sram);
 979		*sram = NULL;
 980		return 0;
 981	}
 982	return size;
 983}
 984
 985static bool _GBACoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
 986	struct VFile* vf = VFileMemChunk(sram, size);
 987	if (!vf) {
 988		return false;
 989	}
 990	struct GBA* gba = core->board;
 991	bool success = true;
 992	if (writeback) {
 993		success = GBASavedataLoad(&gba->memory.savedata, vf);
 994		vf->close(vf);
 995	} else {
 996		GBASavedataMask(&gba->memory.savedata, vf, true);
 997	}
 998	return success;
 999}
1000
1001static size_t _GBACoreListVideoLayers(const struct mCore* core, const struct mCoreChannelInfo** info) {
1002	UNUSED(core);
1003	if (info) {
1004		*info = _GBAVideoLayers;
1005	}
1006	return sizeof(_GBAVideoLayers) / sizeof(*_GBAVideoLayers);
1007}
1008
1009static size_t _GBACoreListAudioChannels(const struct mCore* core, const struct mCoreChannelInfo** info) {
1010	UNUSED(core);
1011	if (info) {
1012		*info = _GBAAudioChannels;
1013	}
1014	return sizeof(_GBAAudioChannels) / sizeof(*_GBAAudioChannels);
1015}
1016
1017static void _GBACoreEnableVideoLayer(struct mCore* core, size_t id, bool enable) {
1018	struct GBA* gba = core->board;
1019	switch (id) {
1020	case 0:
1021	case 1:
1022	case 2:
1023	case 3:
1024		gba->video.renderer->disableBG[id] = !enable;
1025		break;
1026	case 4:
1027		gba->video.renderer->disableOBJ = !enable;
1028		break;
1029	default:
1030		break;
1031	}
1032}
1033
1034static void _GBACoreEnableAudioChannel(struct mCore* core, size_t id, bool enable) {
1035	struct GBA* gba = core->board;
1036	switch (id) {
1037	case 0:
1038	case 1:
1039	case 2:
1040	case 3:
1041		gba->audio.psg.forceDisableCh[id] = !enable;
1042		break;
1043	case 4:
1044		gba->audio.forceDisableChA = !enable;
1045		break;
1046	case 5:
1047		gba->audio.forceDisableChB = !enable;
1048		break;
1049	default:
1050		break;
1051	}
1052}
1053
1054static void _GBACoreAdjustVideoLayer(struct mCore* core, size_t id, int32_t x, int32_t y) {
1055	struct GBACore* gbacore = (struct GBACore*) core;
1056	switch (id) {
1057	case 0:
1058	case 1:
1059	case 2:
1060	case 3:
1061		gbacore->renderer.bg[id].offsetX = x;
1062		gbacore->renderer.bg[id].offsetY = y;
1063		break;
1064	case 4:
1065		gbacore->renderer.objOffsetX = x;
1066		gbacore->renderer.objOffsetY = y;
1067		gbacore->renderer.oamDirty = 1;
1068		break;
1069	default:
1070		return;
1071	}
1072	memset(gbacore->renderer.scanlineDirty, 0xFFFFFFFF, sizeof(gbacore->renderer.scanlineDirty));
1073}
1074
1075#ifndef MINIMAL_CORE
1076static void _GBACoreStartVideoLog(struct mCore* core, struct mVideoLogContext* context) {
1077	struct GBACore* gbacore = (struct GBACore*) core;
1078	struct GBA* gba = core->board;
1079	gbacore->logContext = context;
1080
1081	struct GBASerializedState* state = mVideoLogContextInitialState(context, NULL);
1082	state->id = 0;
1083	state->cpu.gprs[ARM_PC] = BASE_WORKING_RAM;
1084
1085	int channelId = mVideoLoggerAddChannel(context);
1086	gbacore->vlProxy.logger = malloc(sizeof(struct mVideoLogger));
1087	mVideoLoggerRendererCreate(gbacore->vlProxy.logger, false);
1088	mVideoLoggerAttachChannel(gbacore->vlProxy.logger, context, channelId);
1089	gbacore->vlProxy.logger->block = false;
1090
1091	GBAVideoProxyRendererCreate(&gbacore->vlProxy, gba->video.renderer);
1092	GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1093}
1094
1095static void _GBACoreEndVideoLog(struct mCore* core) {
1096	struct GBACore* gbacore = (struct GBACore*) core;
1097	struct GBA* gba = core->board;
1098	if (gbacore->vlProxy.logger) {
1099		GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1100		free(gbacore->vlProxy.logger);
1101		gbacore->vlProxy.logger = NULL;
1102	}
1103}
1104#endif
1105
1106struct mCore* GBACoreCreate(void) {
1107	struct GBACore* gbacore = malloc(sizeof(*gbacore));
1108	struct mCore* core = &gbacore->d;
1109	memset(&core->opts, 0, sizeof(core->opts));
1110	core->cpu = NULL;
1111	core->board = NULL;
1112	core->debugger = NULL;
1113	core->init = _GBACoreInit;
1114	core->deinit = _GBACoreDeinit;
1115	core->platform = _GBACorePlatform;
1116	core->supportsFeature = _GBACoreSupportsFeature;
1117	core->setSync = _GBACoreSetSync;
1118	core->loadConfig = _GBACoreLoadConfig;
1119	core->reloadConfigOption = _GBACoreReloadConfigOption;
1120	core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
1121	core->setVideoBuffer = _GBACoreSetVideoBuffer;
1122	core->setVideoGLTex = _GBACoreSetVideoGLTex;
1123	core->getPixels = _GBACoreGetPixels;
1124	core->putPixels = _GBACorePutPixels;
1125	core->getAudioChannel = _GBACoreGetAudioChannel;
1126	core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
1127	core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
1128	core->addCoreCallbacks = _GBACoreAddCoreCallbacks;
1129	core->clearCoreCallbacks = _GBACoreClearCoreCallbacks;
1130	core->setAVStream = _GBACoreSetAVStream;
1131	core->isROM = GBAIsROM;
1132	core->loadROM = _GBACoreLoadROM;
1133	core->loadBIOS = _GBACoreLoadBIOS;
1134	core->loadSave = _GBACoreLoadSave;
1135	core->loadTemporarySave = _GBACoreLoadTemporarySave;
1136	core->loadPatch = _GBACoreLoadPatch;
1137	core->unloadROM = _GBACoreUnloadROM;
1138	core->checksum = _GBACoreChecksum;
1139	core->reset = _GBACoreReset;
1140	core->runFrame = _GBACoreRunFrame;
1141	core->runLoop = _GBACoreRunLoop;
1142	core->step = _GBACoreStep;
1143	core->stateSize = _GBACoreStateSize;
1144	core->loadState = _GBACoreLoadState;
1145	core->saveState = _GBACoreSaveState;
1146	core->setKeys = _GBACoreSetKeys;
1147	core->addKeys = _GBACoreAddKeys;
1148	core->clearKeys = _GBACoreClearKeys;
1149	core->frameCounter = _GBACoreFrameCounter;
1150	core->frameCycles = _GBACoreFrameCycles;
1151	core->frequency = _GBACoreFrequency;
1152	core->getGameTitle = _GBACoreGetGameTitle;
1153	core->getGameCode = _GBACoreGetGameCode;
1154	core->setPeripheral = _GBACoreSetPeripheral;
1155	core->busRead8 = _GBACoreBusRead8;
1156	core->busRead16 = _GBACoreBusRead16;
1157	core->busRead32 = _GBACoreBusRead32;
1158	core->busWrite8 = _GBACoreBusWrite8;
1159	core->busWrite16 = _GBACoreBusWrite16;
1160	core->busWrite32 = _GBACoreBusWrite32;
1161	core->rawRead8 = _GBACoreRawRead8;
1162	core->rawRead16 = _GBACoreRawRead16;
1163	core->rawRead32 = _GBACoreRawRead32;
1164	core->rawWrite8 = _GBACoreRawWrite8;
1165	core->rawWrite16 = _GBACoreRawWrite16;
1166	core->rawWrite32 = _GBACoreRawWrite32;
1167	core->listMemoryBlocks = _GBAListMemoryBlocks;
1168	core->getMemoryBlock = _GBAGetMemoryBlock;
1169#ifdef USE_DEBUGGERS
1170	core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
1171	core->debuggerPlatform = _GBACoreDebuggerPlatform;
1172	core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
1173	core->attachDebugger = _GBACoreAttachDebugger;
1174	core->detachDebugger = _GBACoreDetachDebugger;
1175	core->loadSymbols = _GBACoreLoadSymbols;
1176	core->lookupIdentifier = _GBACoreLookupIdentifier;
1177#endif
1178	core->cheatDevice = _GBACoreCheatDevice;
1179	core->savedataClone = _GBACoreSavedataClone;
1180	core->savedataRestore = _GBACoreSavedataRestore;
1181	core->listVideoLayers = _GBACoreListVideoLayers;
1182	core->listAudioChannels = _GBACoreListAudioChannels;
1183	core->enableVideoLayer = _GBACoreEnableVideoLayer;
1184	core->enableAudioChannel = _GBACoreEnableAudioChannel;
1185	core->adjustVideoLayer = _GBACoreAdjustVideoLayer;
1186#ifndef MINIMAL_CORE
1187	core->startVideoLog = _GBACoreStartVideoLog;
1188	core->endVideoLog = _GBACoreEndVideoLog;
1189#endif
1190	return core;
1191}
1192
1193#ifndef MINIMAL_CORE
1194static void _GBAVLPStartFrameCallback(void *context) {
1195	struct mCore* core = context;
1196	struct GBACore* gbacore = (struct GBACore*) core;
1197	struct GBA* gba = core->board;
1198
1199	if (!mVideoLoggerRendererRun(gbacore->vlProxy.logger, true)) {
1200		GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1201		mVideoLogContextRewind(gbacore->logContext, core);
1202		GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1203		gba->earlyExit = true;
1204	}
1205}
1206
1207static bool _GBAVLPInit(struct mCore* core) {
1208	struct GBACore* gbacore = (struct GBACore*) core;
1209	if (!_GBACoreInit(core)) {
1210		return false;
1211	}
1212	gbacore->vlProxy.logger = malloc(sizeof(struct mVideoLogger));
1213	mVideoLoggerRendererCreate(gbacore->vlProxy.logger, true);
1214	GBAVideoProxyRendererCreate(&gbacore->vlProxy, NULL);
1215	memset(&gbacore->logCallbacks, 0, sizeof(gbacore->logCallbacks));
1216	gbacore->logCallbacks.videoFrameStarted = _GBAVLPStartFrameCallback;
1217	gbacore->logCallbacks.context = core;
1218	core->addCoreCallbacks(core, &gbacore->logCallbacks);
1219	core->videoLogger = gbacore->vlProxy.logger;
1220	return true;
1221}
1222
1223static void _GBAVLPDeinit(struct mCore* core) {
1224	struct GBACore* gbacore = (struct GBACore*) core;
1225	if (gbacore->logContext) {
1226		mVideoLogContextDestroy(core, gbacore->logContext);
1227	}
1228	_GBACoreDeinit(core);
1229}
1230
1231static void _GBAVLPReset(struct mCore* core) {
1232	struct GBACore* gbacore = (struct GBACore*) core;
1233	struct GBA* gba = (struct GBA*) core->board;
1234	if (gba->video.renderer == &gbacore->vlProxy.d) {
1235		GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1236	} else if (gbacore->renderer.outputBuffer) {
1237		struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
1238		GBAVideoAssociateRenderer(&gba->video, renderer);
1239	}
1240
1241	ARMReset(core->cpu);
1242	mVideoLogContextRewind(gbacore->logContext, core);
1243	GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1244
1245	// Make sure CPU loop never spins
1246	GBAHalt(gba);
1247	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
1248	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
1249}
1250
1251static bool _GBAVLPLoadROM(struct mCore* core, struct VFile* vf) {
1252	struct GBACore* gbacore = (struct GBACore*) core;
1253	gbacore->logContext = mVideoLogContextCreate(NULL);
1254	if (!mVideoLogContextLoad(gbacore->logContext, vf)) {
1255		mVideoLogContextDestroy(core, gbacore->logContext);
1256		gbacore->logContext = NULL;
1257		return false;
1258	}
1259	mVideoLoggerAttachChannel(gbacore->vlProxy.logger, gbacore->logContext, 0);
1260	return true;
1261}
1262
1263static bool _GBAVLPLoadState(struct mCore* core, const void* state) {
1264	struct GBA* gba = (struct GBA*) core->board;
1265
1266	gba->timing.root = NULL;
1267	gba->cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
1268	gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
1269
1270	// Make sure CPU loop never spins
1271	GBAHalt(gba);
1272	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
1273	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
1274	GBAVideoDeserialize(&gba->video, state);
1275	GBAIODeserialize(gba, state);
1276	GBAAudioReset(&gba->audio);
1277
1278	return true;
1279}
1280
1281static bool _returnTrue(struct VFile* vf) {
1282	UNUSED(vf);
1283	return true;
1284}
1285
1286struct mCore* GBAVideoLogPlayerCreate(void) {
1287	struct mCore* core = GBACoreCreate();
1288	core->init = _GBAVLPInit;
1289	core->deinit = _GBAVLPDeinit;
1290	core->reset = _GBAVLPReset;
1291	core->loadROM = _GBAVLPLoadROM;
1292	core->loadState = _GBAVLPLoadState;
1293	core->isROM = _returnTrue;
1294	return core;
1295}
1296#else
1297struct mCore* GBAVideoLogPlayerCreate(void) {
1298	return false;
1299}
1300#endif