src/gb/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 "gb/gb.h"
10#include "gb/renderers/software.h"
11#include "util/memory.h"
12#include "util/patch.h"
13
14struct GBCore {
15 struct mCore d;
16 struct GBVideoSoftwareRenderer renderer;
17 uint8_t keys;
18};
19
20static bool _GBCoreInit(struct mCore* core) {
21 struct GBCore* gbcore = (struct GBCore*) core;
22
23 struct LR35902Core* cpu = anonymousMemoryMap(sizeof(struct LR35902Core));
24 struct GB* gb = anonymousMemoryMap(sizeof(struct GB));
25 if (!cpu || !gb) {
26 free(cpu);
27 free(gb);
28 return false;
29 }
30 core->cpu = cpu;
31 core->board = gb;
32
33 GBCreate(gb);
34 LR35902SetComponents(cpu, &gb->d, 0, 0);
35 LR35902Init(cpu);
36
37 GBVideoSoftwareRendererCreate(&gbcore->renderer);
38
39 gbcore->keys = 0;
40 gb->keySource = &gbcore->keys;
41
42#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
43 mDirectorySetInit(&core->dirs);
44#endif
45
46 return true;
47}
48
49static void _GBCoreDeinit(struct mCore* core) {
50 LR35902Deinit(core->cpu);
51 GBDestroy(core->board);
52 mappedMemoryFree(core->cpu, sizeof(struct LR35902Core));
53 mappedMemoryFree(core->board, sizeof(struct GB));
54#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
55 mDirectorySetDeinit(&core->dirs);
56#endif
57}
58
59static enum mPlatform _GBCorePlatform(struct mCore* core) {
60 UNUSED(core);
61 return PLATFORM_GB;
62}
63
64static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
65 struct GB* gb = core->board;
66 gb->sync = sync;
67}
68
69static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
70 UNUSED(config);
71
72 struct GB* gb = core->board;
73 gb->audio.masterVolume = core->opts.volume;
74 gb->video.frameskip = core->opts.frameskip;
75}
76
77static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
78 UNUSED(core);
79 *width = GB_VIDEO_HORIZONTAL_PIXELS;
80 *height = GB_VIDEO_VERTICAL_PIXELS;
81}
82
83static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
84 struct GBCore* gbcore = (struct GBCore*) core;
85 gbcore->renderer.outputBuffer = buffer;
86 gbcore->renderer.outputBufferStride = stride;
87}
88
89static void _GBCoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
90 struct GBCore* gbcore = (struct GBCore*) core;
91 *buffer = gbcore->renderer.outputBuffer;
92 *stride = gbcore->renderer.outputBufferStride;
93}
94
95static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
96 struct GB* gb = core->board;
97 switch (ch) {
98 case 0:
99 return gb->audio.left;
100 case 1:
101 return gb->audio.right;
102 default:
103 return NULL;
104 }
105}
106
107static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
108 struct GB* gb = core->board;
109 GBAudioResizeBuffer(&gb->audio, samples);
110}
111
112static size_t _GBCoreGetAudioBufferSize(struct mCore* core) {
113 struct GB* gb = core->board;
114 return gb->audio.samples;
115}
116
117static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
118 struct GB* gb = core->board;
119 gb->stream = stream;
120 if (stream && stream->videoDimensionsChanged) {
121 stream->videoDimensionsChanged(stream, GB_VIDEO_HORIZONTAL_PIXELS, GB_VIDEO_VERTICAL_PIXELS);
122 }
123}
124
125static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
126 return GBLoadROM(core->board, vf);
127}
128
129static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
130 UNUSED(core);
131 UNUSED(vf);
132 UNUSED(type);
133 // TODO
134 return false;
135}
136
137static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
138 return GBLoadSave(core->board, vf);
139}
140
141static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
142 if (!vf) {
143 return false;
144 }
145 struct Patch patch;
146 if (!loadPatch(vf, &patch)) {
147 return false;
148 }
149 GBApplyPatch(core->board, &patch);
150 return true;
151}
152
153static void _GBCoreUnloadROM(struct mCore* core) {
154 return GBUnloadROM(core->board);
155}
156
157static void _GBCoreReset(struct mCore* core) {
158 struct GBCore* gbcore = (struct GBCore*) core;
159 struct GB* gb = (struct GB*) core->board;
160 if (gbcore->renderer.outputBuffer) {
161 GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
162 }
163 LR35902Reset(core->cpu);
164}
165
166static void _GBCoreRunFrame(struct mCore* core) {
167 struct GB* gb = core->board;
168 int32_t frameCounter = gb->video.frameCounter;
169 while (gb->video.frameCounter == frameCounter) {
170 LR35902Run(core->cpu);
171 }
172}
173
174static void _GBCoreRunLoop(struct mCore* core) {
175 LR35902Run(core->cpu);
176}
177
178static void _GBCoreStep(struct mCore* core) {
179 LR35902Tick(core->cpu);
180}
181
182static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
183 UNUSED(core);
184 UNUSED(vf);
185 UNUSED(flags);
186 // TODO
187 return false;
188}
189
190static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
191 UNUSED(core);
192 UNUSED(vf);
193 UNUSED(flags);
194 // TODO
195 return false;
196}
197
198static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
199 struct GBCore* gbcore = (struct GBCore*) core;
200 gbcore->keys = keys;
201}
202
203static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
204 struct GBCore* gbcore = (struct GBCore*) core;
205 gbcore->keys |= keys;
206}
207
208static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
209 struct GBCore* gbcore = (struct GBCore*) core;
210 gbcore->keys &= ~keys;
211}
212
213static int32_t _GBCoreFrameCounter(struct mCore* core) {
214 struct GB* gb = core->board;
215 return gb->video.frameCounter;
216}
217
218static int32_t _GBCoreFrameCycles(struct mCore* core) {
219 UNUSED(core);
220 return GB_VIDEO_TOTAL_LENGTH;
221}
222
223static int32_t _GBCoreFrequency(struct mCore* core) {
224 UNUSED(core);
225 // TODO: GB differences
226 return DMG_LR35902_FREQUENCY;
227}
228
229static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
230 GBGetGameTitle(core->board, title);
231}
232
233static void _GBCoreGetGameCode(struct mCore* core, char* title) {
234 GBGetGameCode(core->board, title);
235}
236
237static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
238 struct GB* gb = core->board;
239 gb->memory.rtc = rtc;
240}
241
242static void _GBCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
243 struct GB* gb = core->board;
244 gb->memory.rotation = rotation;
245}
246
247static void _GBCoreSetRumble(struct mCore* core, struct mRumble* rumble) {
248 struct GB* gb = core->board;
249 gb->memory.rumble = rumble;
250}
251
252struct mCore* GBCoreCreate(void) {
253 struct GBCore* gbcore = malloc(sizeof(*gbcore));
254 struct mCore* core = &gbcore->d;
255 memset(&core->opts, 0, sizeof(core->opts));
256 core->cpu = 0;
257 core->board = 0;
258 core->init = _GBCoreInit;
259 core->deinit = _GBCoreDeinit;
260 core->platform = _GBCorePlatform;
261 core->setSync = _GBCoreSetSync;
262 core->loadConfig = _GBCoreLoadConfig;
263 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
264 core->setVideoBuffer = _GBCoreSetVideoBuffer;
265 core->getVideoBuffer = _GBCoreGetVideoBuffer;
266 core->getAudioChannel = _GBCoreGetAudioChannel;
267 core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
268 core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
269 core->setAVStream = _GBCoreSetAVStream;
270 core->isROM = GBIsROM;
271 core->loadROM = _GBCoreLoadROM;
272 core->loadBIOS = _GBCoreLoadBIOS;
273 core->loadSave = _GBCoreLoadSave;
274 core->loadPatch = _GBCoreLoadPatch;
275 core->unloadROM = _GBCoreUnloadROM;
276 core->reset = _GBCoreReset;
277 core->runFrame = _GBCoreRunFrame;
278 core->runLoop = _GBCoreRunLoop;
279 core->step = _GBCoreStep;
280 core->loadState = _GBCoreLoadState;
281 core->saveState = _GBCoreSaveState;
282 core->setKeys = _GBCoreSetKeys;
283 core->addKeys = _GBCoreAddKeys;
284 core->clearKeys = _GBCoreClearKeys;
285 core->frameCounter = _GBCoreFrameCounter;
286 core->frameCycles = _GBCoreFrameCycles;
287 core->frequency = _GBCoreFrequency;
288 core->getGameTitle = _GBCoreGetGameTitle;
289 core->getGameCode = _GBCoreGetGameCode;
290 core->setRTC = _GBCoreSetRTC;
291 core->setRotation = _GBCoreSetRotation;
292 core->setRumble = _GBCoreSetRumble;
293 return core;
294}