all repos — mgba @ f7f8e38dc17ec7d1daeb1c570cfb124490cf2990

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