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