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