all repos — mgba @ e27963bd29653427b99a97742c48a79e7d3a887a

mGBA Game Boy Advance Emulator

src/gba/core.c (view raw)

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