all repos — mgba @ a81259f94b1b6a12b855d13442eb3503ac0920d4

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