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