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