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;
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)) {
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 while (gba->video.frameCounter == frameCounter) {
620 ARMRunLoop(core->cpu);
621 }
622}
623
624static void _GBACoreRunLoop(struct mCore* core) {
625 ARMRunLoop(core->cpu);
626}
627
628static void _GBACoreStep(struct mCore* core) {
629 ARMRun(core->cpu);
630}
631
632static size_t _GBACoreStateSize(struct mCore* core) {
633 UNUSED(core);
634 return sizeof(struct GBASerializedState);
635}
636
637static bool _GBACoreLoadState(struct mCore* core, const void* state) {
638 return GBADeserialize(core->board, state);
639}
640
641static bool _GBACoreSaveState(struct mCore* core, void* state) {
642 GBASerialize(core->board, state);
643 return true;
644}
645
646static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
647 struct GBACore* gbacore = (struct GBACore*) core;
648 gbacore->keys = keys;
649 GBATestKeypadIRQ(core->board);
650}
651
652static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
653 struct GBACore* gbacore = (struct GBACore*) core;
654 gbacore->keys |= keys;
655 GBATestKeypadIRQ(core->board);
656}
657
658static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
659 struct GBACore* gbacore = (struct GBACore*) core;
660 gbacore->keys &= ~keys;
661}
662
663static int32_t _GBACoreFrameCounter(const struct mCore* core) {
664 const struct GBA* gba = core->board;
665 return gba->video.frameCounter;
666}
667
668static int32_t _GBACoreFrameCycles(const struct mCore* core) {
669 UNUSED(core);
670 return VIDEO_TOTAL_LENGTH;
671}
672
673static int32_t _GBACoreFrequency(const struct mCore* core) {
674 UNUSED(core);
675 return GBA_ARM7TDMI_FREQUENCY;
676}
677
678static void _GBACoreGetGameTitle(const struct mCore* core, char* title) {
679 GBAGetGameTitle(core->board, title);
680}
681
682static void _GBACoreGetGameCode(const struct mCore* core, char* title) {
683 GBAGetGameCode(core->board, title);
684}
685
686static void _GBACoreSetPeripheral(struct mCore* core, int type, void* periph) {
687 struct GBA* gba = core->board;
688 switch (type) {
689 case mPERIPH_ROTATION:
690 gba->rotationSource = periph;
691 break;
692 case mPERIPH_RUMBLE:
693 gba->rumble = periph;
694 break;
695 case mPERIPH_GBA_LUMINANCE:
696 gba->luminanceSource = periph;
697 break;
698 case mPERIPH_GBA_BATTLECHIP_GATE:
699 GBASIOSetDriver(&gba->sio, periph, SIO_MULTI);
700 GBASIOSetDriver(&gba->sio, periph, SIO_NORMAL_32);
701 break;
702 default:
703 return;
704 }
705}
706
707static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) {
708 struct ARMCore* cpu = core->cpu;
709 return cpu->memory.load8(cpu, address, 0);
710}
711
712static uint32_t _GBACoreBusRead16(struct mCore* core, uint32_t address) {
713 struct ARMCore* cpu = core->cpu;
714 return cpu->memory.load16(cpu, address, 0);
715
716}
717
718static uint32_t _GBACoreBusRead32(struct mCore* core, uint32_t address) {
719 struct ARMCore* cpu = core->cpu;
720 return cpu->memory.load32(cpu, address, 0);
721}
722
723static void _GBACoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
724 struct ARMCore* cpu = core->cpu;
725 cpu->memory.store8(cpu, address, value, 0);
726}
727
728static void _GBACoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
729 struct ARMCore* cpu = core->cpu;
730 cpu->memory.store16(cpu, address, value, 0);
731}
732
733static void _GBACoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
734 struct ARMCore* cpu = core->cpu;
735 cpu->memory.store32(cpu, address, value, 0);
736}
737
738static uint32_t _GBACoreRawRead8(struct mCore* core, uint32_t address, int segment) {
739 UNUSED(segment);
740 struct ARMCore* cpu = core->cpu;
741 return GBAView8(cpu, address);
742}
743
744static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address, int segment) {
745 UNUSED(segment);
746 struct ARMCore* cpu = core->cpu;
747 return GBAView16(cpu, address);
748}
749
750static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address, int segment) {
751 UNUSED(segment);
752 struct ARMCore* cpu = core->cpu;
753 return GBAView32(cpu, address);
754}
755
756static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
757 UNUSED(segment);
758 struct ARMCore* cpu = core->cpu;
759 GBAPatch8(cpu, address, value, NULL);
760}
761
762static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
763 UNUSED(segment);
764 struct ARMCore* cpu = core->cpu;
765 GBAPatch16(cpu, address, value, NULL);
766}
767
768static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
769 UNUSED(segment);
770 struct ARMCore* cpu = core->cpu;
771 GBAPatch32(cpu, address, value, NULL);
772}
773
774size_t _GBAListMemoryBlocks(const struct mCore* core, const struct mCoreMemoryBlock** blocks) {
775 const struct GBA* gba = core->board;
776 switch (gba->memory.savedata.type) {
777 case SAVEDATA_SRAM:
778 *blocks = _GBAMemoryBlocksSRAM;
779 return sizeof(_GBAMemoryBlocksSRAM) / sizeof(*_GBAMemoryBlocksSRAM);
780 case SAVEDATA_FLASH512:
781 *blocks = _GBAMemoryBlocksFlash512;
782 return sizeof(_GBAMemoryBlocksFlash512) / sizeof(*_GBAMemoryBlocksFlash512);
783 case SAVEDATA_FLASH1M:
784 *blocks = _GBAMemoryBlocksFlash1M;
785 return sizeof(_GBAMemoryBlocksFlash1M) / sizeof(*_GBAMemoryBlocksFlash1M);
786 case SAVEDATA_EEPROM:
787 *blocks = _GBAMemoryBlocksEEPROM;
788 return sizeof(_GBAMemoryBlocksEEPROM) / sizeof(*_GBAMemoryBlocksEEPROM);
789 default:
790 *blocks = _GBAMemoryBlocks;
791 return sizeof(_GBAMemoryBlocks) / sizeof(*_GBAMemoryBlocks);
792 }
793}
794
795void* _GBAGetMemoryBlock(struct mCore* core, size_t id, size_t* sizeOut) {
796 struct GBA* gba = core->board;
797 switch (id) {
798 default:
799 return NULL;
800 case REGION_BIOS:
801 *sizeOut = SIZE_BIOS;
802 return gba->memory.bios;
803 case REGION_WORKING_RAM:
804 *sizeOut = SIZE_WORKING_RAM;
805 return gba->memory.wram;
806 case REGION_WORKING_IRAM:
807 *sizeOut = SIZE_WORKING_IRAM;
808 return gba->memory.iwram;
809 case REGION_PALETTE_RAM:
810 *sizeOut = SIZE_PALETTE_RAM;
811 return gba->video.palette;
812 case REGION_VRAM:
813 *sizeOut = SIZE_VRAM;
814 return gba->video.vram;
815 case REGION_OAM:
816 *sizeOut = SIZE_OAM;
817 return gba->video.oam.raw;
818 case REGION_CART0:
819 case REGION_CART1:
820 case REGION_CART2:
821 *sizeOut = gba->memory.romSize;
822 return gba->memory.rom;
823 case REGION_CART_SRAM:
824 if (gba->memory.savedata.type == SAVEDATA_FLASH1M) {
825 *sizeOut = SIZE_CART_FLASH1M;
826 return gba->memory.savedata.currentBank;
827 }
828 // Fall through
829 case REGION_CART_SRAM_MIRROR:
830 *sizeOut = GBASavedataSize(&gba->memory.savedata);
831 return gba->memory.savedata.data;
832 }
833}
834
835#ifdef USE_DEBUGGERS
836static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
837 UNUSED(core);
838 switch (type) {
839 case DEBUGGER_CUSTOM:
840 case DEBUGGER_CLI:
841 case DEBUGGER_GDB:
842 return true;
843 default:
844 return false;
845 }
846}
847
848static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
849 struct GBACore* gbacore = (struct GBACore*) core;
850 if (!gbacore->debuggerPlatform) {
851 gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
852 }
853 return gbacore->debuggerPlatform;
854}
855
856static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
857 return &GBACLIDebuggerCreate(core)->d;
858}
859
860static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
861 if (core->debugger) {
862 GBADetachDebugger(core->board);
863 }
864 GBAAttachDebugger(core->board, debugger);
865 core->debugger = debugger;
866}
867
868static void _GBACoreDetachDebugger(struct mCore* core) {
869 GBADetachDebugger(core->board);
870 core->debugger = NULL;
871}
872
873static void _GBACoreLoadSymbols(struct mCore* core, struct VFile* vf) {
874 bool closeAfter = false;
875 core->symbolTable = mDebuggerSymbolTableCreate();
876#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
877#ifdef USE_ELF
878 if (!vf) {
879 closeAfter = true;
880 vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".elf", O_RDONLY);
881 }
882#endif
883 if (!vf) {
884 closeAfter = true;
885 vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".sym", O_RDONLY);
886 }
887#endif
888 if (!vf) {
889 return;
890 }
891#ifdef USE_ELF
892 struct ELF* elf = ELFOpen(vf);
893 if (elf) {
894#ifdef USE_DEBUGGERS
895 mCoreLoadELFSymbols(core->symbolTable, elf);
896#endif
897 ELFClose(elf);
898 } else
899#endif
900 {
901 mDebuggerLoadARMIPSSymbols(core->symbolTable, vf);
902 }
903 if (closeAfter) {
904 vf->close(vf);
905 }
906}
907
908static bool _GBACoreLookupIdentifier(struct mCore* core, const char* name, int32_t* value, int* segment) {
909 UNUSED(core);
910 *segment = -1;
911 int i;
912 for (i = 0; i < REG_MAX; i += 2) {
913 const char* reg = GBAIORegisterNames[i >> 1];
914 if (reg && strcasecmp(reg, name) == 0) {
915 *value = BASE_IO | i;
916 return true;
917 }
918 }
919 return false;
920}
921#endif
922
923static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
924 struct GBACore* gbacore = (struct GBACore*) core;
925 if (!gbacore->cheatDevice) {
926 gbacore->cheatDevice = GBACheatDeviceCreate();
927 ((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
928 ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
929 gbacore->cheatDevice->p = core;
930 }
931 return gbacore->cheatDevice;
932}
933
934static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
935 struct GBA* gba = core->board;
936 size_t size = GBASavedataSize(&gba->memory.savedata);
937 if (!size) {
938 *sram = NULL;
939 return 0;
940 }
941 *sram = malloc(size);
942 struct VFile* vf = VFileFromMemory(*sram, size);
943 if (!vf) {
944 free(*sram);
945 *sram = NULL;
946 return 0;
947 }
948 bool success = GBASavedataClone(&gba->memory.savedata, vf);
949 vf->close(vf);
950 if (!success) {
951 free(*sram);
952 *sram = NULL;
953 return 0;
954 }
955 return size;
956}
957
958static bool _GBACoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
959 struct VFile* vf = VFileMemChunk(sram, size);
960 if (!vf) {
961 return false;
962 }
963 struct GBA* gba = core->board;
964 bool success = true;
965 if (writeback) {
966 success = GBASavedataLoad(&gba->memory.savedata, vf);
967 vf->close(vf);
968 } else {
969 GBASavedataMask(&gba->memory.savedata, vf, true);
970 }
971 return success;
972}
973
974static size_t _GBACoreListVideoLayers(const struct mCore* core, const struct mCoreChannelInfo** info) {
975 UNUSED(core);
976 if (info) {
977 *info = _GBAVideoLayers;
978 }
979 return sizeof(_GBAVideoLayers) / sizeof(*_GBAVideoLayers);
980}
981
982static size_t _GBACoreListAudioChannels(const struct mCore* core, const struct mCoreChannelInfo** info) {
983 UNUSED(core);
984 if (info) {
985 *info = _GBAAudioChannels;
986 }
987 return sizeof(_GBAAudioChannels) / sizeof(*_GBAAudioChannels);
988}
989
990static void _GBACoreEnableVideoLayer(struct mCore* core, size_t id, bool enable) {
991 struct GBA* gba = core->board;
992 switch (id) {
993 case 0:
994 case 1:
995 case 2:
996 case 3:
997 gba->video.renderer->disableBG[id] = !enable;
998 break;
999 case 4:
1000 gba->video.renderer->disableOBJ = !enable;
1001 break;
1002 default:
1003 break;
1004 }
1005}
1006
1007static void _GBACoreEnableAudioChannel(struct mCore* core, size_t id, bool enable) {
1008 struct GBA* gba = core->board;
1009 switch (id) {
1010 case 0:
1011 case 1:
1012 case 2:
1013 case 3:
1014 gba->audio.psg.forceDisableCh[id] = !enable;
1015 break;
1016 case 4:
1017 gba->audio.forceDisableChA = !enable;
1018 break;
1019 case 5:
1020 gba->audio.forceDisableChB = !enable;
1021 break;
1022 default:
1023 break;
1024 }
1025}
1026
1027static void _GBACoreAdjustVideoLayer(struct mCore* core, size_t id, int32_t x, int32_t y) {
1028 struct GBACore* gbacore = (struct GBACore*) core;
1029 switch (id) {
1030 case 0:
1031 case 1:
1032 case 2:
1033 case 3:
1034 gbacore->renderer.bg[id].offsetX = x;
1035 gbacore->renderer.bg[id].offsetY = y;
1036 break;
1037 case 4:
1038 gbacore->renderer.objOffsetX = x;
1039 gbacore->renderer.objOffsetY = y;
1040 gbacore->renderer.oamDirty = 1;
1041 break;
1042 default:
1043 return;
1044 }
1045 memset(gbacore->renderer.scanlineDirty, 0xFFFFFFFF, sizeof(gbacore->renderer.scanlineDirty));
1046}
1047
1048#ifndef MINIMAL_CORE
1049static void _GBACoreStartVideoLog(struct mCore* core, struct mVideoLogContext* context) {
1050 struct GBACore* gbacore = (struct GBACore*) core;
1051 struct GBA* gba = core->board;
1052 gbacore->logContext = context;
1053
1054 struct GBASerializedState* state = mVideoLogContextInitialState(context, NULL);
1055 state->id = 0;
1056 state->cpu.gprs[ARM_PC] = BASE_WORKING_RAM;
1057
1058 int channelId = mVideoLoggerAddChannel(context);
1059 gbacore->vlProxy.logger = malloc(sizeof(struct mVideoLogger));
1060 mVideoLoggerRendererCreate(gbacore->vlProxy.logger, false);
1061 mVideoLoggerAttachChannel(gbacore->vlProxy.logger, context, channelId);
1062 gbacore->vlProxy.logger->block = false;
1063
1064 GBAVideoProxyRendererCreate(&gbacore->vlProxy, gba->video.renderer);
1065 GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1066}
1067
1068static void _GBACoreEndVideoLog(struct mCore* core) {
1069 struct GBACore* gbacore = (struct GBACore*) core;
1070 struct GBA* gba = core->board;
1071 if (gbacore->vlProxy.logger) {
1072 GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1073 free(gbacore->vlProxy.logger);
1074 gbacore->vlProxy.logger = NULL;
1075 }
1076}
1077#endif
1078
1079struct mCore* GBACoreCreate(void) {
1080 struct GBACore* gbacore = malloc(sizeof(*gbacore));
1081 struct mCore* core = &gbacore->d;
1082 memset(&core->opts, 0, sizeof(core->opts));
1083 core->cpu = NULL;
1084 core->board = NULL;
1085 core->debugger = NULL;
1086 core->init = _GBACoreInit;
1087 core->deinit = _GBACoreDeinit;
1088 core->platform = _GBACorePlatform;
1089 core->supportsFeature = _GBACoreSupportsFeature;
1090 core->setSync = _GBACoreSetSync;
1091 core->loadConfig = _GBACoreLoadConfig;
1092 core->reloadConfigOption = _GBACoreReloadConfigOption;
1093 core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
1094 core->setVideoBuffer = _GBACoreSetVideoBuffer;
1095 core->setVideoGLTex = _GBACoreSetVideoGLTex;
1096 core->getPixels = _GBACoreGetPixels;
1097 core->putPixels = _GBACorePutPixels;
1098 core->getAudioChannel = _GBACoreGetAudioChannel;
1099 core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
1100 core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
1101 core->addCoreCallbacks = _GBACoreAddCoreCallbacks;
1102 core->clearCoreCallbacks = _GBACoreClearCoreCallbacks;
1103 core->setAVStream = _GBACoreSetAVStream;
1104 core->isROM = GBAIsROM;
1105 core->loadROM = _GBACoreLoadROM;
1106 core->loadBIOS = _GBACoreLoadBIOS;
1107 core->loadSave = _GBACoreLoadSave;
1108 core->loadTemporarySave = _GBACoreLoadTemporarySave;
1109 core->loadPatch = _GBACoreLoadPatch;
1110 core->unloadROM = _GBACoreUnloadROM;
1111 core->checksum = _GBACoreChecksum;
1112 core->reset = _GBACoreReset;
1113 core->runFrame = _GBACoreRunFrame;
1114 core->runLoop = _GBACoreRunLoop;
1115 core->step = _GBACoreStep;
1116 core->stateSize = _GBACoreStateSize;
1117 core->loadState = _GBACoreLoadState;
1118 core->saveState = _GBACoreSaveState;
1119 core->setKeys = _GBACoreSetKeys;
1120 core->addKeys = _GBACoreAddKeys;
1121 core->clearKeys = _GBACoreClearKeys;
1122 core->frameCounter = _GBACoreFrameCounter;
1123 core->frameCycles = _GBACoreFrameCycles;
1124 core->frequency = _GBACoreFrequency;
1125 core->getGameTitle = _GBACoreGetGameTitle;
1126 core->getGameCode = _GBACoreGetGameCode;
1127 core->setPeripheral = _GBACoreSetPeripheral;
1128 core->busRead8 = _GBACoreBusRead8;
1129 core->busRead16 = _GBACoreBusRead16;
1130 core->busRead32 = _GBACoreBusRead32;
1131 core->busWrite8 = _GBACoreBusWrite8;
1132 core->busWrite16 = _GBACoreBusWrite16;
1133 core->busWrite32 = _GBACoreBusWrite32;
1134 core->rawRead8 = _GBACoreRawRead8;
1135 core->rawRead16 = _GBACoreRawRead16;
1136 core->rawRead32 = _GBACoreRawRead32;
1137 core->rawWrite8 = _GBACoreRawWrite8;
1138 core->rawWrite16 = _GBACoreRawWrite16;
1139 core->rawWrite32 = _GBACoreRawWrite32;
1140 core->listMemoryBlocks = _GBAListMemoryBlocks;
1141 core->getMemoryBlock = _GBAGetMemoryBlock;
1142#ifdef USE_DEBUGGERS
1143 core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
1144 core->debuggerPlatform = _GBACoreDebuggerPlatform;
1145 core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
1146 core->attachDebugger = _GBACoreAttachDebugger;
1147 core->detachDebugger = _GBACoreDetachDebugger;
1148 core->loadSymbols = _GBACoreLoadSymbols;
1149 core->lookupIdentifier = _GBACoreLookupIdentifier;
1150#endif
1151 core->cheatDevice = _GBACoreCheatDevice;
1152 core->savedataClone = _GBACoreSavedataClone;
1153 core->savedataRestore = _GBACoreSavedataRestore;
1154 core->listVideoLayers = _GBACoreListVideoLayers;
1155 core->listAudioChannels = _GBACoreListAudioChannels;
1156 core->enableVideoLayer = _GBACoreEnableVideoLayer;
1157 core->enableAudioChannel = _GBACoreEnableAudioChannel;
1158 core->adjustVideoLayer = _GBACoreAdjustVideoLayer;
1159#ifndef MINIMAL_CORE
1160 core->startVideoLog = _GBACoreStartVideoLog;
1161 core->endVideoLog = _GBACoreEndVideoLog;
1162#endif
1163 return core;
1164}
1165
1166#ifndef MINIMAL_CORE
1167static void _GBAVLPStartFrameCallback(void *context) {
1168 struct mCore* core = context;
1169 struct GBACore* gbacore = (struct GBACore*) core;
1170 struct GBA* gba = core->board;
1171
1172 if (!mVideoLoggerRendererRun(gbacore->vlProxy.logger, true)) {
1173 GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1174 mVideoLogContextRewind(gbacore->logContext, core);
1175 GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1176 gba->earlyExit = true;
1177 }
1178}
1179
1180static bool _GBAVLPInit(struct mCore* core) {
1181 struct GBACore* gbacore = (struct GBACore*) core;
1182 if (!_GBACoreInit(core)) {
1183 return false;
1184 }
1185 gbacore->vlProxy.logger = malloc(sizeof(struct mVideoLogger));
1186 mVideoLoggerRendererCreate(gbacore->vlProxy.logger, true);
1187 GBAVideoProxyRendererCreate(&gbacore->vlProxy, NULL);
1188 memset(&gbacore->logCallbacks, 0, sizeof(gbacore->logCallbacks));
1189 gbacore->logCallbacks.videoFrameStarted = _GBAVLPStartFrameCallback;
1190 gbacore->logCallbacks.context = core;
1191 core->addCoreCallbacks(core, &gbacore->logCallbacks);
1192 core->videoLogger = gbacore->vlProxy.logger;
1193 return true;
1194}
1195
1196static void _GBAVLPDeinit(struct mCore* core) {
1197 struct GBACore* gbacore = (struct GBACore*) core;
1198 if (gbacore->logContext) {
1199 mVideoLogContextDestroy(core, gbacore->logContext);
1200 }
1201 _GBACoreDeinit(core);
1202}
1203
1204static void _GBAVLPReset(struct mCore* core) {
1205 struct GBACore* gbacore = (struct GBACore*) core;
1206 struct GBA* gba = (struct GBA*) core->board;
1207 if (gba->video.renderer == &gbacore->vlProxy.d) {
1208 GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1209 } else if (gbacore->renderer.outputBuffer) {
1210 struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
1211 GBAVideoAssociateRenderer(&gba->video, renderer);
1212 }
1213
1214 ARMReset(core->cpu);
1215 mVideoLogContextRewind(gbacore->logContext, core);
1216 GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1217
1218 // Make sure CPU loop never spins
1219 GBAHalt(gba);
1220 gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
1221 gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
1222}
1223
1224static bool _GBAVLPLoadROM(struct mCore* core, struct VFile* vf) {
1225 struct GBACore* gbacore = (struct GBACore*) core;
1226 gbacore->logContext = mVideoLogContextCreate(NULL);
1227 if (!mVideoLogContextLoad(gbacore->logContext, vf)) {
1228 mVideoLogContextDestroy(core, gbacore->logContext);
1229 gbacore->logContext = NULL;
1230 return false;
1231 }
1232 mVideoLoggerAttachChannel(gbacore->vlProxy.logger, gbacore->logContext, 0);
1233 return true;
1234}
1235
1236static bool _GBAVLPLoadState(struct mCore* core, const void* state) {
1237 struct GBA* gba = (struct GBA*) core->board;
1238
1239 gba->timing.root = NULL;
1240 gba->cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
1241 gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
1242
1243 // Make sure CPU loop never spins
1244 GBAHalt(gba);
1245 gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
1246 gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
1247 GBAVideoDeserialize(&gba->video, state);
1248 GBAIODeserialize(gba, state);
1249 GBAAudioReset(&gba->audio);
1250
1251 return true;
1252}
1253
1254static bool _returnTrue(struct VFile* vf) {
1255 UNUSED(vf);
1256 return true;
1257}
1258
1259struct mCore* GBAVideoLogPlayerCreate(void) {
1260 struct mCore* core = GBACoreCreate();
1261 core->init = _GBAVLPInit;
1262 core->deinit = _GBAVLPDeinit;
1263 core->reset = _GBAVLPReset;
1264 core->loadROM = _GBAVLPLoadROM;
1265 core->loadState = _GBAVLPLoadState;
1266 core->isROM = _returnTrue;
1267 return core;
1268}
1269#else
1270struct mCore* GBAVideoLogPlayerCreate(void) {
1271 return false;
1272}
1273#endif