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