all repos — mgba @ 03c17cdc5637d3609b08caaa2c2c416fee9f9cbc

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