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 "core.h"
7
8#include "core/core.h"
9#include "core/log.h"
10#include "gba/gba.h"
11#include "gba/context/overrides.h"
12#include "gba/renderers/video-software.h"
13#include "gba/serialize.h"
14#include "util/memory.h"
15#include "util/patch.h"
16#include "util/vfs.h"
17
18struct GBACore {
19 struct mCore d;
20 struct GBAVideoSoftwareRenderer renderer;
21 int keys;
22 struct mCPUComponent* components[GBA_COMPONENT_MAX];
23 const struct Configuration* overrides;
24};
25
26static bool _GBACoreInit(struct mCore* core) {
27 struct GBACore* gbacore = (struct GBACore*) core;
28
29 struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
30 struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
31 if (!cpu || !gba) {
32 free(cpu);
33 free(gba);
34 return false;
35 }
36 core->cpu = cpu;
37 core->board = gba;
38 gbacore->overrides = 0;
39
40 GBACreate(gba);
41 // TODO: Restore debugger and cheats
42 memset(gbacore->components, 0, sizeof(gbacore->components));
43 ARMSetComponents(cpu, &gba->d, GBA_COMPONENT_MAX, gbacore->components);
44 ARMInit(cpu);
45
46 GBAVideoSoftwareRendererCreate(&gbacore->renderer);
47
48 gbacore->keys = 0;
49 gba->keySource = &gbacore->keys;
50
51#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
52 mDirectorySetInit(&core->dirs);
53#endif
54
55 return true;
56}
57
58static void _GBACoreDeinit(struct mCore* core) {
59 ARMDeinit(core->cpu);
60 GBADestroy(core->board);
61 mappedMemoryFree(core->cpu, sizeof(struct ARMCore));
62 mappedMemoryFree(core->board, sizeof(struct GBA));
63#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
64 mDirectorySetDeinit(&core->dirs);
65#endif
66}
67
68static enum mPlatform _GBACorePlatform(struct mCore* core) {
69 UNUSED(core);
70 return PLATFORM_GBA;
71}
72
73static void _GBACoreSetSync(struct mCore* core, struct mCoreSync* sync) {
74 struct GBA* gba = core->board;
75 gba->sync = sync;
76}
77
78static void _GBACoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
79 struct GBA* gba = core->board;
80 gba->audio.masterVolume = core->opts.volume;
81
82#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
83 struct GBACore* gbacore = (struct GBACore*) core;
84 gbacore->overrides = mCoreConfigGetOverridesConst(config);
85
86 struct VFile* bios = 0;
87 if (core->opts.useBios) {
88 bios = VFileOpen(core->opts.bios, O_RDONLY);
89 }
90 if (bios) {
91 GBALoadBIOS(gba, bios);
92 }
93#endif
94
95 const char* idleOptimization = mCoreConfigGetValue(config, "idleOptimization");
96 if (idleOptimization) {
97 if (strcasecmp(idleOptimization, "ignore") == 0) {
98 gba->idleOptimization = IDLE_LOOP_IGNORE;
99 } else if (strcasecmp(idleOptimization, "remove") == 0) {
100 gba->idleOptimization = IDLE_LOOP_REMOVE;
101 } else if (strcasecmp(idleOptimization, "detect") == 0) {
102 gba->idleOptimization = IDLE_LOOP_DETECT;
103 }
104 }
105}
106
107static void _GBACoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
108 UNUSED(core);
109 *width = VIDEO_HORIZONTAL_PIXELS;
110 *height = VIDEO_VERTICAL_PIXELS;
111}
112
113static void _GBACoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
114 struct GBACore* gbacore = (struct GBACore*) core;
115 gbacore->renderer.outputBuffer = buffer;
116 gbacore->renderer.outputBufferStride = stride;
117}
118
119static void _GBACoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
120 struct GBACore* gbacore = (struct GBACore*) core;
121 *buffer = gbacore->renderer.outputBuffer;
122 *stride = gbacore->renderer.outputBufferStride;
123}
124
125static struct blip_t* _GBACoreGetAudioChannel(struct mCore* core, int ch) {
126 struct GBA* gba = core->board;
127 switch (ch) {
128 case 0:
129 return gba->audio.psg.left;
130 case 1:
131 return gba->audio.psg.right;
132 default:
133 return NULL;
134 }
135}
136
137static void _GBACoreSetAudioBufferSize(struct mCore* core, size_t samples) {
138 struct GBA* gba = core->board;
139 GBAAudioResizeBuffer(&gba->audio, samples);
140}
141
142static size_t _GBACoreGetAudioBufferSize(struct mCore* core) {
143 struct GBA* gba = core->board;
144 return gba->audio.samples;
145}
146
147static void _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
148 struct GBA* gba = core->board;
149 gba->stream = stream;
150}
151
152static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
153 return GBALoadROM2(core->board, vf);
154}
155
156static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
157 UNUSED(type);
158 if (!GBAIsBIOS(vf)) {
159 return false;
160 }
161 GBALoadBIOS(core->board, vf);
162 return true;
163}
164
165static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
166 return GBALoadSave(core->board, vf);
167}
168
169static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
170 if (!vf) {
171 return false;
172 }
173 struct Patch patch;
174 if (!loadPatch(vf, &patch)) {
175 return false;
176 }
177 GBAApplyPatch(core->board, &patch);
178 return true;
179}
180
181static void _GBACoreUnloadROM(struct mCore* core) {
182 return GBAUnloadROM(core->board);
183}
184
185static void _GBACoreReset(struct mCore* core) {
186 struct GBACore* gbacore = (struct GBACore*) core;
187 struct GBA* gba = (struct GBA*) core->board;
188 if (gbacore->renderer.outputBuffer) {
189 GBAVideoAssociateRenderer(&gba->video, &gbacore->renderer.d);
190 }
191 ARMReset(core->cpu);
192 if (core->opts.skipBios) {
193 GBASkipBIOS(core->board);
194 }
195
196 struct GBACartridgeOverride override;
197 const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
198 memcpy(override.id, &cart->id, sizeof(override.id));
199 if (GBAOverrideFind(gbacore->overrides, &override)) {
200 GBAOverrideApply(gba, &override);
201 }
202}
203
204static void _GBACoreRunFrame(struct mCore* core) {
205 struct GBA* gba = core->board;
206 int32_t frameCounter = gba->video.frameCounter;
207 while (gba->video.frameCounter == frameCounter) {
208 ARMRunLoop(core->cpu);
209 }
210}
211
212static void _GBACoreRunLoop(struct mCore* core) {
213 ARMRunLoop(core->cpu);
214}
215
216static void _GBACoreStep(struct mCore* core) {
217 ARMRun(core->cpu);
218}
219
220static bool _GBACoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
221 return GBALoadStateNamed(core->board, vf, flags);
222}
223
224static bool _GBACoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
225 return GBASaveStateNamed(core->board, vf, flags);
226}
227
228static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
229 struct GBACore* gbacore = (struct GBACore*) core;
230 gbacore->keys = keys;
231}
232
233static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
234 struct GBACore* gbacore = (struct GBACore*) core;
235 gbacore->keys |= keys;
236}
237
238static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
239 struct GBACore* gbacore = (struct GBACore*) core;
240 gbacore->keys &= ~keys;
241}
242
243static int32_t _GBACoreFrameCounter(struct mCore* core) {
244 struct GBA* gba = core->board;
245 return gba->video.frameCounter;
246}
247
248static int32_t _GBACoreFrameCycles(struct mCore* core) {
249 UNUSED(core);
250 return VIDEO_TOTAL_LENGTH;
251}
252
253static int32_t _GBACoreFrequency(struct mCore* core) {
254 UNUSED(core);
255 return GBA_ARM7TDMI_FREQUENCY;
256}
257
258static void _GBACoreGetGameTitle(struct mCore* core, char* title) {
259 GBAGetGameTitle(core->board, title);
260}
261
262static void _GBACoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
263 struct GBA* gba = core->board;
264 gba->rtcSource = rtc;
265}
266
267struct mCore* GBACoreCreate(void) {
268 struct GBACore* gbacore = malloc(sizeof(*gbacore));
269 struct mCore* core = &gbacore->d;
270 memset(&core->opts, 0, sizeof(core->opts));
271 core->cpu = 0;
272 core->board = 0;
273 core->init = _GBACoreInit;
274 core->deinit = _GBACoreDeinit;
275 core->platform = _GBACorePlatform;
276 core->setSync = _GBACoreSetSync;
277 core->loadConfig = _GBACoreLoadConfig;
278 core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
279 core->setVideoBuffer = _GBACoreSetVideoBuffer;
280 core->getVideoBuffer = _GBACoreGetVideoBuffer;
281 core->getAudioChannel = _GBACoreGetAudioChannel;
282 core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
283 core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
284 core->setAVStream = _GBACoreSetAVStream;
285 core->isROM = GBAIsROM;
286 core->loadROM = _GBACoreLoadROM;
287 core->loadBIOS = _GBACoreLoadBIOS;
288 core->loadSave = _GBACoreLoadSave;
289 core->loadPatch = _GBACoreLoadPatch;
290 core->unloadROM = _GBACoreUnloadROM;
291 core->reset = _GBACoreReset;
292 core->runFrame = _GBACoreRunFrame;
293 core->runLoop = _GBACoreRunLoop;
294 core->step = _GBACoreStep;
295 core->loadState = _GBACoreLoadState;
296 core->saveState = _GBACoreSaveState;
297 core->setKeys = _GBACoreSetKeys;
298 core->addKeys = _GBACoreAddKeys;
299 core->clearKeys = _GBACoreClearKeys;
300 core->frameCounter = _GBACoreFrameCounter;
301 core->frameCycles = _GBACoreFrameCycles;
302 core->frequency = _GBACoreFrequency;
303 core->getGameTitle = _GBACoreGetGameTitle;
304 core->setRTC = _GBACoreSetRTC;
305 return core;
306}