all repos — mgba @ 6b84383d1ad29d9dd39a62a80c1c8c5878979f45

mGBA Game Boy Advance Emulator

src/core/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/core/core.h>
  7
  8#include <mgba/core/cheats.h>
  9#include <mgba/core/log.h>
 10#include <mgba/core/serialize.h>
 11#include <mgba-util/vfs.h>
 12#include <mgba/internal/debugger/symbols.h>
 13
 14#ifdef USE_ELF
 15#include <mgba-util/elf-read.h>
 16#endif
 17
 18#ifdef M_CORE_GB
 19#include <mgba/gb/core.h>
 20#include <mgba/gb/interface.h>
 21#endif
 22#ifdef M_CORE_GBA
 23#include <mgba/gba/core.h>
 24#include <mgba/gba/interface.h>
 25#endif
 26#ifdef M_CORE_DS
 27#include <mgba/ds/core.h>
 28#include <mgba/internal/ds/ds.h>
 29#endif
 30#ifndef MINIMAL_CORE
 31#include <mgba/feature/video-logger.h>
 32#endif
 33
 34static const struct mCoreFilter {
 35	bool (*filter)(struct VFile*);
 36	struct mCore* (*open)(void);
 37	enum mPlatform platform;
 38} _filters[] = {
 39#ifdef M_CORE_DS
 40	{ DSIsROM, DSCoreCreate, PLATFORM_DS },
 41#endif
 42#ifdef M_CORE_GBA
 43	{ GBAIsROM, GBACoreCreate, PLATFORM_GBA },
 44#endif
 45#ifdef M_CORE_GB
 46	{ GBIsROM, GBCoreCreate, PLATFORM_GB },
 47#endif
 48	{ 0, 0, PLATFORM_NONE }
 49};
 50
 51struct mCore* mCoreFindVF(struct VFile* vf) {
 52	if (!vf) {
 53		return NULL;
 54	}
 55	const struct mCoreFilter* filter;
 56	for (filter = &_filters[0]; filter->filter; ++filter) {
 57		if (filter->filter(vf)) {
 58			break;
 59		}
 60	}
 61	if (filter->open) {
 62		return filter->open();
 63	}
 64#ifndef MINIMAL_CORE
 65	return mVideoLogCoreFind(vf);
 66#endif
 67	return NULL;
 68}
 69
 70enum mPlatform mCoreIsCompatible(struct VFile* vf) {
 71	if (!vf) {
 72		return false;
 73	}
 74	const struct mCoreFilter* filter;
 75	for (filter = &_filters[0]; filter->filter; ++filter) {
 76		if (filter->filter(vf)) {
 77			return filter->platform;
 78		}
 79	}
 80	return PLATFORM_NONE;
 81}
 82
 83#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 84#include <mgba-util/png-io.h>
 85
 86#ifdef PSP2
 87#include <psp2/photoexport.h>
 88#endif
 89
 90struct mCore* mCoreFind(const char* path) {
 91	struct VDir* archive = VDirOpenArchive(path);
 92	struct mCore* core = NULL;
 93	if (archive) {
 94		struct VDirEntry* dirent = archive->listNext(archive);
 95		while (dirent) {
 96			struct VFile* vf = archive->openFile(archive, dirent->name(dirent), O_RDONLY);
 97			if (!vf) {
 98				dirent = archive->listNext(archive);
 99				continue;
100			}
101			core = mCoreFindVF(vf);
102			vf->close(vf);
103			if (core) {
104				break;
105			}
106			dirent = archive->listNext(archive);
107		}
108		archive->close(archive);
109	} else {
110		struct VFile* vf = VFileOpen(path, O_RDONLY);
111		if (!vf) {
112			return NULL;
113		}
114		core = mCoreFindVF(vf);
115		vf->close(vf);
116	}
117	if (core) {
118		return core;
119	}
120	return NULL;
121}
122
123bool mCoreLoadFile(struct mCore* core, const char* path) {
124#ifdef FIXED_ROM_BUFFER
125	return mCorePreloadFile(core, path);
126#else
127	struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
128	if (!rom) {
129		return false;
130	}
131
132	bool ret = core->loadROM(core, rom);
133	if (!ret) {
134		rom->close(rom);
135	}
136	return ret;
137#endif
138}
139
140bool mCorePreloadVF(struct mCore* core, struct VFile* vf) {
141	return mCorePreloadVFCB(core, vf, NULL, NULL);
142}
143
144bool mCorePreloadFile(struct mCore* core, const char* path) {
145	return mCorePreloadFileCB(core, path, NULL, NULL);
146}
147
148bool mCorePreloadVFCB(struct mCore* core, struct VFile* vf, void (cb)(size_t, size_t, void*), void* context) {
149	struct VFile* vfm;
150	size_t size = vf->size(vf);
151
152#ifdef FIXED_ROM_BUFFER
153	extern uint32_t* romBuffer;
154	extern size_t romBufferSize;
155	vfm = VFileFromMemory(romBuffer, romBufferSize);
156#else
157	vfm = VFileMemChunk(NULL, size);
158#endif
159
160	uint8_t buffer[2048];
161	ssize_t read;
162	size_t total = 0;
163	vf->seek(vf, 0, SEEK_SET);
164	while ((read = vf->read(vf, buffer, sizeof(buffer))) > 0) {
165		vfm->write(vfm, buffer, read);
166		total += read;
167		if (cb) {
168			cb(total, size, context);
169		}
170	}
171	vf->close(vf);
172	bool ret = core->loadROM(core, vfm);
173	if (!ret) {
174		vfm->close(vfm);
175	}
176	return ret;
177}
178
179bool mCorePreloadFileCB(struct mCore* core, const char* path, void (cb)(size_t, size_t, void*), void* context) {
180	struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
181	if (!rom) {
182		return false;
183	}
184
185	bool ret = mCorePreloadVFCB(core, rom, cb, context);
186	if (!ret) {
187		rom->close(rom);
188	}
189	return ret;
190}
191
192bool mCoreAutoloadSave(struct mCore* core) {
193	if (!core->dirs.save) {
194		return false;
195	}
196	return core->loadSave(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.save, ".sav", O_CREAT | O_RDWR));
197}
198
199bool mCoreAutoloadPatch(struct mCore* core) {
200	if (!core->dirs.patch) {
201		return false;
202	}
203	return core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ups", O_RDONLY)) ||
204	       core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ips", O_RDONLY)) ||
205	       core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".bps", O_RDONLY));
206}
207
208bool mCoreAutoloadCheats(struct mCore* core) {
209	bool success = true;
210	int cheatAuto;
211	if (!mCoreConfigGetIntValue(&core->config, "cheatAutoload", &cheatAuto) || cheatAuto) {
212		struct VFile* vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.cheats, ".cheats", O_RDONLY);
213		if (vf) {
214			struct mCheatDevice* device = core->cheatDevice(core);
215			if (!device) {
216				success = false;
217			} else {
218				success = mCheatParseFile(device, vf);
219			}
220			vf->close(vf);
221		}
222	}
223	if (!mCoreConfigGetIntValue(&core->config, "cheatAutosave", &cheatAuto) || cheatAuto) {
224		struct mCheatDevice* device = core->cheatDevice(core);
225		if (device) {
226			device->autosave = true;
227		}
228	}
229	return success;
230}
231
232bool mCoreSaveState(struct mCore* core, int slot, int flags) {
233	struct VFile* vf = mCoreGetState(core, slot, true);
234	if (!vf) {
235		return false;
236	}
237	bool success = mCoreSaveStateNamed(core, vf, flags);
238	vf->close(vf);
239	if (success) {
240		mLOG(STATUS, INFO, "State %i saved", slot);
241	} else {
242		mLOG(STATUS, INFO, "State %i failed to save", slot);
243	}
244
245	return success;
246}
247
248bool mCoreLoadState(struct mCore* core, int slot, int flags) {
249	struct VFile* vf = mCoreGetState(core, slot, false);
250	if (!vf) {
251		return false;
252	}
253	bool success = mCoreLoadStateNamed(core, vf, flags);
254	vf->close(vf);
255	if (success) {
256		mLOG(STATUS, INFO, "State %i loaded", slot);
257	} else {
258		mLOG(STATUS, INFO, "State %i failed to load", slot);
259	}
260
261	return success;
262}
263
264struct VFile* mCoreGetState(struct mCore* core, int slot, bool write) {
265	if (!core->dirs.state) {
266		return NULL;
267	}
268	char name[PATH_MAX + 14]; // Quash warning
269	snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
270	return core->dirs.state->openFile(core->dirs.state, name, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY);
271}
272
273void mCoreDeleteState(struct mCore* core, int slot) {
274	char name[PATH_MAX + 14]; // Quash warning
275	snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
276	core->dirs.state->deleteFile(core->dirs.state, name);
277}
278
279void mCoreTakeScreenshot(struct mCore* core) {
280#ifdef USE_PNG
281	size_t stride;
282	const void* pixels = 0;
283	unsigned width, height;
284	core->desiredVideoDimensions(core, &width, &height);
285	struct VFile* vf;
286#ifndef PSP2
287	vf = VDirFindNextAvailable(core->dirs.screenshot, core->dirs.baseName, "-", ".png", O_CREAT | O_TRUNC | O_WRONLY);
288#else
289	vf = VFileMemChunk(0, 0);
290#endif
291	bool success = false;
292	if (vf) {
293		core->getPixels(core, &pixels, &stride);
294		png_structp png = PNGWriteOpen(vf);
295		png_infop info = PNGWriteHeader(png, width, height);
296		success = PNGWritePixels(png, width, height, stride, pixels);
297		PNGWriteClose(png, info);
298#ifdef PSP2
299		void* data = vf->map(vf, 0, 0);
300		PhotoExportParam param = {
301			0,
302			NULL,
303			NULL,
304			NULL,
305			{ 0 }
306		};
307		scePhotoExportFromData(data, vf->size(vf), &param, NULL, NULL, NULL, NULL, 0);
308#endif
309		vf->close(vf);
310	}
311	if (success) {
312		mLOG(STATUS, INFO, "Screenshot saved");
313		return;
314	}
315#else
316	UNUSED(core);
317#endif
318	mLOG(STATUS, WARN, "Failed to take screenshot");
319}
320#endif
321
322void mCoreInitConfig(struct mCore* core, const char* port) {
323	mCoreConfigInit(&core->config, port);
324}
325
326void mCoreLoadConfig(struct mCore* core) {
327#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
328	mCoreConfigLoad(&core->config);
329#endif
330	mCoreLoadForeignConfig(core, &core->config);
331}
332
333void mCoreLoadForeignConfig(struct mCore* core, const struct mCoreConfig* config) {
334	mCoreConfigMap(config, &core->opts);
335#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
336	mDirectorySetMapOptions(&core->dirs, &core->opts);
337#endif
338	if (core->opts.audioBuffers) {
339		core->setAudioBufferSize(core, core->opts.audioBuffers);
340	}
341
342	mCoreConfigCopyValue(&core->config, config, "cheatAutosave");
343	mCoreConfigCopyValue(&core->config, config, "cheatAutoload");
344
345	core->loadConfig(core, config);
346}
347
348void mCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
349	core->rtc.custom = rtc;
350	core->rtc.override = RTC_CUSTOM_START;
351}
352
353void* mCoreGetMemoryBlock(struct mCore* core, uint32_t start, size_t* size) {
354	const struct mCoreMemoryBlock* blocks;
355	size_t nBlocks = core->listMemoryBlocks(core, &blocks);
356	size_t i;
357	for (i = 0; i < nBlocks; ++i) {
358		if (!(blocks[i].flags & mCORE_MEMORY_MAPPED)) {
359			continue;
360		}
361		if (start < blocks[i].start) {
362			continue;
363		}
364		if (start >= blocks[i].start + blocks[i].size) {
365			continue;
366		}
367		uint8_t* out = core->getMemoryBlock(core, blocks[i].id, size);
368		out += start - blocks[i].start;
369		*size -= start - blocks[i].start;
370		return out;
371	}
372	return NULL;
373}
374
375#ifdef USE_ELF
376bool mCoreLoadELF(struct mCore* core, struct ELF* elf) {
377	struct ELFProgramHeaders ph;
378	ELFProgramHeadersInit(&ph, 0);
379	ELFGetProgramHeaders(elf, &ph);
380	size_t i;
381	for (i = 0; i < ELFProgramHeadersSize(&ph); ++i) {
382		size_t bsize, esize;
383		Elf32_Phdr* phdr = ELFProgramHeadersGetPointer(&ph, i);
384		void* block = mCoreGetMemoryBlock(core, phdr->p_paddr, &bsize);
385		char* bytes = ELFBytes(elf, &esize);
386		if (block && bsize >= phdr->p_filesz && esize >= phdr->p_filesz + phdr->p_offset) {
387			memcpy(block, &bytes[phdr->p_offset], phdr->p_filesz);
388		} else {
389			return false;
390		}
391	}
392	return true;
393}
394
395#ifdef USE_DEBUGGERS
396void mCoreLoadELFSymbols(struct mDebuggerSymbols* symbols, struct ELF* elf) {
397	size_t symIndex = ELFFindSection(elf, ".symtab");
398	size_t names = ELFFindSection(elf, ".strtab");
399	Elf32_Shdr* symHeader = ELFGetSectionHeader(elf, symIndex);
400	char* bytes = ELFBytes(elf, NULL);
401
402	Elf32_Sym* syms = (Elf32_Sym*) &bytes[symHeader->sh_offset];
403	size_t i;
404	for (i = 0; i * sizeof(*syms) < symHeader->sh_size; ++i) {
405		if (!syms[i].st_name || ELF32_ST_TYPE(syms[i].st_info) == STT_FILE) {
406			continue;
407		}
408		const char* name = ELFGetString(elf, names, syms[i].st_name);
409		if (name[0] == '$') {
410			continue;
411		}
412		mDebuggerSymbolAdd(symbols, name, syms[i].st_value, -1);
413	}
414}
415#endif
416#endif