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 return NULL;
150}
151
152static void _DSCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
153}
154
155static size_t _DSCoreGetAudioBufferSize(struct mCore* core) {
156 return 2048;
157}
158
159static void _DSCoreSetCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) {
160 struct DS* ds = core->board;
161 ds->coreCallbacks = coreCallbacks;
162}
163
164static void _DSCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
165 struct DS* ds = core->board;
166 ds->stream = stream;
167 if (stream && stream->videoDimensionsChanged) {
168 stream->videoDimensionsChanged(stream, DS_VIDEO_HORIZONTAL_PIXELS, DS_VIDEO_VERTICAL_PIXELS * 2);
169 }
170}
171
172static bool _DSCoreLoadROM(struct mCore* core, struct VFile* vf) {
173 return DSLoadROM(core->board, vf);
174}
175
176static bool _DSCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
177 UNUSED(type);
178 return DSLoadBIOS(core->board, vf);
179}
180
181static bool _DSCoreLoadSave(struct mCore* core, struct VFile* vf) {
182 return DSLoadSave(core->board, vf);
183}
184
185static bool _DSCoreLoadPatch(struct mCore* core, struct VFile* vf) {
186 return false;
187}
188
189static void _DSCoreUnloadROM(struct mCore* core) {
190 return DSUnloadROM(core->board);
191}
192
193static void _DSCoreChecksum(const struct mCore* core, void* data, enum mCoreChecksumType type) {
194}
195
196static void _DSCoreReset(struct mCore* core) {
197 struct DSCore* dscore = (struct DSCore*) core;
198 struct DS* ds = (struct DS*) core->board;
199
200 if (dscore->renderer.outputBuffer) {
201 struct DSVideoRenderer* renderer = &dscore->renderer.d;
202 DSVideoAssociateRenderer(&ds->video, renderer);
203
204 struct DSGXRenderer* gxRenderer = &dscore->gxRenderer.d;
205 DSGXAssociateRenderer(&ds->gx, gxRenderer);
206 }
207
208#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
209 struct VFile* bios7 = NULL;
210 struct VFile* bios9 = NULL;
211 struct VFile* firm = NULL;
212 if (core->opts.useBios) {
213 bool found7 = false;
214 bool found9 = false;
215 bool foundFirm = false;
216
217 if (!found7) {
218 const char* configPath = mCoreConfigGetValue(&core->config, "ds.bios7");
219 bios7 = VFileOpen(configPath, O_RDONLY);
220 if (bios7 && DSIsBIOS7(bios7)) {
221 found7 = true;
222 } else if (bios7) {
223 bios7->close(bios7);
224 bios7 = NULL;
225 }
226 }
227
228 if (!found9) {
229 const char* configPath = mCoreConfigGetValue(&core->config, "ds.bios9");
230 bios9 = VFileOpen(configPath, O_RDONLY);
231 if (bios9 && DSIsBIOS9(bios9)) {
232 found9 = true;
233 } else if (bios9) {
234 bios9->close(bios9);
235 bios9 = NULL;
236 }
237 }
238
239 if (!foundFirm) {
240 const char* configPath = mCoreConfigGetValue(&core->config, "ds.firmware");
241 firm = VFileOpen(configPath, O_RDONLY);
242 if (firm && DSIsFirmware(firm)) {
243 foundFirm = true;
244 } else if (firm) {
245 firm->close(firm);
246 firm = NULL;
247 }
248 }
249
250 if (!found7) {
251 char path[PATH_MAX];
252 mCoreConfigDirectory(path, PATH_MAX);
253 strncat(path, PATH_SEP "ds7_bios.bin", PATH_MAX - strlen(path));
254 bios7 = VFileOpen(path, O_RDONLY);
255 }
256
257 if (!found9) {
258 char path[PATH_MAX];
259 mCoreConfigDirectory(path, PATH_MAX);
260 strncat(path, PATH_SEP "ds9_bios.bin", PATH_MAX - strlen(path));
261 bios9 = VFileOpen(path, O_RDONLY);
262 }
263
264 if (!foundFirm) {
265 char path[PATH_MAX];
266 mCoreConfigDirectory(path, PATH_MAX);
267 strncat(path, PATH_SEP "ds_firmware.bin", PATH_MAX - strlen(path));
268 firm = VFileOpen(path, O_RDWR);
269 }
270 }
271 if (bios7) {
272 DSLoadBIOS(ds, bios7);
273 }
274 if (bios9) {
275 DSLoadBIOS(ds, bios9);
276 }
277 if (firm) {
278 DSLoadFirmware(ds, firm);
279 }
280#endif
281
282 ARMReset(ds->ds7.cpu);
283 ARMReset(ds->ds9.cpu);
284}
285
286static void _DSCoreRunFrame(struct mCore* core) {
287 struct DSCore* dscore = (struct DSCore*) core;
288 struct DS* ds = core->board;
289 int32_t frameCounter = ds->video.frameCounter;
290 while (ds->video.frameCounter == frameCounter) {
291 DSRunLoop(core->board);
292 }
293}
294
295static void _DSCoreRunLoop(struct mCore* core) {
296 DSRunLoop(core->board);
297}
298
299static void _DSCoreStep(struct mCore* core) {
300 struct DSCore* dscore = (struct DSCore*) core;
301 if (core->cpu == dscore->arm9) {
302 DS9Step(core->board);
303 } else {
304 DS7Step(core->board);
305 }
306}
307
308static size_t _DSCoreStateSize(struct mCore* core) {
309 UNUSED(core);
310 return 0;
311}
312
313static bool _DSCoreLoadState(struct mCore* core, const void* state) {
314 return false;
315}
316
317static bool _DSCoreSaveState(struct mCore* core, void* state) {
318 return false;
319}
320
321static void _DSCoreSetKeys(struct mCore* core, uint32_t keys) {
322 struct DSCore* dscore = (struct DSCore*) core;
323 dscore->keys = keys;
324}
325
326static void _DSCoreAddKeys(struct mCore* core, uint32_t keys) {
327 struct DSCore* dscore = (struct DSCore*) core;
328 dscore->keys |= keys;
329}
330
331static void _DSCoreClearKeys(struct mCore* core, uint32_t keys) {
332 struct DSCore* dscore = (struct DSCore*) core;
333 dscore->keys &= ~keys;
334}
335
336static void _DSCoreSetCursorLocation(struct mCore* core, int x, int y) {
337 struct DSCore* dscore = (struct DSCore*) core;
338 dscore->cursorX = x;
339 dscore->cursorY = y - DS_VIDEO_VERTICAL_PIXELS;
340 if (dscore->cursorY < 0) {
341 dscore->cursorY = 0;
342 }
343}
344
345static void _DSCoreSetCursorDown(struct mCore* core, bool down) {
346 struct DSCore* dscore = (struct DSCore*) core;
347 dscore->touchDown = down;
348}
349
350static int32_t _DSCoreFrameCounter(const struct mCore* core) {
351 struct DS* ds = core->board;
352 return ds->video.frameCounter;
353}
354
355static int32_t _DSCoreFrameCycles(const struct mCore* core) {
356 UNUSED(core);
357 return DS_VIDEO_TOTAL_LENGTH;
358}
359
360static int32_t _DSCoreFrequency(const struct mCore* core) {
361 UNUSED(core);
362 return DS_ARM946ES_FREQUENCY;
363}
364
365static void _DSCoreGetGameTitle(const struct mCore* core, char* title) {
366 DSGetGameTitle(core->board, title);
367}
368
369static void _DSCoreGetGameCode(const struct mCore* core, char* title) {
370 DSGetGameCode(core->board, title);
371}
372
373static void _DSCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
374 struct DS* ds = core->board;
375 ds->rtcSource = rtc;
376}
377
378static void _DSCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
379}
380
381static void _DSCoreSetRumble(struct mCore* core, struct mRumble* rumble) {
382 struct DS* ds = core->board;
383 ds->rumble = rumble;
384}
385
386static uint32_t _DSCoreBusRead8(struct mCore* core, uint32_t address) {
387 struct ARMCore* cpu = core->cpu;
388 return cpu->memory.load8(cpu, address, 0);
389}
390
391static uint32_t _DSCoreBusRead16(struct mCore* core, uint32_t address) {
392 struct ARMCore* cpu = core->cpu;
393 return cpu->memory.load16(cpu, address, 0);
394
395}
396
397static uint32_t _DSCoreBusRead32(struct mCore* core, uint32_t address) {
398 struct ARMCore* cpu = core->cpu;
399 return cpu->memory.load32(cpu, address, 0);
400}
401
402static void _DSCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
403 struct ARMCore* cpu = core->cpu;
404 cpu->memory.store8(cpu, address, value, 0);
405}
406
407static void _DSCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
408 struct ARMCore* cpu = core->cpu;
409 cpu->memory.store16(cpu, address, value, 0);
410}
411
412static void _DSCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
413 struct ARMCore* cpu = core->cpu;
414 cpu->memory.store32(cpu, address, value, 0);
415}
416
417static uint32_t _DSCoreRawRead8(struct mCore* core, uint32_t address, int segment) {
418 // TODO: Raw
419 struct ARMCore* cpu = core->cpu;
420 return cpu->memory.load8(cpu, address, 0);
421}
422
423static uint32_t _DSCoreRawRead16(struct mCore* core, uint32_t address, int segment) {
424 // TODO: Raw
425 struct ARMCore* cpu = core->cpu;
426 return cpu->memory.load16(cpu, address, 0);
427}
428
429static uint32_t _DSCoreRawRead32(struct mCore* core, uint32_t address, int segment) {
430 // TODO: Raw
431 struct ARMCore* cpu = core->cpu;
432 return cpu->memory.load32(cpu, address, 0);
433}
434
435static void _DSCoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
436}
437
438static void _DSCoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
439}
440
441static void _DSCoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
442}
443
444#ifdef USE_DEBUGGERS
445static bool _DSCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
446 UNUSED(core);
447 switch (type) {
448 case DEBUGGER_CLI:
449 return true;
450#ifdef USE_GDB_STUB
451 case DEBUGGER_GDB:
452 return true;
453#endif
454 default:
455 return false;
456 }
457}
458
459static struct mDebuggerPlatform* _DSCoreDebuggerPlatform(struct mCore* core) {
460 struct DSCore* dscore = (struct DSCore*) core;
461 if (!dscore->debuggerPlatform) {
462 dscore->debuggerPlatform = ARMDebuggerPlatformCreate();
463 }
464 return dscore->debuggerPlatform;
465}
466
467static struct CLIDebuggerSystem* _DSCoreCliDebuggerSystem(struct mCore* core) {
468 return &DSCLIDebuggerCreate(core)->d;
469}
470
471static void _DSCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
472 if (core->debugger) {
473 DSDetachDebugger(core->board);
474 }
475 DSAttachDebugger(core->board, debugger);
476 core->debugger = debugger;
477}
478
479static void _DSCoreDetachDebugger(struct mCore* core) {
480 DSDetachDebugger(core->board);
481 core->debugger = NULL;
482}
483#endif
484
485static struct mCheatDevice* _DSCoreCheatDevice(struct mCore* core) {
486 return NULL;
487}
488
489static size_t _DSCoreSavedataClone(struct mCore* core, void** sram) {
490 return 0;
491}
492
493static bool _DSCoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
494 return false;
495}
496
497struct mCore* DSCoreCreate(void) {
498 struct DSCore* dscore = malloc(sizeof(*dscore));
499 struct mCore* core = &dscore->d;
500 memset(&core->opts, 0, sizeof(core->opts));
501 core->cpu = NULL;
502 core->board = NULL;
503 core->debugger = NULL;
504 core->init = _DSCoreInit;
505 core->deinit = _DSCoreDeinit;
506 core->platform = _DSCorePlatform;
507 core->setSync = _DSCoreSetSync;
508 core->loadConfig = _DSCoreLoadConfig;
509 core->desiredVideoDimensions = _DSCoreDesiredVideoDimensions;
510 core->setVideoBuffer = _DSCoreSetVideoBuffer;
511 core->getPixels = _DSCoreGetPixels;
512 core->putPixels = _DSCorePutPixels;
513 core->getAudioChannel = _DSCoreGetAudioChannel;
514 core->setAudioBufferSize = _DSCoreSetAudioBufferSize;
515 core->getAudioBufferSize = _DSCoreGetAudioBufferSize;
516 core->setCoreCallbacks = _DSCoreSetCoreCallbacks;
517 core->setAVStream = _DSCoreSetAVStream;
518 core->isROM = DSIsROM;
519 core->loadROM = _DSCoreLoadROM;
520 core->loadBIOS = _DSCoreLoadBIOS;
521 core->loadSave = _DSCoreLoadSave;
522 core->loadPatch = _DSCoreLoadPatch;
523 core->unloadROM = _DSCoreUnloadROM;
524 core->checksum = _DSCoreChecksum;
525 core->reset = _DSCoreReset;
526 core->runFrame = _DSCoreRunFrame;
527 core->runLoop = _DSCoreRunLoop;
528 core->step = _DSCoreStep;
529 core->stateSize = _DSCoreStateSize;
530 core->loadState = _DSCoreLoadState;
531 core->saveState = _DSCoreSaveState;
532 core->setKeys = _DSCoreSetKeys;
533 core->addKeys = _DSCoreAddKeys;
534 core->clearKeys = _DSCoreClearKeys;
535 core->setCursorLocation = _DSCoreSetCursorLocation;
536 core->setCursorDown = _DSCoreSetCursorDown;
537 core->frameCounter = _DSCoreFrameCounter;
538 core->frameCycles = _DSCoreFrameCycles;
539 core->frequency = _DSCoreFrequency;
540 core->getGameTitle = _DSCoreGetGameTitle;
541 core->getGameCode = _DSCoreGetGameCode;
542 core->setRTC = _DSCoreSetRTC;
543 core->setRotation = _DSCoreSetRotation;
544 core->setRumble = _DSCoreSetRumble;
545 core->busRead8 = _DSCoreBusRead8;
546 core->busRead16 = _DSCoreBusRead16;
547 core->busRead32 = _DSCoreBusRead32;
548 core->busWrite8 = _DSCoreBusWrite8;
549 core->busWrite16 = _DSCoreBusWrite16;
550 core->busWrite32 = _DSCoreBusWrite32;
551 core->rawRead8 = _DSCoreRawRead8;
552 core->rawRead16 = _DSCoreRawRead16;
553 core->rawRead32 = _DSCoreRawRead32;
554 core->rawWrite8 = _DSCoreRawWrite8;
555 core->rawWrite16 = _DSCoreRawWrite16;
556 core->rawWrite32 = _DSCoreRawWrite32;
557#ifdef USE_DEBUGGERS
558 core->supportsDebuggerType = _DSCoreSupportsDebuggerType;
559 core->debuggerPlatform = _DSCoreDebuggerPlatform;
560 core->cliDebuggerSystem = _DSCoreCliDebuggerSystem;
561 core->attachDebugger = _DSCoreAttachDebugger;
562 core->detachDebugger = _DSCoreDetachDebugger;
563#endif
564 core->cheatDevice = _DSCoreCheatDevice;
565 core->savedataClone = _DSCoreSavedataClone;
566 core->savedataRestore = _DSCoreSavedataRestore;
567 return core;
568}