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_CLI:
823 return true;
824#ifdef USE_GDB_STUB
825 case DEBUGGER_GDB:
826 return true;
827#endif
828 default:
829 return false;
830 }
831}
832
833static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
834 struct GBACore* gbacore = (struct GBACore*) core;
835 if (!gbacore->debuggerPlatform) {
836 gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
837 }
838 return gbacore->debuggerPlatform;
839}
840
841static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
842 return &GBACLIDebuggerCreate(core)->d;
843}
844
845static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
846 if (core->debugger) {
847 GBADetachDebugger(core->board);
848 }
849 GBAAttachDebugger(core->board, debugger);
850 core->debugger = debugger;
851}
852
853static void _GBACoreDetachDebugger(struct mCore* core) {
854 GBADetachDebugger(core->board);
855 core->debugger = NULL;
856}
857
858static void _GBACoreLoadSymbols(struct mCore* core, struct VFile* vf) {
859 bool closeAfter = false;
860 core->symbolTable = mDebuggerSymbolTableCreate();
861#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
862#ifdef USE_ELF
863 if (!vf) {
864 closeAfter = true;
865 vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".elf", O_RDONLY);
866 }
867#endif
868 if (!vf) {
869 closeAfter = true;
870 vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".sym", O_RDONLY);
871 }
872#endif
873 if (!vf) {
874 return;
875 }
876#ifdef USE_ELF
877 struct ELF* elf = ELFOpen(vf);
878 if (elf) {
879#ifdef USE_DEBUGGERS
880 mCoreLoadELFSymbols(core->symbolTable, elf);
881#endif
882 ELFClose(elf);
883 } else
884#endif
885 {
886 mDebuggerLoadARMIPSSymbols(core->symbolTable, vf);
887 }
888 if (closeAfter) {
889 vf->close(vf);
890 }
891}
892
893static bool _GBACoreLookupIdentifier(struct mCore* core, const char* name, int32_t* value, int* segment) {
894 UNUSED(core);
895 *segment = -1;
896 int i;
897 for (i = 0; i < REG_MAX; i += 2) {
898 const char* reg = GBAIORegisterNames[i >> 1];
899 if (reg && strcasecmp(reg, name) == 0) {
900 *value = BASE_IO | i;
901 return true;
902 }
903 }
904 return false;
905}
906#endif
907
908static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
909 struct GBACore* gbacore = (struct GBACore*) core;
910 if (!gbacore->cheatDevice) {
911 gbacore->cheatDevice = GBACheatDeviceCreate();
912 ((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
913 ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
914 gbacore->cheatDevice->p = core;
915 }
916 return gbacore->cheatDevice;
917}
918
919static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
920 struct GBA* gba = core->board;
921 size_t size = GBASavedataSize(&gba->memory.savedata);
922 if (!size) {
923 *sram = NULL;
924 return 0;
925 }
926 *sram = malloc(size);
927 struct VFile* vf = VFileFromMemory(*sram, size);
928 if (!vf) {
929 free(*sram);
930 *sram = NULL;
931 return 0;
932 }
933 bool success = GBASavedataClone(&gba->memory.savedata, vf);
934 vf->close(vf);
935 if (!success) {
936 free(*sram);
937 *sram = NULL;
938 return 0;
939 }
940 return size;
941}
942
943static bool _GBACoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
944 struct VFile* vf = VFileMemChunk(sram, size);
945 if (!vf) {
946 return false;
947 }
948 struct GBA* gba = core->board;
949 bool success = true;
950 if (writeback) {
951 success = GBASavedataLoad(&gba->memory.savedata, vf);
952 vf->close(vf);
953 } else {
954 GBASavedataMask(&gba->memory.savedata, vf, true);
955 }
956 return success;
957}
958
959static size_t _GBACoreListVideoLayers(const struct mCore* core, const struct mCoreChannelInfo** info) {
960 UNUSED(core);
961 if (info) {
962 *info = _GBAVideoLayers;
963 }
964 return sizeof(_GBAVideoLayers) / sizeof(*_GBAVideoLayers);
965}
966
967static size_t _GBACoreListAudioChannels(const struct mCore* core, const struct mCoreChannelInfo** info) {
968 UNUSED(core);
969 if (info) {
970 *info = _GBAAudioChannels;
971 }
972 return sizeof(_GBAAudioChannels) / sizeof(*_GBAAudioChannels);
973}
974
975static void _GBACoreEnableVideoLayer(struct mCore* core, size_t id, bool enable) {
976 struct GBA* gba = core->board;
977 switch (id) {
978 case 0:
979 case 1:
980 case 2:
981 case 3:
982 gba->video.renderer->disableBG[id] = !enable;
983 break;
984 case 4:
985 gba->video.renderer->disableOBJ = !enable;
986 break;
987 default:
988 break;
989 }
990}
991
992static void _GBACoreEnableAudioChannel(struct mCore* core, size_t id, bool enable) {
993 struct GBA* gba = core->board;
994 switch (id) {
995 case 0:
996 case 1:
997 case 2:
998 case 3:
999 gba->audio.psg.forceDisableCh[id] = !enable;
1000 break;
1001 case 4:
1002 gba->audio.forceDisableChA = !enable;
1003 break;
1004 case 5:
1005 gba->audio.forceDisableChB = !enable;
1006 break;
1007 default:
1008 break;
1009 }
1010}
1011
1012static void _GBACoreAdjustVideoLayer(struct mCore* core, size_t id, int32_t x, int32_t y) {
1013 struct GBACore* gbacore = (struct GBACore*) core;
1014 switch (id) {
1015 case 0:
1016 case 1:
1017 case 2:
1018 case 3:
1019 gbacore->renderer.bg[id].offsetX = x;
1020 gbacore->renderer.bg[id].offsetY = y;
1021 break;
1022 case 4:
1023 gbacore->renderer.objOffsetX = x;
1024 gbacore->renderer.objOffsetY = y;
1025 gbacore->renderer.oamDirty = 1;
1026 break;
1027 default:
1028 return;
1029 }
1030 memset(gbacore->renderer.scanlineDirty, 0xFFFFFFFF, sizeof(gbacore->renderer.scanlineDirty));
1031}
1032
1033#ifndef MINIMAL_CORE
1034static void _GBACoreStartVideoLog(struct mCore* core, struct mVideoLogContext* context) {
1035 struct GBACore* gbacore = (struct GBACore*) core;
1036 struct GBA* gba = core->board;
1037 gbacore->logContext = context;
1038
1039 struct GBASerializedState* state = mVideoLogContextInitialState(context, NULL);
1040 state->id = 0;
1041 state->cpu.gprs[ARM_PC] = BASE_WORKING_RAM;
1042
1043 int channelId = mVideoLoggerAddChannel(context);
1044 gbacore->vlProxy.logger = malloc(sizeof(struct mVideoLogger));
1045 mVideoLoggerRendererCreate(gbacore->vlProxy.logger, false);
1046 mVideoLoggerAttachChannel(gbacore->vlProxy.logger, context, channelId);
1047 gbacore->vlProxy.logger->block = false;
1048
1049 GBAVideoProxyRendererCreate(&gbacore->vlProxy, gba->video.renderer);
1050 GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1051}
1052
1053static void _GBACoreEndVideoLog(struct mCore* core) {
1054 struct GBACore* gbacore = (struct GBACore*) core;
1055 struct GBA* gba = core->board;
1056 if (gbacore->vlProxy.logger) {
1057 GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1058 free(gbacore->vlProxy.logger);
1059 gbacore->vlProxy.logger = NULL;
1060 }
1061}
1062#endif
1063
1064struct mCore* GBACoreCreate(void) {
1065 struct GBACore* gbacore = malloc(sizeof(*gbacore));
1066 struct mCore* core = &gbacore->d;
1067 memset(&core->opts, 0, sizeof(core->opts));
1068 core->cpu = NULL;
1069 core->board = NULL;
1070 core->debugger = NULL;
1071 core->init = _GBACoreInit;
1072 core->deinit = _GBACoreDeinit;
1073 core->platform = _GBACorePlatform;
1074 core->supportsFeature = _GBACoreSupportsFeature;
1075 core->setSync = _GBACoreSetSync;
1076 core->loadConfig = _GBACoreLoadConfig;
1077 core->reloadConfigOption = _GBACoreReloadConfigOption;
1078 core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
1079 core->setVideoBuffer = _GBACoreSetVideoBuffer;
1080 core->setVideoGLTex = _GBACoreSetVideoGLTex;
1081 core->getPixels = _GBACoreGetPixels;
1082 core->putPixels = _GBACorePutPixels;
1083 core->getAudioChannel = _GBACoreGetAudioChannel;
1084 core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
1085 core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
1086 core->addCoreCallbacks = _GBACoreAddCoreCallbacks;
1087 core->clearCoreCallbacks = _GBACoreClearCoreCallbacks;
1088 core->setAVStream = _GBACoreSetAVStream;
1089 core->isROM = GBAIsROM;
1090 core->loadROM = _GBACoreLoadROM;
1091 core->loadBIOS = _GBACoreLoadBIOS;
1092 core->loadSave = _GBACoreLoadSave;
1093 core->loadTemporarySave = _GBACoreLoadTemporarySave;
1094 core->loadPatch = _GBACoreLoadPatch;
1095 core->unloadROM = _GBACoreUnloadROM;
1096 core->checksum = _GBACoreChecksum;
1097 core->reset = _GBACoreReset;
1098 core->runFrame = _GBACoreRunFrame;
1099 core->runLoop = _GBACoreRunLoop;
1100 core->step = _GBACoreStep;
1101 core->stateSize = _GBACoreStateSize;
1102 core->loadState = _GBACoreLoadState;
1103 core->saveState = _GBACoreSaveState;
1104 core->setKeys = _GBACoreSetKeys;
1105 core->addKeys = _GBACoreAddKeys;
1106 core->clearKeys = _GBACoreClearKeys;
1107 core->frameCounter = _GBACoreFrameCounter;
1108 core->frameCycles = _GBACoreFrameCycles;
1109 core->frequency = _GBACoreFrequency;
1110 core->getGameTitle = _GBACoreGetGameTitle;
1111 core->getGameCode = _GBACoreGetGameCode;
1112 core->setPeripheral = _GBACoreSetPeripheral;
1113 core->busRead8 = _GBACoreBusRead8;
1114 core->busRead16 = _GBACoreBusRead16;
1115 core->busRead32 = _GBACoreBusRead32;
1116 core->busWrite8 = _GBACoreBusWrite8;
1117 core->busWrite16 = _GBACoreBusWrite16;
1118 core->busWrite32 = _GBACoreBusWrite32;
1119 core->rawRead8 = _GBACoreRawRead8;
1120 core->rawRead16 = _GBACoreRawRead16;
1121 core->rawRead32 = _GBACoreRawRead32;
1122 core->rawWrite8 = _GBACoreRawWrite8;
1123 core->rawWrite16 = _GBACoreRawWrite16;
1124 core->rawWrite32 = _GBACoreRawWrite32;
1125 core->listMemoryBlocks = _GBAListMemoryBlocks;
1126 core->getMemoryBlock = _GBAGetMemoryBlock;
1127#ifdef USE_DEBUGGERS
1128 core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
1129 core->debuggerPlatform = _GBACoreDebuggerPlatform;
1130 core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
1131 core->attachDebugger = _GBACoreAttachDebugger;
1132 core->detachDebugger = _GBACoreDetachDebugger;
1133 core->loadSymbols = _GBACoreLoadSymbols;
1134 core->lookupIdentifier = _GBACoreLookupIdentifier;
1135#endif
1136 core->cheatDevice = _GBACoreCheatDevice;
1137 core->savedataClone = _GBACoreSavedataClone;
1138 core->savedataRestore = _GBACoreSavedataRestore;
1139 core->listVideoLayers = _GBACoreListVideoLayers;
1140 core->listAudioChannels = _GBACoreListAudioChannels;
1141 core->enableVideoLayer = _GBACoreEnableVideoLayer;
1142 core->enableAudioChannel = _GBACoreEnableAudioChannel;
1143 core->adjustVideoLayer = _GBACoreAdjustVideoLayer;
1144#ifndef MINIMAL_CORE
1145 core->startVideoLog = _GBACoreStartVideoLog;
1146 core->endVideoLog = _GBACoreEndVideoLog;
1147#endif
1148 return core;
1149}
1150
1151#ifndef MINIMAL_CORE
1152static void _GBAVLPStartFrameCallback(void *context) {
1153 struct mCore* core = context;
1154 struct GBACore* gbacore = (struct GBACore*) core;
1155 struct GBA* gba = core->board;
1156
1157 if (!mVideoLoggerRendererRun(gbacore->vlProxy.logger, true)) {
1158 GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1159 mVideoLogContextRewind(gbacore->logContext, core);
1160 GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1161 gba->earlyExit = true;
1162 }
1163}
1164
1165static bool _GBAVLPInit(struct mCore* core) {
1166 struct GBACore* gbacore = (struct GBACore*) core;
1167 if (!_GBACoreInit(core)) {
1168 return false;
1169 }
1170 gbacore->vlProxy.logger = malloc(sizeof(struct mVideoLogger));
1171 mVideoLoggerRendererCreate(gbacore->vlProxy.logger, true);
1172 GBAVideoProxyRendererCreate(&gbacore->vlProxy, NULL);
1173 memset(&gbacore->logCallbacks, 0, sizeof(gbacore->logCallbacks));
1174 gbacore->logCallbacks.videoFrameStarted = _GBAVLPStartFrameCallback;
1175 gbacore->logCallbacks.context = core;
1176 core->addCoreCallbacks(core, &gbacore->logCallbacks);
1177 core->videoLogger = gbacore->vlProxy.logger;
1178 return true;
1179}
1180
1181static void _GBAVLPDeinit(struct mCore* core) {
1182 struct GBACore* gbacore = (struct GBACore*) core;
1183 if (gbacore->logContext) {
1184 mVideoLogContextDestroy(core, gbacore->logContext);
1185 }
1186 _GBACoreDeinit(core);
1187}
1188
1189static void _GBAVLPReset(struct mCore* core) {
1190 struct GBACore* gbacore = (struct GBACore*) core;
1191 struct GBA* gba = (struct GBA*) core->board;
1192 if (gba->video.renderer == &gbacore->vlProxy.d) {
1193 GBAVideoProxyRendererUnshim(&gba->video, &gbacore->vlProxy);
1194 } else if (gbacore->renderer.outputBuffer) {
1195 struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
1196 GBAVideoAssociateRenderer(&gba->video, renderer);
1197 }
1198
1199 ARMReset(core->cpu);
1200 mVideoLogContextRewind(gbacore->logContext, core);
1201 GBAVideoProxyRendererShim(&gba->video, &gbacore->vlProxy);
1202
1203 // Make sure CPU loop never spins
1204 GBAHalt(gba);
1205 gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
1206 gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
1207}
1208
1209static bool _GBAVLPLoadROM(struct mCore* core, struct VFile* vf) {
1210 struct GBACore* gbacore = (struct GBACore*) core;
1211 gbacore->logContext = mVideoLogContextCreate(NULL);
1212 if (!mVideoLogContextLoad(gbacore->logContext, vf)) {
1213 mVideoLogContextDestroy(core, gbacore->logContext);
1214 gbacore->logContext = NULL;
1215 return false;
1216 }
1217 mVideoLoggerAttachChannel(gbacore->vlProxy.logger, gbacore->logContext, 0);
1218 return true;
1219}
1220
1221static bool _GBAVLPLoadState(struct mCore* core, const void* state) {
1222 struct GBA* gba = (struct GBA*) core->board;
1223
1224 gba->timing.root = NULL;
1225 gba->cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
1226 gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
1227
1228 // Make sure CPU loop never spins
1229 GBAHalt(gba);
1230 gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
1231 gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
1232 GBAVideoDeserialize(&gba->video, state);
1233 GBAIODeserialize(gba, state);
1234 GBAAudioReset(&gba->audio);
1235
1236 return true;
1237}
1238
1239static bool _returnTrue(struct VFile* vf) {
1240 UNUSED(vf);
1241 return true;
1242}
1243
1244struct mCore* GBAVideoLogPlayerCreate(void) {
1245 struct mCore* core = GBACoreCreate();
1246 core->init = _GBAVLPInit;
1247 core->deinit = _GBAVLPDeinit;
1248 core->reset = _GBAVLPReset;
1249 core->loadROM = _GBAVLPLoadROM;
1250 core->loadState = _GBAVLPLoadState;
1251 core->isROM = _returnTrue;
1252 return core;
1253}
1254#else
1255struct mCore* GBAVideoLogPlayerCreate(void) {
1256 return false;
1257}
1258#endif