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
27static const 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
35static const struct mCoreChannelInfo _GBAAudioChannels[] = {
36 { 0, "ch1", "PSG Channel 1", "Square/Sweep" },
37 { 1, "ch2", "PSG Channel 2", "Square" },
38 { 2, "ch3", "PSG Channel 3", "PCM" },
39 { 3, "ch4", "PSG Channel 4", "Noise" },
40 { 4, "chA", "FIFO Channel A", NULL },
41 { 5, "chB", "FIFO Channel B", NULL },
42};
43
44static const 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
58static const 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
73static const 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
88static const 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
103static const 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
389 if (!strncmp("pokemon red version", &((const char*) gba->memory.rom)[0x108], 20) && gba->romCrc32 != 0xDD88761C) {
390 // Enable FLASH1M and RTC on Pokémon FireRed ROM hacks
391 override.savetype = SAVEDATA_FLASH1M;
392 override.hardware = HW_RTC;
393 GBAOverrideApply(gba, &override);
394 } else if (GBAOverrideFind(gbacore->overrides, &override)) {
395 GBAOverrideApply(gba, &override);
396 }
397 }
398#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
399 if (!gba->biosVf && core->opts.useBios) {
400 struct VFile* bios = NULL;
401 bool found = false;
402 if (core->opts.bios) {
403 bios = VFileOpen(core->opts.bios, O_RDONLY);
404 if (bios && GBAIsBIOS(bios)) {
405 found = true;
406 } else if (bios) {
407 bios->close(bios);
408 bios = NULL;
409 }
410 }
411 if (!found) {
412 const char* configPath = mCoreConfigGetValue(&core->config, "gba.bios");
413 if (configPath) {
414 bios = VFileOpen(configPath, O_RDONLY);
415 }
416 if (bios && GBAIsBIOS(bios)) {
417 found = true;
418 } else if (bios) {
419 bios->close(bios);
420 bios = NULL;
421 }
422 }
423 if (!found) {
424 char path[PATH_MAX];
425 mCoreConfigDirectory(path, PATH_MAX);
426 strncat(path, PATH_SEP "gba_bios.bin", PATH_MAX - strlen(path));
427 bios = VFileOpen(path, O_RDONLY);
428 if (bios && GBAIsBIOS(bios)) {
429 found = true;
430 } else if (bios) {
431 bios->close(bios);
432 bios = NULL;
433 }
434 }
435 if (bios) {
436 GBALoadBIOS(gba, bios);
437 }
438 }
439#endif
440
441 ARMReset(core->cpu);
442 if (core->opts.skipBios && gba->isPristine) {
443 GBASkipBIOS(core->board);
444 }
445}
446
447static void _GBACoreRunFrame(struct mCore* core) {
448 struct GBA* gba = core->board;
449 int32_t frameCounter = gba->video.frameCounter;
450 while (gba->video.frameCounter == frameCounter) {
451 ARMRunLoop(core->cpu);
452 }
453}
454
455static void _GBACoreRunLoop(struct mCore* core) {
456 ARMRunLoop(core->cpu);
457}
458
459static void _GBACoreStep(struct mCore* core) {
460 ARMRun(core->cpu);
461}
462
463static size_t _GBACoreStateSize(struct mCore* core) {
464 UNUSED(core);
465 return sizeof(struct GBASerializedState);
466}
467
468static bool _GBACoreLoadState(struct mCore* core, const void* state) {
469 return GBADeserialize(core->board, state);
470}
471
472static bool _GBACoreSaveState(struct mCore* core, void* state) {
473 GBASerialize(core->board, state);
474 return true;
475}
476
477static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
478 struct GBACore* gbacore = (struct GBACore*) core;
479 gbacore->keys = keys;
480 GBATestKeypadIRQ(core->board);
481}
482
483static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
484 struct GBACore* gbacore = (struct GBACore*) core;
485 gbacore->keys |= keys;
486 GBATestKeypadIRQ(core->board);
487}
488
489static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
490 struct GBACore* gbacore = (struct GBACore*) core;
491 gbacore->keys &= ~keys;
492 GBATestKeypadIRQ(core->board);
493}
494
495static int32_t _GBACoreFrameCounter(const struct mCore* core) {
496 const struct GBA* gba = core->board;
497 return gba->video.frameCounter;
498}
499
500static int32_t _GBACoreFrameCycles(const struct mCore* core) {
501 UNUSED(core);
502 return VIDEO_TOTAL_LENGTH;
503}
504
505static int32_t _GBACoreFrequency(const struct mCore* core) {
506 UNUSED(core);
507 return GBA_ARM7TDMI_FREQUENCY;
508}
509
510static void _GBACoreGetGameTitle(const struct mCore* core, char* title) {
511 GBAGetGameTitle(core->board, title);
512}
513
514static void _GBACoreGetGameCode(const struct mCore* core, char* title) {
515 GBAGetGameCode(core->board, title);
516}
517
518static void _GBACoreSetPeripheral(struct mCore* core, int type, void* periph) {
519 struct GBA* gba = core->board;
520 switch (type) {
521 case mPERIPH_ROTATION:
522 gba->rotationSource = periph;
523 break;
524 case mPERIPH_RUMBLE:
525 gba->rumble = periph;
526 break;
527 case mPERIPH_GBA_LUMINANCE:
528 gba->luminanceSource = periph;
529 break;
530 default:
531 return;
532 }
533}
534
535static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) {
536 struct ARMCore* cpu = core->cpu;
537 return cpu->memory.load8(cpu, address, 0);
538}
539
540static uint32_t _GBACoreBusRead16(struct mCore* core, uint32_t address) {
541 struct ARMCore* cpu = core->cpu;
542 return cpu->memory.load16(cpu, address, 0);
543
544}
545
546static uint32_t _GBACoreBusRead32(struct mCore* core, uint32_t address) {
547 struct ARMCore* cpu = core->cpu;
548 return cpu->memory.load32(cpu, address, 0);
549}
550
551static void _GBACoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
552 struct ARMCore* cpu = core->cpu;
553 cpu->memory.store8(cpu, address, value, 0);
554}
555
556static void _GBACoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
557 struct ARMCore* cpu = core->cpu;
558 cpu->memory.store16(cpu, address, value, 0);
559}
560
561static void _GBACoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
562 struct ARMCore* cpu = core->cpu;
563 cpu->memory.store32(cpu, address, value, 0);
564}
565
566static uint32_t _GBACoreRawRead8(struct mCore* core, uint32_t address, int segment) {
567 UNUSED(segment);
568 struct ARMCore* cpu = core->cpu;
569 return GBAView8(cpu, address);
570}
571
572static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address, int segment) {
573 UNUSED(segment);
574 struct ARMCore* cpu = core->cpu;
575 return GBAView16(cpu, address);
576}
577
578static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address, int segment) {
579 UNUSED(segment);
580 struct ARMCore* cpu = core->cpu;
581 return GBAView32(cpu, address);
582}
583
584static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
585 UNUSED(segment);
586 struct ARMCore* cpu = core->cpu;
587 GBAPatch8(cpu, address, value, NULL);
588}
589
590static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
591 UNUSED(segment);
592 struct ARMCore* cpu = core->cpu;
593 GBAPatch16(cpu, address, value, NULL);
594}
595
596static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
597 UNUSED(segment);
598 struct ARMCore* cpu = core->cpu;
599 GBAPatch32(cpu, address, value, NULL);
600}
601
602size_t _GBAListMemoryBlocks(const struct mCore* core, const struct mCoreMemoryBlock** blocks) {
603 const struct GBA* gba = core->board;
604 switch (gba->memory.savedata.type) {
605 case SAVEDATA_SRAM:
606 *blocks = _GBAMemoryBlocksSRAM;
607 return sizeof(_GBAMemoryBlocksSRAM) / sizeof(*_GBAMemoryBlocksSRAM);
608 case SAVEDATA_FLASH512:
609 *blocks = _GBAMemoryBlocksFlash512;
610 return sizeof(_GBAMemoryBlocksFlash512) / sizeof(*_GBAMemoryBlocksFlash512);
611 case SAVEDATA_FLASH1M:
612 *blocks = _GBAMemoryBlocksFlash1M;
613 return sizeof(_GBAMemoryBlocksFlash1M) / sizeof(*_GBAMemoryBlocksFlash1M);
614 case SAVEDATA_EEPROM:
615 *blocks = _GBAMemoryBlocksEEPROM;
616 return sizeof(_GBAMemoryBlocksEEPROM) / sizeof(*_GBAMemoryBlocksEEPROM);
617 default:
618 *blocks = _GBAMemoryBlocks;
619 return sizeof(_GBAMemoryBlocks) / sizeof(*_GBAMemoryBlocks);
620 }
621}
622
623void* _GBAGetMemoryBlock(struct mCore* core, size_t id, size_t* sizeOut) {
624 struct GBA* gba = core->board;
625 switch (id) {
626 default:
627 return NULL;
628 case REGION_BIOS:
629 *sizeOut = SIZE_BIOS;
630 return gba->memory.bios;
631 case REGION_WORKING_RAM:
632 *sizeOut = SIZE_WORKING_RAM;
633 return gba->memory.wram;
634 case REGION_WORKING_IRAM:
635 *sizeOut = SIZE_WORKING_IRAM;
636 return gba->memory.iwram;
637 case REGION_PALETTE_RAM:
638 *sizeOut = SIZE_PALETTE_RAM;
639 return gba->video.palette;
640 case REGION_VRAM:
641 *sizeOut = SIZE_VRAM;
642 return gba->video.vram;
643 case REGION_OAM:
644 *sizeOut = SIZE_OAM;
645 return gba->video.oam.raw;
646 case REGION_CART0:
647 case REGION_CART1:
648 case REGION_CART2:
649 *sizeOut = gba->memory.romSize;
650 return gba->memory.rom;
651 case REGION_CART_SRAM:
652 if (gba->memory.savedata.type == SAVEDATA_FLASH1M) {
653 *sizeOut = SIZE_CART_FLASH1M;
654 return gba->memory.savedata.currentBank;
655 }
656 // Fall through
657 case REGION_CART_SRAM_MIRROR:
658 *sizeOut = GBASavedataSize(&gba->memory.savedata);
659 return gba->memory.savedata.data;
660 }
661}
662
663#ifdef USE_DEBUGGERS
664static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
665 UNUSED(core);
666 switch (type) {
667 case DEBUGGER_CLI:
668 return true;
669#ifdef USE_GDB_STUB
670 case DEBUGGER_GDB:
671 return true;
672#endif
673 default:
674 return false;
675 }
676}
677
678static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
679 struct GBACore* gbacore = (struct GBACore*) core;
680 if (!gbacore->debuggerPlatform) {
681 gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
682 }
683 return gbacore->debuggerPlatform;
684}
685
686static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
687 return &GBACLIDebuggerCreate(core)->d;
688}
689
690static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
691 if (core->debugger) {
692 GBADetachDebugger(core->board);
693 }
694 GBAAttachDebugger(core->board, debugger);
695 core->debugger = debugger;
696}
697
698static void _GBACoreDetachDebugger(struct mCore* core) {
699 GBADetachDebugger(core->board);
700 core->debugger = NULL;
701}
702
703static void _GBACoreLoadSymbols(struct mCore* core, struct VFile* vf) {
704 // TODO
705}
706#endif
707
708static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
709 struct GBACore* gbacore = (struct GBACore*) core;
710 if (!gbacore->cheatDevice) {
711 gbacore->cheatDevice = GBACheatDeviceCreate();
712 ((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
713 ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
714 gbacore->cheatDevice->p = core;
715 }
716 return gbacore->cheatDevice;
717}
718
719static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
720 struct GBA* gba = core->board;
721 size_t size = GBASavedataSize(&gba->memory.savedata);
722 if (!size) {
723 *sram = NULL;
724 return 0;
725 }
726 *sram = malloc(size);
727 struct VFile* vf = VFileFromMemory(*sram, size);
728 if (!vf) {
729 free(*sram);
730 *sram = NULL;
731 return 0;
732 }
733 bool success = GBASavedataClone(&gba->memory.savedata, vf);
734 vf->close(vf);
735 if (!success) {
736 free(*sram);
737 *sram = NULL;
738 return 0;
739 }
740 return size;
741}
742
743static bool _GBACoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
744 struct VFile* vf = VFileMemChunk(sram, size);
745 if (!vf) {
746 return false;
747 }
748 struct GBA* gba = core->board;
749 bool success = true;
750 if (writeback) {
751 success = GBASavedataLoad(&gba->memory.savedata, vf);
752 vf->close(vf);
753 } else {
754 GBASavedataMask(&gba->memory.savedata, vf, true);
755 }
756 return success;
757}
758
759static size_t _GBACoreListVideoLayers(const struct mCore* core, const struct mCoreChannelInfo** info) {
760 UNUSED(core);
761 *info = _GBAVideoLayers;
762 return sizeof(_GBAVideoLayers) / sizeof(*_GBAVideoLayers);
763}
764
765static size_t _GBACoreListAudioChannels(const struct mCore* core, const struct mCoreChannelInfo** info) {
766 UNUSED(core);
767 *info = _GBAAudioChannels;
768 return sizeof(_GBAAudioChannels) / sizeof(*_GBAAudioChannels);
769}
770
771static void _GBACoreEnableVideoLayer(struct mCore* core, size_t id, bool enable) {
772 struct GBA* gba = core->board;
773 switch (id) {
774 case 0:
775 case 1:
776 case 2:
777 case 3:
778 gba->video.renderer->disableBG[id] = !enable;
779 break;
780 case 4:
781 gba->video.renderer->disableOBJ = !enable;
782 break;
783 default:
784 break;
785 }
786}
787
788static void _GBACoreEnableAudioChannel(struct mCore* core, size_t id, bool enable) {
789 struct GBA* gba = core->board;
790 switch (id) {
791 case 0:
792 case 1:
793 case 2:
794 case 3:
795 gba->audio.psg.forceDisableCh[id] = !enable;
796 break;
797 case 4:
798 gba->audio.forceDisableChA = !enable;
799 case 5:
800 gba->audio.forceDisableChB = !enable;
801 break;
802 default:
803 break;
804 }
805}
806
807static void _GBACoreStartVideoLog(struct mCore* core, struct mVideoLogContext* context) {
808 struct GBACore* gbacore = (struct GBACore*) core;
809 struct GBA* gba = core->board;
810 gbacore->logContext = context;
811
812 struct GBASerializedState* state = mVideoLogContextInitialState(context, NULL);
813 state->id = 0;
814 state->cpu.gprs[ARM_PC] = BASE_WORKING_RAM;
815
816 int channelId = mVideoLoggerAddChannel(context);
817 gbacore->proxyRenderer.logger = malloc(sizeof(struct mVideoLogger));
818 mVideoLoggerRendererCreate(gbacore->proxyRenderer.logger, false);
819 mVideoLoggerAttachChannel(gbacore->proxyRenderer.logger, context, channelId);
820 gbacore->proxyRenderer.logger->block = false;
821
822 GBAVideoProxyRendererCreate(&gbacore->proxyRenderer, &gbacore->renderer.d);
823 GBAVideoProxyRendererShim(&gba->video, &gbacore->proxyRenderer);
824}
825
826static void _GBACoreEndVideoLog(struct mCore* core) {
827 struct GBACore* gbacore = (struct GBACore*) core;
828 struct GBA* gba = core->board;
829 GBAVideoProxyRendererUnshim(&gba->video, &gbacore->proxyRenderer);
830 free(gbacore->proxyRenderer.logger);
831 gbacore->proxyRenderer.logger = NULL;
832}
833
834struct mCore* GBACoreCreate(void) {
835 struct GBACore* gbacore = malloc(sizeof(*gbacore));
836 struct mCore* core = &gbacore->d;
837 memset(&core->opts, 0, sizeof(core->opts));
838 core->cpu = NULL;
839 core->board = NULL;
840 core->debugger = NULL;
841 core->init = _GBACoreInit;
842 core->deinit = _GBACoreDeinit;
843 core->platform = _GBACorePlatform;
844 core->setSync = _GBACoreSetSync;
845 core->loadConfig = _GBACoreLoadConfig;
846 core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
847 core->setVideoBuffer = _GBACoreSetVideoBuffer;
848 core->getPixels = _GBACoreGetPixels;
849 core->putPixels = _GBACorePutPixels;
850 core->getAudioChannel = _GBACoreGetAudioChannel;
851 core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
852 core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
853 core->addCoreCallbacks = _GBACoreAddCoreCallbacks;
854 core->clearCoreCallbacks = _GBACoreClearCoreCallbacks;
855 core->setAVStream = _GBACoreSetAVStream;
856 core->isROM = GBAIsROM;
857 core->loadROM = _GBACoreLoadROM;
858 core->loadBIOS = _GBACoreLoadBIOS;
859 core->loadSave = _GBACoreLoadSave;
860 core->loadTemporarySave = _GBACoreLoadTemporarySave;
861 core->loadPatch = _GBACoreLoadPatch;
862 core->unloadROM = _GBACoreUnloadROM;
863 core->checksum = _GBACoreChecksum;
864 core->reset = _GBACoreReset;
865 core->runFrame = _GBACoreRunFrame;
866 core->runLoop = _GBACoreRunLoop;
867 core->step = _GBACoreStep;
868 core->stateSize = _GBACoreStateSize;
869 core->loadState = _GBACoreLoadState;
870 core->saveState = _GBACoreSaveState;
871 core->setKeys = _GBACoreSetKeys;
872 core->addKeys = _GBACoreAddKeys;
873 core->clearKeys = _GBACoreClearKeys;
874 core->frameCounter = _GBACoreFrameCounter;
875 core->frameCycles = _GBACoreFrameCycles;
876 core->frequency = _GBACoreFrequency;
877 core->getGameTitle = _GBACoreGetGameTitle;
878 core->getGameCode = _GBACoreGetGameCode;
879 core->setPeripheral = _GBACoreSetPeripheral;
880 core->busRead8 = _GBACoreBusRead8;
881 core->busRead16 = _GBACoreBusRead16;
882 core->busRead32 = _GBACoreBusRead32;
883 core->busWrite8 = _GBACoreBusWrite8;
884 core->busWrite16 = _GBACoreBusWrite16;
885 core->busWrite32 = _GBACoreBusWrite32;
886 core->rawRead8 = _GBACoreRawRead8;
887 core->rawRead16 = _GBACoreRawRead16;
888 core->rawRead32 = _GBACoreRawRead32;
889 core->rawWrite8 = _GBACoreRawWrite8;
890 core->rawWrite16 = _GBACoreRawWrite16;
891 core->rawWrite32 = _GBACoreRawWrite32;
892 core->listMemoryBlocks = _GBAListMemoryBlocks;
893 core->getMemoryBlock = _GBAGetMemoryBlock;
894#ifdef USE_DEBUGGERS
895 core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
896 core->debuggerPlatform = _GBACoreDebuggerPlatform;
897 core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
898 core->attachDebugger = _GBACoreAttachDebugger;
899 core->detachDebugger = _GBACoreDetachDebugger;
900 core->loadSymbols = _GBACoreLoadSymbols;
901#endif
902 core->cheatDevice = _GBACoreCheatDevice;
903 core->savedataClone = _GBACoreSavedataClone;
904 core->savedataRestore = _GBACoreSavedataRestore;
905 core->listVideoLayers = _GBACoreListVideoLayers;
906 core->listAudioChannels = _GBACoreListAudioChannels;
907 core->enableVideoLayer = _GBACoreEnableVideoLayer;
908 core->enableAudioChannel = _GBACoreEnableAudioChannel;
909#ifndef MINIMAL_CORE
910 core->startVideoLog = _GBACoreStartVideoLog;
911 core->endVideoLog = _GBACoreEndVideoLog;
912#endif
913 return core;
914}
915
916#ifndef MINIMAL_CORE
917static void _GBAVLPStartFrameCallback(void *context) {
918 struct mCore* core = context;
919 struct GBACore* gbacore = (struct GBACore*) core;
920 struct GBA* gba = core->board;
921
922 if (!mVideoLoggerRendererRun(gbacore->proxyRenderer.logger, true)) {
923 GBAVideoProxyRendererUnshim(&gba->video, &gbacore->proxyRenderer);
924 mVideoLogContextRewind(gbacore->logContext, core);
925 GBAVideoProxyRendererShim(&gba->video, &gbacore->proxyRenderer);
926 }
927}
928
929static bool _GBAVLPInit(struct mCore* core) {
930 struct GBACore* gbacore = (struct GBACore*) core;
931 if (!_GBACoreInit(core)) {
932 return false;
933 }
934 gbacore->proxyRenderer.logger = malloc(sizeof(struct mVideoLogger));
935 mVideoLoggerRendererCreate(gbacore->proxyRenderer.logger, true);
936 GBAVideoProxyRendererCreate(&gbacore->proxyRenderer, NULL);
937 memset(&gbacore->logCallbacks, 0, sizeof(gbacore->logCallbacks));
938 gbacore->logCallbacks.videoFrameStarted = _GBAVLPStartFrameCallback;
939 gbacore->logCallbacks.context = core;
940 core->addCoreCallbacks(core, &gbacore->logCallbacks);
941 return true;
942}
943
944static void _GBAVLPDeinit(struct mCore* core) {
945 struct GBACore* gbacore = (struct GBACore*) core;
946 if (gbacore->logContext) {
947 mVideoLogContextDestroy(core, gbacore->logContext);
948 }
949 _GBACoreDeinit(core);
950}
951
952static void _GBAVLPReset(struct mCore* core) {
953 struct GBACore* gbacore = (struct GBACore*) core;
954 struct GBA* gba = (struct GBA*) core->board;
955 if (gba->video.renderer == &gbacore->proxyRenderer.d) {
956 GBAVideoProxyRendererUnshim(&gba->video, &gbacore->proxyRenderer);
957 } else if (gbacore->renderer.outputBuffer) {
958 struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
959 GBAVideoAssociateRenderer(&gba->video, renderer);
960 }
961
962 ARMReset(core->cpu);
963 mVideoLogContextRewind(gbacore->logContext, core);
964 GBAVideoProxyRendererShim(&gba->video, &gbacore->proxyRenderer);
965
966 // Make sure CPU loop never spins
967 GBAHalt(gba);
968 gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
969 gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
970}
971
972static bool _GBAVLPLoadROM(struct mCore* core, struct VFile* vf) {
973 struct GBACore* gbacore = (struct GBACore*) core;
974 gbacore->logContext = mVideoLogContextCreate(NULL);
975 if (!mVideoLogContextLoad(gbacore->logContext, vf)) {
976 mVideoLogContextDestroy(core, gbacore->logContext);
977 gbacore->logContext = NULL;
978 return false;
979 }
980 mVideoLoggerAttachChannel(gbacore->proxyRenderer.logger, gbacore->logContext, 0);
981 return true;
982}
983
984static bool _GBAVLPLoadState(struct mCore* core, const void* state) {
985 struct GBA* gba = (struct GBA*) core->board;
986
987 gba->timing.root = NULL;
988 gba->cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
989 gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
990
991 // Make sure CPU loop never spins
992 GBAHalt(gba);
993 gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IME, 0, NULL);
994 gba->cpu->memory.store16(gba->cpu, BASE_IO | REG_IE, 0, NULL);
995 GBAVideoDeserialize(&gba->video, state);
996 GBAIODeserialize(gba, state);
997 GBAAudioReset(&gba->audio);
998
999 return true;
1000}
1001
1002static bool _returnTrue(struct VFile* vf) {
1003 UNUSED(vf);
1004 return true;
1005}
1006
1007struct mCore* GBAVideoLogPlayerCreate(void) {
1008 struct mCore* core = GBACoreCreate();
1009 core->init = _GBAVLPInit;
1010 core->deinit = _GBAVLPDeinit;
1011 core->reset = _GBAVLPReset;
1012 core->loadROM = _GBAVLPLoadROM;
1013 core->loadState = _GBAVLPLoadState;
1014 core->isROM = _returnTrue;
1015 return core;
1016}
1017#else
1018struct mCore* GBAVideoLogPlayerCreate(void) {
1019 return false;
1020}
1021#endif