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