all repos — mgba @ ca6b2e7b09c66ad14db5977a68c428f7ee4e7f7d

mGBA Game Boy Advance Emulator

src/gba/cheats.c (view raw)

  1/* Copyright (c) 2013-2015 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 "gba/gba.h"
  9#include "gba/io.h"
 10#include "util/vfs.h"
 11
 12#define MAX_LINE_LENGTH 128
 13
 14const uint32_t GBA_CHEAT_DEVICE_ID = 0xABADC0DE;
 15
 16DEFINE_VECTOR(GBACheatList, struct GBACheat);
 17DEFINE_VECTOR(GBACheatSets, struct GBACheatSet*);
 18DEFINE_VECTOR(StringList, char*);
 19
 20static const uint32_t _gsa1S[4] = { 0x09F4FBBD, 0x9681884A, 0x352027E9, 0xF3DEE5A7 };
 21static const uint32_t _par3S[4] = { 0x7AA9648F, 0x7FAE6994, 0xC0EFAAD5, 0x42712C57 };
 22
 23static const uint8_t _gsa1T1[256] = {
 24	0x31, 0x1C, 0x23, 0xE5, 0x89, 0x8E, 0xA1, 0x37, 0x74, 0x6D, 0x67, 0xFC, 0x1F, 0xC0, 0xB1, 0x94,
 25	0x3B, 0x05, 0x56, 0x86, 0x00, 0x24, 0xF0, 0x17, 0x72, 0xA2, 0x3D, 0x1B, 0xE3, 0x17, 0xC5, 0x0B,
 26	0xB9, 0xE2, 0xBD, 0x58, 0x71, 0x1B, 0x2C, 0xFF, 0xE4, 0xC9, 0x4C, 0x5E, 0xC9, 0x55, 0x33, 0x45,
 27	0x7C, 0x3F, 0xB2, 0x51, 0xFE, 0x10, 0x7E, 0x75, 0x3C, 0x90, 0x8D, 0xDA, 0x94, 0x38, 0xC3, 0xE9,
 28	0x95, 0xEA, 0xCE, 0xA6, 0x06, 0xE0, 0x4F, 0x3F, 0x2A, 0xE3, 0x3A, 0xE4, 0x43, 0xBD, 0x7F, 0xDA,
 29	0x55, 0xF0, 0xEA, 0xCB, 0x2C, 0xA8, 0x47, 0x61, 0xA0, 0xEF, 0xCB, 0x13, 0x18, 0x20, 0xAF, 0x3E,
 30	0x4D, 0x9E, 0x1E, 0x77, 0x51, 0xC5, 0x51, 0x20, 0xCF, 0x21, 0xF9, 0x39, 0x94, 0xDE, 0xDD, 0x79,
 31	0x4E, 0x80, 0xC4, 0x9D, 0x94, 0xD5, 0x95, 0x01, 0x27, 0x27, 0xBD, 0x6D, 0x78, 0xB5, 0xD1, 0x31,
 32	0x6A, 0x65, 0x74, 0x74, 0x58, 0xB3, 0x7C, 0xC9, 0x5A, 0xED, 0x50, 0x03, 0xC4, 0xA2, 0x94, 0x4B,
 33	0xF0, 0x58, 0x09, 0x6F, 0x3E, 0x7D, 0xAE, 0x7D, 0x58, 0xA0, 0x2C, 0x91, 0xBB, 0xE1, 0x70, 0xEB,
 34	0x73, 0xA6, 0x9A, 0x44, 0x25, 0x90, 0x16, 0x62, 0x53, 0xAE, 0x08, 0xEB, 0xDC, 0xF0, 0xEE, 0x77,
 35	0xC2, 0xDE, 0x81, 0xE8, 0x30, 0x89, 0xDB, 0xFE, 0xBC, 0xC2, 0xDF, 0x26, 0xE9, 0x8B, 0xD6, 0x93,
 36	0xF0, 0xCB, 0x56, 0x90, 0xC0, 0x46, 0x68, 0x15, 0x43, 0xCB, 0xE9, 0x98, 0xE3, 0xAF, 0x31, 0x25,
 37	0x4D, 0x7B, 0xF3, 0xB1, 0x74, 0xE2, 0x64, 0xAC, 0xD9, 0xF6, 0xA0, 0xD5, 0x0B, 0x9B, 0x49, 0x52,
 38	0x69, 0x3B, 0x71, 0x00, 0x2F, 0xBB, 0xBA, 0x08, 0xB1, 0xAE, 0xBB, 0xB3, 0xE1, 0xC9, 0xA6, 0x7F,
 39	0x17, 0x97, 0x28, 0x72, 0x12, 0x6E, 0x91, 0xAE, 0x3A, 0xA2, 0x35, 0x46, 0x27, 0xF8, 0x12, 0x50
 40};
 41
 42static const uint8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, 0x76, 0xE5, 0x15, 0xFB, 0x1A, 0xBC, 0x87, 0x02, 0x2A,
 68	0x58, 0x8F, 0x9A, 0x95, 0xBD, 0xAE, 0x8D, 0x0C, 0xA5, 0x4C, 0xF2, 0x5C, 0x7D, 0xAD, 0x51, 0xFB,
 69	0xB1, 0x22, 0x07, 0xE0, 0x29, 0x7C, 0xEB, 0x98, 0x14, 0xC6, 0x31, 0x97, 0xE4, 0x34, 0x8F, 0xCC,
 70	0x99, 0x56, 0x9F, 0x78, 0x43, 0x91, 0x85, 0x3F, 0xC2, 0xD0, 0xD1, 0x80, 0xD1, 0x77, 0xA7, 0xE2,
 71	0x43, 0x99, 0x1D, 0x2F, 0x8B, 0x6A, 0xE4, 0x66, 0x82, 0xF7, 0x2B, 0x0B, 0x65, 0x14, 0xC0, 0xC2,
 72	0x1D, 0x96, 0x78, 0x1C, 0xC4, 0xC3, 0xD2, 0xB1, 0x64, 0x07, 0xD7, 0x6F, 0x02, 0xE9, 0x44, 0x31,
 73	0xDB, 0x3C, 0xEB, 0x93, 0xED, 0x9A, 0x57, 0x05, 0xB9, 0x0E, 0xAF, 0x1F, 0x48, 0x11, 0xDC, 0x35,
 74	0x6C, 0xB8, 0xEE, 0x2A, 0x48, 0x2B, 0xBC, 0x89, 0x12, 0x59, 0xCB, 0xD1, 0x18, 0xEA, 0x72, 0x11,
 75	0x01, 0x75, 0x3B, 0xB5, 0x56, 0xF4, 0x8B, 0xA0, 0x41, 0x75, 0x86, 0x7B, 0x94, 0x12, 0x2D, 0x4C,
 76	0x0C, 0x22, 0xC9, 0x4A, 0xD8, 0xB1, 0x8D, 0xF0, 0x55, 0x2E, 0x77, 0x50, 0x1C, 0x64, 0x77, 0xAA,
 77	0x3E, 0xAC, 0xD3, 0x3D, 0xCE, 0x60, 0xCA, 0x5D, 0xA0, 0x92, 0x78, 0xC6, 0x51, 0xFE, 0xF9, 0x30
 78};
 79
 80static const uint8_t _par3T2[256] = {
 81	0xAA, 0xAF, 0xF0, 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 0xF0, 0xAE, 0xC2, 0xD3, 0x28, 0x41, 0x9B, 0xC8, 0x04, 0xB9, 0x4A, 0xBA,
 93	0x72, 0xE2, 0xB5, 0x06, 0x2C, 0x1E, 0x0B, 0x2C, 0x7F, 0x11, 0xA9, 0x26, 0x51, 0x9D, 0x3F, 0xF8,
 94	0x62, 0x11, 0x2E, 0x89, 0xD2, 0x9D, 0x35, 0xB1, 0xE4, 0x0A, 0x4D, 0x93, 0x01, 0xA7, 0xD1, 0x2D,
 95	0x00, 0x87, 0xE2, 0x2D, 0xA4, 0xE9, 0x0A, 0x06, 0x66, 0xF8, 0x1F, 0x44, 0x75, 0xB5, 0x6B, 0x1C,
 96	0xFC, 0x31, 0x09, 0x48, 0xA3, 0xFF, 0x92, 0x12, 0x58, 0xE9, 0xFA, 0xAE, 0x4F, 0xE2, 0xB4, 0xCC
 97};
 98
 99static int32_t _readMem(struct ARMCore* cpu, uint32_t address, int width) {
100	switch (width) {
101	case 1:
102		return cpu->memory.load8(cpu, address, 0);
103	case 2:
104		return cpu->memory.load16(cpu, address, 0);
105	case 4:
106		return cpu->memory.load32(cpu, address, 0);
107	}
108	return 0;
109}
110
111static void _writeMem(struct ARMCore* cpu, uint32_t address, int width, int32_t value) {
112	switch (width) {
113	case 1:
114		cpu->memory.store8(cpu, address, value, 0);
115		break;
116	case 2:
117		cpu->memory.store16(cpu, address, value, 0);
118		break;
119	case 4:
120		cpu->memory.store32(cpu, address, value, 0);
121		break;
122	}
123}
124
125static int _hexDigit(char digit) {
126	switch (digit) {
127	case '0':
128	case '1':
129	case '2':
130	case '3':
131	case '4':
132	case '5':
133	case '6':
134	case '7':
135	case '8':
136	case '9':
137		return digit - '0';
138
139	case 'a':
140	case 'b':
141	case 'c':
142	case 'd':
143	case 'e':
144	case 'f':
145		return digit - 'a' + 10;
146
147	case 'A':
148	case 'B':
149	case 'C':
150	case 'D':
151	case 'E':
152	case 'F':
153		return digit - 'A' + 10;
154
155	default:
156		return -1;
157	}
158}
159
160static const char* _hex32(const char* line, uint32_t* out) {
161	uint32_t value = 0;
162	int i;
163	for (i = 0; i < 8; ++i, ++line) {
164		char digit = *line;
165		value <<= 4;
166		int nybble = _hexDigit(digit);
167		if (nybble < 0) {
168			return 0;
169		}
170		value |= nybble;
171	}
172	*out = value;
173	return line;
174}
175
176static const char* _hex16(const char* line, uint16_t* out) {
177	uint16_t value = 0;
178	int i;
179	for (i = 0; i < 4; ++i, ++line) {
180		char digit = *line;
181		value <<= 4;
182		int nybble = _hexDigit(digit);
183		if (nybble < 0) {
184			return 0;
185		}
186		value |= nybble;
187	}
188	*out = value;
189	return line;
190}
191
192static void _registerLine(struct GBACheatSet* cheats, const char* line) {
193	*StringListAppend(&cheats->lines) = strdup(line);
194}
195
196// http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
197static void _decryptGameShark(uint32_t* op1, uint32_t* op2, const uint32_t* seeds) {
198	uint32_t sum = 0xC6EF3720;
199	int i;
200	for (i = 0; i < 32; ++i) {
201		*op2 -= ((*op1 << 4) + seeds[2]) ^ (*op1 + sum) ^ ((*op1 >> 5) + seeds[3]);
202		*op1 -= ((*op2 << 4) + seeds[0]) ^ (*op2 + sum) ^ ((*op2 >> 5) + seeds[1]);
203		sum -= 0x9E3779B9;
204	}
205}
206
207static void _reseedGameShark(uint32_t* seeds, uint16_t params, const uint8_t* t1, const uint8_t* t2) {
208	int x, y;
209	int s0 = params >> 8;
210	int s1 = params & 0xFF;
211	for (y = 0; y < 4; ++y) {
212		for (x = 0; x < 4; ++x) {
213			uint8_t z = t1[(s0 + x) & 0xFF] + t2[(s1 + y) & 0xFF];
214			seeds[y] <<= 8;
215			seeds[y] |= z;
216		}
217	}
218}
219
220static void _setGameSharkVersion(struct GBACheatSet* cheats, int version) {
221	cheats->gsaVersion = 1;
222	switch (version) {
223	case 1:
224		memcpy(cheats->gsaSeeds, _gsa1S, 4 * sizeof(uint32_t));
225		break;
226	case 3:
227		memcpy(cheats->gsaSeeds, _par3S, 4 * sizeof(uint32_t));
228		break;
229	}
230}
231
232static bool _addGSA1(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) {
233	enum GBAGameSharkType type = op1 >> 28;
234	struct GBACheat* cheat = 0;
235
236	if (cheats->incompleteCheat) {
237		if (cheats->remainingAddresses > 0) {
238			cheat = GBACheatListAppend(&cheats->list);
239			cheat->type = CHEAT_ASSIGN;
240			cheat->width = 4;
241			cheat->address = op1;
242			cheat->operand = cheats->incompleteCheat->operand;
243			cheat->repeat = 1;
244			--cheats->remainingAddresses;
245		}
246		if (cheats->remainingAddresses > 0) {
247			cheat = GBACheatListAppend(&cheats->list);
248			cheat->type = CHEAT_ASSIGN;
249			cheat->width = 4;
250			cheat->address = op2;
251			cheat->operand = cheats->incompleteCheat->operand;
252			cheat->repeat = 1;
253			--cheats->remainingAddresses;
254		}
255		if (cheats->remainingAddresses == 0) {
256			cheats->incompleteCheat = 0;
257		}
258		return true;
259	}
260
261	switch (type) {
262	case GSA_ASSIGN_1:
263		cheat = GBACheatListAppend(&cheats->list);
264		cheat->type = CHEAT_ASSIGN;
265		cheat->width = 1;
266		cheat->address = op1 & 0x0FFFFFFF;
267		break;
268	case GSA_ASSIGN_2:
269		cheat = GBACheatListAppend(&cheats->list);
270		cheat->type = CHEAT_ASSIGN;
271		cheat->width = 2;
272		cheat->address = op1 & 0x0FFFFFFF;
273		break;
274	case GSA_ASSIGN_4:
275		cheat = GBACheatListAppend(&cheats->list);
276		cheat->type = CHEAT_ASSIGN;
277		cheat->width = 4;
278		cheat->address = op1 & 0x0FFFFFFF;
279		break;
280	case GSA_ASSIGN_LIST:
281		cheats->remainingAddresses = (op1 & 0xFFFF) - 1;
282		cheat = GBACheatListAppend(&cheats->list);
283		cheat->type = CHEAT_ASSIGN;
284		cheat->width = 4;
285		cheat->address = op2;
286		cheats->incompleteCheat = cheat;
287		break;
288	case GSA_PATCH:
289		cheats->romPatches[0].address = (op1 & 0xFFFFFF) << 1;
290		cheats->romPatches[0].newValue = op2;
291		cheats->romPatches[0].applied = false;
292		cheats->romPatches[0].exists = true;
293		return true;
294	case GSA_BUTTON:
295		// TODO: Implement button
296		return false;
297	case GSA_IF_EQ:
298		if (op1 == 0xDEADFACE) {
299			_reseedGameShark(cheats->gsaSeeds, op2, _gsa1T1, _gsa1T2);
300			return true;
301		}
302		cheat = GBACheatListAppend(&cheats->list);
303		cheat->type = CHEAT_IF_EQ;
304		cheat->width = 2;
305		cheat->address = op1 & 0x0FFFFFFF;
306		break;
307	case GSA_IF_EQ_RANGE:
308		cheat = GBACheatListAppend(&cheats->list);
309		cheat->type = CHEAT_IF_EQ;
310		cheat->width = 2;
311		cheat->address = op2 & 0x0FFFFFFF;
312		cheat->operand = op1 & 0xFFFF;
313		cheat->repeat = (op1 >> 16) & 0xFF;
314		return true;
315	case GSA_HOOK:
316		if (cheats->hook) {
317			return false;
318		}
319		cheats->hook = malloc(sizeof(*cheats->hook));
320		cheats->hook->address = BASE_CART0 | (op1 & (SIZE_CART0 - 1));
321		cheats->hook->mode = MODE_THUMB;
322		cheats->hook->refs = 1;
323		cheats->hook->reentries = 0;
324		return true;
325	default:
326		return false;
327	}
328	cheat->operand = op2;
329	cheat->repeat = 1;
330	return true;
331}
332
333static bool _addGSA3(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) {
334	// TODO
335	UNUSED(cheats);
336	UNUSED(op1);
337	UNUSED(op2);
338	return false;
339}
340
341static void _addBreakpoint(struct GBACheatDevice* device, struct GBACheatSet* cheats) {
342	if (!device->p || !cheats->hook) {
343		return;
344	}
345	++cheats->hook->reentries;
346	if (cheats->hook->reentries > 1) {
347		return;
348	}
349	GBASetBreakpoint(device->p, &device->d, cheats->hook->address, cheats->hook->mode, &cheats->hook->patchedOpcode);
350}
351
352static void _removeBreakpoint(struct GBACheatDevice* device, struct GBACheatSet* cheats) {
353	if (!device->p || !cheats->hook) {
354		return;
355	}
356	--cheats->hook->reentries;
357	if (cheats->hook->reentries > 0) {
358		return;
359	}
360	GBAClearBreakpoint(device->p, cheats->hook->address, cheats->hook->mode, cheats->hook->patchedOpcode);
361}
362
363static void _patchROM(struct GBACheatDevice* device, struct GBACheatSet* cheats) {
364	if (!device->p) {
365		return;
366	}
367	int i;
368	for (i = 0; i < MAX_ROM_PATCHES; ++i) {
369		if (!cheats->romPatches[i].exists || cheats->romPatches[i].applied) {
370			continue;
371		}
372		GBAPatch16(device->p->cpu, cheats->romPatches[i].address, cheats->romPatches[i].newValue, &cheats->romPatches[i].oldValue);
373		cheats->romPatches[i].applied = true;
374	}
375}
376
377static void _unpatchROM(struct GBACheatDevice* device, struct GBACheatSet* cheats) {
378	if (!device->p) {
379		return;
380	}
381	int i;
382	for (i = 0; i < MAX_ROM_PATCHES; ++i) {
383		if (!cheats->romPatches[i].exists || !cheats->romPatches[i].applied) {
384			continue;
385		}
386		GBAPatch16(device->p->cpu, cheats->romPatches[i].address, cheats->romPatches[i].oldValue, 0);
387		cheats->romPatches[i].applied = false;
388	}
389}
390
391static void GBACheatDeviceInit(struct ARMCore*, struct ARMComponent*);
392static void GBACheatDeviceDeinit(struct ARMComponent*);
393
394void GBACheatDeviceCreate(struct GBACheatDevice* device) {
395	device->d.id = GBA_CHEAT_DEVICE_ID;
396	device->d.init = GBACheatDeviceInit;
397	device->d.deinit = GBACheatDeviceDeinit;
398	GBACheatSetsInit(&device->cheats, 4);
399}
400
401void GBACheatDeviceDestroy(struct GBACheatDevice* device) {
402	size_t i;
403	for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) {
404		struct GBACheatSet* set = *GBACheatSetsGetPointer(&device->cheats, i);
405		GBACheatSetDeinit(set);
406		free(set);
407	}
408	GBACheatSetsDeinit(&device->cheats);
409}
410
411void GBACheatSetInit(struct GBACheatSet* set, const char* name) {
412	GBACheatListInit(&set->list, 4);
413	StringListInit(&set->lines, 4);
414	set->incompleteCheat = 0;
415	set->gsaVersion = 0;
416	set->remainingAddresses = 0;
417	set->hook = 0;
418	int i;
419	for (i = 0; i < MAX_ROM_PATCHES; ++i) {
420		set->romPatches[i].exists = false;
421	}
422	if (name) {
423		set->name = strdup(name);
424	} else {
425		set->name = 0;
426	}
427}
428
429void GBACheatSetDeinit(struct GBACheatSet* set) {
430	GBACheatListDeinit(&set->list);
431	size_t i;
432	for (i = 0; i < StringListSize(&set->lines); ++i) {
433		free(*StringListGetPointer(&set->lines, i));
434	}
435	if (set->name) {
436		free(set->name);
437	}
438	if (set->hook) {
439		--set->hook->refs;
440		if (set->hook->refs == 0) {
441			free(set->hook);
442		}
443	}
444}
445
446void GBACheatAttachDevice(struct GBA* gba, struct GBACheatDevice* device) {
447	if (gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE]) {
448		ARMHotplugDetach(gba->cpu, GBA_COMPONENT_CHEAT_DEVICE);
449	}
450	gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE] = &device->d;
451	ARMHotplugAttach(gba->cpu, GBA_COMPONENT_CHEAT_DEVICE);
452}
453
454void GBACheatAddSet(struct GBACheatDevice* device, struct GBACheatSet* cheats) {
455	*GBACheatSetsAppend(&device->cheats) = cheats;
456	_addBreakpoint(device, cheats);
457	_patchROM(device, cheats);
458}
459
460void GBACheatRemoveSet(struct GBACheatDevice* device, struct GBACheatSet* cheats) {
461	size_t i;
462	for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) {
463		if (*GBACheatSetsGetPointer(&device->cheats, i) == cheats) {
464			break;
465		}
466	}
467	if (i == GBACheatSetsSize(&device->cheats)) {
468		return;
469	}
470	GBACheatSetsShift(&device->cheats, i, 1);
471	_unpatchROM(device, cheats);
472	_removeBreakpoint(device, cheats);
473}
474
475bool GBACheatAddCodeBreaker(struct GBACheatSet* cheats, uint32_t op1, uint16_t op2) {
476	char line[14] = "XXXXXXXX XXXX";
477	snprintf(line, sizeof(line), "%08X %04X", op1, op2);
478	_registerLine(cheats, line);
479
480	enum GBACodeBreakerType type = op1 >> 28;
481	struct GBACheat* cheat = 0;
482
483	if (cheats->incompleteCheat) {
484		cheats->incompleteCheat->repeat = op1 & 0xFFFF;
485		cheats->incompleteCheat->addressOffset = op2;
486		cheats->incompleteCheat->operandOffset = 0;
487		cheats->incompleteCheat = 0;
488		return true;
489	}
490
491	switch (type) {
492	case CB_GAME_ID:
493		// TODO: Run checksum
494		return true;
495	case CB_HOOK:
496		if (cheats->hook) {
497			return false;
498		}
499		cheats->hook = malloc(sizeof(*cheats->hook));
500		cheats->hook->address = BASE_CART0 | (op1 & (SIZE_CART0 - 1));
501		cheats->hook->mode = MODE_THUMB;
502		cheats->hook->refs = 1;
503		cheats->hook->reentries = 0;
504		return true;
505	case CB_OR_2:
506		cheat = GBACheatListAppend(&cheats->list);
507		cheat->type = CHEAT_OR;
508		cheat->width = 2;
509		break;
510	case CB_ASSIGN_1:
511		cheat = GBACheatListAppend(&cheats->list);
512		cheat->type = CHEAT_ASSIGN;
513		cheat->width = 1;
514		break;
515	case CB_FILL:
516		cheat = GBACheatListAppend(&cheats->list);
517		cheat->type = CHEAT_ASSIGN;
518		cheat->width = 2;
519		cheats->incompleteCheat = cheat;
520		break;
521	case CB_FILL_8:
522		GBALog(0, GBA_LOG_STUB, "[Cheat] CodeBreaker code %08X %04X not supported", op1, op2);
523		return false;
524	case CB_AND_2:
525		cheat = GBACheatListAppend(&cheats->list);
526		cheat->type = CHEAT_AND;
527		cheat->width = 2;
528		break;
529	case CB_IF_EQ:
530		cheat = GBACheatListAppend(&cheats->list);
531		cheat->type = CHEAT_IF_EQ;
532		cheat->width = 2;
533		break;
534	case CB_ASSIGN_2:
535		cheat = GBACheatListAppend(&cheats->list);
536		cheat->type = CHEAT_ASSIGN;
537		cheat->width = 2;
538		break;
539	case CB_ENCRYPT:
540		GBALog(0, GBA_LOG_STUB, "[Cheat] CodeBreaker encryption not supported");
541		return false;
542	case CB_IF_NE:
543		cheat = GBACheatListAppend(&cheats->list);
544		cheat->type = CHEAT_IF_NE;
545		cheat->width = 2;
546		break;
547	case CB_IF_GT:
548		cheat = GBACheatListAppend(&cheats->list);
549		cheat->type = CHEAT_IF_GT;
550		cheat->width = 2;
551		break;
552	case CB_IF_LT:
553		cheat = GBACheatListAppend(&cheats->list);
554		cheat->type = CHEAT_IF_LT;
555		cheat->width = 2;
556		break;
557	case CB_IF_SPECIAL:
558		switch (op1 & 0x0FFFFFFF) {
559		case 0x20:
560			cheat = GBACheatListAppend(&cheats->list);
561			cheat->type = CHEAT_IF_AND;
562			cheat->width = 2;
563			cheat->address = BASE_IO | REG_JOYSTAT;
564			cheat->operand = op2;
565			cheat->repeat = 1;
566			return true;
567		default:
568			GBALog(0, GBA_LOG_STUB, "[Cheat] CodeBreaker code %08X %04X not supported", op1, op2);
569			return false;
570		}
571	case CB_ADD_2:
572		cheat = GBACheatListAppend(&cheats->list);
573		cheat->type = CHEAT_ADD;
574		cheat->width = 2;
575		break;
576	case CB_IF_AND:
577		cheat = GBACheatListAppend(&cheats->list);
578		cheat->type = CHEAT_IF_AND;
579		cheat->width = 2;
580		break;
581	}
582
583	cheat->address = op1 & 0x0FFFFFFF;
584	cheat->operand = op2;
585	cheat->repeat = 1;
586	return true;
587}
588
589bool GBACheatAddCodeBreakerLine(struct GBACheatSet* cheats, const char* line) {
590	uint32_t op1;
591	uint16_t op2;
592	line = _hex32(line, &op1);
593	if (!line) {
594		return false;
595	}
596	while (*line == ' ') {
597		++line;
598	}
599	line = _hex16(line, &op2);
600	if (!line) {
601		return false;
602	}
603	return GBACheatAddCodeBreaker(cheats, op1, op2);
604}
605
606bool GBACheatAddGameShark(struct GBACheatSet* set, uint32_t op1, uint32_t op2) {
607	uint32_t o1 = op1;
608	uint32_t o2 = op2;
609	char line[18] = "XXXXXXXX XXXXXXXX";
610	snprintf(line, sizeof(line), "%08X %08X", op1, op2);
611	_registerLine(set, line);
612
613	switch (set->gsaVersion) {
614	case 0:
615		_setGameSharkVersion(set, 1);
616		// Fall through
617	case 1:
618		_decryptGameShark(&o1, &o2, set->gsaSeeds);
619		return _addGSA1(set, o1, o2);
620	}
621	return false;
622}
623
624bool GBACheatAddGameSharkLine(struct GBACheatSet* cheats, const char* line) {
625	uint32_t op1;
626	uint32_t op2;
627	line = _hex32(line, &op1);
628	if (!line) {
629		return false;
630	}
631	while (*line == ' ') {
632		++line;
633	}
634	line = _hex32(line, &op2);
635	if (!line) {
636		return false;
637	}
638	return GBACheatAddGameShark(cheats, op1, op2);
639}
640
641bool GBACheatAddAutodetect(struct GBACheatSet* set, uint32_t op1, uint32_t op2) {
642	uint32_t o1 = op1;
643	uint32_t o2 = op2;
644	char line[18] = "XXXXXXXX XXXXXXXX";
645	snprintf(line, sizeof(line), "%08X %08X", op1, op2);
646	_registerLine(set, line);
647
648	switch (set->gsaVersion) {
649	case 0:
650		// Try to detect GameShark version
651		_decryptGameShark(&o1, &o2, _gsa1S);
652		if ((o1 & 0xF0000000) == 0xF0000000 && !(o2 & 0xFFFFFCFE)) {
653			_setGameSharkVersion(set, 1);
654			return _addGSA1(set, o1, o2);
655		}
656		o1 = op1;
657		o2 = op2;
658		_decryptGameShark(&o1, &o2, _par3S);
659		if ((o1 & 0xFE000000) == 0xC4000000 && !(o2 & 0xFFFF0000)) {
660			_setGameSharkVersion(set, 3);
661			return _addGSA3(set, o1, o2);
662		}
663		break;
664	case 1:
665		_decryptGameShark(&o1, &o2, set->gsaSeeds);
666		return _addGSA1(set, o1, o2);
667	case 3:
668		_decryptGameShark(&o1, &o2, set->gsaSeeds);
669		return _addGSA3(set, o1, o2);
670	}
671	return false;
672}
673
674bool GBACheatAutodetectLine(struct GBACheatSet* cheats, const char* line) {
675	uint32_t op1;
676	uint32_t op2;
677	line = _hex32(line, &op1);
678	if (!line) {
679		return false;
680	}
681	while (*line == ' ') {
682		++line;
683	}
684	line = _hex32(line, &op2);
685	if (!line) {
686		return false;
687	}
688	return GBACheatAddAutodetect(cheats, op1, op2);
689}
690
691bool GBACheatParseFile(struct GBACheatDevice* device, struct VFile* vf) {
692	char cheat[MAX_LINE_LENGTH];
693	struct GBACheatSet* set = 0;
694	struct GBACheatSet* newSet;
695	int gsaVersion = 0;
696	while (true) {
697		size_t i = 0;
698		ssize_t bytesRead = vf->readline(vf, cheat, sizeof(cheat));
699		if (bytesRead == 0) {
700			break;
701		}
702		if (bytesRead < 0) {
703			return false;
704		}
705		while (isspace(cheat[i])) {
706			++i;
707		}
708		switch (cheat[i]) {
709		case '#':
710			do {
711				++i;
712			} while (isspace(cheat[i]));
713			newSet = malloc(sizeof(*set));
714			GBACheatSetInit(newSet, &cheat[i]);
715			if (set) {
716				GBACheatAddSet(device, set);
717				newSet->gsaVersion = set->gsaVersion;
718				memcpy(newSet->gsaSeeds, set->gsaSeeds, sizeof(newSet->gsaSeeds));
719				if (set->hook) {
720					newSet->hook = set->hook;
721					++newSet->hook->refs;
722				}
723			} else {
724				_setGameSharkVersion(newSet, gsaVersion);
725			}
726			set = newSet;
727			break;
728		case '!':
729			do {
730				++i;
731			} while (isspace(cheat[i]));
732			if (strncasecmp(&cheat[i], "GSAv", 4) == 0 || strncasecmp(&cheat[i], "PARv", 4) == 0) {
733				i += 4;
734				gsaVersion = atoi(&cheat[i]);
735			}
736			break;
737		default:
738			if (!set) {
739				set = malloc(sizeof(*set));
740				GBACheatSetInit(set, 0);
741				_setGameSharkVersion(set, gsaVersion);
742			}
743			GBACheatAddLine(set, cheat);
744			break;
745		}
746	}
747	if (set) {
748		GBACheatAddSet(device, set);
749	}
750	return true;
751}
752
753bool GBACheatAddLine(struct GBACheatSet* cheats, const char* line) {
754	uint32_t op1;
755	uint16_t op2;
756	uint16_t op3;
757	line = _hex32(line, &op1);
758	if (!line) {
759		return false;
760	}
761	while (isspace(line[0])) {
762		++line;
763	}
764	line = _hex16(line, &op2);
765	if (!line) {
766		return false;
767	}
768	if (!line[0] || isspace(line[0])) {
769		return GBACheatAddCodeBreaker(cheats, op1, op2);
770	}
771	line = _hex16(line, &op3);
772	if (!line) {
773		return false;
774	}
775	uint32_t realOp2 = op2;
776	realOp2 <<= 16;
777	realOp2 |= op3;
778	return GBACheatAddAutodetect(cheats, op1, realOp2);
779}
780
781void GBACheatRefresh(struct GBACheatDevice* device, struct GBACheatSet* cheats) {
782	bool condition = true;
783	int conditionRemaining = 0;
784	_patchROM(device, cheats);
785
786	size_t nCodes = GBACheatListSize(&cheats->list);
787	size_t i;
788	for (i = 0; i < nCodes; ++i) {
789		if (conditionRemaining > 0) {
790			--conditionRemaining;
791			if (!condition) {
792				continue;
793			}
794		} else {
795			condition = true;
796		}
797		struct GBACheat* cheat = GBACheatListGetPointer(&cheats->list, i);
798		int32_t value = 0;
799		int32_t operand = cheat->operand;
800		uint32_t operationsRemaining = cheat->repeat;
801		uint32_t address = cheat->address;
802		bool performAssignment = false;
803		for (; operationsRemaining; --operationsRemaining) {
804			switch (cheat->type) {
805			case CHEAT_ASSIGN:
806				value = operand;
807				performAssignment = true;
808				break;
809			case CHEAT_AND:
810				value = _readMem(device->p->cpu, address, cheat->width) & operand;
811				performAssignment = true;
812				break;
813			case CHEAT_ADD:
814				value = _readMem(device->p->cpu, address, cheat->width) + operand;
815				performAssignment = true;
816				break;
817			case CHEAT_OR:
818				value = _readMem(device->p->cpu, address, cheat->width) | operand;
819				performAssignment = true;
820				break;
821			case CHEAT_IF_EQ:
822				condition = _readMem(device->p->cpu, address, cheat->width) == operand;
823				conditionRemaining = cheat->repeat;
824				break;
825			case CHEAT_IF_NE:
826				condition = _readMem(device->p->cpu, address, cheat->width) != operand;
827				conditionRemaining = cheat->repeat;
828				break;
829			case CHEAT_IF_LT:
830				condition = _readMem(device->p->cpu, address, cheat->width) < operand;
831				conditionRemaining = cheat->repeat;
832				break;
833			case CHEAT_IF_GT:
834				condition = _readMem(device->p->cpu, address, cheat->width) > operand;
835				conditionRemaining = cheat->repeat;
836				break;
837			case CHEAT_IF_ULT:
838				condition = (uint32_t) _readMem(device->p->cpu, address, cheat->width) < (uint32_t) operand;
839				conditionRemaining = cheat->repeat;
840				break;
841			case CHEAT_IF_UGT:
842				condition = (uint32_t) _readMem(device->p->cpu, address, cheat->width) > (uint32_t) operand;
843				conditionRemaining = cheat->repeat;
844				break;
845			case CHEAT_IF_AND:
846				condition = _readMem(device->p->cpu, address, cheat->width) & operand;
847				conditionRemaining = cheat->repeat;
848				break;
849			case CHEAT_IF_LAND:
850				condition = _readMem(device->p->cpu, address, cheat->width) && operand;
851				conditionRemaining = cheat->repeat;
852				break;
853			}
854
855			if (performAssignment) {
856				_writeMem(device->p->cpu, address, cheat->width, value);
857			}
858
859			address += cheat->addressOffset;
860			operand += cheat->operandOffset;
861		}
862	}
863}
864
865void GBACheatDeviceInit(struct ARMCore* cpu, struct ARMComponent* component) {
866	struct GBACheatDevice* device = (struct GBACheatDevice*) component;
867	device->p = (struct GBA*) cpu->master;
868	size_t i;
869	for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) {
870		struct GBACheatSet* cheats = *GBACheatSetsGetPointer(&device->cheats, i);
871		_addBreakpoint(device, cheats);
872		_patchROM(device, cheats);
873	}
874}
875
876void GBACheatDeviceDeinit(struct ARMComponent* component) {
877	struct GBACheatDevice* device = (struct GBACheatDevice*) component;
878	size_t i;
879	for (i = GBACheatSetsSize(&device->cheats); i--;) {
880		struct GBACheatSet* cheats = *GBACheatSetsGetPointer(&device->cheats, i);
881		_unpatchROM(device, cheats);
882		_removeBreakpoint(device, cheats);
883	}
884}