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