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