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