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