all repos — mgba @ 2d6087aa985fc401b1753f4482f07a512828f170

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	if (size > romBufferSize) {
149		return false;
150	}
151	vfm = VFileFromMemory(romBuffer, size);
152#else
153	vfm = VFileMemChunk(NULL, size);
154#endif
155
156	size_t chunkSize;
157#ifdef FIXED_ROM_BUFFER
158	uint8_t* buffer = (uint8_t*) romBuffer;
159	chunkSize = 0x10000;
160#else
161	uint8_t buffer[0x4000];
162	chunkSize = sizeof(buffer);
163#endif
164	ssize_t read;
165	size_t total = 0;
166	vf->seek(vf, 0, SEEK_SET);
167	while ((read = vf->read(vf, buffer, chunkSize)) > 0) {
168#ifdef FIXED_ROM_BUFFER
169		buffer += read;
170#else
171		vfm->write(vfm, buffer, read);
172#endif
173		total += read;
174		if (cb) {
175			cb(total, size, context);
176		}
177	}
178	vf->close(vf);
179	if (read < 0) {
180		vfm->close(vfm);
181		return false;
182	}
183	bool ret = core->loadROM(core, vfm);
184	if (!ret) {
185		vfm->close(vfm);
186	}
187	return ret;
188}
189
190bool mCorePreloadFileCB(struct mCore* core, const char* path, void (cb)(size_t, size_t, void*), void* context) {
191	struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
192	if (!rom) {
193		return false;
194	}
195
196	bool ret = mCorePreloadVFCB(core, rom, cb, context);
197	if (!ret) {
198		rom->close(rom);
199	}
200	return ret;
201}
202
203bool mCoreAutoloadSave(struct mCore* core) {
204	if (!core->dirs.save) {
205		return false;
206	}
207	return core->loadSave(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.save, ".sav", O_CREAT | O_RDWR));
208}
209
210bool mCoreAutoloadPatch(struct mCore* core) {
211	if (!core->dirs.patch) {
212		return false;
213	}
214	return core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ups", O_RDONLY)) ||
215	       core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ips", O_RDONLY)) ||
216	       core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".bps", O_RDONLY));
217}
218
219bool mCoreAutoloadCheats(struct mCore* core) {
220	bool success = true;
221	int cheatAuto;
222	if (!mCoreConfigGetIntValue(&core->config, "cheatAutoload", &cheatAuto) || cheatAuto) {
223		struct VFile* vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.cheats, ".cheats", O_RDONLY);
224		if (vf) {
225			struct mCheatDevice* device = core->cheatDevice(core);
226			success = mCheatParseFile(device, vf);
227			vf->close(vf);
228		}
229	}
230	if (!mCoreConfigGetIntValue(&core->config, "cheatAutosave", &cheatAuto) || cheatAuto) {
231		struct mCheatDevice* device = core->cheatDevice(core);
232		device->autosave = true;
233	}
234	return success;
235}
236
237bool mCoreSaveState(struct mCore* core, int slot, int flags) {
238	struct VFile* vf = mCoreGetState(core, slot, true);
239	if (!vf) {
240		return false;
241	}
242	bool success = mCoreSaveStateNamed(core, vf, flags);
243	vf->close(vf);
244	if (success) {
245		mLOG(STATUS, INFO, "State %i saved", slot);
246	} else {
247		mLOG(STATUS, INFO, "State %i failed to save", slot);
248	}
249
250	return success;
251}
252
253bool mCoreLoadState(struct mCore* core, int slot, int flags) {
254	struct VFile* vf = mCoreGetState(core, slot, false);
255	if (!vf) {
256		return false;
257	}
258	bool success = mCoreLoadStateNamed(core, vf, flags);
259	vf->close(vf);
260	if (success) {
261		mLOG(STATUS, INFO, "State %i loaded", slot);
262	} else {
263		mLOG(STATUS, INFO, "State %i failed to load", slot);
264	}
265
266	return success;
267}
268
269struct VFile* mCoreGetState(struct mCore* core, int slot, bool write) {
270	if (!core->dirs.state) {
271		return NULL;
272	}
273	char name[PATH_MAX + 14]; // Quash warning
274	snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
275	return core->dirs.state->openFile(core->dirs.state, name, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY);
276}
277
278void mCoreDeleteState(struct mCore* core, int slot) {
279	char name[PATH_MAX + 14]; // Quash warning
280	snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
281	core->dirs.state->deleteFile(core->dirs.state, name);
282}
283
284void mCoreTakeScreenshot(struct mCore* core) {
285#ifdef USE_PNG
286	size_t stride;
287	const void* pixels = 0;
288	unsigned width, height;
289	core->desiredVideoDimensions(core, &width, &height);
290	struct VFile* vf;
291#ifndef PSP2
292	vf = VDirFindNextAvailable(core->dirs.screenshot, core->dirs.baseName, "-", ".png", O_CREAT | O_TRUNC | O_WRONLY);
293#else
294	vf = VFileMemChunk(0, 0);
295#endif
296	bool success = false;
297	if (vf) {
298		core->getPixels(core, &pixels, &stride);
299		png_structp png = PNGWriteOpen(vf);
300		png_infop info = PNGWriteHeader(png, width, height);
301		success = PNGWritePixels(png, width, height, stride, pixels);
302		PNGWriteClose(png, info);
303#ifdef PSP2
304		void* data = vf->map(vf, 0, 0);
305		PhotoExportParam param = {
306			0,
307			NULL,
308			NULL,
309			NULL,
310			{ 0 }
311		};
312		scePhotoExportFromData(data, vf->size(vf), &param, NULL, NULL, NULL, NULL, 0);
313#endif
314		vf->close(vf);
315	}
316	if (success) {
317		mLOG(STATUS, INFO, "Screenshot saved");
318		return;
319	}
320#else
321	UNUSED(core);
322#endif
323	mLOG(STATUS, WARN, "Failed to take screenshot");
324}
325#endif
326
327void mCoreInitConfig(struct mCore* core, const char* port) {
328	mCoreConfigInit(&core->config, port);
329}
330
331void mCoreLoadConfig(struct mCore* core) {
332#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
333	mCoreConfigLoad(&core->config);
334#endif
335	mCoreLoadForeignConfig(core, &core->config);
336}
337
338void mCoreLoadForeignConfig(struct mCore* core, const struct mCoreConfig* config) {
339	mCoreConfigMap(config, &core->opts);
340#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
341	mDirectorySetMapOptions(&core->dirs, &core->opts);
342#endif
343	if (core->opts.audioBuffers) {
344		core->setAudioBufferSize(core, core->opts.audioBuffers);
345	}
346
347	mCoreConfigCopyValue(&core->config, config, "cheatAutosave");
348	mCoreConfigCopyValue(&core->config, config, "cheatAutoload");
349
350	core->loadConfig(core, config);
351}
352
353void mCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
354	core->rtc.custom = rtc;
355	core->rtc.override = RTC_CUSTOM_START;
356}
357
358void* mCoreGetMemoryBlock(struct mCore* core, uint32_t start, size_t* size) {
359	return mCoreGetMemoryBlockMasked(core, start, size, mCORE_MEMORY_MAPPED);
360}
361
362void* mCoreGetMemoryBlockMasked(struct mCore* core, uint32_t start, size_t* size, uint32_t mask) {
363	const struct mCoreMemoryBlock* blocks;
364	size_t nBlocks = core->listMemoryBlocks(core, &blocks);
365	size_t i;
366	for (i = 0; i < nBlocks; ++i) {
367		if (!(blocks[i].flags & mCORE_MEMORY_MAPPED)) {
368			continue;
369		}
370		if (!(blocks[i].flags & mask)) {
371			continue;
372		}
373		if (start < blocks[i].start) {
374			continue;
375		}
376		if (start >= blocks[i].start + blocks[i].size) {
377			continue;
378		}
379		uint8_t* out = core->getMemoryBlock(core, blocks[i].id, size);
380		out += start - blocks[i].start;
381		*size -= start - blocks[i].start;
382		return out;
383	}
384	return NULL;
385}
386
387#ifdef USE_ELF
388bool mCoreLoadELF(struct mCore* core, struct ELF* elf) {
389	struct ELFProgramHeaders ph;
390	ELFProgramHeadersInit(&ph, 0);
391	ELFGetProgramHeaders(elf, &ph);
392	size_t i;
393	for (i = 0; i < ELFProgramHeadersSize(&ph); ++i) {
394		size_t bsize, esize;
395		Elf32_Phdr* phdr = ELFProgramHeadersGetPointer(&ph, i);
396		if (!phdr->p_filesz) {
397			continue;
398		}
399		void* block = mCoreGetMemoryBlockMasked(core, phdr->p_paddr, &bsize, mCORE_MEMORY_WRITE | mCORE_MEMORY_WORM);
400		char* bytes = ELFBytes(elf, &esize);
401		if (block && bsize >= phdr->p_filesz && esize > phdr->p_offset && esize >= phdr->p_filesz + phdr->p_offset) {
402			memcpy(block, &bytes[phdr->p_offset], phdr->p_filesz);
403		} else {
404			ELFProgramHeadersDeinit(&ph);
405			return false;
406		}
407	}
408	ELFProgramHeadersDeinit(&ph);
409	return true;
410}
411
412#ifdef USE_DEBUGGERS
413void mCoreLoadELFSymbols(struct mDebuggerSymbols* symbols, struct ELF* elf) {
414	size_t symIndex = ELFFindSection(elf, ".symtab");
415	size_t names = ELFFindSection(elf, ".strtab");
416	Elf32_Shdr* symHeader = ELFGetSectionHeader(elf, symIndex);
417	char* bytes = ELFBytes(elf, NULL);
418
419	Elf32_Sym* syms = (Elf32_Sym*) &bytes[symHeader->sh_offset];
420	size_t i;
421	for (i = 0; i * sizeof(*syms) < symHeader->sh_size; ++i) {
422		if (!syms[i].st_name || ELF32_ST_TYPE(syms[i].st_info) == STT_FILE) {
423			continue;
424		}
425		const char* name = ELFGetString(elf, names, syms[i].st_name);
426		if (name[0] == '$') {
427			continue;
428		}
429		mDebuggerSymbolAdd(symbols, name, syms[i].st_value, -1);
430	}
431}
432#endif
433#endif