all repos — mgba @ cee1a3e08cef18c1322f8d6b6e416a5d78ac4c0e

mGBA Game Boy Advance Emulator

src/gba/core.c (view raw)

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