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