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