all repos — mgba @ a760c7bb4a85c8abea17ff9db466e96ebe20a8de

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