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