all repos — mgba @ 21e7d763206959ac325f1d5d6e55bb61433c700e

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