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 gba->video.frameskip = core->opts.frameskip;
82
83#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
84 struct GBACore* gbacore = (struct GBACore*) core;
85 gbacore->overrides = mCoreConfigGetOverridesConst(config);
86
87 struct VFile* bios = 0;
88 if (core->opts.useBios && core->opts.bios) {
89 bios = VFileOpen(core->opts.bios, O_RDONLY);
90 }
91 if (bios) {
92 GBALoadBIOS(gba, bios);
93 }
94#endif
95
96 const char* idleOptimization = mCoreConfigGetValue(config, "idleOptimization");
97 if (idleOptimization) {
98 if (strcasecmp(idleOptimization, "ignore") == 0) {
99 gba->idleOptimization = IDLE_LOOP_IGNORE;
100 } else if (strcasecmp(idleOptimization, "remove") == 0) {
101 gba->idleOptimization = IDLE_LOOP_REMOVE;
102 } else if (strcasecmp(idleOptimization, "detect") == 0) {
103 gba->idleOptimization = IDLE_LOOP_DETECT;
104 }
105 }
106}
107
108static void _GBACoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
109 UNUSED(core);
110 *width = VIDEO_HORIZONTAL_PIXELS;
111 *height = VIDEO_VERTICAL_PIXELS;
112}
113
114static void _GBACoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
115 struct GBACore* gbacore = (struct GBACore*) core;
116 gbacore->renderer.outputBuffer = buffer;
117 gbacore->renderer.outputBufferStride = stride;
118}
119
120static void _GBACoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
121 struct GBACore* gbacore = (struct GBACore*) core;
122 *buffer = gbacore->renderer.outputBuffer;
123 *stride = gbacore->renderer.outputBufferStride;
124}
125
126static struct blip_t* _GBACoreGetAudioChannel(struct mCore* core, int ch) {
127 struct GBA* gba = core->board;
128 switch (ch) {
129 case 0:
130 return gba->audio.psg.left;
131 case 1:
132 return gba->audio.psg.right;
133 default:
134 return NULL;
135 }
136}
137
138static void _GBACoreSetAudioBufferSize(struct mCore* core, size_t samples) {
139 struct GBA* gba = core->board;
140 GBAAudioResizeBuffer(&gba->audio, samples);
141}
142
143static size_t _GBACoreGetAudioBufferSize(struct mCore* core) {
144 struct GBA* gba = core->board;
145 return gba->audio.samples;
146}
147
148static void _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
149 struct GBA* gba = core->board;
150 gba->stream = stream;
151 if (stream && stream->videoDimensionsChanged) {
152 stream->videoDimensionsChanged(stream, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
153 }
154}
155
156static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
157 return GBALoadROM(core->board, vf);
158}
159
160static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
161 UNUSED(type);
162 if (!GBAIsBIOS(vf)) {
163 return false;
164 }
165 GBALoadBIOS(core->board, vf);
166 return true;
167}
168
169static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
170 return GBALoadSave(core->board, vf);
171}
172
173static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
174 if (!vf) {
175 return false;
176 }
177 struct Patch patch;
178 if (!loadPatch(vf, &patch)) {
179 return false;
180 }
181 GBAApplyPatch(core->board, &patch);
182 return true;
183}
184
185static void _GBACoreUnloadROM(struct mCore* core) {
186 return GBAUnloadROM(core->board);
187}
188
189static void _GBACoreReset(struct mCore* core) {
190 struct GBACore* gbacore = (struct GBACore*) core;
191 struct GBA* gba = (struct GBA*) core->board;
192 if (gbacore->renderer.outputBuffer) {
193 GBAVideoAssociateRenderer(&gba->video, &gbacore->renderer.d);
194 }
195 ARMReset(core->cpu);
196 if (core->opts.skipBios) {
197 GBASkipBIOS(core->board);
198 }
199
200 struct GBACartridgeOverride override;
201 const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
202 memcpy(override.id, &cart->id, sizeof(override.id));
203 if (GBAOverrideFind(gbacore->overrides, &override)) {
204 GBAOverrideApply(gba, &override);
205 }
206}
207
208static void _GBACoreRunFrame(struct mCore* core) {
209 struct GBA* gba = core->board;
210 int32_t frameCounter = gba->video.frameCounter;
211 while (gba->video.frameCounter == frameCounter) {
212 ARMRunLoop(core->cpu);
213 }
214}
215
216static void _GBACoreRunLoop(struct mCore* core) {
217 ARMRunLoop(core->cpu);
218}
219
220static void _GBACoreStep(struct mCore* core) {
221 ARMRun(core->cpu);
222}
223
224static bool _GBACoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
225 return GBALoadStateNamed(core->board, vf, flags);
226}
227
228static bool _GBACoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
229 return GBASaveStateNamed(core->board, vf, flags);
230}
231
232static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
233 struct GBACore* gbacore = (struct GBACore*) core;
234 gbacore->keys = keys;
235}
236
237static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
238 struct GBACore* gbacore = (struct GBACore*) core;
239 gbacore->keys |= keys;
240}
241
242static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
243 struct GBACore* gbacore = (struct GBACore*) core;
244 gbacore->keys &= ~keys;
245}
246
247static int32_t _GBACoreFrameCounter(struct mCore* core) {
248 struct GBA* gba = core->board;
249 return gba->video.frameCounter;
250}
251
252static int32_t _GBACoreFrameCycles(struct mCore* core) {
253 UNUSED(core);
254 return VIDEO_TOTAL_LENGTH;
255}
256
257static int32_t _GBACoreFrequency(struct mCore* core) {
258 UNUSED(core);
259 return GBA_ARM7TDMI_FREQUENCY;
260}
261
262static void _GBACoreGetGameTitle(struct mCore* core, char* title) {
263 GBAGetGameTitle(core->board, title);
264}
265
266static void _GBACoreGetGameCode(struct mCore* core, char* title) {
267 GBAGetGameCode(core->board, title);
268}
269
270static void _GBACoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
271 struct GBA* gba = core->board;
272 gba->rtcSource = rtc;
273}
274
275static void _GBACoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
276 struct GBA* gba = core->board;
277 gba->rotationSource = rotation;
278}
279
280static void _GBACoreSetRumble(struct mCore* core, struct mRumble* rumble) {
281 struct GBA* gba = core->board;
282 gba->rumble = rumble;
283}
284
285struct mCore* GBACoreCreate(void) {
286 struct GBACore* gbacore = malloc(sizeof(*gbacore));
287 struct mCore* core = &gbacore->d;
288 memset(&core->opts, 0, sizeof(core->opts));
289 core->cpu = 0;
290 core->board = 0;
291 core->init = _GBACoreInit;
292 core->deinit = _GBACoreDeinit;
293 core->platform = _GBACorePlatform;
294 core->setSync = _GBACoreSetSync;
295 core->loadConfig = _GBACoreLoadConfig;
296 core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
297 core->setVideoBuffer = _GBACoreSetVideoBuffer;
298 core->getVideoBuffer = _GBACoreGetVideoBuffer;
299 core->getAudioChannel = _GBACoreGetAudioChannel;
300 core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
301 core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
302 core->setAVStream = _GBACoreSetAVStream;
303 core->isROM = GBAIsROM;
304 core->loadROM = _GBACoreLoadROM;
305 core->loadBIOS = _GBACoreLoadBIOS;
306 core->loadSave = _GBACoreLoadSave;
307 core->loadPatch = _GBACoreLoadPatch;
308 core->unloadROM = _GBACoreUnloadROM;
309 core->reset = _GBACoreReset;
310 core->runFrame = _GBACoreRunFrame;
311 core->runLoop = _GBACoreRunLoop;
312 core->step = _GBACoreStep;
313 core->loadState = _GBACoreLoadState;
314 core->saveState = _GBACoreSaveState;
315 core->setKeys = _GBACoreSetKeys;
316 core->addKeys = _GBACoreAddKeys;
317 core->clearKeys = _GBACoreClearKeys;
318 core->frameCounter = _GBACoreFrameCounter;
319 core->frameCycles = _GBACoreFrameCycles;
320 core->frequency = _GBACoreFrequency;
321 core->getGameTitle = _GBACoreGetGameTitle;
322 core->getGameCode = _GBACoreGetGameCode;
323 core->setRTC = _GBACoreSetRTC;
324 core->setRotation = _GBACoreSetRotation;
325 core->setRumble = _GBACoreSetRumble;
326 return core;
327}