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) {
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}
152
153static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
154 return GBALoadROM2(core->board, vf);
155}
156
157static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
158 UNUSED(type);
159 if (!GBAIsBIOS(vf)) {
160 return false;
161 }
162 GBALoadBIOS(core->board, vf);
163 return true;
164}
165
166static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
167 return GBALoadSave(core->board, vf);
168}
169
170static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
171 if (!vf) {
172 return false;
173 }
174 struct Patch patch;
175 if (!loadPatch(vf, &patch)) {
176 return false;
177 }
178 GBAApplyPatch(core->board, &patch);
179 return true;
180}
181
182static void _GBACoreUnloadROM(struct mCore* core) {
183 return GBAUnloadROM(core->board);
184}
185
186static void _GBACoreReset(struct mCore* core) {
187 struct GBACore* gbacore = (struct GBACore*) core;
188 struct GBA* gba = (struct GBA*) core->board;
189 if (gbacore->renderer.outputBuffer) {
190 GBAVideoAssociateRenderer(&gba->video, &gbacore->renderer.d);
191 }
192 ARMReset(core->cpu);
193 if (core->opts.skipBios) {
194 GBASkipBIOS(core->board);
195 }
196
197 struct GBACartridgeOverride override;
198 const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
199 memcpy(override.id, &cart->id, sizeof(override.id));
200 if (GBAOverrideFind(gbacore->overrides, &override)) {
201 GBAOverrideApply(gba, &override);
202 }
203}
204
205static void _GBACoreRunFrame(struct mCore* core) {
206 struct GBA* gba = core->board;
207 int32_t frameCounter = gba->video.frameCounter;
208 while (gba->video.frameCounter == frameCounter) {
209 ARMRunLoop(core->cpu);
210 }
211}
212
213static void _GBACoreRunLoop(struct mCore* core) {
214 ARMRunLoop(core->cpu);
215}
216
217static void _GBACoreStep(struct mCore* core) {
218 ARMRun(core->cpu);
219}
220
221static bool _GBACoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
222 return GBALoadStateNamed(core->board, vf, flags);
223}
224
225static bool _GBACoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
226 return GBASaveStateNamed(core->board, vf, flags);
227}
228
229static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
230 struct GBACore* gbacore = (struct GBACore*) core;
231 gbacore->keys = keys;
232}
233
234static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
235 struct GBACore* gbacore = (struct GBACore*) core;
236 gbacore->keys |= keys;
237}
238
239static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
240 struct GBACore* gbacore = (struct GBACore*) core;
241 gbacore->keys &= ~keys;
242}
243
244static int32_t _GBACoreFrameCounter(struct mCore* core) {
245 struct GBA* gba = core->board;
246 return gba->video.frameCounter;
247}
248
249static int32_t _GBACoreFrameCycles(struct mCore* core) {
250 UNUSED(core);
251 return VIDEO_TOTAL_LENGTH;
252}
253
254static int32_t _GBACoreFrequency(struct mCore* core) {
255 UNUSED(core);
256 return GBA_ARM7TDMI_FREQUENCY;
257}
258
259static void _GBACoreGetGameTitle(struct mCore* core, char* title) {
260 GBAGetGameTitle(core->board, title);
261}
262
263static void _GBACoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
264 struct GBA* gba = core->board;
265 gba->rtcSource = rtc;
266}
267
268struct mCore* GBACoreCreate(void) {
269 struct GBACore* gbacore = malloc(sizeof(*gbacore));
270 struct mCore* core = &gbacore->d;
271 memset(&core->opts, 0, sizeof(core->opts));
272 core->cpu = 0;
273 core->board = 0;
274 core->init = _GBACoreInit;
275 core->deinit = _GBACoreDeinit;
276 core->platform = _GBACorePlatform;
277 core->setSync = _GBACoreSetSync;
278 core->loadConfig = _GBACoreLoadConfig;
279 core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
280 core->setVideoBuffer = _GBACoreSetVideoBuffer;
281 core->getVideoBuffer = _GBACoreGetVideoBuffer;
282 core->getAudioChannel = _GBACoreGetAudioChannel;
283 core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
284 core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
285 core->setAVStream = _GBACoreSetAVStream;
286 core->isROM = GBAIsROM;
287 core->loadROM = _GBACoreLoadROM;
288 core->loadBIOS = _GBACoreLoadBIOS;
289 core->loadSave = _GBACoreLoadSave;
290 core->loadPatch = _GBACoreLoadPatch;
291 core->unloadROM = _GBACoreUnloadROM;
292 core->reset = _GBACoreReset;
293 core->runFrame = _GBACoreRunFrame;
294 core->runLoop = _GBACoreRunLoop;
295 core->step = _GBACoreStep;
296 core->loadState = _GBACoreLoadState;
297 core->saveState = _GBACoreSaveState;
298 core->setKeys = _GBACoreSetKeys;
299 core->addKeys = _GBACoreAddKeys;
300 core->clearKeys = _GBACoreClearKeys;
301 core->frameCounter = _GBACoreFrameCounter;
302 core->frameCycles = _GBACoreFrameCycles;
303 core->frequency = _GBACoreFrequency;
304 core->getGameTitle = _GBACoreGetGameTitle;
305 core->setRTC = _GBACoreSetRTC;
306 return core;
307}