all repos — mgba @ 1f0f943526b06790aaf9dfaffa9538f21988af19

mGBA Game Boy Advance Emulator

src/core/cheats.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 "cheats.h"
  7
  8#include "core/core.h"
  9#include "util/string.h"
 10#include "util/vfs.h"
 11
 12#define MAX_LINE_LENGTH 128
 13
 14const uint32_t M_CHEAT_DEVICE_ID = 0xABADC0DE;
 15
 16mLOG_DEFINE_CATEGORY(CHEATS, "Cheats");
 17
 18DEFINE_VECTOR(mCheatList, struct mCheat);
 19DEFINE_VECTOR(mCheatSets, struct mCheatSet*);
 20DEFINE_VECTOR(StringList, char*);
 21
 22static int32_t _readMem(struct mCore* core, uint32_t address, int width) {
 23	switch (width) {
 24	case 1:
 25		return core->busRead8(core, address);
 26	case 2:
 27		return core->busRead16(core, address);
 28	case 4:
 29		return core->busRead32(core, address);
 30	}
 31	return 0;
 32}
 33
 34static void _writeMem(struct mCore* core, uint32_t address, int width, int32_t value) {
 35	switch (width) {
 36	case 1:
 37		core->busWrite8(core, address, value);
 38		break;
 39	case 2:
 40		core->busWrite16(core, address, value);
 41		break;
 42	case 4:
 43		core->busWrite32(core, address, value);
 44		break;
 45	}
 46}
 47
 48static void mCheatDeviceInit(void*, struct mCPUComponent*);
 49static void mCheatDeviceDeinit(struct mCPUComponent*);
 50
 51void mCheatDeviceCreate(struct mCheatDevice* device) {
 52	device->d.id = M_CHEAT_DEVICE_ID;
 53	device->d.init = mCheatDeviceInit;
 54	device->d.deinit = mCheatDeviceDeinit;
 55	mCheatSetsInit(&device->cheats, 4);
 56}
 57
 58void mCheatDeviceDestroy(struct mCheatDevice* device) {
 59	mCheatDeviceClear(device);
 60	mCheatSetsDeinit(&device->cheats);
 61}
 62
 63void mCheatDeviceClear(struct mCheatDevice* device) {
 64	size_t i;
 65	for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
 66		struct mCheatSet* set = *mCheatSetsGetPointer(&device->cheats, i);
 67		mCheatSetDeinit(set);
 68	}
 69	mCheatSetsClear(&device->cheats);
 70}
 71
 72void mCheatSetInit(struct mCheatSet* set, const char* name) {
 73	mCheatListInit(&set->list, 4);
 74	StringListInit(&set->lines, 4);
 75	if (name) {
 76		set->name = strdup(name);
 77	} else {
 78		set->name = 0;
 79	}
 80	set->enabled = true;
 81}
 82
 83void mCheatSetDeinit(struct mCheatSet* set) {
 84	mCheatListDeinit(&set->list);
 85	size_t i;
 86	for (i = 0; i < StringListSize(&set->lines); ++i) {
 87		free(*StringListGetPointer(&set->lines, i));
 88	}
 89	if (set->name) {
 90		free(set->name);
 91	}
 92	set->deinit(set);
 93	free(set);
 94}
 95
 96void mCheatSetRename(struct mCheatSet* set, const char* name) {
 97	if (set->name) {
 98		free(set->name);
 99		set->name = NULL;
100	}
101	if (name) {
102		set->name = strdup(name);
103	}
104}
105
106bool mCheatAddLine(struct mCheatSet* set, const char* line, int type) {
107	if (!set->addLine(set, line, type)) {
108		return false;
109	}
110	*StringListAppend(&set->lines) = strdup(line);
111	return true;
112}
113
114void mCheatAddSet(struct mCheatDevice* device, struct mCheatSet* cheats) {
115	*mCheatSetsAppend(&device->cheats) = cheats;
116	cheats->add(cheats, device);
117}
118
119void mCheatRemoveSet(struct mCheatDevice* device, struct mCheatSet* cheats) {
120	size_t i;
121	for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
122		if (*mCheatSetsGetPointer(&device->cheats, i) == cheats) {
123			break;
124		}
125	}
126	if (i == mCheatSetsSize(&device->cheats)) {
127		return;
128	}
129	mCheatSetsShift(&device->cheats, i, 1);
130	cheats->remove(cheats, device);
131}
132
133bool mCheatParseFile(struct mCheatDevice* device, struct VFile* vf) {
134	char cheat[MAX_LINE_LENGTH];
135	struct mCheatSet* set = NULL;
136	struct mCheatSet* newSet;
137	bool nextDisabled = false;
138	struct StringList directives;
139	StringListInit(&directives, 4);
140
141	while (true) {
142		size_t i = 0;
143		ssize_t bytesRead = vf->readline(vf, cheat, sizeof(cheat));
144		rtrim(cheat);
145		if (bytesRead == 0) {
146			break;
147		}
148		if (bytesRead < 0) {
149			StringListDeinit(&directives);
150			return false;
151		}
152		while (isspace((int) cheat[i])) {
153			++i;
154		}
155		switch (cheat[i]) {
156		case '#':
157			do {
158				++i;
159			} while (isspace((int) cheat[i]));
160			newSet = device->createSet(device, &cheat[i]);
161			newSet->enabled = !nextDisabled;
162			nextDisabled = false;
163			if (set) {
164				mCheatAddSet(device, set);
165			}
166			if (set) {
167				newSet->copyProperties(newSet, set);
168			}
169			newSet->parseDirectives(newSet, &directives);
170			set = newSet;
171			break;
172		case '!':
173			do {
174				++i;
175			} while (isspace((int) cheat[i]));
176			if (strcasecmp(&cheat[i], "disabled") == 0) {
177				nextDisabled = true;
178				break;
179			}
180			if (strcasecmp(&cheat[i], "reset") == 0) {
181				size_t d;
182				for (d = 0; d < StringListSize(&directives); ++d) {
183					free(*StringListGetPointer(&directives, d));
184				}
185				StringListClear(&directives);
186				break;
187			}
188			*StringListAppend(&directives) = strdup(&cheat[i]);
189			break;
190		default:
191			if (!set) {
192				set = device->createSet(device, NULL);
193				set->enabled = !nextDisabled;
194				nextDisabled = false;
195			}
196			mCheatAddLine(set, cheat, 0);
197			break;
198		}
199	}
200	if (set) {
201		mCheatAddSet(device, set);
202	}
203	size_t d;
204	for (d = 0; d < StringListSize(&directives); ++d) {
205		free(*StringListGetPointer(&directives, d));
206	}
207	StringListClear(&directives);
208	StringListDeinit(&directives);
209	return true;
210}
211
212bool mCheatSaveFile(struct mCheatDevice* device, struct VFile* vf) {
213	static const char lineStart[3] = "# ";
214	static const char lineEnd = '\n';
215	struct StringList directives;
216	StringListInit(&directives, 4);
217
218	size_t i;
219	for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
220		struct mCheatSet* set = *mCheatSetsGetPointer(&device->cheats, i);
221		set->dumpDirectives(set, &directives);
222		if (!set->enabled) {
223			static const char* disabledDirective = "!disabled\n";
224			vf->write(vf, disabledDirective, strlen(disabledDirective));
225		}
226		size_t d;
227		for (d = 0; d < StringListSize(&directives); ++d) {
228			char directive[64];
229			ssize_t len = snprintf(directive, sizeof(directive) - 1, "!%s\n", *StringListGetPointer(&directives, d));
230			if (len > 1) {
231				vf->write(vf, directive, (size_t) len > sizeof(directive) ? sizeof(directive) : len);
232			}
233		}
234
235		vf->write(vf, lineStart, 2);
236		if (set->name) {
237			vf->write(vf, set->name, strlen(set->name));
238		}
239		vf->write(vf, &lineEnd, 1);
240		size_t c;
241		for (c = 0; c < StringListSize(&set->lines); ++c) {
242			const char* line = *StringListGetPointer(&set->lines, c);
243			vf->write(vf, line, strlen(line));
244			vf->write(vf, &lineEnd, 1);
245		}
246	}
247	size_t d;
248	for (d = 0; d < StringListSize(&directives); ++d) {
249		free(*StringListGetPointer(&directives, d));
250	}
251	StringListClear(&directives);
252	StringListDeinit(&directives);
253	return true;
254}
255
256void mCheatRefresh(struct mCheatDevice* device, struct mCheatSet* cheats) {
257	if (!cheats->enabled) {
258		return;
259	}
260	bool condition = true;
261	int conditionRemaining = 0;
262	int negativeConditionRemaining = 0;
263	cheats->refresh(cheats, device);
264
265	size_t nCodes = mCheatListSize(&cheats->list);
266	size_t i;
267	for (i = 0; i < nCodes; ++i) {
268		if (conditionRemaining > 0) {
269			--conditionRemaining;
270			if (!condition) {
271				continue;
272			}
273		} else if (negativeConditionRemaining > 0) {
274			conditionRemaining = negativeConditionRemaining - 1;
275			negativeConditionRemaining = 0;
276			condition = !condition;
277			if (!condition) {
278				continue;
279			}
280		} else {
281			condition = true;
282		}
283		struct mCheat* cheat = mCheatListGetPointer(&cheats->list, i);
284		int32_t value = 0;
285		int32_t operand = cheat->operand;
286		uint32_t operationsRemaining = cheat->repeat;
287		uint32_t address = cheat->address;
288		bool performAssignment = false;
289		for (; operationsRemaining; --operationsRemaining) {
290			switch (cheat->type) {
291			case CHEAT_ASSIGN:
292				value = operand;
293				performAssignment = true;
294				break;
295			case CHEAT_ASSIGN_INDIRECT:
296				value = operand;
297				address = _readMem(device->p, address + cheat->addressOffset, 4);
298				performAssignment = true;
299				break;
300			case CHEAT_AND:
301				value = _readMem(device->p, address, cheat->width) & operand;
302				performAssignment = true;
303				break;
304			case CHEAT_ADD:
305				value = _readMem(device->p, address, cheat->width) + operand;
306				performAssignment = true;
307				break;
308			case CHEAT_OR:
309				value = _readMem(device->p, address, cheat->width) | operand;
310				performAssignment = true;
311				break;
312			case CHEAT_IF_EQ:
313				condition = _readMem(device->p, address, cheat->width) == operand;
314				conditionRemaining = cheat->repeat;
315				negativeConditionRemaining = cheat->negativeRepeat;
316				break;
317			case CHEAT_IF_NE:
318				condition = _readMem(device->p, address, cheat->width) != operand;
319				conditionRemaining = cheat->repeat;
320				negativeConditionRemaining = cheat->negativeRepeat;
321				break;
322			case CHEAT_IF_LT:
323				condition = _readMem(device->p, address, cheat->width) < operand;
324				conditionRemaining = cheat->repeat;
325				negativeConditionRemaining = cheat->negativeRepeat;
326				break;
327			case CHEAT_IF_GT:
328				condition = _readMem(device->p, address, cheat->width) > operand;
329				conditionRemaining = cheat->repeat;
330				negativeConditionRemaining = cheat->negativeRepeat;
331				break;
332			case CHEAT_IF_ULT:
333				condition = (uint32_t) _readMem(device->p, address, cheat->width) < (uint32_t) operand;
334				conditionRemaining = cheat->repeat;
335				negativeConditionRemaining = cheat->negativeRepeat;
336				break;
337			case CHEAT_IF_UGT:
338				condition = (uint32_t) _readMem(device->p, address, cheat->width) > (uint32_t) operand;
339				conditionRemaining = cheat->repeat;
340				negativeConditionRemaining = cheat->negativeRepeat;
341				break;
342			case CHEAT_IF_AND:
343				condition = _readMem(device->p, address, cheat->width) & operand;
344				conditionRemaining = cheat->repeat;
345				negativeConditionRemaining = cheat->negativeRepeat;
346				break;
347			case CHEAT_IF_LAND:
348				condition = _readMem(device->p, address, cheat->width) && operand;
349				conditionRemaining = cheat->repeat;
350				negativeConditionRemaining = cheat->negativeRepeat;
351				break;
352			}
353
354			if (performAssignment) {
355				_writeMem(device->p, address, cheat->width, value);
356			}
357
358			address += cheat->addressOffset;
359			operand += cheat->operandOffset;
360		}
361	}
362}
363
364void mCheatDeviceInit(void* cpu, struct mCPUComponent* component) {
365	UNUSED(cpu);
366	struct mCheatDevice* device = (struct mCheatDevice*) component;
367	size_t i;
368	for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
369		struct mCheatSet* cheats = *mCheatSetsGetPointer(&device->cheats, i);
370		cheats->add(cheats, device);
371	}
372}
373
374void mCheatDeviceDeinit(struct mCPUComponent* component) {
375	struct mCheatDevice* device = (struct mCheatDevice*) component;
376	size_t i;
377	for (i = mCheatSetsSize(&device->cheats); i--;) {
378		struct mCheatSet* cheats = *mCheatSetsGetPointer(&device->cheats, i);
379		cheats->remove(cheats, device);
380	}
381}