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