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