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