all repos — mgba @ 908b0a425ec5e33391b47ac674e7cef9de01e8bc

mGBA Game Boy Advance Emulator

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