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