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 bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
100 return GBLoadROM(core->board, vf);
101}
102
103static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
104 return GBLoadSave(core->board, vf);
105}
106
107static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
108 if (!vf) {
109 return false;
110 }
111 struct Patch patch;
112 if (!loadPatch(vf, &patch)) {
113 return false;
114 }
115 GBApplyPatch(core->board, &patch);
116 return true;
117}
118
119static void _GBCoreUnloadROM(struct mCore* core) {
120 return GBUnloadROM(core->board);
121}
122
123static void _GBCoreReset(struct mCore* core) {
124 struct GBCore* gbcore = (struct GBCore*) core;
125 struct GB* gb = (struct GB*) core->board;
126 GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
127 LR35902Reset(core->cpu);
128}
129
130static void _GBCoreRunFrame(struct mCore* core) {
131 struct GB* gb = core->board;
132 int32_t frameCounter = gb->video.frameCounter;
133 while (gb->video.frameCounter == frameCounter) {
134 LR35902Run(core->cpu);
135 }
136}
137
138static void _GBCoreRunLoop(struct mCore* core) {
139 LR35902Run(core->cpu);
140}
141
142static void _GBCoreStep(struct mCore* core) {
143 LR35902Tick(core->cpu);
144}
145
146static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
147 UNUSED(core);
148 UNUSED(vf);
149 UNUSED(flags);
150 // TODO
151 return false;
152}
153
154static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
155 UNUSED(core);
156 UNUSED(vf);
157 UNUSED(flags);
158 // TODO
159 return false;
160}
161
162static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
163 struct GBCore* gbcore = (struct GBCore*) core;
164 gbcore->keys = keys;
165}
166
167static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
168 struct GBCore* gbcore = (struct GBCore*) core;
169 gbcore->keys |= keys;
170}
171
172static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
173 struct GBCore* gbcore = (struct GBCore*) core;
174 gbcore->keys &= ~keys;
175}
176
177static int32_t _GBCoreFrameCounter(struct mCore* core) {
178 struct GB* gb = core->board;
179 return gb->video.frameCounter;
180}
181
182static int32_t _GBCoreFrameCycles(struct mCore* core) {
183 UNUSED(core);
184 return GB_VIDEO_TOTAL_LENGTH;
185}
186
187static int32_t _GBCoreFrequency(struct mCore* core) {
188 UNUSED(core);
189 // TODO: GB differences
190 return DMG_LR35902_FREQUENCY;
191}
192
193static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
194 struct GB* gb = core->board;
195 gb->memory.rtc = rtc;
196}
197
198struct mCore* GBCoreCreate(void) {
199 struct GBCore* gbcore = malloc(sizeof(*gbcore));
200 struct mCore* core = &gbcore->d;
201 memset(&core->opts, 0, sizeof(core->opts));
202 core->cpu = 0;
203 core->board = 0;
204 core->init = _GBCoreInit;
205 core->deinit = _GBCoreDeinit;
206 core->setSync = _GBCoreSetSync;
207 core->loadConfig = _GBCoreLoadConfig;
208 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
209 core->setVideoBuffer = _GBCoreSetVideoBuffer;
210 core->getVideoBuffer = _GBCoreGetVideoBuffer;
211 core->getAudioChannel = _GBCoreGetAudioChannel;
212 core->isROM = GBIsROM;
213 core->loadROM = _GBCoreLoadROM;
214 core->loadSave = _GBCoreLoadSave;
215 core->loadPatch = _GBCoreLoadPatch;
216 core->unloadROM = _GBCoreUnloadROM;
217 core->reset = _GBCoreReset;
218 core->runFrame = _GBCoreRunFrame;
219 core->runLoop = _GBCoreRunLoop;
220 core->step = _GBCoreStep;
221 core->loadState = _GBCoreLoadState;
222 core->saveState = _GBCoreSaveState;
223 core->setKeys = _GBCoreSetKeys;
224 core->addKeys = _GBCoreAddKeys;
225 core->clearKeys = _GBCoreClearKeys;
226 core->frameCounter = _GBCoreFrameCounter;
227 core->frameCycles = _GBCoreFrameCycles;
228 core->frequency = _GBCoreFrequency;
229 core->setRTC = _GBCoreSetRTC;
230 return core;
231}