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 if (core->opts.mute) {
74 gb->audio.masterVolume = 0;
75 } else {
76 gb->audio.masterVolume = core->opts.volume;
77 }
78 gb->video.frameskip = core->opts.frameskip;
79}
80
81static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
82 UNUSED(core);
83 *width = GB_VIDEO_HORIZONTAL_PIXELS;
84 *height = GB_VIDEO_VERTICAL_PIXELS;
85}
86
87static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
88 struct GBCore* gbcore = (struct GBCore*) core;
89 gbcore->renderer.outputBuffer = buffer;
90 gbcore->renderer.outputBufferStride = stride;
91}
92
93static void _GBCoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
94 struct GBCore* gbcore = (struct GBCore*) core;
95 *buffer = gbcore->renderer.outputBuffer;
96 *stride = gbcore->renderer.outputBufferStride;
97}
98
99static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
100 struct GB* gb = core->board;
101 switch (ch) {
102 case 0:
103 return gb->audio.left;
104 case 1:
105 return gb->audio.right;
106 default:
107 return NULL;
108 }
109}
110
111static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
112 struct GB* gb = core->board;
113 GBAudioResizeBuffer(&gb->audio, samples);
114}
115
116static size_t _GBCoreGetAudioBufferSize(struct mCore* core) {
117 struct GB* gb = core->board;
118 return gb->audio.samples;
119}
120
121static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
122 struct GB* gb = core->board;
123 gb->stream = stream;
124 if (stream && stream->videoDimensionsChanged) {
125 stream->videoDimensionsChanged(stream, GB_VIDEO_HORIZONTAL_PIXELS, GB_VIDEO_VERTICAL_PIXELS);
126 }
127}
128
129static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
130 return GBLoadROM(core->board, vf);
131}
132
133static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
134 UNUSED(core);
135 UNUSED(vf);
136 UNUSED(type);
137 // TODO
138 return false;
139}
140
141static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
142 return GBLoadSave(core->board, vf);
143}
144
145static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
146 if (!vf) {
147 return false;
148 }
149 struct Patch patch;
150 if (!loadPatch(vf, &patch)) {
151 return false;
152 }
153 GBApplyPatch(core->board, &patch);
154 return true;
155}
156
157static void _GBCoreUnloadROM(struct mCore* core) {
158 return GBUnloadROM(core->board);
159}
160
161static void _GBCoreReset(struct mCore* core) {
162 struct GBCore* gbcore = (struct GBCore*) core;
163 struct GB* gb = (struct GB*) core->board;
164 if (gbcore->renderer.outputBuffer) {
165 GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
166 }
167 LR35902Reset(core->cpu);
168}
169
170static void _GBCoreRunFrame(struct mCore* core) {
171 struct GB* gb = core->board;
172 int32_t frameCounter = gb->video.frameCounter;
173 while (gb->video.frameCounter == frameCounter) {
174 LR35902Run(core->cpu);
175 }
176}
177
178static void _GBCoreRunLoop(struct mCore* core) {
179 LR35902Run(core->cpu);
180}
181
182static void _GBCoreStep(struct mCore* core) {
183 LR35902Tick(core->cpu);
184}
185
186static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
187 UNUSED(core);
188 UNUSED(vf);
189 UNUSED(flags);
190 // TODO
191 return false;
192}
193
194static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
195 UNUSED(core);
196 UNUSED(vf);
197 UNUSED(flags);
198 // TODO
199 return false;
200}
201
202static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
203 struct GBCore* gbcore = (struct GBCore*) core;
204 gbcore->keys = keys;
205}
206
207static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
208 struct GBCore* gbcore = (struct GBCore*) core;
209 gbcore->keys |= keys;
210}
211
212static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
213 struct GBCore* gbcore = (struct GBCore*) core;
214 gbcore->keys &= ~keys;
215}
216
217static int32_t _GBCoreFrameCounter(struct mCore* core) {
218 struct GB* gb = core->board;
219 return gb->video.frameCounter;
220}
221
222static int32_t _GBCoreFrameCycles(struct mCore* core) {
223 UNUSED(core);
224 return GB_VIDEO_TOTAL_LENGTH;
225}
226
227static int32_t _GBCoreFrequency(struct mCore* core) {
228 UNUSED(core);
229 // TODO: GB differences
230 return DMG_LR35902_FREQUENCY;
231}
232
233static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
234 GBGetGameTitle(core->board, title);
235}
236
237static void _GBCoreGetGameCode(struct mCore* core, char* title) {
238 GBGetGameCode(core->board, title);
239}
240
241static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
242 struct GB* gb = core->board;
243 gb->memory.rtc = rtc;
244}
245
246static void _GBCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
247 struct GB* gb = core->board;
248 gb->memory.rotation = rotation;
249}
250
251static void _GBCoreSetRumble(struct mCore* core, struct mRumble* rumble) {
252 struct GB* gb = core->board;
253 gb->memory.rumble = rumble;
254}
255
256struct mCore* GBCoreCreate(void) {
257 struct GBCore* gbcore = malloc(sizeof(*gbcore));
258 struct mCore* core = &gbcore->d;
259 memset(&core->opts, 0, sizeof(core->opts));
260 core->cpu = 0;
261 core->board = 0;
262 core->init = _GBCoreInit;
263 core->deinit = _GBCoreDeinit;
264 core->platform = _GBCorePlatform;
265 core->setSync = _GBCoreSetSync;
266 core->loadConfig = _GBCoreLoadConfig;
267 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
268 core->setVideoBuffer = _GBCoreSetVideoBuffer;
269 core->getVideoBuffer = _GBCoreGetVideoBuffer;
270 core->getAudioChannel = _GBCoreGetAudioChannel;
271 core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
272 core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
273 core->setAVStream = _GBCoreSetAVStream;
274 core->isROM = GBIsROM;
275 core->loadROM = _GBCoreLoadROM;
276 core->loadBIOS = _GBCoreLoadBIOS;
277 core->loadSave = _GBCoreLoadSave;
278 core->loadPatch = _GBCoreLoadPatch;
279 core->unloadROM = _GBCoreUnloadROM;
280 core->reset = _GBCoreReset;
281 core->runFrame = _GBCoreRunFrame;
282 core->runLoop = _GBCoreRunLoop;
283 core->step = _GBCoreStep;
284 core->loadState = _GBCoreLoadState;
285 core->saveState = _GBCoreSaveState;
286 core->setKeys = _GBCoreSetKeys;
287 core->addKeys = _GBCoreAddKeys;
288 core->clearKeys = _GBCoreClearKeys;
289 core->frameCounter = _GBCoreFrameCounter;
290 core->frameCycles = _GBCoreFrameCycles;
291 core->frequency = _GBCoreFrequency;
292 core->getGameTitle = _GBCoreGetGameTitle;
293 core->getGameCode = _GBCoreGetGameCode;
294 core->setRTC = _GBCoreSetRTC;
295 core->setRotation = _GBCoreSetRotation;
296 core->setRumble = _GBCoreSetRumble;
297 return core;
298}