all repos — mgba @ 64409d9ca7b3af1e83f7fb68737935015739cd40

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