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