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
13struct GBCore {
14 struct mCore d;
15 struct GBVideoSoftwareRenderer renderer;
16 uint8_t keys;
17};
18
19static bool _GBCoreInit(struct mCore* core) {
20 struct GBCore* gbcore = (struct GBCore*) core;
21
22 struct LR35902Core* cpu = anonymousMemoryMap(sizeof(struct LR35902Core));
23 struct GB* gb = anonymousMemoryMap(sizeof(struct GB));
24 if (!cpu || !gb) {
25 free(cpu);
26 free(gb);
27 return false;
28 }
29 core->cpu = cpu;
30 core->board = gb;
31
32 GBCreate(gb);
33 LR35902SetComponents(cpu, &gb->d, 0, 0);
34 LR35902Init(cpu);
35
36 GBVideoSoftwareRendererCreate(&gbcore->renderer);
37 GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
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 struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
81 struct GB* gb = core->board;
82 switch (ch) {
83 case 0:
84 return gb->audio.left;
85 case 1:
86 return gb->audio.right;
87 default:
88 return NULL;
89 }
90}
91
92static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
93 return GBLoadROM(core->board, vf);
94}
95
96static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
97 return GBLoadSave(core->board, vf);
98}
99
100static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
101 // TODO
102 UNUSED(core);
103 UNUSED(vf);
104 mLOG(GB, STUB, "Patches are not yet supported");
105 return false;
106}
107
108static void _GBCoreUnloadROM(struct mCore* core) {
109 return GBUnloadROM(core->board);
110}
111
112static void _GBCoreReset(struct mCore* core) {
113 LR35902Reset(core->cpu);
114}
115
116static void _GBCoreRunFrame(struct mCore* core) {
117 struct GB* gb = core->board;
118 int32_t frameCounter = gb->video.frameCounter;
119 while (gb->video.frameCounter == frameCounter) {
120 LR35902Run(core->cpu);
121 }
122}
123
124static void _GBCoreRunLoop(struct mCore* core) {
125 LR35902Run(core->cpu);
126}
127
128static void _GBCoreStep(struct mCore* core) {
129 LR35902Tick(core->cpu);
130}
131
132static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
133 UNUSED(core);
134 UNUSED(vf);
135 UNUSED(flags);
136 // TODO
137 return false;
138}
139
140static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
141 UNUSED(core);
142 UNUSED(vf);
143 UNUSED(flags);
144 // TODO
145 return false;
146}
147
148static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
149 struct GBCore* gbcore = (struct GBCore*) core;
150 gbcore->keys = keys;
151}
152
153static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
154 struct GBCore* gbcore = (struct GBCore*) core;
155 gbcore->keys |= keys;
156}
157
158static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
159 struct GBCore* gbcore = (struct GBCore*) core;
160 gbcore->keys &= ~keys;
161}
162
163static int32_t _GBCoreFrameCounter(struct mCore* core) {
164 struct GB* gb = core->board;
165 return gb->video.frameCounter;
166}
167
168static int32_t _GBCoreFrameCycles(struct mCore* core) {
169 UNUSED(core);
170 return GB_VIDEO_TOTAL_LENGTH;
171}
172
173static int32_t _GBCoreFrequency(struct mCore* core) {
174 UNUSED(core);
175 // TODO: GB differences
176 return DMG_LR35902_FREQUENCY;
177}
178
179static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
180 struct GB* gb = core->board;
181 gb->memory.rtc = rtc;
182}
183
184struct mCore* GBCoreCreate(void) {
185 struct GBCore* gbcore = malloc(sizeof(*gbcore));
186 struct mCore* core = &gbcore->d;
187 core->cpu = 0;
188 core->board = 0;
189 core->init = _GBCoreInit;
190 core->deinit = _GBCoreDeinit;
191 core->setSync = _GBCoreSetSync;
192 core->loadConfig = _GBCoreLoadConfig;
193 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
194 core->setVideoBuffer = _GBCoreSetVideoBuffer;
195 core->getAudioChannel = _GBCoreGetAudioChannel;
196 core->isROM = GBIsROM;
197 core->loadROM = _GBCoreLoadROM;
198 core->loadSave = _GBCoreLoadSave;
199 core->loadPatch = _GBCoreLoadPatch;
200 core->unloadROM = _GBCoreUnloadROM;
201 core->reset = _GBCoreReset;
202 core->runFrame = _GBCoreRunFrame;
203 core->runLoop = _GBCoreRunLoop;
204 core->step = _GBCoreStep;
205 core->loadState = _GBCoreLoadState;
206 core->saveState = _GBCoreSaveState;
207 core->setKeys = _GBCoreSetKeys;
208 core->addKeys = _GBCoreAddKeys;
209 core->clearKeys = _GBCoreClearKeys;
210 core->frameCounter = _GBCoreFrameCounter;
211 core->frameCycles = _GBCoreFrameCycles;
212 core->frequency = _GBCoreFrequency;
213 core->setRTC = _GBCoreSetRTC;
214 return core;
215}