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