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