all repos — mgba @ a59ffbc9a698ceda535dcff7f5cfb9004a6c2ae8

mGBA Game Boy Advance Emulator

src/gba/cheats/gameshark.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 <mgba/internal/gba/cheats.h>
  7
  8#include "gba/cheats/gameshark.h"
  9#include "gba/cheats/parv3.h"
 10#include <mgba/internal/gba/gba.h>
 11#include <mgba-util/string.h>
 12
 13const uint32_t GBACheatGameSharkSeeds[4] = { 0x09F4FBBD, 0x9681884A, 0x352027E9, 0xF3DEE5A7 };
 14
 15static const uint8_t _gsa1T1[256] = {
 16	0x31, 0x1C, 0x23, 0xE5, 0x89, 0x8E, 0xA1, 0x37, 0x74, 0x6D, 0x67, 0xFC, 0x1F, 0xC0, 0xB1, 0x94,
 17	0x3B, 0x05, 0x56, 0x86, 0x00, 0x24, 0xF0, 0x17, 0x72, 0xA2, 0x3D, 0x1B, 0xE3, 0x17, 0xC5, 0x0B,
 18	0xB9, 0xE2, 0xBD, 0x58, 0x71, 0x1B, 0x2C, 0xFF, 0xE4, 0xC9, 0x4C, 0x5E, 0xC9, 0x55, 0x33, 0x45,
 19	0x7C, 0x3F, 0xB2, 0x51, 0xFE, 0x10, 0x7E, 0x75, 0x3C, 0x90, 0x8D, 0xDA, 0x94, 0x38, 0xC3, 0xE9,
 20	0x95, 0xEA, 0xCE, 0xA6, 0x06, 0xE0, 0x4F, 0x3F, 0x2A, 0xE3, 0x3A, 0xE4, 0x43, 0xBD, 0x7F, 0xDA,
 21	0x55, 0xF0, 0xEA, 0xCB, 0x2C, 0xA8, 0x47, 0x61, 0xA0, 0xEF, 0xCB, 0x13, 0x18, 0x20, 0xAF, 0x3E,
 22	0x4D, 0x9E, 0x1E, 0x77, 0x51, 0xC5, 0x51, 0x20, 0xCF, 0x21, 0xF9, 0x39, 0x94, 0xDE, 0xDD, 0x79,
 23	0x4E, 0x80, 0xC4, 0x9D, 0x94, 0xD5, 0x95, 0x01, 0x27, 0x27, 0xBD, 0x6D, 0x78, 0xB5, 0xD1, 0x31,
 24	0x6A, 0x65, 0x74, 0x74, 0x58, 0xB3, 0x7C, 0xC9, 0x5A, 0xED, 0x50, 0x03, 0xC4, 0xA2, 0x94, 0x4B,
 25	0xF0, 0x58, 0x09, 0x6F, 0x3E, 0x7D, 0xAE, 0x7D, 0x58, 0xA0, 0x2C, 0x91, 0xBB, 0xE1, 0x70, 0xEB,
 26	0x73, 0xA6, 0x9A, 0x44, 0x25, 0x90, 0x16, 0x62, 0x53, 0xAE, 0x08, 0xEB, 0xDC, 0xF0, 0xEE, 0x77,
 27	0xC2, 0xDE, 0x81, 0xE8, 0x30, 0x89, 0xDB, 0xFE, 0xBC, 0xC2, 0xDF, 0x26, 0xE9, 0x8B, 0xD6, 0x93,
 28	0xF0, 0xCB, 0x56, 0x90, 0xC0, 0x46, 0x68, 0x15, 0x43, 0xCB, 0xE9, 0x98, 0xE3, 0xAF, 0x31, 0x25,
 29	0x4D, 0x7B, 0xF3, 0xB1, 0x74, 0xE2, 0x64, 0xAC, 0xD9, 0xF6, 0xA0, 0xD5, 0x0B, 0x9B, 0x49, 0x52,
 30	0x69, 0x3B, 0x71, 0x00, 0x2F, 0xBB, 0xBA, 0x08, 0xB1, 0xAE, 0xBB, 0xB3, 0xE1, 0xC9, 0xA6, 0x7F,
 31	0x17, 0x97, 0x28, 0x72, 0x12, 0x6E, 0x91, 0xAE, 0x3A, 0xA2, 0x35, 0x46, 0x27, 0xF8, 0x12, 0x50
 32};
 33
 34static const uint8_t _gsa1T2[256] = {
 35	0xD8, 0x65, 0x04, 0xC2, 0x65, 0xD5, 0xB0, 0x0C, 0xDF, 0x9D, 0xF0, 0xC3, 0x9A, 0x17, 0xC9, 0xA6,
 36	0xE1, 0xAC, 0x0D, 0x14, 0x2F, 0x3C, 0x2C, 0x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D, 0xD9, 0xC0, 0x5B, 0x08, 0x35, 0x05, 0xC1, 0x75,
 48	0x4F, 0xD0, 0x51, 0x2D, 0x2E, 0x5E, 0x69, 0xE7, 0x3B, 0xC2, 0xDA, 0xFF, 0xF6, 0xCE, 0x3E, 0x76,
 49	0xE8, 0x36, 0x8C, 0x39, 0xD8, 0xF3, 0xE9, 0xA6, 0x42, 0xE6, 0xC1, 0x4C, 0x05, 0xBE, 0x17, 0xF2,
 50	0x5C, 0x1B, 0x19, 0xDB, 0x0F, 0xF3, 0xF8, 0x49, 0xEB, 0x36, 0xF6, 0x40, 0x6F, 0xAD, 0xC1, 0x8C
 51};
 52
 53// http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
 54void GBACheatDecryptGameShark(uint32_t* op1, uint32_t* op2, const uint32_t* seeds) {
 55	uint32_t sum = 0xC6EF3720;
 56	int i;
 57	for (i = 0; i < 32; ++i) {
 58		*op2 -= ((*op1 << 4) + seeds[2]) ^ (*op1 + sum) ^ ((*op1 >> 5) + seeds[3]);
 59		*op1 -= ((*op2 << 4) + seeds[0]) ^ (*op2 + sum) ^ ((*op2 >> 5) + seeds[1]);
 60		sum -= 0x9E3779B9;
 61	}
 62}
 63
 64void GBACheatReseedGameShark(uint32_t* seeds, uint16_t params, const uint8_t* t1, const uint8_t* t2) {
 65	int x, y;
 66	int s0 = params >> 8;
 67	int s1 = params & 0xFF;
 68	for (y = 0; y < 4; ++y) {
 69		for (x = 0; x < 4; ++x) {
 70			uint8_t z = t1[(s0 + x) & 0xFF] + t2[(s1 + y) & 0xFF];
 71			seeds[y] <<= 8;
 72			seeds[y] |= z;
 73		}
 74	}
 75}
 76
 77void GBACheatSetGameSharkVersion(struct GBACheatSet* cheats, enum GBACheatGameSharkVersion version) {
 78	cheats->gsaVersion = version;
 79	switch (version) {
 80	case GBA_GS_GSAV1:
 81	case GBA_GS_GSAV1_RAW:
 82		memcpy(cheats->gsaSeeds, GBACheatGameSharkSeeds, 4 * sizeof(uint32_t));
 83		break;
 84	case GBA_GS_PARV3:
 85	case GBA_GS_PARV3_RAW:
 86		memcpy(cheats->gsaSeeds, GBACheatProActionReplaySeeds, 4 * sizeof(uint32_t));
 87		break;
 88	default:
 89		break;
 90	}
 91}
 92
 93bool GBACheatAddGameSharkRaw(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) {
 94	enum GBAGameSharkType type = op1 >> 28;
 95	struct mCheat* cheat = 0;
 96	struct GBACheatPatch* romPatch;
 97
 98	if (cheats->incompleteCheat != COMPLETE) {
 99		struct mCheat* incompleteCheat = mCheatListGetPointer(&cheats->d.list, cheats->incompleteCheat);
100		if (cheats->remainingAddresses > 0) {
101			cheat = mCheatListAppend(&cheats->d.list);
102			cheat->type = CHEAT_ASSIGN;
103			cheat->width = 4;
104			cheat->address = op1;
105			cheat->operand = incompleteCheat->operand;
106			cheat->repeat = 1;
107			--cheats->remainingAddresses;
108		}
109		if (cheats->remainingAddresses > 0) {
110			cheat = mCheatListAppend(&cheats->d.list);
111			cheat->type = CHEAT_ASSIGN;
112			cheat->width = 4;
113			cheat->address = op2;
114			cheat->operand = incompleteCheat->operand;
115			cheat->repeat = 1;
116			--cheats->remainingAddresses;
117		}
118		if (cheats->remainingAddresses == 0) {
119			cheats->incompleteCheat = COMPLETE;
120		}
121		return true;
122	}
123
124	switch (type) {
125	case GSA_ASSIGN_1:
126		cheat = mCheatListAppend(&cheats->d.list);
127		cheat->type = CHEAT_ASSIGN;
128		cheat->width = 1;
129		cheat->address = op1 & 0x0FFFFFFF;
130		break;
131	case GSA_ASSIGN_2:
132		cheat = mCheatListAppend(&cheats->d.list);
133		cheat->type = CHEAT_ASSIGN;
134		cheat->width = 2;
135		cheat->address = op1 & 0x0FFFFFFF;
136		break;
137	case GSA_ASSIGN_4:
138		cheat = mCheatListAppend(&cheats->d.list);
139		cheat->type = CHEAT_ASSIGN;
140		cheat->width = 4;
141		cheat->address = op1 & 0x0FFFFFFF;
142		break;
143	case GSA_ASSIGN_LIST:
144		cheats->remainingAddresses = (op1 & 0xFFFF) - 1;
145		cheat = mCheatListAppend(&cheats->d.list);
146		cheat->type = CHEAT_ASSIGN;
147		cheat->width = 4;
148		cheat->address = op2;
149		cheats->incompleteCheat = mCheatListIndex(&cheats->d.list, cheat);
150		break;
151	case GSA_PATCH:
152		romPatch = GBACheatPatchListAppend(&cheats->romPatches);
153		romPatch->address = BASE_CART0 | ((op1 & 0xFFFFFF) << 1);
154		romPatch->newValue = op2;
155		romPatch->applied = false;
156		return true;
157	case GSA_BUTTON:
158		switch (op1 & 0x00F00000) {
159		case 0x00100000:
160			cheat = mCheatListAppend(&cheats->d.list);
161			cheat->type = CHEAT_IF_BUTTON;
162			cheat->repeat = 1;
163			cheat->negativeRepeat = 0;
164			cheat = mCheatListAppend(&cheats->d.list);
165			cheat->type = CHEAT_ASSIGN;
166			cheat->width = 1;
167			cheat->address = op1 & 0x0F0FFFFF;
168			break;
169		case 0x00200000:
170			cheat = mCheatListAppend(&cheats->d.list);
171			cheat->type = CHEAT_IF_BUTTON;
172			cheat->repeat = 1;
173			cheat->negativeRepeat = 0;
174			cheat = mCheatListAppend(&cheats->d.list);
175			cheat->type = CHEAT_ASSIGN;
176			cheat->width = 2;
177			cheat->address = op1 & 0x0F0FFFFF;
178			break;
179		default:
180			mLOG(CHEATS, STUB, "GameShark button type unimplemented");
181			return false;
182		}
183		break;
184	case GSA_IF_EQ:
185		if (op1 == 0xDEADFACE) {
186			GBACheatReseedGameShark(cheats->gsaSeeds, op2, _gsa1T1, _gsa1T2);
187			return true;
188		}
189		cheat = mCheatListAppend(&cheats->d.list);
190		cheat->type = CHEAT_IF_EQ;
191		cheat->width = 2;
192		cheat->address = op1 & 0x0FFFFFFF;
193		break;
194	case GSA_IF_EQ_RANGE:
195		cheat = mCheatListAppend(&cheats->d.list);
196		cheat->type = CHEAT_IF_EQ;
197		cheat->width = 2;
198		cheat->address = op2 & 0x0FFFFFFF;
199		cheat->operand = op1 & 0xFFFF;
200		cheat->repeat = (op1 >> 16) & 0xFF;
201		cheat->negativeRepeat = 0;
202		return true;
203	case GSA_HOOK:
204		if (cheats->hook) {
205			return false;
206		}
207		cheats->hook = malloc(sizeof(*cheats->hook));
208		cheats->hook->address = BASE_CART0 | (op1 & (SIZE_CART0 - 1));
209		cheats->hook->mode = MODE_THUMB;
210		cheats->hook->refs = 1;
211		cheats->hook->reentries = 0;
212		return true;
213	default:
214		return false;
215	}
216	cheat->operand = op2;
217	cheat->repeat = 1;
218	cheat->negativeRepeat = 0;
219	return true;
220}
221
222bool GBACheatAddGameShark(struct GBACheatSet* set, uint32_t op1, uint32_t op2) {
223	uint32_t o1 = op1;
224	uint32_t o2 = op2;
225	char line[18] = "XXXXXXXX XXXXXXXX";
226	snprintf(line, sizeof(line), "%08X %08X", op1, op2);
227
228	switch (set->gsaVersion) {
229	default:
230		GBACheatSetGameSharkVersion(set, GBA_GS_GSAV1);
231	// Fall through
232	case GBA_GS_GSAV1:
233		GBACheatDecryptGameShark(&o1, &o2, set->gsaSeeds);
234	// Fall through
235	case GBA_GS_GSAV1_RAW:
236		return GBACheatAddGameSharkRaw(set, o1, o2);
237	}
238	return false;
239}
240
241bool GBACheatAddGameSharkLine(struct GBACheatSet* cheats, const char* line) {
242	uint32_t op1;
243	uint32_t op2;
244	line = hex32(line, &op1);
245	if (!line) {
246		return false;
247	}
248	while (*line == ' ') {
249		++line;
250	}
251	line = hex32(line, &op2);
252	if (!line) {
253		return false;
254	}
255	return GBACheatAddGameShark(cheats, op1, op2);
256}
257
258int GBACheatGameSharkProbability(uint32_t op1, uint32_t op2) {
259	int probability = 0;
260	if (op2 == 0x001DC0DE) {
261		return 0x100;
262	}
263	uint32_t address = op1 & 0x0FFFFFFF;
264	switch (op1 >> 28) {
265	case GSA_ASSIGN_1:
266		probability += 0x20;
267		if (op2 & 0xFFFFFF00) {
268			probability -= 0x10;
269		}
270		probability += GBACheatAddressIsReal(address);
271		break;
272	case GSA_ASSIGN_2:
273		probability += 0x20;
274		if (op2 & 0xFFFF0000) {
275			probability -= 0x10;
276		}
277		probability += GBACheatAddressIsReal(address);
278		break;
279	case GSA_ASSIGN_4:
280		probability += 0x20;
281		probability += GBACheatAddressIsReal(address);
282		break;
283	case GSA_PATCH:
284		probability += 0x20;
285		if (op2 & 0xCFFF0000) {
286			probability -= 0x10;
287		}
288		break;
289	case GSA_BUTTON:
290		probability += 0x10;
291		break;
292	case GSA_IF_EQ:
293		probability += 0x20;
294		if (op2 & 0xFFFF0000) {
295			probability -= 0x10;
296		}
297		probability += GBACheatAddressIsReal(address);
298		break;
299	case GSA_IF_EQ_RANGE:
300		probability += 0x20;
301		probability += GBACheatAddressIsReal(op2);
302		if (op1 & 0x0F000000) {
303			probability -= 0x10;
304		}
305		break;
306	case GSA_HOOK:
307		probability += 0x20;
308		if (op2 & 0xFFFF0000) {
309			probability -= 0x10;
310		}
311		break;
312	default:
313		probability -= 0x40;
314	}
315	return probability;
316}