all repos — mgba @ 9a0da39848a540ef3909f0051160843f093dfd83

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_MAPPED },
  62	{ REGION_CART1, "cart1", "ROM WS1", "Game Pak (Waitstate 1)", BASE_CART1, BASE_CART1 + SIZE_CART1, SIZE_CART1, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  63	{ REGION_CART2, "cart2", "ROM WS2", "Game Pak (Waitstate 2)", BASE_CART2, BASE_CART2 + SIZE_CART2, SIZE_CART2, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  64};
  65
  66static const struct mCoreMemoryBlock _GBAMemoryBlocksSRAM[] = {
  67	{ -1, "mem", "All", "All", 0, 0x10000000, 0x10000000, mCORE_MEMORY_VIRTUAL },
  68	{ REGION_BIOS, "bios", "BIOS", "BIOS (16kiB)", BASE_BIOS, SIZE_BIOS, SIZE_BIOS, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  69	{ REGION_WORKING_RAM, "wram", "EWRAM", "Working RAM (256kiB)", BASE_WORKING_RAM, BASE_WORKING_RAM + SIZE_WORKING_RAM, SIZE_WORKING_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  70	{ REGION_WORKING_IRAM, "iwram", "IWRAM", "Internal Working RAM (32kiB)", BASE_WORKING_IRAM, BASE_WORKING_IRAM + SIZE_WORKING_IRAM, SIZE_WORKING_IRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  71	{ REGION_IO, "io", "MMIO", "Memory-Mapped I/O", BASE_IO, BASE_IO + SIZE_IO, SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  72	{ REGION_PALETTE_RAM, "palette", "Palette", "Palette RAM (1kiB)", BASE_PALETTE_RAM, BASE_PALETTE_RAM + SIZE_PALETTE_RAM, SIZE_PALETTE_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  73	{ REGION_VRAM, "vram", "VRAM", "Video RAM (96kiB)", BASE_VRAM, BASE_VRAM + SIZE_VRAM, SIZE_VRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  74	{ REGION_OAM, "oam", "OAM", "OBJ Attribute Memory (1kiB)", BASE_OAM, BASE_OAM + SIZE_OAM, SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  75	{ REGION_CART0, "cart0", "ROM", "Game Pak (32MiB)", BASE_CART0, BASE_CART0 + SIZE_CART0, SIZE_CART0, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  76	{ REGION_CART1, "cart1", "ROM WS1", "Game Pak (Waitstate 1)", BASE_CART1, BASE_CART1 + SIZE_CART1, SIZE_CART1, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  77	{ REGION_CART2, "cart2", "ROM WS2", "Game Pak (Waitstate 2)", BASE_CART2, BASE_CART2 + SIZE_CART2, SIZE_CART2, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  78	{ REGION_CART_SRAM, "sram", "SRAM", "Static RAM (64kiB)", BASE_CART_SRAM, BASE_CART_SRAM + SIZE_CART_SRAM, SIZE_CART_SRAM, true },
  79};
  80
  81static const struct mCoreMemoryBlock _GBAMemoryBlocksFlash512[] = {
  82	{ -1, "mem", "All", "All", 0, 0x10000000, 0x10000000, mCORE_MEMORY_VIRTUAL },
  83	{ REGION_BIOS, "bios", "BIOS", "BIOS (16kiB)", BASE_BIOS, SIZE_BIOS, SIZE_BIOS, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  84	{ REGION_WORKING_RAM, "wram", "EWRAM", "Working RAM (256kiB)", BASE_WORKING_RAM, BASE_WORKING_RAM + SIZE_WORKING_RAM, SIZE_WORKING_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  85	{ REGION_WORKING_IRAM, "iwram", "IWRAM", "Internal Working RAM (32kiB)", BASE_WORKING_IRAM, BASE_WORKING_IRAM + SIZE_WORKING_IRAM, SIZE_WORKING_IRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  86	{ REGION_IO, "io", "MMIO", "Memory-Mapped I/O", BASE_IO, BASE_IO + SIZE_IO, SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  87	{ REGION_PALETTE_RAM, "palette", "Palette", "Palette RAM (1kiB)", BASE_PALETTE_RAM, BASE_PALETTE_RAM + SIZE_PALETTE_RAM, SIZE_PALETTE_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  88	{ REGION_VRAM, "vram", "VRAM", "Video RAM (96kiB)", BASE_VRAM, BASE_VRAM + SIZE_VRAM, SIZE_VRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  89	{ REGION_OAM, "oam", "OAM", "OBJ Attribute Memory (1kiB)", BASE_OAM, BASE_OAM + SIZE_OAM, SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  90	{ REGION_CART0, "cart0", "ROM", "Game Pak (32MiB)", BASE_CART0, BASE_CART0 + SIZE_CART0, SIZE_CART0, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  91	{ REGION_CART1, "cart1", "ROM WS1", "Game Pak (Waitstate 1)", BASE_CART1, BASE_CART1 + SIZE_CART1, SIZE_CART1, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  92	{ REGION_CART2, "cart2", "ROM WS2", "Game Pak (Waitstate 2)", BASE_CART2, BASE_CART2 + SIZE_CART2, SIZE_CART2, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  93	{ REGION_CART_SRAM, "sram", "Flash", "Flash Memory (64kiB)", BASE_CART_SRAM, BASE_CART_SRAM + SIZE_CART_FLASH512, SIZE_CART_FLASH512, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
  94};
  95
  96static const struct mCoreMemoryBlock _GBAMemoryBlocksFlash1M[] = {
  97	{ -1, "mem", "All", "All", 0, 0x10000000, 0x10000000, mCORE_MEMORY_VIRTUAL },
  98	{ REGION_BIOS, "bios", "BIOS", "BIOS (16kiB)", BASE_BIOS, SIZE_BIOS, SIZE_BIOS, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
  99	{ REGION_WORKING_RAM, "wram", "EWRAM", "Working RAM (256kiB)", BASE_WORKING_RAM, BASE_WORKING_RAM + SIZE_WORKING_RAM, SIZE_WORKING_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 100	{ REGION_WORKING_IRAM, "iwram", "IWRAM", "Internal Working RAM (32kiB)", BASE_WORKING_IRAM, BASE_WORKING_IRAM + SIZE_WORKING_IRAM, SIZE_WORKING_IRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 101	{ REGION_IO, "io", "MMIO", "Memory-Mapped I/O", BASE_IO, BASE_IO + SIZE_IO, SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 102	{ REGION_PALETTE_RAM, "palette", "Palette", "Palette RAM (1kiB)", BASE_PALETTE_RAM, BASE_PALETTE_RAM + SIZE_PALETTE_RAM, SIZE_PALETTE_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 103	{ REGION_VRAM, "vram", "VRAM", "Video RAM (96kiB)", BASE_VRAM, BASE_VRAM + SIZE_VRAM, SIZE_VRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 104	{ REGION_OAM, "oam", "OAM", "OBJ Attribute Memory (1kiB)", BASE_OAM, BASE_OAM + SIZE_OAM, SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 105	{ REGION_CART0, "cart0", "ROM", "Game Pak (32MiB)", BASE_CART0, BASE_CART0 + SIZE_CART0, SIZE_CART0, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
 106	{ REGION_CART1, "cart1", "ROM WS1", "Game Pak (Waitstate 1)", BASE_CART1, BASE_CART1 + SIZE_CART1, SIZE_CART1, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
 107	{ REGION_CART2, "cart2", "ROM WS2", "Game Pak (Waitstate 2)", BASE_CART2, BASE_CART2 + SIZE_CART2, SIZE_CART2, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
 108	{ REGION_CART_SRAM, "sram", "Flash", "Flash Memory (64kiB)", BASE_CART_SRAM, BASE_CART_SRAM + SIZE_CART_FLASH512, SIZE_CART_FLASH1M, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED, 1 },
 109};
 110
 111static const struct mCoreMemoryBlock _GBAMemoryBlocksEEPROM[] = {
 112	{ -1, "mem", "All", "All", 0, 0x10000000, 0x10000000, mCORE_MEMORY_VIRTUAL },
 113	{ REGION_BIOS, "bios", "BIOS", "BIOS (16kiB)", BASE_BIOS, SIZE_BIOS, SIZE_BIOS, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
 114	{ REGION_WORKING_RAM, "wram", "EWRAM", "Working RAM (256kiB)", BASE_WORKING_RAM, BASE_WORKING_RAM + SIZE_WORKING_RAM, SIZE_WORKING_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 115	{ REGION_WORKING_IRAM, "iwram", "IWRAM", "Internal Working RAM (32kiB)", BASE_WORKING_IRAM, BASE_WORKING_IRAM + SIZE_WORKING_IRAM, SIZE_WORKING_IRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 116	{ REGION_IO, "io", "MMIO", "Memory-Mapped I/O", BASE_IO, BASE_IO + SIZE_IO, SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 117	{ REGION_PALETTE_RAM, "palette", "Palette", "Palette RAM (1kiB)", BASE_PALETTE_RAM, BASE_PALETTE_RAM + SIZE_PALETTE_RAM, SIZE_PALETTE_RAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 118	{ REGION_VRAM, "vram", "VRAM", "Video RAM (96kiB)", BASE_VRAM, BASE_VRAM + SIZE_VRAM, SIZE_VRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 119	{ REGION_OAM, "oam", "OAM", "OBJ Attribute Memory (1kiB)", BASE_OAM, BASE_OAM + SIZE_OAM, SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
 120	{ REGION_CART0, "cart0", "ROM", "Game Pak (32MiB)", BASE_CART0, BASE_CART0 + SIZE_CART0, SIZE_CART0, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
 121	{ REGION_CART1, "cart1", "ROM WS1", "Game Pak (Waitstate 1)", BASE_CART1, BASE_CART1 + SIZE_CART1, SIZE_CART1, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
 122	{ REGION_CART2, "cart2", "ROM WS2", "Game Pak (Waitstate 2)", BASE_CART2, BASE_CART2 + SIZE_CART2, SIZE_CART2, mCORE_MEMORY_READ | mCORE_MEMORY_MAPPED },
 123	{ REGION_CART_SRAM_MIRROR, "eeprom", "EEPROM", "EEPROM (8kiB)", 0, SIZE_CART_EEPROM, SIZE_CART_EEPROM, mCORE_MEMORY_RW },
 124};
 125
 126struct mVideoLogContext;
 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
 352static void _GBACoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
 353#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 354	struct GBACore* gbacore = (struct GBACore*) core;
 355	int scale = gbacore->glRenderer.scale;
 356#else
 357	UNUSED(core);
 358	int scale = 1;
 359#endif
 360
 361	*width = GBA_VIDEO_HORIZONTAL_PIXELS * scale;
 362	*height = GBA_VIDEO_VERTICAL_PIXELS * scale;
 363}
 364
 365static void _GBACoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
 366	struct GBACore* gbacore = (struct GBACore*) core;
 367	gbacore->renderer.outputBuffer = buffer;
 368	gbacore->renderer.outputBufferStride = stride;
 369	memset(gbacore->renderer.scanlineDirty, 0xFFFFFFFF, sizeof(gbacore->renderer.scanlineDirty));
 370}
 371
 372static void _GBACoreSetVideoGLTex(struct mCore* core, unsigned texid) {
 373#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 374	struct GBACore* gbacore = (struct GBACore*) core;
 375	gbacore->glRenderer.outputTex = texid;
 376#else
 377	UNUSED(core);
 378	UNUSED(texid);
 379#endif
 380}
 381
 382static void _GBACoreGetPixels(struct mCore* core, const void** buffer, size_t* stride) {
 383	struct GBA* gba = core->board;
 384	gba->video.renderer->getPixels(gba->video.renderer, stride, buffer);
 385}
 386
 387static void _GBACorePutPixels(struct mCore* core, const void* buffer, size_t stride) {
 388	struct GBA* gba = core->board;
 389	gba->video.renderer->putPixels(gba->video.renderer, stride, buffer);
 390}
 391
 392static struct blip_t* _GBACoreGetAudioChannel(struct mCore* core, int ch) {
 393	struct GBA* gba = core->board;
 394	switch (ch) {
 395	case 0:
 396		return gba->audio.psg.left;
 397	case 1:
 398		return gba->audio.psg.right;
 399	default:
 400		return NULL;
 401	}
 402}
 403
 404static void _GBACoreSetAudioBufferSize(struct mCore* core, size_t samples) {
 405	struct GBA* gba = core->board;
 406	GBAAudioResizeBuffer(&gba->audio, samples);
 407}
 408
 409static size_t _GBACoreGetAudioBufferSize(struct mCore* core) {
 410	struct GBA* gba = core->board;
 411	return gba->audio.samples;
 412}
 413
 414static void _GBACoreAddCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) {
 415	struct GBA* gba = core->board;
 416	*mCoreCallbacksListAppend(&gba->coreCallbacks) = *coreCallbacks;
 417}
 418
 419static void _GBACoreClearCoreCallbacks(struct mCore* core) {
 420	struct GBA* gba = core->board;
 421	mCoreCallbacksListClear(&gba->coreCallbacks);
 422}
 423
 424static void _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
 425	struct GBA* gba = core->board;
 426	gba->stream = stream;
 427	if (stream && stream->videoDimensionsChanged) {
 428		unsigned width, height;
 429		core->desiredVideoDimensions(core, &width, &height);
 430		stream->videoDimensionsChanged(stream, width, height);
 431	}
 432}
 433
 434static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
 435#ifdef USE_ELF
 436	struct ELF* elf = ELFOpen(vf);
 437	if (elf) {
 438		GBALoadNull(core->board);
 439		bool success = mCoreLoadELF(core, elf);
 440		ELFClose(elf);
 441		return success;
 442	}
 443#endif
 444	if (GBAIsMB(vf)) {
 445		return GBALoadMB(core->board, vf);
 446	}
 447	return GBALoadROM(core->board, vf);
 448}
 449
 450static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
 451	UNUSED(type);
 452	if (!GBAIsBIOS(vf)) {
 453		return false;
 454	}
 455	GBALoadBIOS(core->board, vf);
 456	return true;
 457}
 458
 459static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
 460	return GBALoadSave(core->board, vf);
 461}
 462
 463static bool _GBACoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
 464	struct GBA* gba = core->board;
 465	GBASavedataMask(&gba->memory.savedata, vf, false);
 466	return true; // TODO: Return a real value
 467}
 468
 469static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
 470	if (!vf) {
 471		return false;
 472	}
 473	struct Patch patch;
 474	if (!loadPatch(vf, &patch)) {
 475		return false;
 476	}
 477	GBAApplyPatch(core->board, &patch);
 478	return true;
 479}
 480
 481static void _GBACoreUnloadROM(struct mCore* core) {
 482	struct GBACore* gbacore = (struct GBACore*) core;
 483	struct ARMCore* cpu = core->cpu;
 484	if (gbacore->cheatDevice) {
 485		ARMHotplugDetach(cpu, CPU_COMPONENT_CHEAT_DEVICE);
 486		cpu->components[CPU_COMPONENT_CHEAT_DEVICE] = NULL;
 487		mCheatDeviceDestroy(gbacore->cheatDevice);
 488		gbacore->cheatDevice = NULL;
 489	}
 490	return GBAUnloadROM(core->board);
 491}
 492
 493static void _GBACoreChecksum(const struct mCore* core, void* data, enum mCoreChecksumType type) {
 494	struct GBA* gba = (struct GBA*) core->board;
 495	switch (type) {
 496	case CHECKSUM_CRC32:
 497		memcpy(data, &gba->romCrc32, sizeof(gba->romCrc32));
 498		break;
 499	}
 500	return;
 501}
 502
 503static void _GBACoreReset(struct mCore* core) {
 504	struct GBACore* gbacore = (struct GBACore*) core;
 505	struct GBA* gba = (struct GBA*) core->board;
 506	if (gbacore->renderer.outputBuffer
 507#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 508	    || gbacore->glRenderer.outputTex != (unsigned) -1
 509#endif
 510	) {
 511		struct GBAVideoRenderer* renderer = NULL;
 512		if (gbacore->renderer.outputBuffer) {
 513			renderer = &gbacore->renderer.d;
 514		}
 515		int fakeBool;
 516#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
 517		if (gbacore->glRenderer.outputTex != (unsigned) -1 && mCoreConfigGetIntValue(&core->config, "hwaccelVideo", &fakeBool) && fakeBool) {
 518			renderer = &gbacore->glRenderer.d;
 519			mCoreConfigGetIntValue(&core->config, "videoScale", &gbacore->glRenderer.scale);
 520		}
 521#endif
 522#ifndef DISABLE_THREADING
 523		if (mCoreConfigGetIntValue(&core->config, "threadedVideo", &fakeBool) && fakeBool) {
 524			if (!core->videoLogger) {
 525				core->videoLogger = &gbacore->threadProxy.d;
 526			}
 527		}
 528#endif
 529#ifndef MINIMAL_CORE
 530		if (renderer && core->videoLogger) {
 531			gbacore->proxyRenderer.logger = core->videoLogger;
 532			GBAVideoProxyRendererCreate(&gbacore->proxyRenderer, renderer);
 533			renderer = &gbacore->proxyRenderer.d;
 534		}
 535#endif
 536		if (renderer) {
 537			GBAVideoAssociateRenderer(&gba->video, renderer);
 538		}
 539	}
 540
 541#ifndef MINIMAL_CORE
 542	int useAudioMixer;
 543	if (!gbacore->audioMixer && mCoreConfigGetIntValue(&core->config, "gba.audioHle", &useAudioMixer) && useAudioMixer) {
 544		gbacore->audioMixer = malloc(sizeof(*gbacore->audioMixer));
 545		GBAAudioMixerCreate(gbacore->audioMixer);
 546		((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_AUDIO_MIXER] = &gbacore->audioMixer->d;
 547		ARMHotplugAttach(core->cpu, CPU_COMPONENT_AUDIO_MIXER);
 548	}
 549#endif
 550
 551	GBAOverrideApplyDefaults(gba, gbacore->overrides);
 552
 553#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 554	if (!gba->biosVf && core->opts.useBios) {
 555		struct VFile* bios = NULL;
 556		bool found = false;
 557		if (core->opts.bios) {
 558			bios = VFileOpen(core->opts.bios, O_RDONLY);
 559			if (bios && GBAIsBIOS(bios)) {
 560				found = true;
 561			} else if (bios) {
 562				bios->close(bios);
 563				bios = NULL;
 564			}
 565		}
 566		if (!found) {
 567			const char* configPath = mCoreConfigGetValue(&core->config, "gba.bios");
 568			if (configPath) {
 569				bios = VFileOpen(configPath, O_RDONLY);
 570			}
 571			if (bios && GBAIsBIOS(bios)) {
 572				found = true;
 573			} else if (bios) {
 574				bios->close(bios);
 575				bios = NULL;
 576			}
 577		}
 578		if (!found) {
 579			char path[PATH_MAX];
 580			mCoreConfigDirectory(path, PATH_MAX);
 581			strncat(path, PATH_SEP "gba_bios.bin", PATH_MAX - strlen(path));
 582			bios = VFileOpen(path, O_RDONLY);
 583			if (bios && GBAIsBIOS(bios)) {
 584				found = true;
 585			} else if (bios) {
 586				bios->close(bios);
 587				bios = NULL;
 588			}
 589		}
 590		if (found && bios) {
 591			GBALoadBIOS(gba, bios);
 592		}
 593	}
 594#endif
 595
 596	ARMReset(core->cpu);
 597	if (core->opts.skipBios && (gba->romVf || gba->memory.rom)) {
 598		GBASkipBIOS(core->board);
 599	}
 600}
 601
 602static void _GBACoreRunFrame(struct mCore* core) {
 603	struct GBA* gba = core->board;
 604	int32_t frameCounter = gba->video.frameCounter;
 605	while (gba->video.frameCounter == frameCounter) {
 606		ARMRunLoop(core->cpu);
 607	}
 608}
 609
 610static void _GBACoreRunLoop(struct mCore* core) {
 611	ARMRunLoop(core->cpu);
 612}
 613
 614static void _GBACoreStep(struct mCore* core) {
 615	ARMRun(core->cpu);
 616}
 617
 618static size_t _GBACoreStateSize(struct mCore* core) {
 619	UNUSED(core);
 620	return sizeof(struct GBASerializedState);
 621}
 622
 623static bool _GBACoreLoadState(struct mCore* core, const void* state) {
 624	return GBADeserialize(core->board, state);
 625}
 626
 627static bool _GBACoreSaveState(struct mCore* core, void* state) {
 628	GBASerialize(core->board, state);
 629	return true;
 630}
 631
 632static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
 633	struct GBACore* gbacore = (struct GBACore*) core;
 634	gbacore->keys = keys;
 635	GBATestKeypadIRQ(core->board);
 636}
 637
 638static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
 639	struct GBACore* gbacore = (struct GBACore*) core;
 640	gbacore->keys |= keys;
 641	GBATestKeypadIRQ(core->board);
 642}
 643
 644static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
 645	struct GBACore* gbacore = (struct GBACore*) core;
 646	gbacore->keys &= ~keys;
 647}
 648
 649static int32_t _GBACoreFrameCounter(const struct mCore* core) {
 650	const struct GBA* gba = core->board;
 651	return gba->video.frameCounter;
 652}
 653
 654static int32_t _GBACoreFrameCycles(const struct mCore* core) {
 655	UNUSED(core);
 656	return VIDEO_TOTAL_LENGTH;
 657}
 658
 659static int32_t _GBACoreFrequency(const struct mCore* core) {
 660	UNUSED(core);
 661	return GBA_ARM7TDMI_FREQUENCY;
 662}
 663
 664static void _GBACoreGetGameTitle(const struct mCore* core, char* title) {
 665	GBAGetGameTitle(core->board, title);
 666}
 667
 668static void _GBACoreGetGameCode(const struct mCore* core, char* title) {
 669	GBAGetGameCode(core->board, title);
 670}
 671
 672static void _GBACoreSetPeripheral(struct mCore* core, int type, void* periph) {
 673	struct GBA* gba = core->board;
 674	switch (type) {
 675	case mPERIPH_ROTATION:
 676		gba->rotationSource = periph;
 677		break;
 678	case mPERIPH_RUMBLE:
 679		gba->rumble = periph;
 680		break;
 681	case mPERIPH_GBA_LUMINANCE:
 682		gba->luminanceSource = periph;
 683		break;
 684	case mPERIPH_GBA_BATTLECHIP_GATE:
 685		GBASIOSetDriver(&gba->sio, periph, SIO_MULTI);
 686		GBASIOSetDriver(&gba->sio, periph, SIO_NORMAL_32);
 687		break;
 688	default:
 689		return;
 690	}
 691}
 692
 693static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) {
 694	struct ARMCore* cpu = core->cpu;
 695	return cpu->memory.load8(cpu, address, 0);
 696}
 697
 698static uint32_t _GBACoreBusRead16(struct mCore* core, uint32_t address) {
 699	struct ARMCore* cpu = core->cpu;
 700	return cpu->memory.load16(cpu, address, 0);
 701
 702}
 703
 704static uint32_t _GBACoreBusRead32(struct mCore* core, uint32_t address) {
 705	struct ARMCore* cpu = core->cpu;
 706	return cpu->memory.load32(cpu, address, 0);
 707}
 708
 709static void _GBACoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
 710	struct ARMCore* cpu = core->cpu;
 711	cpu->memory.store8(cpu, address, value, 0);
 712}
 713
 714static void _GBACoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
 715	struct ARMCore* cpu = core->cpu;
 716	cpu->memory.store16(cpu, address, value, 0);
 717}
 718
 719static void _GBACoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
 720	struct ARMCore* cpu = core->cpu;
 721	cpu->memory.store32(cpu, address, value, 0);
 722}
 723
 724static uint32_t _GBACoreRawRead8(struct mCore* core, uint32_t address, int segment) {
 725	UNUSED(segment);
 726	struct ARMCore* cpu = core->cpu;
 727	return GBAView8(cpu, address);
 728}
 729
 730static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address, int segment) {
 731	UNUSED(segment);
 732	struct ARMCore* cpu = core->cpu;
 733	return GBAView16(cpu, address);
 734}
 735
 736static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address, int segment) {
 737	UNUSED(segment);
 738	struct ARMCore* cpu = core->cpu;
 739	return GBAView32(cpu, address);
 740}
 741
 742static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
 743	UNUSED(segment);
 744	struct ARMCore* cpu = core->cpu;
 745	GBAPatch8(cpu, address, value, NULL);
 746}
 747
 748static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
 749	UNUSED(segment);
 750	struct ARMCore* cpu = core->cpu;
 751	GBAPatch16(cpu, address, value, NULL);
 752}
 753
 754static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
 755	UNUSED(segment);
 756	struct ARMCore* cpu = core->cpu;
 757	GBAPatch32(cpu, address, value, NULL);
 758}
 759
 760size_t _GBAListMemoryBlocks(const struct mCore* core, const struct mCoreMemoryBlock** blocks) {
 761	const struct GBA* gba = core->board;
 762	switch (gba->memory.savedata.type) {
 763	case SAVEDATA_SRAM:
 764		*blocks = _GBAMemoryBlocksSRAM;
 765		return sizeof(_GBAMemoryBlocksSRAM) / sizeof(*_GBAMemoryBlocksSRAM);
 766	case SAVEDATA_FLASH512:
 767		*blocks = _GBAMemoryBlocksFlash512;
 768		return sizeof(_GBAMemoryBlocksFlash512) / sizeof(*_GBAMemoryBlocksFlash512);
 769	case SAVEDATA_FLASH1M:
 770		*blocks = _GBAMemoryBlocksFlash1M;
 771		return sizeof(_GBAMemoryBlocksFlash1M) / sizeof(*_GBAMemoryBlocksFlash1M);
 772	case SAVEDATA_EEPROM:
 773		*blocks = _GBAMemoryBlocksEEPROM;
 774		return sizeof(_GBAMemoryBlocksEEPROM) / sizeof(*_GBAMemoryBlocksEEPROM);
 775	default:
 776		*blocks = _GBAMemoryBlocks;
 777		return sizeof(_GBAMemoryBlocks) / sizeof(*_GBAMemoryBlocks);
 778	}
 779}
 780
 781void* _GBAGetMemoryBlock(struct mCore* core, size_t id, size_t* sizeOut) {
 782	struct GBA* gba = core->board;
 783	switch (id) {
 784	default:
 785		return NULL;
 786	case REGION_BIOS:
 787		*sizeOut = SIZE_BIOS;
 788		return gba->memory.bios;
 789	case REGION_WORKING_RAM:
 790		*sizeOut = SIZE_WORKING_RAM;
 791		return gba->memory.wram;
 792	case REGION_WORKING_IRAM:
 793		*sizeOut = SIZE_WORKING_IRAM;
 794		return gba->memory.iwram;
 795	case REGION_PALETTE_RAM:
 796		*sizeOut = SIZE_PALETTE_RAM;
 797		return gba->video.palette;
 798	case REGION_VRAM:
 799		*sizeOut = SIZE_VRAM;
 800		return gba->video.vram;
 801	case REGION_OAM:
 802		*sizeOut = SIZE_OAM;
 803		return gba->video.oam.raw;
 804	case REGION_CART0:
 805	case REGION_CART1:
 806	case REGION_CART2:
 807		*sizeOut = gba->memory.romSize;
 808		return gba->memory.rom;
 809	case REGION_CART_SRAM:
 810		if (gba->memory.savedata.type == SAVEDATA_FLASH1M) {
 811			*sizeOut = SIZE_CART_FLASH1M;
 812			return gba->memory.savedata.currentBank;
 813		}
 814		// Fall through
 815	case REGION_CART_SRAM_MIRROR:
 816		*sizeOut = GBASavedataSize(&gba->memory.savedata);
 817		return gba->memory.savedata.data;
 818	}
 819}
 820
 821#ifdef USE_DEBUGGERS
 822static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
 823	UNUSED(core);
 824	switch (type) {
 825	case DEBUGGER_CUSTOM:
 826	case DEBUGGER_CLI:
 827	case DEBUGGER_GDB:
 828		return true;
 829	default:
 830		return false;
 831	}
 832}
 833
 834static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
 835	struct GBACore* gbacore = (struct GBACore*) core;
 836	if (!gbacore->debuggerPlatform) {
 837		gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
 838	}
 839	return gbacore->debuggerPlatform;
 840}
 841
 842static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
 843	return &GBACLIDebuggerCreate(core)->d;
 844}
 845
 846static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
 847	if (core->debugger) {
 848		GBADetachDebugger(core->board);
 849	}
 850	GBAAttachDebugger(core->board, debugger);
 851	core->debugger = debugger;
 852}
 853
 854static void _GBACoreDetachDebugger(struct mCore* core) {
 855	GBADetachDebugger(core->board);
 856	core->debugger = NULL;
 857}
 858
 859static void _GBACoreLoadSymbols(struct mCore* core, struct VFile* vf) {
 860	bool closeAfter = false;
 861	core->symbolTable = mDebuggerSymbolTableCreate();
 862#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 863#ifdef USE_ELF
 864	if (!vf) {
 865		closeAfter = true;
 866		vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".elf", O_RDONLY);
 867	}
 868#endif
 869	if (!vf) {
 870		closeAfter = true;
 871		vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".sym", O_RDONLY);
 872	}
 873#endif
 874	if (!vf) {
 875		return;
 876	}
 877#ifdef USE_ELF
 878	struct ELF* elf = ELFOpen(vf);
 879	if (elf) {
 880#ifdef USE_DEBUGGERS
 881		mCoreLoadELFSymbols(core->symbolTable, elf);
 882#endif
 883		ELFClose(elf);
 884	} else
 885#endif
 886	{
 887		mDebuggerLoadARMIPSSymbols(core->symbolTable, vf);
 888	}
 889	if (closeAfter) {
 890		vf->close(vf);
 891	}
 892}
 893
 894static bool _GBACoreLookupIdentifier(struct mCore* core, const char* name, int32_t* value, int* segment) {
 895	UNUSED(core);
 896	*segment = -1;
 897	int i;
 898	for (i = 0; i < REG_MAX; i += 2) {
 899		const char* reg = GBAIORegisterNames[i >> 1];
 900		if (reg && strcasecmp(reg, name) == 0) {
 901			*value = BASE_IO | i;
 902			return true;
 903		}
 904	}
 905	return false;
 906}
 907#endif
 908
 909static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
 910	struct GBACore* gbacore = (struct GBACore*) core;
 911	if (!gbacore->cheatDevice) {
 912		gbacore->cheatDevice = GBACheatDeviceCreate();
 913		((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
 914		ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
 915		gbacore->cheatDevice->p = core;
 916	}
 917	return gbacore->cheatDevice;
 918}
 919
 920static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
 921	struct GBA* gba = core->board;
 922	size_t size = GBASavedataSize(&gba->memory.savedata);
 923	if (!size) {
 924		*sram = NULL;
 925		return 0;
 926	}
 927	*sram = malloc(size);
 928	struct VFile* vf = VFileFromMemory(*sram, size);
 929	if (!vf) {
 930		free(*sram);
 931		*sram = NULL;
 932		return 0;
 933	}
 934	bool success = GBASavedataClone(&gba->memory.savedata, vf);
 935	vf->close(vf);
 936	if (!success) {
 937		free(*sram);
 938		*sram = NULL;
 939		return 0;
 940	}
 941	return size;
 942}
 943
 944static bool _GBACoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
 945	struct VFile* vf = VFileMemChunk(sram, size);
 946	if (!vf) {
 947		return false;
 948	}
 949	struct GBA* gba = core->board;
 950	bool success = true;
 951	if (writeback) {
 952		success = GBASavedataLoad(&gba->memory.savedata, vf);
 953		vf->close(vf);
 954	} else {
 955		GBASavedataMask(&gba->memory.savedata, vf, true);
 956	}
 957	return success;
 958}
 959
 960static size_t _GBACoreListVideoLayers(const struct mCore* core, const struct mCoreChannelInfo** info) {
 961	UNUSED(core);
 962	if (info) {
 963		*info = _GBAVideoLayers;
 964	}
 965	return sizeof(_GBAVideoLayers) / sizeof(*_GBAVideoLayers);
 966}
 967
 968static size_t _GBACoreListAudioChannels(const struct mCore* core, const struct mCoreChannelInfo** info) {
 969	UNUSED(core);
 970	if (info) {
 971		*info = _GBAAudioChannels;
 972	}
 973	return sizeof(_GBAAudioChannels) / sizeof(*_GBAAudioChannels);
 974}
 975
 976static void _GBACoreEnableVideoLayer(struct mCore* core, size_t id, bool enable) {
 977	struct GBA* gba = core->board;
 978	switch (id) {
 979	case 0:
 980	case 1:
 981	case 2:
 982	case 3:
 983		gba->video.renderer->disableBG[id] = !enable;
 984		break;
 985	case 4:
 986		gba->video.renderer->disableOBJ = !enable;
 987		break;
 988	default:
 989		break;
 990	}
 991}
 992
 993static void _GBACoreEnableAudioChannel(struct mCore* core, size_t id, bool enable) {
 994	struct GBA* gba = core->board;
 995	switch (id) {
 996	case 0:
 997	case 1:
 998	case 2:
 999	case 3:
1000		gba->audio.psg.forceDisableCh[id] = !enable;
1001		break;
1002	case 4:
1003		gba->audio.forceDisableChA = !enable;
1004		break;
1005	case 5:
1006		gba->audio.forceDisableChB = !enable;
1007		break;
1008	default:
1009		break;
1010	}
1011}
1012
1013static void _GBACoreAdjustVideoLayer(struct mCore* core, size_t id, int32_t x, int32_t y) {
1014	struct GBACore* gbacore = (struct GBACore*) core;
1015	switch (id) {
1016	case 0:
1017	case 1:
1018	case 2:
1019	case 3:
1020		gbacore->renderer.bg[id].offsetX = x;
1021		gbacore->renderer.bg[id].offsetY = y;
1022		break;
1023	case 4:
1024		gbacore->renderer.objOffsetX = x;
1025		gbacore->renderer.objOffsetY = y;
1026		gbacore->renderer.oamDirty = 1;
1027		break;
1028	default:
1029		return;
1030	}
1031	memset(gbacore->renderer.scanlineDirty, 0xFFFFFFFF, sizeof(gbacore->renderer.scanlineDirty));
1032}
1033
1034#ifndef MINIMAL_CORE
1035static void _GBACoreStartVideoLog(struct mCore* core, struct mVideoLogContext* context) {
1036	struct GBACore* gbacore = (struct GBACore*) core;
1037	struct GBA* gba = core->board;
1038	gbacore->logContext = context;
1039
1040	struct GBASerializedState* state = mVideoLogContextInitialState(context, NULL);
1041	state->id = 0;
1042	state->cpu.gprs[ARM_PC] = BASE_WORKING_RAM;
1043
1044	int channelId = mVideoLoggerAddChannel(context);
1045	gbacore->vlProxy.logger = malloc(sizeof(struct mVideoLogger));
1046	mVideoLoggerRendererCreate(gbacore->vlProxy.logger, false);
1047	mVideoLoggerAttachChannel(gbacore->vlProxy.logger, context, channelId);
1048	gbacore->vlProxy.logger->block = false;
1049
1050	GBAVideoProxyRendererCreate(&gbacore->vlProxy, gba->video.renderer);
1051	GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1052}
1053
1054static void _GBACoreEndVideoLog(struct mCore* core) {
1055	struct GBACore* gbacore = (struct GBACore*) core;
1056	struct GBA* gba = core->board;
1057	if (gbacore->vlProxy.logger) {
1058		GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1059		free(gbacore->vlProxy.logger);
1060		gbacore->vlProxy.logger = NULL;
1061	}
1062}
1063#endif
1064
1065struct mCore* GBACoreCreate(void) {
1066	struct GBACore* gbacore = malloc(sizeof(*gbacore));
1067	struct mCore* core = &gbacore->d;
1068	memset(&core->opts, 0, sizeof(core->opts));
1069	core->cpu = NULL;
1070	core->board = NULL;
1071	core->debugger = NULL;
1072	core->init = _GBACoreInit;
1073	core->deinit = _GBACoreDeinit;
1074	core->platform = _GBACorePlatform;
1075	core->supportsFeature = _GBACoreSupportsFeature;
1076	core->setSync = _GBACoreSetSync;
1077	core->loadConfig = _GBACoreLoadConfig;
1078	core->reloadConfigOption = _GBACoreReloadConfigOption;
1079	core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
1080	core->setVideoBuffer = _GBACoreSetVideoBuffer;
1081	core->setVideoGLTex = _GBACoreSetVideoGLTex;
1082	core->getPixels = _GBACoreGetPixels;
1083	core->putPixels = _GBACorePutPixels;
1084	core->getAudioChannel = _GBACoreGetAudioChannel;
1085	core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
1086	core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
1087	core->addCoreCallbacks = _GBACoreAddCoreCallbacks;
1088	core->clearCoreCallbacks = _GBACoreClearCoreCallbacks;
1089	core->setAVStream = _GBACoreSetAVStream;
1090	core->isROM = GBAIsROM;
1091	core->loadROM = _GBACoreLoadROM;
1092	core->loadBIOS = _GBACoreLoadBIOS;
1093	core->loadSave = _GBACoreLoadSave;
1094	core->loadTemporarySave = _GBACoreLoadTemporarySave;
1095	core->loadPatch = _GBACoreLoadPatch;
1096	core->unloadROM = _GBACoreUnloadROM;
1097	core->checksum = _GBACoreChecksum;
1098	core->reset = _GBACoreReset;
1099	core->runFrame = _GBACoreRunFrame;
1100	core->runLoop = _GBACoreRunLoop;
1101	core->step = _GBACoreStep;
1102	core->stateSize = _GBACoreStateSize;
1103	core->loadState = _GBACoreLoadState;
1104	core->saveState = _GBACoreSaveState;
1105	core->setKeys = _GBACoreSetKeys;
1106	core->addKeys = _GBACoreAddKeys;
1107	core->clearKeys = _GBACoreClearKeys;
1108	core->frameCounter = _GBACoreFrameCounter;
1109	core->frameCycles = _GBACoreFrameCycles;
1110	core->frequency = _GBACoreFrequency;
1111	core->getGameTitle = _GBACoreGetGameTitle;
1112	core->getGameCode = _GBACoreGetGameCode;
1113	core->setPeripheral = _GBACoreSetPeripheral;
1114	core->busRead8 = _GBACoreBusRead8;
1115	core->busRead16 = _GBACoreBusRead16;
1116	core->busRead32 = _GBACoreBusRead32;
1117	core->busWrite8 = _GBACoreBusWrite8;
1118	core->busWrite16 = _GBACoreBusWrite16;
1119	core->busWrite32 = _GBACoreBusWrite32;
1120	core->rawRead8 = _GBACoreRawRead8;
1121	core->rawRead16 = _GBACoreRawRead16;
1122	core->rawRead32 = _GBACoreRawRead32;
1123	core->rawWrite8 = _GBACoreRawWrite8;
1124	core->rawWrite16 = _GBACoreRawWrite16;
1125	core->rawWrite32 = _GBACoreRawWrite32;
1126	core->listMemoryBlocks = _GBAListMemoryBlocks;
1127	core->getMemoryBlock = _GBAGetMemoryBlock;
1128#ifdef USE_DEBUGGERS
1129	core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
1130	core->debuggerPlatform = _GBACoreDebuggerPlatform;
1131	core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
1132	core->attachDebugger = _GBACoreAttachDebugger;
1133	core->detachDebugger = _GBACoreDetachDebugger;
1134	core->loadSymbols = _GBACoreLoadSymbols;
1135	core->lookupIdentifier = _GBACoreLookupIdentifier;
1136#endif
1137	core->cheatDevice = _GBACoreCheatDevice;
1138	core->savedataClone = _GBACoreSavedataClone;
1139	core->savedataRestore = _GBACoreSavedataRestore;
1140	core->listVideoLayers = _GBACoreListVideoLayers;
1141	core->listAudioChannels = _GBACoreListAudioChannels;
1142	core->enableVideoLayer = _GBACoreEnableVideoLayer;
1143	core->enableAudioChannel = _GBACoreEnableAudioChannel;
1144	core->adjustVideoLayer = _GBACoreAdjustVideoLayer;
1145#ifndef MINIMAL_CORE
1146	core->startVideoLog = _GBACoreStartVideoLog;
1147	core->endVideoLog = _GBACoreEndVideoLog;
1148#endif
1149	return core;
1150}
1151
1152#ifndef MINIMAL_CORE
1153static void _GBAVLPStartFrameCallback(void *context) {
1154	struct mCore* core = context;
1155	struct GBACore* gbacore = (struct GBACore*) core;
1156	struct GBA* gba = core->board;
1157
1158	if (!mVideoLoggerRendererRun(gbacore->vlProxy.logger, true)) {
1159		GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1160		mVideoLogContextRewind(gbacore->logContext, core);
1161		GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1162		gba->earlyExit = true;
1163	}
1164}
1165
1166static bool _GBAVLPInit(struct mCore* core) {
1167	struct GBACore* gbacore = (struct GBACore*) core;
1168	if (!_GBACoreInit(core)) {
1169		return false;
1170	}
1171	gbacore->vlProxy.logger = malloc(sizeof(struct mVideoLogger));
1172	mVideoLoggerRendererCreate(gbacore->vlProxy.logger, true);
1173	GBAVideoProxyRendererCreate(&gbacore->vlProxy, NULL);
1174	memset(&gbacore->logCallbacks, 0, sizeof(gbacore->logCallbacks));
1175	gbacore->logCallbacks.videoFrameStarted = _GBAVLPStartFrameCallback;
1176	gbacore->logCallbacks.context = core;
1177	core->addCoreCallbacks(core, &gbacore->logCallbacks);
1178	core->videoLogger = gbacore->vlProxy.logger;
1179	return true;
1180}
1181
1182static void _GBAVLPDeinit(struct mCore* core) {
1183	struct GBACore* gbacore = (struct GBACore*) core;
1184	if (gbacore->logContext) {
1185		mVideoLogContextDestroy(core, gbacore->logContext);
1186	}
1187	_GBACoreDeinit(core);
1188}
1189
1190static void _GBAVLPReset(struct mCore* core) {
1191	struct GBACore* gbacore = (struct GBACore*) core;
1192	struct GBA* gba = (struct GBA*) core->board;
1193	if (gba->video.renderer == &gbacore->vlProxy.d) {
1194		GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1195	} else if (gbacore->renderer.outputBuffer) {
1196		struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
1197		GBAVideoAssociateRenderer(&gba->video, renderer);
1198	}
1199
1200	ARMReset(core->cpu);
1201	mVideoLogContextRewind(gbacore->logContext, core);
1202	GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1203
1204	// Make sure CPU loop never spins
1205	GBAHalt(gba);
1206	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
1207	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
1208}
1209
1210static bool _GBAVLPLoadROM(struct mCore* core, struct VFile* vf) {
1211	struct GBACore* gbacore = (struct GBACore*) core;
1212	gbacore->logContext = mVideoLogContextCreate(NULL);
1213	if (!mVideoLogContextLoad(gbacore->logContext, vf)) {
1214		mVideoLogContextDestroy(core, gbacore->logContext);
1215		gbacore->logContext = NULL;
1216		return false;
1217	}
1218	mVideoLoggerAttachChannel(gbacore->vlProxy.logger, gbacore->logContext, 0);
1219	return true;
1220}
1221
1222static bool _GBAVLPLoadState(struct mCore* core, const void* state) {
1223	struct GBA* gba = (struct GBA*) core->board;
1224
1225	gba->timing.root = NULL;
1226	gba->cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
1227	gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
1228
1229	// Make sure CPU loop never spins
1230	GBAHalt(gba);
1231	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
1232	gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
1233	GBAVideoDeserialize(&gba->video, state);
1234	GBAIODeserialize(gba, state);
1235	GBAAudioReset(&gba->audio);
1236
1237	return true;
1238}
1239
1240static bool _returnTrue(struct VFile* vf) {
1241	UNUSED(vf);
1242	return true;
1243}
1244
1245struct mCore* GBAVideoLogPlayerCreate(void) {
1246	struct mCore* core = GBACoreCreate();
1247	core->init = _GBAVLPInit;
1248	core->deinit = _GBAVLPDeinit;
1249	core->reset = _GBAVLPReset;
1250	core->loadROM = _GBAVLPLoadROM;
1251	core->loadState = _GBAVLPLoadState;
1252	core->isROM = _returnTrue;
1253	return core;
1254}
1255#else
1256struct mCore* GBAVideoLogPlayerCreate(void) {
1257	return false;
1258}
1259#endif