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 "core.h"
7
8#include "core/cheats.h"
9#include "core/core.h"
10#include "core/log.h"
11#include "arm/debugger/debugger.h"
12#include "ds/ds.h"
13#include "ds/extra/cli.h"
14#include "util/memory.h"
15#include "util/patch.h"
16#include "util/vfs.h"
17
18#define SLICE_CYCLES 2048
19
20struct DSCore {
21 struct mCore d;
22 struct ARMCore* arm7;
23 struct ARMCore* arm9;
24 int keys;
25 struct mCPUComponent* components[CPU_COMPONENT_MAX];
26 struct mDebuggerPlatform* debuggerPlatform;
27 struct mCheatDevice* cheatDevice;
28 int32_t cycleDrift;
29};
30
31static bool _DSCoreInit(struct mCore* core) {
32 struct DSCore* dscore = (struct DSCore*) core;
33
34 struct ARMCore* arm7 = anonymousMemoryMap(sizeof(struct ARMCore));
35 struct ARMCore* arm9 = anonymousMemoryMap(sizeof(struct ARMCore));
36 struct DS* ds = anonymousMemoryMap(sizeof(struct DS));
37 if (!arm7 || !arm9 || !ds) {
38 free(arm7);
39 free(arm9);
40 free(ds);
41 return false;
42 }
43 core->cpu = arm9;
44 core->board = ds;
45 core->debugger = NULL;
46 dscore->arm7 = arm7;
47 dscore->arm9 = arm9;
48 dscore->debuggerPlatform = NULL;
49 dscore->cheatDevice = NULL;
50 dscore->cycleDrift = 0;
51
52 DSCreate(ds);
53 memset(dscore->components, 0, sizeof(dscore->components));
54 ARMSetComponents(arm7, &ds->d, CPU_COMPONENT_MAX, dscore->components);
55 ARMSetComponents(arm9, &ds->d, CPU_COMPONENT_MAX, dscore->components);
56 ARMInit(arm7);
57 ARMInit(arm9);
58
59 dscore->keys = 0;
60 ds->keySource = &dscore->keys;
61
62#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
63 mDirectorySetInit(&core->dirs);
64#endif
65
66 return true;
67}
68
69static void _DSCoreDeinit(struct mCore* core) {
70 struct DSCore* dscore = (struct DSCore*) core;
71 ARMDeinit(dscore->arm7);
72 ARMDeinit(dscore->arm9);
73 DSDestroy(core->board);
74 mappedMemoryFree(dscore->arm7, sizeof(struct ARMCore));
75 mappedMemoryFree(dscore->arm9, sizeof(struct ARMCore));
76 mappedMemoryFree(core->board, sizeof(struct DS));
77#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
78 mDirectorySetDeinit(&core->dirs);
79#endif
80
81 free(dscore->debuggerPlatform);
82 if (dscore->cheatDevice) {
83 mCheatDeviceDestroy(dscore->cheatDevice);
84 }
85 free(dscore->cheatDevice);
86 free(core);
87}
88
89static enum mPlatform _DSCorePlatform(struct mCore* core) {
90 UNUSED(core);
91 return PLATFORM_DS;
92}
93
94static void _DSCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
95 struct DS* ds = core->board;
96 ds->sync = sync;
97}
98
99static void _DSCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
100 struct DS* ds = core->board;
101 struct VFile* bios = NULL;
102 if (core->opts.useBios && core->opts.bios) {
103 bios = VFileOpen(core->opts.bios, O_RDONLY);
104 }
105 if (bios) {
106 DSLoadBIOS(ds, bios);
107 }
108}
109
110static void _DSCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
111 UNUSED(core);
112 *width = DS_VIDEO_HORIZONTAL_PIXELS;
113 *height = DS_VIDEO_VERTICAL_PIXELS;
114}
115
116static void _DSCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
117}
118
119static void _DSCoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
120}
121
122static struct blip_t* _DSCoreGetAudioChannel(struct mCore* core, int ch) {
123 return NULL;
124}
125
126static void _DSCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
127}
128
129static size_t _DSCoreGetAudioBufferSize(struct mCore* core) {
130 return 2048;
131}
132
133static void _DSCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
134}
135
136static bool _DSCoreLoadROM(struct mCore* core, struct VFile* vf) {
137 return DSLoadROM(core->board, vf);
138}
139
140static bool _DSCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
141 UNUSED(type);
142 return DSLoadBIOS(core->board, vf);
143}
144
145static bool _DSCoreLoadSave(struct mCore* core, struct VFile* vf) {
146 return false;
147}
148
149static bool _DSCoreLoadPatch(struct mCore* core, struct VFile* vf) {
150 return false;
151}
152
153static void _DSCoreUnloadROM(struct mCore* core) {
154 return DSUnloadROM(core->board);
155}
156
157static void _DSCoreReset(struct mCore* core) {
158 struct DSCore* dscore = (struct DSCore*) core;
159 struct DS* ds = (struct DS*) core->board;
160 ARMReset(ds->arm7);
161 ARMReset(ds->arm9);
162 dscore->cycleDrift = 0;
163}
164
165static void _DSCoreRunFrame(struct mCore* core) {
166 struct DSCore* dscore = (struct DSCore*) core;
167 struct DS* ds = core->board;
168 int32_t frameCounter = ds->video.frameCounter;
169 while (ds->video.frameCounter == frameCounter) {
170 if (dscore->cycleDrift < SLICE_CYCLES) {
171 dscore->cycleDrift += ARMv5RunCycles(dscore->arm9, SLICE_CYCLES);
172 }
173 if (dscore->cycleDrift >= SLICE_CYCLES) {
174 dscore->cycleDrift -= ARMv4RunCycles(dscore->arm7, dscore->cycleDrift >> 1) << 1;
175 }
176 }
177}
178
179static void _DSCoreRunLoop(struct mCore* core) {
180 struct DSCore* dscore = (struct DSCore*) core;
181 if (dscore->cycleDrift < SLICE_CYCLES) {
182 dscore->cycleDrift += ARMv5RunCycles(dscore->arm9, SLICE_CYCLES);
183 }
184 if (dscore->cycleDrift >= SLICE_CYCLES) {
185 dscore->cycleDrift -= ARMv4RunCycles(dscore->arm7, dscore->cycleDrift >> 1) << 1;
186 }
187}
188
189static void _DSCoreStep(struct mCore* core) {
190 struct DSCore* dscore = (struct DSCore*) core;
191 if (core->cpu == dscore->arm9) {
192 do {
193 if (dscore->cycleDrift >= SLICE_CYCLES) {
194 dscore->cycleDrift -= ARMv4RunCycles(dscore->arm7, dscore->cycleDrift >> 1) << 1;
195 }
196 if (dscore->cycleDrift < SLICE_CYCLES) {
197 dscore->cycleDrift += ARMv5RunCycles(dscore->arm9, 1);
198 break;
199 }
200 } while (dscore->cycleDrift >= SLICE_CYCLES);
201 } else {
202 do {
203 if (dscore->cycleDrift < SLICE_CYCLES) {
204 dscore->cycleDrift += ARMv5RunCycles(dscore->arm9, SLICE_CYCLES - dscore->cycleDrift);
205 }
206 if (dscore->cycleDrift >= SLICE_CYCLES) {
207 dscore->cycleDrift -= ARMv4RunCycles(dscore->arm7, 1) << 1;
208 break;
209 }
210 } while (dscore->cycleDrift < SLICE_CYCLES);
211 }
212}
213
214static size_t _DSCoreStateSize(struct mCore* core) {
215 UNUSED(core);
216 return 0;
217}
218
219static bool _DSCoreLoadState(struct mCore* core, const void* state) {
220 return false;
221}
222
223static bool _DSCoreSaveState(struct mCore* core, void* state) {
224 return false;
225}
226
227static void _DSCoreSetKeys(struct mCore* core, uint32_t keys) {
228 struct DSCore* dscore = (struct DSCore*) core;
229 dscore->keys = keys;
230}
231
232static void _DSCoreAddKeys(struct mCore* core, uint32_t keys) {
233 struct DSCore* dscore = (struct DSCore*) core;
234 dscore->keys |= keys;
235}
236
237static void _DSCoreClearKeys(struct mCore* core, uint32_t keys) {
238 struct DSCore* dscore = (struct DSCore*) core;
239 dscore->keys &= ~keys;
240}
241
242static int32_t _DSCoreFrameCounter(struct mCore* core) {
243 struct DS* ds = core->board;
244 return ds->video.frameCounter;
245}
246
247static int32_t _DSCoreFrameCycles(struct mCore* core) {
248 UNUSED(core);
249 return DS_VIDEO_TOTAL_LENGTH;
250}
251
252static int32_t _DSCoreFrequency(struct mCore* core) {
253 UNUSED(core);
254 return DS_ARM946ES_FREQUENCY;
255}
256
257static void _DSCoreGetGameTitle(struct mCore* core, char* title) {
258 DSGetGameTitle(core->board, title);
259}
260
261static void _DSCoreGetGameCode(struct mCore* core, char* title) {
262 DSGetGameCode(core->board, title);
263}
264
265static void _DSCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
266 struct DS* ds = core->board;
267 ds->rtcSource = rtc;
268}
269
270static void _DSCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
271}
272
273static void _DSCoreSetRumble(struct mCore* core, struct mRumble* rumble) {
274 struct DS* ds = core->board;
275 ds->rumble = rumble;
276}
277
278static uint32_t _DSCoreBusRead8(struct mCore* core, uint32_t address) {
279 struct ARMCore* cpu = core->cpu;
280 return cpu->memory.load8(cpu, address, 0);
281}
282
283static uint32_t _DSCoreBusRead16(struct mCore* core, uint32_t address) {
284 struct ARMCore* cpu = core->cpu;
285 return cpu->memory.load16(cpu, address, 0);
286
287}
288
289static uint32_t _DSCoreBusRead32(struct mCore* core, uint32_t address) {
290 struct ARMCore* cpu = core->cpu;
291 return cpu->memory.load32(cpu, address, 0);
292}
293
294static void _DSCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
295 struct ARMCore* cpu = core->cpu;
296 cpu->memory.store8(cpu, address, value, 0);
297}
298
299static void _DSCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
300 struct ARMCore* cpu = core->cpu;
301 cpu->memory.store16(cpu, address, value, 0);
302}
303
304static void _DSCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
305 struct ARMCore* cpu = core->cpu;
306 cpu->memory.store32(cpu, address, value, 0);
307}
308
309static uint32_t _DSCoreRawRead8(struct mCore* core, uint32_t address) {
310 return 0;
311}
312
313static uint32_t _DSCoreRawRead16(struct mCore* core, uint32_t address) {
314 return 0;
315}
316
317static uint32_t _DSCoreRawRead32(struct mCore* core, uint32_t address) {
318 return 0;
319}
320
321static void _DSCoreRawWrite8(struct mCore* core, uint32_t address, uint8_t value) {
322}
323
324static void _DSCoreRawWrite16(struct mCore* core, uint32_t address, uint16_t value) {
325}
326
327static void _DSCoreRawWrite32(struct mCore* core, uint32_t address, uint32_t value) {
328}
329
330static bool _DSCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
331 UNUSED(core);
332 switch (type) {
333#ifdef USE_CLI_DEBUGGER
334 case DEBUGGER_CLI:
335 return true;
336#endif
337#ifdef USE_GDB_STUB
338 case DEBUGGER_GDB:
339 return true;
340#endif
341 default:
342 return false;
343 }
344}
345
346static struct mDebuggerPlatform* _DSCoreDebuggerPlatform(struct mCore* core) {
347 struct DSCore* dscore = (struct DSCore*) core;
348 if (!dscore->debuggerPlatform) {
349 dscore->debuggerPlatform = ARMDebuggerPlatformCreate();
350 }
351 return dscore->debuggerPlatform;
352}
353
354static struct CLIDebuggerSystem* _DSCoreCliDebuggerSystem(struct mCore* core) {
355#ifdef USE_CLI_DEBUGGER
356 return &DSCLIDebuggerCreate(core)->d;
357#else
358 UNUSED(core);
359 return NULL;
360#endif
361}
362
363static void _DSCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
364 if (core->debugger) {
365 DSDetachDebugger(core->board);
366 }
367 DSAttachDebugger(core->board, debugger);
368 core->debugger = debugger;
369}
370
371static void _DSCoreDetachDebugger(struct mCore* core) {
372 DSDetachDebugger(core->board);
373 core->debugger = NULL;
374}
375
376static struct mCheatDevice* _DSCoreCheatDevice(struct mCore* core) {
377 return NULL;
378}
379
380static size_t _DSCoreSavedataClone(struct mCore* core, void** sram) {
381 return 0;
382}
383
384static bool _DSCoreSavedataLoad(struct mCore* core, const void* sram, size_t size) {
385 return false;
386}
387
388struct mCore* DSCoreCreate(void) {
389 struct DSCore* dscore = malloc(sizeof(*dscore));
390 struct mCore* core = &dscore->d;
391 memset(&core->opts, 0, sizeof(core->opts));
392 core->cpu = NULL;
393 core->board = NULL;
394 core->debugger = NULL;
395 core->init = _DSCoreInit;
396 core->deinit = _DSCoreDeinit;
397 core->platform = _DSCorePlatform;
398 core->setSync = _DSCoreSetSync;
399 core->loadConfig = _DSCoreLoadConfig;
400 core->desiredVideoDimensions = _DSCoreDesiredVideoDimensions;
401 core->setVideoBuffer = _DSCoreSetVideoBuffer;
402 core->getVideoBuffer = _DSCoreGetVideoBuffer;
403 core->getAudioChannel = _DSCoreGetAudioChannel;
404 core->setAudioBufferSize = _DSCoreSetAudioBufferSize;
405 core->getAudioBufferSize = _DSCoreGetAudioBufferSize;
406 core->setAVStream = _DSCoreSetAVStream;
407 core->isROM = DSIsROM;
408 core->loadROM = _DSCoreLoadROM;
409 core->loadBIOS = _DSCoreLoadBIOS;
410 core->loadSave = _DSCoreLoadSave;
411 core->loadPatch = _DSCoreLoadPatch;
412 core->unloadROM = _DSCoreUnloadROM;
413 core->reset = _DSCoreReset;
414 core->runFrame = _DSCoreRunFrame;
415 core->runLoop = _DSCoreRunLoop;
416 core->step = _DSCoreStep;
417 core->stateSize = _DSCoreStateSize;
418 core->loadState = _DSCoreLoadState;
419 core->saveState = _DSCoreSaveState;
420 core->setKeys = _DSCoreSetKeys;
421 core->addKeys = _DSCoreAddKeys;
422 core->clearKeys = _DSCoreClearKeys;
423 core->frameCounter = _DSCoreFrameCounter;
424 core->frameCycles = _DSCoreFrameCycles;
425 core->frequency = _DSCoreFrequency;
426 core->getGameTitle = _DSCoreGetGameTitle;
427 core->getGameCode = _DSCoreGetGameCode;
428 core->setRTC = _DSCoreSetRTC;
429 core->setRotation = _DSCoreSetRotation;
430 core->setRumble = _DSCoreSetRumble;
431 core->busRead8 = _DSCoreBusRead8;
432 core->busRead16 = _DSCoreBusRead16;
433 core->busRead32 = _DSCoreBusRead32;
434 core->busWrite8 = _DSCoreBusWrite8;
435 core->busWrite16 = _DSCoreBusWrite16;
436 core->busWrite32 = _DSCoreBusWrite32;
437 core->rawRead8 = _DSCoreRawRead8;
438 core->rawRead16 = _DSCoreRawRead16;
439 core->rawRead32 = _DSCoreRawRead32;
440 core->rawWrite8 = _DSCoreRawWrite8;
441 core->rawWrite16 = _DSCoreRawWrite16;
442 core->rawWrite32 = _DSCoreRawWrite32;
443 core->supportsDebuggerType = _DSCoreSupportsDebuggerType;
444 core->debuggerPlatform = _DSCoreDebuggerPlatform;
445 core->cliDebuggerSystem = _DSCoreCliDebuggerSystem;
446 core->attachDebugger = _DSCoreAttachDebugger;
447 core->detachDebugger = _DSCoreDetachDebugger;
448 core->cheatDevice = _DSCoreCheatDevice;
449 core->savedataClone = _DSCoreSavedataClone;
450 core->savedataLoad = _DSCoreSavedataLoad;
451 return core;
452}