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}
121
122static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
123 return GBLoadROM(core->board, vf);
124}
125
126static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
127 UNUSED(core);
128 UNUSED(vf);
129 UNUSED(type);
130 // TODO
131 return false;
132}
133
134static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
135 return GBLoadSave(core->board, vf);
136}
137
138static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
139 if (!vf) {
140 return false;
141 }
142 struct Patch patch;
143 if (!loadPatch(vf, &patch)) {
144 return false;
145 }
146 GBApplyPatch(core->board, &patch);
147 return true;
148}
149
150static void _GBCoreUnloadROM(struct mCore* core) {
151 return GBUnloadROM(core->board);
152}
153
154static void _GBCoreReset(struct mCore* core) {
155 struct GBCore* gbcore = (struct GBCore*) core;
156 struct GB* gb = (struct GB*) core->board;
157 if (gbcore->renderer.outputBuffer) {
158 GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
159 }
160 LR35902Reset(core->cpu);
161}
162
163static void _GBCoreRunFrame(struct mCore* core) {
164 struct GB* gb = core->board;
165 int32_t frameCounter = gb->video.frameCounter;
166 while (gb->video.frameCounter == frameCounter) {
167 LR35902Run(core->cpu);
168 }
169}
170
171static void _GBCoreRunLoop(struct mCore* core) {
172 LR35902Run(core->cpu);
173}
174
175static void _GBCoreStep(struct mCore* core) {
176 LR35902Tick(core->cpu);
177}
178
179static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
180 UNUSED(core);
181 UNUSED(vf);
182 UNUSED(flags);
183 // TODO
184 return false;
185}
186
187static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
188 UNUSED(core);
189 UNUSED(vf);
190 UNUSED(flags);
191 // TODO
192 return false;
193}
194
195static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
196 struct GBCore* gbcore = (struct GBCore*) core;
197 gbcore->keys = keys;
198}
199
200static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
201 struct GBCore* gbcore = (struct GBCore*) core;
202 gbcore->keys |= keys;
203}
204
205static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
206 struct GBCore* gbcore = (struct GBCore*) core;
207 gbcore->keys &= ~keys;
208}
209
210static int32_t _GBCoreFrameCounter(struct mCore* core) {
211 struct GB* gb = core->board;
212 return gb->video.frameCounter;
213}
214
215static int32_t _GBCoreFrameCycles(struct mCore* core) {
216 UNUSED(core);
217 return GB_VIDEO_TOTAL_LENGTH;
218}
219
220static int32_t _GBCoreFrequency(struct mCore* core) {
221 UNUSED(core);
222 // TODO: GB differences
223 return DMG_LR35902_FREQUENCY;
224}
225
226static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
227 GBGetGameTitle(core->board, title);
228}
229
230static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
231 struct GB* gb = core->board;
232 gb->memory.rtc = rtc;
233}
234
235struct mCore* GBCoreCreate(void) {
236 struct GBCore* gbcore = malloc(sizeof(*gbcore));
237 struct mCore* core = &gbcore->d;
238 memset(&core->opts, 0, sizeof(core->opts));
239 core->cpu = 0;
240 core->board = 0;
241 core->init = _GBCoreInit;
242 core->deinit = _GBCoreDeinit;
243 core->platform = _GBCorePlatform;
244 core->setSync = _GBCoreSetSync;
245 core->loadConfig = _GBCoreLoadConfig;
246 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
247 core->setVideoBuffer = _GBCoreSetVideoBuffer;
248 core->getVideoBuffer = _GBCoreGetVideoBuffer;
249 core->getAudioChannel = _GBCoreGetAudioChannel;
250 core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
251 core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
252 core->setAVStream = _GBCoreSetAVStream;
253 core->isROM = GBIsROM;
254 core->loadROM = _GBCoreLoadROM;
255 core->loadBIOS = _GBCoreLoadBIOS;
256 core->loadSave = _GBCoreLoadSave;
257 core->loadPatch = _GBCoreLoadPatch;
258 core->unloadROM = _GBCoreUnloadROM;
259 core->reset = _GBCoreReset;
260 core->runFrame = _GBCoreRunFrame;
261 core->runLoop = _GBCoreRunLoop;
262 core->step = _GBCoreStep;
263 core->loadState = _GBCoreLoadState;
264 core->saveState = _GBCoreSaveState;
265 core->setKeys = _GBCoreSetKeys;
266 core->addKeys = _GBCoreAddKeys;
267 core->clearKeys = _GBCoreClearKeys;
268 core->frameCounter = _GBCoreFrameCounter;
269 core->frameCycles = _GBCoreFrameCycles;
270 core->frequency = _GBCoreFrequency;
271 core->getGameTitle = _GBCoreGetGameTitle;
272 core->setRTC = _GBCoreSetRTC;
273 return core;
274}