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