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