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 void _GBCoreRawWrite8(struct mCore* core, uint32_t address, uint8_t value) {
325 struct LR35902Core* cpu = core->cpu;
326 GBPatch8(cpu, address, value, NULL);
327}
328
329static void _GBCoreRawWrite16(struct mCore* core, uint32_t address, uint16_t value) {
330 struct LR35902Core* cpu = core->cpu;
331 GBPatch8(cpu, address, value, NULL);
332 GBPatch8(cpu, address + 1, value >> 8, NULL);
333}
334
335static void _GBCoreRawWrite32(struct mCore* core, uint32_t address, uint32_t value) {
336 struct LR35902Core* cpu = core->cpu;
337 GBPatch8(cpu, address, value, NULL);
338 GBPatch8(cpu, address + 1, value >> 8, NULL);
339 GBPatch8(cpu, address + 2, value >> 16, NULL);
340 GBPatch8(cpu, address + 3, value >> 24, NULL);
341}
342
343static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
344 UNUSED(core);
345 switch (type) {
346#ifdef USE_CLI_DEBUGGER
347 case DEBUGGER_CLI:
348 return true;
349#endif
350 default:
351 return false;
352 }
353}
354
355static struct mDebuggerPlatform* _GBCoreDebuggerPlatform(struct mCore* core) {
356 struct GBCore* gbcore = (struct GBCore*) core;
357 if (!gbcore->debuggerPlatform) {
358 gbcore->debuggerPlatform = LR35902DebuggerPlatformCreate();
359 }
360 return gbcore->debuggerPlatform;
361}
362
363static struct CLIDebuggerSystem* _GBCoreCliDebuggerSystem(struct mCore* core) {
364#ifdef USE_CLI_DEBUGGER
365 return GBCLIDebuggerCreate(core);
366#else
367 UNUSED(core);
368 return NULL;
369#endif
370}
371
372static void _GBCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
373 struct LR35902Core* cpu = core->cpu;
374 if (core->debugger) {
375 LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
376 }
377 cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
378 LR35902HotplugAttach(cpu, CPU_COMPONENT_DEBUGGER);
379 core->debugger = debugger;
380}
381
382static void _GBCoreDetachDebugger(struct mCore* core) {
383 struct LR35902Core* cpu = core->cpu;
384 LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
385 cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
386 core->debugger = NULL;
387}
388
389static struct mCheatDevice* _GBCoreCheatDevice(struct mCore* core) {
390 struct GBCore* gbcore = (struct GBCore*) core;
391 if (!gbcore->cheatDevice) {
392 gbcore->cheatDevice = GBCheatDeviceCreate();
393 ((struct LR35902Core*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbcore->cheatDevice->d;
394 LR35902HotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
395 gbcore->cheatDevice->p = core;
396 }
397 return gbcore->cheatDevice;
398}
399
400struct mCore* GBCoreCreate(void) {
401 struct GBCore* gbcore = malloc(sizeof(*gbcore));
402 struct mCore* core = &gbcore->d;
403 memset(&core->opts, 0, sizeof(core->opts));
404 core->cpu = NULL;
405 core->board = NULL;
406 core->debugger = NULL;
407 core->init = _GBCoreInit;
408 core->deinit = _GBCoreDeinit;
409 core->platform = _GBCorePlatform;
410 core->setSync = _GBCoreSetSync;
411 core->loadConfig = _GBCoreLoadConfig;
412 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
413 core->setVideoBuffer = _GBCoreSetVideoBuffer;
414 core->getVideoBuffer = _GBCoreGetVideoBuffer;
415 core->getAudioChannel = _GBCoreGetAudioChannel;
416 core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
417 core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
418 core->setAVStream = _GBCoreSetAVStream;
419 core->isROM = GBIsROM;
420 core->loadROM = _GBCoreLoadROM;
421 core->loadBIOS = _GBCoreLoadBIOS;
422 core->loadSave = _GBCoreLoadSave;
423 core->loadPatch = _GBCoreLoadPatch;
424 core->unloadROM = _GBCoreUnloadROM;
425 core->reset = _GBCoreReset;
426 core->runFrame = _GBCoreRunFrame;
427 core->runLoop = _GBCoreRunLoop;
428 core->step = _GBCoreStep;
429 core->loadState = _GBCoreLoadState;
430 core->saveState = _GBCoreSaveState;
431 core->setKeys = _GBCoreSetKeys;
432 core->addKeys = _GBCoreAddKeys;
433 core->clearKeys = _GBCoreClearKeys;
434 core->frameCounter = _GBCoreFrameCounter;
435 core->frameCycles = _GBCoreFrameCycles;
436 core->frequency = _GBCoreFrequency;
437 core->getGameTitle = _GBCoreGetGameTitle;
438 core->getGameCode = _GBCoreGetGameCode;
439 core->setRTC = _GBCoreSetRTC;
440 core->setRotation = _GBCoreSetRotation;
441 core->setRumble = _GBCoreSetRumble;
442 core->busRead8 = _GBCoreBusRead8;
443 core->busRead16 = _GBCoreBusRead16;
444 core->busRead32 = _GBCoreBusRead32;
445 core->busWrite8 = _GBCoreBusWrite8;
446 core->busWrite16 = _GBCoreBusWrite16;
447 core->busWrite32 = _GBCoreBusWrite32;
448 core->rawRead8 = _GBCoreRawRead8;
449 core->rawRead16 = _GBCoreRawRead16;
450 core->rawRead32 = _GBCoreRawRead32;
451 core->rawWrite8 = _GBCoreRawWrite8;
452 core->rawWrite16 = _GBCoreRawWrite16;
453 core->rawWrite32 = _GBCoreRawWrite32;
454 core->supportsDebuggerType = _GBCoreSupportsDebuggerType;
455 core->debuggerPlatform = _GBCoreDebuggerPlatform;
456 core->cliDebuggerSystem = _GBCoreCliDebuggerSystem;
457 core->attachDebugger = _GBCoreAttachDebugger;
458 core->detachDebugger = _GBCoreDetachDebugger;
459 core->cheatDevice = _GBCoreCheatDevice;
460 return core;
461}