all repos — mgba @ ac7ae74822d2740f82b62a644e88fbed789ad2be

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