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 _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
174 if (!vf) {
175 return false;
176 }
177 struct Patch patch;
178 if (!loadPatch(vf, &patch)) {
179 return false;
180 }
181 GBApplyPatch(core->board, &patch);
182 return true;
183}
184
185static void _GBCoreUnloadROM(struct mCore* core) {
186 return GBUnloadROM(core->board);
187}
188
189static void _GBCoreReset(struct mCore* core) {
190 struct GBCore* gbcore = (struct GBCore*) core;
191 struct GB* gb = (struct GB*) core->board;
192 if (gbcore->renderer.outputBuffer) {
193 GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
194 }
195 LR35902Reset(core->cpu);
196}
197
198static void _GBCoreRunFrame(struct mCore* core) {
199 struct GB* gb = core->board;
200 int32_t frameCounter = gb->video.frameCounter;
201 while (gb->video.frameCounter == frameCounter) {
202 LR35902Run(core->cpu);
203 }
204}
205
206static void _GBCoreRunLoop(struct mCore* core) {
207 LR35902Run(core->cpu);
208}
209
210static void _GBCoreStep(struct mCore* core) {
211 struct LR35902Core* cpu = core->cpu;
212 do {
213 LR35902Tick(cpu);
214 } while (cpu->executionState != LR35902_CORE_FETCH);
215}
216
217static size_t _GBCoreStateSize(struct mCore* core) {
218 UNUSED(core);
219 return sizeof(struct GBSerializedState);
220}
221
222static bool _GBCoreLoadState(struct mCore* core, const void* state) {
223 return GBDeserialize(core->board, state);
224}
225
226static bool _GBCoreSaveState(struct mCore* core, void* state) {
227 return GBSerialize(core->board, state);
228}
229
230static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
231 struct GBCore* gbcore = (struct GBCore*) core;
232 gbcore->keys = keys;
233}
234
235static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
236 struct GBCore* gbcore = (struct GBCore*) core;
237 gbcore->keys |= keys;
238}
239
240static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
241 struct GBCore* gbcore = (struct GBCore*) core;
242 gbcore->keys &= ~keys;
243}
244
245static int32_t _GBCoreFrameCounter(struct mCore* core) {
246 struct GB* gb = core->board;
247 return gb->video.frameCounter;
248}
249
250static int32_t _GBCoreFrameCycles(struct mCore* core) {
251 UNUSED(core);
252 return GB_VIDEO_TOTAL_LENGTH;
253}
254
255static int32_t _GBCoreFrequency(struct mCore* core) {
256 UNUSED(core);
257 // TODO: GB differences
258 return DMG_LR35902_FREQUENCY;
259}
260
261static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
262 GBGetGameTitle(core->board, title);
263}
264
265static void _GBCoreGetGameCode(struct mCore* core, char* title) {
266 GBGetGameCode(core->board, title);
267}
268
269static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
270 struct GB* gb = core->board;
271 gb->memory.rtc = rtc;
272}
273
274static void _GBCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
275 struct GB* gb = core->board;
276 gb->memory.rotation = rotation;
277}
278
279static void _GBCoreSetRumble(struct mCore* core, struct mRumble* rumble) {
280 struct GB* gb = core->board;
281 gb->memory.rumble = rumble;
282}
283
284static uint32_t _GBCoreBusRead8(struct mCore* core, uint32_t address) {
285 struct LR35902Core* cpu = core->cpu;
286 return cpu->memory.load8(cpu, address);
287}
288
289static uint32_t _GBCoreBusRead16(struct mCore* core, uint32_t address) {
290 struct LR35902Core* cpu = core->cpu;
291 return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8);
292}
293
294static uint32_t _GBCoreBusRead32(struct mCore* core, uint32_t address) {
295 struct LR35902Core* cpu = core->cpu;
296 return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8) |
297 (cpu->memory.load8(cpu, address + 2) << 16) | (cpu->memory.load8(cpu, address + 3) << 24);
298}
299
300static void _GBCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
301 struct LR35902Core* cpu = core->cpu;
302 cpu->memory.store8(cpu, address, value);
303}
304
305static void _GBCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
306 struct LR35902Core* cpu = core->cpu;
307 cpu->memory.store8(cpu, address, value);
308 cpu->memory.store8(cpu, address + 1, value >> 8);
309}
310
311static void _GBCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
312 struct LR35902Core* cpu = core->cpu;
313 cpu->memory.store8(cpu, address, value);
314 cpu->memory.store8(cpu, address + 1, value >> 8);
315 cpu->memory.store8(cpu, address + 2, value >> 16);
316 cpu->memory.store8(cpu, address + 3, value >> 24);
317}
318
319static uint32_t _GBCoreRawRead8(struct mCore* core, uint32_t address) {
320 struct LR35902Core* cpu = core->cpu;
321 return GBLoad8(cpu, address);
322}
323
324static uint32_t _GBCoreRawRead16(struct mCore* core, uint32_t address) {
325 struct LR35902Core* cpu = core->cpu;
326 return GBLoad8(cpu, address) | (GBLoad8(cpu, address + 1) << 8);
327}
328
329static uint32_t _GBCoreRawRead32(struct mCore* core, uint32_t address) {
330 struct LR35902Core* cpu = core->cpu;
331 return GBLoad8(cpu, address) | (GBLoad8(cpu, address + 1) << 8) |
332 (GBLoad8(cpu, address + 2) << 16) | (GBLoad8(cpu, address + 3) << 24);
333}
334
335static void _GBCoreRawWrite8(struct mCore* core, uint32_t address, uint8_t value) {
336 struct LR35902Core* cpu = core->cpu;
337 GBPatch8(cpu, address, value, NULL);
338}
339
340static void _GBCoreRawWrite16(struct mCore* core, uint32_t address, uint16_t value) {
341 struct LR35902Core* cpu = core->cpu;
342 GBPatch8(cpu, address, value, NULL);
343 GBPatch8(cpu, address + 1, value >> 8, NULL);
344}
345
346static void _GBCoreRawWrite32(struct mCore* core, uint32_t address, uint32_t value) {
347 struct LR35902Core* cpu = core->cpu;
348 GBPatch8(cpu, address, value, NULL);
349 GBPatch8(cpu, address + 1, value >> 8, NULL);
350 GBPatch8(cpu, address + 2, value >> 16, NULL);
351 GBPatch8(cpu, address + 3, value >> 24, NULL);
352}
353
354static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
355 UNUSED(core);
356 switch (type) {
357#ifdef USE_CLI_DEBUGGER
358 case DEBUGGER_CLI:
359 return true;
360#endif
361 default:
362 return false;
363 }
364}
365
366static struct mDebuggerPlatform* _GBCoreDebuggerPlatform(struct mCore* core) {
367 struct GBCore* gbcore = (struct GBCore*) core;
368 if (!gbcore->debuggerPlatform) {
369 gbcore->debuggerPlatform = LR35902DebuggerPlatformCreate();
370 }
371 return gbcore->debuggerPlatform;
372}
373
374static struct CLIDebuggerSystem* _GBCoreCliDebuggerSystem(struct mCore* core) {
375#ifdef USE_CLI_DEBUGGER
376 return GBCLIDebuggerCreate(core);
377#else
378 UNUSED(core);
379 return NULL;
380#endif
381}
382
383static void _GBCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
384 struct LR35902Core* cpu = core->cpu;
385 if (core->debugger) {
386 LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
387 }
388 cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
389 LR35902HotplugAttach(cpu, CPU_COMPONENT_DEBUGGER);
390 core->debugger = debugger;
391}
392
393static void _GBCoreDetachDebugger(struct mCore* core) {
394 struct LR35902Core* cpu = core->cpu;
395 LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
396 cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
397 core->debugger = NULL;
398}
399
400static struct mCheatDevice* _GBCoreCheatDevice(struct mCore* core) {
401 struct GBCore* gbcore = (struct GBCore*) core;
402 if (!gbcore->cheatDevice) {
403 gbcore->cheatDevice = GBCheatDeviceCreate();
404 ((struct LR35902Core*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbcore->cheatDevice->d;
405 LR35902HotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
406 gbcore->cheatDevice->p = core;
407 }
408 return gbcore->cheatDevice;
409}
410
411struct mCore* GBCoreCreate(void) {
412 struct GBCore* gbcore = malloc(sizeof(*gbcore));
413 struct mCore* core = &gbcore->d;
414 memset(&core->opts, 0, sizeof(core->opts));
415 core->cpu = NULL;
416 core->board = NULL;
417 core->debugger = NULL;
418 core->init = _GBCoreInit;
419 core->deinit = _GBCoreDeinit;
420 core->platform = _GBCorePlatform;
421 core->setSync = _GBCoreSetSync;
422 core->loadConfig = _GBCoreLoadConfig;
423 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
424 core->setVideoBuffer = _GBCoreSetVideoBuffer;
425 core->getVideoBuffer = _GBCoreGetVideoBuffer;
426 core->getAudioChannel = _GBCoreGetAudioChannel;
427 core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
428 core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
429 core->setAVStream = _GBCoreSetAVStream;
430 core->isROM = GBIsROM;
431 core->loadROM = _GBCoreLoadROM;
432 core->loadBIOS = _GBCoreLoadBIOS;
433 core->loadSave = _GBCoreLoadSave;
434 core->loadPatch = _GBCoreLoadPatch;
435 core->unloadROM = _GBCoreUnloadROM;
436 core->reset = _GBCoreReset;
437 core->runFrame = _GBCoreRunFrame;
438 core->runLoop = _GBCoreRunLoop;
439 core->step = _GBCoreStep;
440 core->stateSize = _GBCoreStateSize;
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}