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