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