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/cli.h"
10#include "gb/gb.h"
11#include "gb/renderers/software.h"
12#include "lr35902/debugger.h"
13#include "util/memory.h"
14#include "util/patch.h"
15
16struct GBCore {
17 struct mCore d;
18 struct GBVideoSoftwareRenderer renderer;
19 uint8_t keys;
20 struct mCPUComponent* components[CPU_COMPONENT_MAX];
21 struct mDebuggerPlatform* debuggerPlatform;
22};
23
24static bool _GBCoreInit(struct mCore* core) {
25 struct GBCore* gbcore = (struct GBCore*) core;
26
27 struct LR35902Core* cpu = anonymousMemoryMap(sizeof(struct LR35902Core));
28 struct GB* gb = anonymousMemoryMap(sizeof(struct GB));
29 if (!cpu || !gb) {
30 free(cpu);
31 free(gb);
32 return false;
33 }
34 core->cpu = cpu;
35 core->board = gb;
36 gbcore->debuggerPlatform = NULL;
37
38 GBCreate(gb);
39 memset(gbcore->components, 0, sizeof(gbcore->components));
40 LR35902SetComponents(cpu, &gb->d, CPU_COMPONENT_MAX, gbcore->components);
41 LR35902Init(cpu);
42
43 GBVideoSoftwareRendererCreate(&gbcore->renderer);
44
45 gbcore->keys = 0;
46 gb->keySource = &gbcore->keys;
47
48#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
49 mDirectorySetInit(&core->dirs);
50#endif
51
52 return true;
53}
54
55static void _GBCoreDeinit(struct mCore* core) {
56 LR35902Deinit(core->cpu);
57 GBDestroy(core->board);
58 mappedMemoryFree(core->cpu, sizeof(struct LR35902Core));
59 mappedMemoryFree(core->board, sizeof(struct GB));
60#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
61 mDirectorySetDeinit(&core->dirs);
62#endif
63 free(core);
64}
65
66static enum mPlatform _GBCorePlatform(struct mCore* core) {
67 UNUSED(core);
68 return PLATFORM_GB;
69}
70
71static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
72 struct GB* gb = core->board;
73 gb->sync = sync;
74}
75
76static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
77 UNUSED(config);
78
79 struct GB* gb = core->board;
80 if (core->opts.mute) {
81 gb->audio.masterVolume = 0;
82 } else {
83 gb->audio.masterVolume = core->opts.volume;
84 }
85 gb->video.frameskip = core->opts.frameskip;
86}
87
88static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
89 UNUSED(core);
90 *width = GB_VIDEO_HORIZONTAL_PIXELS;
91 *height = GB_VIDEO_VERTICAL_PIXELS;
92}
93
94static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
95 struct GBCore* gbcore = (struct GBCore*) core;
96 gbcore->renderer.outputBuffer = buffer;
97 gbcore->renderer.outputBufferStride = stride;
98}
99
100static void _GBCoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
101 struct GBCore* gbcore = (struct GBCore*) core;
102 *buffer = gbcore->renderer.outputBuffer;
103 *stride = gbcore->renderer.outputBufferStride;
104}
105
106static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
107 struct GB* gb = core->board;
108 switch (ch) {
109 case 0:
110 return gb->audio.left;
111 case 1:
112 return gb->audio.right;
113 default:
114 return NULL;
115 }
116}
117
118static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
119 struct GB* gb = core->board;
120 GBAudioResizeBuffer(&gb->audio, samples);
121}
122
123static size_t _GBCoreGetAudioBufferSize(struct mCore* core) {
124 struct GB* gb = core->board;
125 return gb->audio.samples;
126}
127
128static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
129 struct GB* gb = core->board;
130 gb->stream = stream;
131 if (stream && stream->videoDimensionsChanged) {
132 stream->videoDimensionsChanged(stream, GB_VIDEO_HORIZONTAL_PIXELS, GB_VIDEO_VERTICAL_PIXELS);
133 }
134}
135
136static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
137 return GBLoadROM(core->board, vf);
138}
139
140static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
141 UNUSED(core);
142 UNUSED(vf);
143 UNUSED(type);
144 // TODO
145 return false;
146}
147
148static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
149 return GBLoadSave(core->board, vf);
150}
151
152static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
153 if (!vf) {
154 return false;
155 }
156 struct Patch patch;
157 if (!loadPatch(vf, &patch)) {
158 return false;
159 }
160 GBApplyPatch(core->board, &patch);
161 return true;
162}
163
164static void _GBCoreUnloadROM(struct mCore* core) {
165 return GBUnloadROM(core->board);
166}
167
168static void _GBCoreReset(struct mCore* core) {
169 struct GBCore* gbcore = (struct GBCore*) core;
170 struct GB* gb = (struct GB*) core->board;
171 if (gbcore->renderer.outputBuffer) {
172 GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
173 }
174 LR35902Reset(core->cpu);
175}
176
177static void _GBCoreRunFrame(struct mCore* core) {
178 struct GB* gb = core->board;
179 int32_t frameCounter = gb->video.frameCounter;
180 while (gb->video.frameCounter == frameCounter) {
181 LR35902Run(core->cpu);
182 }
183}
184
185static void _GBCoreRunLoop(struct mCore* core) {
186 LR35902Run(core->cpu);
187}
188
189static void _GBCoreStep(struct mCore* core) {
190 LR35902Tick(core->cpu);
191}
192
193static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
194 UNUSED(core);
195 UNUSED(vf);
196 UNUSED(flags);
197 // TODO
198 return false;
199}
200
201static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
202 UNUSED(core);
203 UNUSED(vf);
204 UNUSED(flags);
205 // TODO
206 return false;
207}
208
209static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
210 struct GBCore* gbcore = (struct GBCore*) core;
211 gbcore->keys = keys;
212}
213
214static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
215 struct GBCore* gbcore = (struct GBCore*) core;
216 gbcore->keys |= keys;
217}
218
219static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
220 struct GBCore* gbcore = (struct GBCore*) core;
221 gbcore->keys &= ~keys;
222}
223
224static int32_t _GBCoreFrameCounter(struct mCore* core) {
225 struct GB* gb = core->board;
226 return gb->video.frameCounter;
227}
228
229static int32_t _GBCoreFrameCycles(struct mCore* core) {
230 UNUSED(core);
231 return GB_VIDEO_TOTAL_LENGTH;
232}
233
234static int32_t _GBCoreFrequency(struct mCore* core) {
235 UNUSED(core);
236 // TODO: GB differences
237 return DMG_LR35902_FREQUENCY;
238}
239
240static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
241 GBGetGameTitle(core->board, title);
242}
243
244static void _GBCoreGetGameCode(struct mCore* core, char* title) {
245 GBGetGameCode(core->board, title);
246}
247
248static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
249 struct GB* gb = core->board;
250 gb->memory.rtc = rtc;
251}
252
253static void _GBCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
254 struct GB* gb = core->board;
255 gb->memory.rotation = rotation;
256}
257
258static void _GBCoreSetRumble(struct mCore* core, struct mRumble* rumble) {
259 struct GB* gb = core->board;
260 gb->memory.rumble = rumble;
261}
262
263static uint32_t _GBCoreBusRead8(struct mCore* core, uint32_t address) {
264 struct LR35902Core* cpu = core->cpu;
265 return cpu->memory.load8(cpu, address);
266}
267
268static uint32_t _GBCoreBusRead16(struct mCore* core, uint32_t address) {
269 struct LR35902Core* cpu = core->cpu;
270 return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8);
271}
272
273static uint32_t _GBCoreBusRead32(struct mCore* core, uint32_t address) {
274 struct LR35902Core* cpu = core->cpu;
275 return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8) |
276 (cpu->memory.load8(cpu, address + 2) << 16) | (cpu->memory.load8(cpu, address + 3) << 24);
277}
278
279static void _GBCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
280 struct LR35902Core* cpu = core->cpu;
281 cpu->memory.store8(cpu, address, value);
282}
283
284static void _GBCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
285 struct LR35902Core* cpu = core->cpu;
286 cpu->memory.store8(cpu, address, value);
287 cpu->memory.store8(cpu, address + 1, value >> 8);
288}
289
290static void _GBCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
291 struct LR35902Core* cpu = core->cpu;
292 cpu->memory.store8(cpu, address, value);
293 cpu->memory.store8(cpu, address + 1, value >> 8);
294 cpu->memory.store8(cpu, address + 2, value >> 16);
295 cpu->memory.store8(cpu, address + 3, value >> 24);
296}
297
298static uint32_t _GBCoreRawRead8(struct mCore* core, uint32_t address) {
299 struct LR35902Core* cpu = core->cpu;
300 return GBLoad8(cpu, address);
301}
302
303static uint32_t _GBCoreRawRead16(struct mCore* core, uint32_t address) {
304 struct LR35902Core* cpu = core->cpu;
305 return GBLoad8(cpu, address) | (GBLoad8(cpu, address + 1) << 8);
306}
307
308static uint32_t _GBCoreRawRead32(struct mCore* core, uint32_t address) {
309 struct LR35902Core* cpu = core->cpu;
310 return GBLoad8(cpu, address) | (GBLoad8(cpu, address + 1) << 8) |
311 (GBLoad8(cpu, address + 2) << 16) | (GBLoad8(cpu, address + 3) << 24);
312}
313
314static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
315 UNUSED(core);
316 switch (type) {
317#ifdef USE_CLI_DEBUGGER
318 case DEBUGGER_CLI:
319 return true;
320#endif
321 default:
322 return false;
323 }
324}
325
326static struct mDebuggerPlatform* _GBCoreDebuggerPlatform(struct mCore* core) {
327 struct GBCore* gbcore = (struct GBCore*) core;
328 if (!gbcore->debuggerPlatform) {
329 gbcore->debuggerPlatform = LR35902DebuggerPlatformCreate();
330 }
331 return gbcore->debuggerPlatform;
332}
333
334static struct CLIDebuggerSystem* _GBCoreCliDebuggerSystem(struct mCore* core) {
335#ifdef USE_CLI_DEBUGGER
336 return GBCLIDebuggerCreate(core);
337#else
338 UNUSED(core);
339 return NULL;
340#endif
341}
342
343static void _GBCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
344 struct LR35902Core* cpu = core->cpu;
345 if (core->debugger) {
346 LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
347 }
348 cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
349 LR35902HotplugAttach(cpu, CPU_COMPONENT_DEBUGGER);
350 core->debugger = debugger;
351}
352
353static void _GBCoreDetachDebugger(struct mCore* core) {
354 struct LR35902Core* cpu = core->cpu;
355 LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
356 cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
357 core->debugger = NULL;
358}
359
360struct mCore* GBCoreCreate(void) {
361 struct GBCore* gbcore = malloc(sizeof(*gbcore));
362 struct mCore* core = &gbcore->d;
363 memset(&core->opts, 0, sizeof(core->opts));
364 core->cpu = NULL;
365 core->board = NULL;
366 core->debugger = NULL;
367 core->init = _GBCoreInit;
368 core->deinit = _GBCoreDeinit;
369 core->platform = _GBCorePlatform;
370 core->setSync = _GBCoreSetSync;
371 core->loadConfig = _GBCoreLoadConfig;
372 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
373 core->setVideoBuffer = _GBCoreSetVideoBuffer;
374 core->getVideoBuffer = _GBCoreGetVideoBuffer;
375 core->getAudioChannel = _GBCoreGetAudioChannel;
376 core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
377 core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
378 core->setAVStream = _GBCoreSetAVStream;
379 core->isROM = GBIsROM;
380 core->loadROM = _GBCoreLoadROM;
381 core->loadBIOS = _GBCoreLoadBIOS;
382 core->loadSave = _GBCoreLoadSave;
383 core->loadPatch = _GBCoreLoadPatch;
384 core->unloadROM = _GBCoreUnloadROM;
385 core->reset = _GBCoreReset;
386 core->runFrame = _GBCoreRunFrame;
387 core->runLoop = _GBCoreRunLoop;
388 core->step = _GBCoreStep;
389 core->loadState = _GBCoreLoadState;
390 core->saveState = _GBCoreSaveState;
391 core->setKeys = _GBCoreSetKeys;
392 core->addKeys = _GBCoreAddKeys;
393 core->clearKeys = _GBCoreClearKeys;
394 core->frameCounter = _GBCoreFrameCounter;
395 core->frameCycles = _GBCoreFrameCycles;
396 core->frequency = _GBCoreFrequency;
397 core->getGameTitle = _GBCoreGetGameTitle;
398 core->getGameCode = _GBCoreGetGameCode;
399 core->setRTC = _GBCoreSetRTC;
400 core->setRotation = _GBCoreSetRotation;
401 core->setRumble = _GBCoreSetRumble;
402 core->busRead8 = _GBCoreBusRead8;
403 core->busRead16 = _GBCoreBusRead16;
404 core->busRead32 = _GBCoreBusRead32;
405 core->busWrite8 = _GBCoreBusWrite8;
406 core->busWrite16 = _GBCoreBusWrite16;
407 core->busWrite32 = _GBCoreBusWrite32;
408 core->rawRead8 = _GBCoreRawRead8;
409 core->rawRead16 = _GBCoreRawRead16;
410 core->rawRead32 = _GBCoreRawRead32;
411 core->rawWrite8 = NULL;
412 core->rawWrite16 = NULL;
413 core->rawWrite32 = NULL;
414 core->supportsDebuggerType = _GBCoreSupportsDebuggerType;
415 core->debuggerPlatform = _GBCoreDebuggerPlatform;
416 core->cliDebuggerSystem = _GBCoreCliDebuggerSystem;
417 core->attachDebugger = _GBCoreAttachDebugger;
418 core->detachDebugger = _GBCoreDetachDebugger;
419 return core;
420}