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