src/ds/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 <mgba/ds/core.h>
7
8#include <mgba/core/cheats.h>
9#include <mgba/core/core.h>
10#include <mgba/core/log.h>
11#include <mgba/internal/arm/debugger/debugger.h>
12#include <mgba/internal/ds/ds.h>
13#include <mgba/internal/ds/extra/cli.h>
14#include <mgba/internal/ds/gx/software.h>
15#include <mgba/internal/ds/input.h>
16#include <mgba/internal/ds/renderers/software.h>
17#include <mgba-util/memory.h>
18#include <mgba-util/patch.h>
19#include <mgba-util/vfs.h>
20
21struct DSCore {
22 struct mCore d;
23 struct ARMCore* arm7;
24 struct ARMCore* arm9;
25 struct DSVideoSoftwareRenderer renderer;
26 struct DSGXSoftwareRenderer gxRenderer;
27 int keys;
28 int cursorX;
29 int cursorY;
30 bool touchDown;
31 struct mCPUComponent* components[CPU_COMPONENT_MAX];
32 struct mDebuggerPlatform* debuggerPlatform;
33 struct mCheatDevice* cheatDevice;
34};
35
36static bool _DSCoreInit(struct mCore* core) {
37 struct DSCore* dscore = (struct DSCore*) core;
38
39 struct ARMCore* arm7 = anonymousMemoryMap(sizeof(struct ARMCore));
40 struct ARMCore* arm9 = anonymousMemoryMap(sizeof(struct ARMCore));
41 struct DS* ds = anonymousMemoryMap(sizeof(struct DS));
42 if (!arm7 || !arm9 || !ds) {
43 free(arm7);
44 free(arm9);
45 free(ds);
46 return false;
47 }
48 core->cpu = arm9;
49 core->board = ds;
50 core->debugger = NULL;
51 dscore->arm7 = arm7;
52 dscore->arm9 = arm9;
53 dscore->debuggerPlatform = NULL;
54 dscore->cheatDevice = NULL;
55
56 DSCreate(ds);
57 memset(dscore->components, 0, sizeof(dscore->components));
58 ARMSetComponents(arm7, &ds->d, CPU_COMPONENT_MAX, dscore->components);
59 ARMSetComponents(arm9, &ds->d, CPU_COMPONENT_MAX, dscore->components);
60 ARMInit(arm7);
61 ARMInit(arm9);
62
63 DSVideoSoftwareRendererCreate(&dscore->renderer);
64 DSGXSoftwareRendererCreate(&dscore->gxRenderer);
65 dscore->renderer.outputBuffer = NULL;
66
67 dscore->keys = 0;
68 ds->keySource = &dscore->keys;
69 dscore->cursorX = 0;
70 ds->cursorSourceX = &dscore->cursorX;
71 dscore->cursorY = 0;
72 ds->cursorSourceY = &dscore->cursorY;
73 dscore->touchDown = false;
74 ds->touchSource = &dscore->touchDown;
75
76#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
77 mDirectorySetInit(&core->dirs);
78#endif
79
80#ifndef MINIMAL_CORE
81 core->inputInfo = &DSInputInfo; // TODO: GBInputInfo
82#endif
83
84 return true;
85}
86
87static void _DSCoreDeinit(struct mCore* core) {
88 struct DSCore* dscore = (struct DSCore*) core;
89 ARMDeinit(dscore->arm7);
90 ARMDeinit(dscore->arm9);
91 DSDestroy(core->board);
92 mappedMemoryFree(dscore->arm7, sizeof(struct ARMCore));
93 mappedMemoryFree(dscore->arm9, sizeof(struct ARMCore));
94 mappedMemoryFree(core->board, sizeof(struct DS));
95#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
96 mDirectorySetDeinit(&core->dirs);
97#endif
98
99 free(dscore->debuggerPlatform);
100 if (dscore->cheatDevice) {
101 mCheatDeviceDestroy(dscore->cheatDevice);
102 }
103 free(dscore->cheatDevice);
104 free(core);
105}
106
107static enum mPlatform _DSCorePlatform(const struct mCore* core) {
108 UNUSED(core);
109 return PLATFORM_DS;
110}
111
112static void _DSCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
113 struct DS* ds = core->board;
114 ds->sync = sync;
115}
116
117static void _DSCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
118 struct DS* ds = core->board;
119 struct VFile* bios = NULL;
120
121 mCoreConfigCopyValue(&core->config, config, "ds.bios7");
122 mCoreConfigCopyValue(&core->config, config, "ds.bios9");
123 mCoreConfigCopyValue(&core->config, config, "ds.firmware");
124}
125
126static void _DSCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
127 UNUSED(core);
128 *width = DS_VIDEO_HORIZONTAL_PIXELS;
129 *height = DS_VIDEO_VERTICAL_PIXELS * 2;
130}
131
132static void _DSCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
133 struct DSCore* dscore = (struct DSCore*) core;
134 dscore->renderer.outputBuffer = buffer;
135 dscore->renderer.outputBufferStride = stride;
136}
137
138static void _DSCoreGetPixels(struct mCore* core, const void** buffer, size_t* stride) {
139 struct DSCore* dscore = (struct DSCore*) core;
140 dscore->renderer.d.getPixels(&dscore->renderer.d, stride, buffer);
141}
142
143static void _DSCorePutPixels(struct mCore* core, const void* buffer, size_t stride) {
144 struct DSCore* dscore = (struct DSCore*) core;
145 dscore->renderer.d.putPixels(&dscore->renderer.d, stride, buffer);
146}
147
148static struct blip_t* _DSCoreGetAudioChannel(struct mCore* core, int ch) {
149 struct DS* ds = core->board;
150 switch (ch) {
151 case 0:
152 return ds->audio.left;
153 case 1:
154 return ds->audio.right;
155 default:
156 return NULL;
157 }
158}
159
160static void _DSCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
161 struct DS* ds = core->board;
162 DSAudioResizeBuffer(&ds->audio, samples);
163}
164
165static size_t _DSCoreGetAudioBufferSize(struct mCore* core) {
166 struct DS* ds = core->board;
167 return ds->audio.samples;
168}
169
170static void _DSCoreAddCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) {
171 struct DS* ds = core->board;
172 *mCoreCallbacksListAppend(&ds->coreCallbacks) = *coreCallbacks;
173}
174
175static void _DSCoreClearCoreCallbacks(struct mCore* core) {
176 struct DS* ds = core->board;
177 mCoreCallbacksListClear(&ds->coreCallbacks);
178}
179
180static void _DSCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
181 struct DS* ds = core->board;
182 ds->stream = stream;
183 if (stream && stream->videoDimensionsChanged) {
184 stream->videoDimensionsChanged(stream, DS_VIDEO_HORIZONTAL_PIXELS, DS_VIDEO_VERTICAL_PIXELS * 2);
185 }
186}
187
188static bool _DSCoreLoadROM(struct mCore* core, struct VFile* vf) {
189 return DSLoadROM(core->board, vf);
190}
191
192static bool _DSCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
193 UNUSED(type);
194 return DSLoadBIOS(core->board, vf);
195}
196
197static bool _DSCoreLoadSave(struct mCore* core, struct VFile* vf) {
198 return DSLoadSave(core->board, vf);
199}
200
201static bool _DSCoreLoadPatch(struct mCore* core, struct VFile* vf) {
202 return false;
203}
204
205static void _DSCoreUnloadROM(struct mCore* core) {
206 return DSUnloadROM(core->board);
207}
208
209static void _DSCoreChecksum(const struct mCore* core, void* data, enum mCoreChecksumType type) {
210}
211
212static void _DSCoreReset(struct mCore* core) {
213 struct DSCore* dscore = (struct DSCore*) core;
214 struct DS* ds = (struct DS*) core->board;
215
216 if (dscore->renderer.outputBuffer) {
217 struct DSVideoRenderer* renderer = &dscore->renderer.d;
218 DSVideoAssociateRenderer(&ds->video, renderer);
219
220 struct DSGXRenderer* gxRenderer = &dscore->gxRenderer.d;
221 DSGXAssociateRenderer(&ds->gx, gxRenderer);
222 }
223
224#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
225 struct VFile* bios7 = NULL;
226 struct VFile* bios9 = NULL;
227 struct VFile* firm = NULL;
228 if (core->opts.useBios) {
229 bool found7 = false;
230 bool found9 = false;
231 bool foundFirm = false;
232
233 if (!found7) {
234 const char* configPath = mCoreConfigGetValue(&core->config, "ds.bios7");
235 bios7 = VFileOpen(configPath, O_RDONLY);
236 if (bios7 && DSIsBIOS7(bios7)) {
237 found7 = true;
238 } else if (bios7) {
239 bios7->close(bios7);
240 bios7 = NULL;
241 }
242 }
243
244 if (!found9) {
245 const char* configPath = mCoreConfigGetValue(&core->config, "ds.bios9");
246 bios9 = VFileOpen(configPath, O_RDONLY);
247 if (bios9 && DSIsBIOS9(bios9)) {
248 found9 = true;
249 } else if (bios9) {
250 bios9->close(bios9);
251 bios9 = NULL;
252 }
253 }
254
255 if (!foundFirm) {
256 const char* configPath = mCoreConfigGetValue(&core->config, "ds.firmware");
257 firm = VFileOpen(configPath, O_RDONLY);
258 if (firm && DSIsFirmware(firm)) {
259 foundFirm = true;
260 } else if (firm) {
261 firm->close(firm);
262 firm = NULL;
263 }
264 }
265
266 if (!found7) {
267 char path[PATH_MAX];
268 mCoreConfigDirectory(path, PATH_MAX);
269 strncat(path, PATH_SEP "ds7_bios.bin", PATH_MAX - strlen(path));
270 bios7 = VFileOpen(path, O_RDONLY);
271 }
272
273 if (!found9) {
274 char path[PATH_MAX];
275 mCoreConfigDirectory(path, PATH_MAX);
276 strncat(path, PATH_SEP "ds9_bios.bin", PATH_MAX - strlen(path));
277 bios9 = VFileOpen(path, O_RDONLY);
278 }
279
280 if (!foundFirm) {
281 char path[PATH_MAX];
282 mCoreConfigDirectory(path, PATH_MAX);
283 strncat(path, PATH_SEP "ds_firmware.bin", PATH_MAX - strlen(path));
284 firm = VFileOpen(path, O_RDWR);
285 }
286 }
287 if (bios7) {
288 DSLoadBIOS(ds, bios7);
289 }
290 if (bios9) {
291 DSLoadBIOS(ds, bios9);
292 }
293 if (firm) {
294 DSLoadFirmware(ds, firm);
295 }
296#endif
297
298 ARMReset(ds->ds7.cpu);
299 ARMReset(ds->ds9.cpu);
300}
301
302static void _DSCoreRunFrame(struct mCore* core) {
303 struct DSCore* dscore = (struct DSCore*) core;
304 struct DS* ds = core->board;
305 int32_t frameCounter = ds->video.frameCounter;
306 while (ds->video.frameCounter == frameCounter) {
307 DSRunLoop(core->board);
308 }
309}
310
311static void _DSCoreRunLoop(struct mCore* core) {
312 DSRunLoop(core->board);
313}
314
315static void _DSCoreStep(struct mCore* core) {
316 struct DSCore* dscore = (struct DSCore*) core;
317 if (core->cpu == dscore->arm9) {
318 DS9Step(core->board);
319 } else {
320 DS7Step(core->board);
321 }
322}
323
324static size_t _DSCoreStateSize(struct mCore* core) {
325 UNUSED(core);
326 return 0;
327}
328
329static bool _DSCoreLoadState(struct mCore* core, const void* state) {
330 return false;
331}
332
333static bool _DSCoreSaveState(struct mCore* core, void* state) {
334 return false;
335}
336
337static void _DSCoreSetKeys(struct mCore* core, uint32_t keys) {
338 struct DSCore* dscore = (struct DSCore*) core;
339 dscore->keys = keys;
340}
341
342static void _DSCoreAddKeys(struct mCore* core, uint32_t keys) {
343 struct DSCore* dscore = (struct DSCore*) core;
344 dscore->keys |= keys;
345}
346
347static void _DSCoreClearKeys(struct mCore* core, uint32_t keys) {
348 struct DSCore* dscore = (struct DSCore*) core;
349 dscore->keys &= ~keys;
350}
351
352static void _DSCoreSetCursorLocation(struct mCore* core, int x, int y) {
353 struct DSCore* dscore = (struct DSCore*) core;
354 dscore->cursorX = x;
355 dscore->cursorY = y - DS_VIDEO_VERTICAL_PIXELS;
356 if (dscore->cursorY < 0) {
357 dscore->cursorY = 0;
358 }
359}
360
361static void _DSCoreSetCursorDown(struct mCore* core, bool down) {
362 struct DSCore* dscore = (struct DSCore*) core;
363 dscore->touchDown = down;
364}
365
366static int32_t _DSCoreFrameCounter(const struct mCore* core) {
367 struct DS* ds = core->board;
368 return ds->video.frameCounter;
369}
370
371static int32_t _DSCoreFrameCycles(const struct mCore* core) {
372 UNUSED(core);
373 return DS_VIDEO_TOTAL_LENGTH;
374}
375
376static int32_t _DSCoreFrequency(const struct mCore* core) {
377 UNUSED(core);
378 return DS_ARM946ES_FREQUENCY;
379}
380
381static void _DSCoreGetGameTitle(const struct mCore* core, char* title) {
382 DSGetGameTitle(core->board, title);
383}
384
385static void _DSCoreGetGameCode(const struct mCore* core, char* title) {
386 DSGetGameCode(core->board, title);
387}
388
389static void _DSCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
390}
391
392static void _DSCoreSetRumble(struct mCore* core, struct mRumble* rumble) {
393 struct DS* ds = core->board;
394 ds->rumble = rumble;
395}
396
397static uint32_t _DSCoreBusRead8(struct mCore* core, uint32_t address) {
398 struct ARMCore* cpu = core->cpu;
399 return cpu->memory.load8(cpu, address, 0);
400}
401
402static uint32_t _DSCoreBusRead16(struct mCore* core, uint32_t address) {
403 struct ARMCore* cpu = core->cpu;
404 return cpu->memory.load16(cpu, address, 0);
405
406}
407
408static uint32_t _DSCoreBusRead32(struct mCore* core, uint32_t address) {
409 struct ARMCore* cpu = core->cpu;
410 return cpu->memory.load32(cpu, address, 0);
411}
412
413static void _DSCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
414 struct ARMCore* cpu = core->cpu;
415 cpu->memory.store8(cpu, address, value, 0);
416}
417
418static void _DSCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
419 struct ARMCore* cpu = core->cpu;
420 cpu->memory.store16(cpu, address, value, 0);
421}
422
423static void _DSCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
424 struct ARMCore* cpu = core->cpu;
425 cpu->memory.store32(cpu, address, value, 0);
426}
427
428static uint32_t _DSCoreRawRead8(struct mCore* core, uint32_t address, int segment) {
429 // TODO: Raw
430 struct ARMCore* cpu = core->cpu;
431 return cpu->memory.load8(cpu, address, 0);
432}
433
434static uint32_t _DSCoreRawRead16(struct mCore* core, uint32_t address, int segment) {
435 // TODO: Raw
436 struct ARMCore* cpu = core->cpu;
437 return cpu->memory.load16(cpu, address, 0);
438}
439
440static uint32_t _DSCoreRawRead32(struct mCore* core, uint32_t address, int segment) {
441 // TODO: Raw
442 struct ARMCore* cpu = core->cpu;
443 return cpu->memory.load32(cpu, address, 0);
444}
445
446static void _DSCoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
447}
448
449static void _DSCoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
450}
451
452static void _DSCoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
453}
454
455#ifdef USE_DEBUGGERS
456static bool _DSCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
457 UNUSED(core);
458 switch (type) {
459 case DEBUGGER_CLI:
460 return true;
461#ifdef USE_GDB_STUB
462 case DEBUGGER_GDB:
463 return true;
464#endif
465 default:
466 return false;
467 }
468}
469
470static struct mDebuggerPlatform* _DSCoreDebuggerPlatform(struct mCore* core) {
471 struct DSCore* dscore = (struct DSCore*) core;
472 if (!dscore->debuggerPlatform) {
473 dscore->debuggerPlatform = ARMDebuggerPlatformCreate();
474 }
475 return dscore->debuggerPlatform;
476}
477
478static struct CLIDebuggerSystem* _DSCoreCliDebuggerSystem(struct mCore* core) {
479 return &DSCLIDebuggerCreate(core)->d;
480}
481
482static void _DSCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
483 if (core->debugger) {
484 DSDetachDebugger(core->board);
485 }
486 DSAttachDebugger(core->board, debugger);
487 core->debugger = debugger;
488}
489
490static void _DSCoreDetachDebugger(struct mCore* core) {
491 DSDetachDebugger(core->board);
492 core->debugger = NULL;
493}
494#endif
495
496static struct mCheatDevice* _DSCoreCheatDevice(struct mCore* core) {
497 return NULL;
498}
499
500static size_t _DSCoreSavedataClone(struct mCore* core, void** sram) {
501 return 0;
502}
503
504static bool _DSCoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
505 return false;
506}
507
508struct mCore* DSCoreCreate(void) {
509 struct DSCore* dscore = malloc(sizeof(*dscore));
510 struct mCore* core = &dscore->d;
511 memset(&core->opts, 0, sizeof(core->opts));
512 core->cpu = NULL;
513 core->board = NULL;
514 core->debugger = NULL;
515 core->init = _DSCoreInit;
516 core->deinit = _DSCoreDeinit;
517 core->platform = _DSCorePlatform;
518 core->setSync = _DSCoreSetSync;
519 core->loadConfig = _DSCoreLoadConfig;
520 core->desiredVideoDimensions = _DSCoreDesiredVideoDimensions;
521 core->setVideoBuffer = _DSCoreSetVideoBuffer;
522 core->getPixels = _DSCoreGetPixels;
523 core->putPixels = _DSCorePutPixels;
524 core->getAudioChannel = _DSCoreGetAudioChannel;
525 core->setAudioBufferSize = _DSCoreSetAudioBufferSize;
526 core->getAudioBufferSize = _DSCoreGetAudioBufferSize;
527 core->addCoreCallbacks = _DSCoreAddCoreCallbacks;
528 core->clearCoreCallbacks = _DSCoreClearCoreCallbacks;
529 core->setAVStream = _DSCoreSetAVStream;
530 core->isROM = DSIsROM;
531 core->loadROM = _DSCoreLoadROM;
532 core->loadBIOS = _DSCoreLoadBIOS;
533 core->loadSave = _DSCoreLoadSave;
534 core->loadPatch = _DSCoreLoadPatch;
535 core->unloadROM = _DSCoreUnloadROM;
536 core->checksum = _DSCoreChecksum;
537 core->reset = _DSCoreReset;
538 core->runFrame = _DSCoreRunFrame;
539 core->runLoop = _DSCoreRunLoop;
540 core->step = _DSCoreStep;
541 core->stateSize = _DSCoreStateSize;
542 core->loadState = _DSCoreLoadState;
543 core->saveState = _DSCoreSaveState;
544 core->setKeys = _DSCoreSetKeys;
545 core->addKeys = _DSCoreAddKeys;
546 core->clearKeys = _DSCoreClearKeys;
547 core->setCursorLocation = _DSCoreSetCursorLocation;
548 core->setCursorDown = _DSCoreSetCursorDown;
549 core->frameCounter = _DSCoreFrameCounter;
550 core->frameCycles = _DSCoreFrameCycles;
551 core->frequency = _DSCoreFrequency;
552 core->getGameTitle = _DSCoreGetGameTitle;
553 core->getGameCode = _DSCoreGetGameCode;
554 core->setRotation = _DSCoreSetRotation;
555 core->setRumble = _DSCoreSetRumble;
556 core->busRead8 = _DSCoreBusRead8;
557 core->busRead16 = _DSCoreBusRead16;
558 core->busRead32 = _DSCoreBusRead32;
559 core->busWrite8 = _DSCoreBusWrite8;
560 core->busWrite16 = _DSCoreBusWrite16;
561 core->busWrite32 = _DSCoreBusWrite32;
562 core->rawRead8 = _DSCoreRawRead8;
563 core->rawRead16 = _DSCoreRawRead16;
564 core->rawRead32 = _DSCoreRawRead32;
565 core->rawWrite8 = _DSCoreRawWrite8;
566 core->rawWrite16 = _DSCoreRawWrite16;
567 core->rawWrite32 = _DSCoreRawWrite32;
568#ifdef USE_DEBUGGERS
569 core->supportsDebuggerType = _DSCoreSupportsDebuggerType;
570 core->debuggerPlatform = _DSCoreDebuggerPlatform;
571 core->cliDebuggerSystem = _DSCoreCliDebuggerSystem;
572 core->attachDebugger = _DSCoreAttachDebugger;
573 core->detachDebugger = _DSCoreDetachDebugger;
574#endif
575 core->cheatDevice = _DSCoreCheatDevice;
576 core->savedataClone = _DSCoreSavedataClone;
577 core->savedataRestore = _DSCoreSavedataRestore;
578 return core;
579}