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