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