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