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