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 gb->keySource = &gbcore->keys;
40
41#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
42 mDirectorySetInit(&core->dirs);
43#endif
44
45 return true;
46}
47
48static void _GBCoreDeinit(struct mCore* core) {
49 LR35902Deinit(core->cpu);
50 GBDestroy(core->board);
51 mappedMemoryFree(core->cpu, sizeof(struct LR35902Core));
52 mappedMemoryFree(core->board, sizeof(struct GB));
53#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
54 mDirectorySetDeinit(&core->dirs);
55#endif
56}
57
58static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
59 struct GB* gb = core->board;
60 gb->sync = sync;
61}
62
63static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
64 UNUSED(core);
65 UNUSED(config);
66 // TODO
67}
68
69static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
70 UNUSED(core);
71 *width = GB_VIDEO_HORIZONTAL_PIXELS;
72 *height = GB_VIDEO_VERTICAL_PIXELS;
73}
74
75static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
76 struct GBCore* gbcore = (struct GBCore*) core;
77 gbcore->renderer.outputBuffer = buffer;
78 gbcore->renderer.outputBufferStride = stride;
79}
80
81static void _GBCoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
82 struct GBCore* gbcore = (struct GBCore*) core;
83 *buffer = gbcore->renderer.outputBuffer;
84 *stride = gbcore->renderer.outputBufferStride;
85}
86
87static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
88 struct GB* gb = core->board;
89 switch (ch) {
90 case 0:
91 return gb->audio.left;
92 case 1:
93 return gb->audio.right;
94 default:
95 return NULL;
96 }
97}
98
99static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
100 // TODO
101}
102
103static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
104 return GBLoadROM(core->board, vf);
105}
106
107static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
108 return GBLoadSave(core->board, vf);
109}
110
111static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
112 if (!vf) {
113 return false;
114 }
115 struct Patch patch;
116 if (!loadPatch(vf, &patch)) {
117 return false;
118 }
119 GBApplyPatch(core->board, &patch);
120 return true;
121}
122
123static void _GBCoreUnloadROM(struct mCore* core) {
124 return GBUnloadROM(core->board);
125}
126
127static void _GBCoreReset(struct mCore* core) {
128 struct GBCore* gbcore = (struct GBCore*) core;
129 struct GB* gb = (struct GB*) core->board;
130 GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
131 LR35902Reset(core->cpu);
132}
133
134static void _GBCoreRunFrame(struct mCore* core) {
135 struct GB* gb = core->board;
136 int32_t frameCounter = gb->video.frameCounter;
137 while (gb->video.frameCounter == frameCounter) {
138 LR35902Run(core->cpu);
139 }
140}
141
142static void _GBCoreRunLoop(struct mCore* core) {
143 LR35902Run(core->cpu);
144}
145
146static void _GBCoreStep(struct mCore* core) {
147 LR35902Tick(core->cpu);
148}
149
150static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
151 UNUSED(core);
152 UNUSED(vf);
153 UNUSED(flags);
154 // TODO
155 return false;
156}
157
158static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
159 UNUSED(core);
160 UNUSED(vf);
161 UNUSED(flags);
162 // TODO
163 return false;
164}
165
166static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
167 struct GBCore* gbcore = (struct GBCore*) core;
168 gbcore->keys = keys;
169}
170
171static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
172 struct GBCore* gbcore = (struct GBCore*) core;
173 gbcore->keys |= keys;
174}
175
176static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
177 struct GBCore* gbcore = (struct GBCore*) core;
178 gbcore->keys &= ~keys;
179}
180
181static int32_t _GBCoreFrameCounter(struct mCore* core) {
182 struct GB* gb = core->board;
183 return gb->video.frameCounter;
184}
185
186static int32_t _GBCoreFrameCycles(struct mCore* core) {
187 UNUSED(core);
188 return GB_VIDEO_TOTAL_LENGTH;
189}
190
191static int32_t _GBCoreFrequency(struct mCore* core) {
192 UNUSED(core);
193 // TODO: GB differences
194 return DMG_LR35902_FREQUENCY;
195}
196
197static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
198 GBGetGameTitle(core->board, title);
199}
200
201static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
202 struct GB* gb = core->board;
203 gb->memory.rtc = rtc;
204}
205
206struct mCore* GBCoreCreate(void) {
207 struct GBCore* gbcore = malloc(sizeof(*gbcore));
208 struct mCore* core = &gbcore->d;
209 memset(&core->opts, 0, sizeof(core->opts));
210 core->cpu = 0;
211 core->board = 0;
212 core->init = _GBCoreInit;
213 core->deinit = _GBCoreDeinit;
214 core->setSync = _GBCoreSetSync;
215 core->loadConfig = _GBCoreLoadConfig;
216 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
217 core->setVideoBuffer = _GBCoreSetVideoBuffer;
218 core->getVideoBuffer = _GBCoreGetVideoBuffer;
219 core->getAudioChannel = _GBCoreGetAudioChannel;
220 core->setAVStream = _GBCoreSetAVStream;
221 core->isROM = GBIsROM;
222 core->loadROM = _GBCoreLoadROM;
223 core->loadSave = _GBCoreLoadSave;
224 core->loadPatch = _GBCoreLoadPatch;
225 core->unloadROM = _GBCoreUnloadROM;
226 core->reset = _GBCoreReset;
227 core->runFrame = _GBCoreRunFrame;
228 core->runLoop = _GBCoreRunLoop;
229 core->step = _GBCoreStep;
230 core->loadState = _GBCoreLoadState;
231 core->saveState = _GBCoreSaveState;
232 core->setKeys = _GBCoreSetKeys;
233 core->addKeys = _GBCoreAddKeys;
234 core->clearKeys = _GBCoreClearKeys;
235 core->frameCounter = _GBCoreFrameCounter;
236 core->frameCycles = _GBCoreFrameCycles;
237 core->frequency = _GBCoreFrequency;
238 core->getGameTitle = _GBCoreGetGameTitle;
239 core->setRTC = _GBCoreSetRTC;
240 return core;
241}