all repos — mgba @ 4dcb28ea703bc111d979ffdb85c7fdb8c5ea3656

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