all repos — mgba @ 5fd48c25dccc5b2b9acdf9436f4f972daaa914f8

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