all repos — mgba @ 0cd28060e07c587ab10901ce815563a9e998f297

mGBA Game Boy Advance Emulator

src/gba/cheats/parv3.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 "parv3.h"
  7
  8#include "gba/cheats/cheats-private.h"
  9#include "gba/cheats/gameshark.h"
 10#include "gba/gba.h"
 11#include "util/string.h"
 12
 13const uint32_t GBACheatProActionReplaySeeds[4] = { 0x7AA9648F, 0x7FAE6994, 0xC0EFAAD5, 0x42712C57 };
 14
 15static const uint8_t _par3T1[256] = {
 16	0xD0, 0xFF, 0xBA, 0xE5, 0xC1, 0xC7, 0xDB, 0x5B, 0x16, 0xE3, 0x6E, 0x26, 0x62, 0x31, 0x2E, 0x2A,
 17	0xD1, 0xBB, 0x4A, 0xE6, 0xAE, 0x2F, 0x0A, 0x90, 0x29, 0x90, 0xB6, 0x67, 0x58, 0x2A, 0xB4, 0x45,
 18	0x7B, 0xCB, 0xF0, 0x73, 0x84, 0x30, 0x81, 0xC2, 0xD7, 0xBE, 0x89, 0xD7, 0x4E, 0x73, 0x5C, 0xC7,
 19	0x80, 0x1B, 0xE5, 0xE4, 0x43, 0xC7, 0x46, 0xD6, 0x6F, 0x7B, 0xBF, 0xED, 0xE5, 0x27, 0xD1, 0xB5,
 20	0xD0, 0xD8, 0xA3, 0xCB, 0x2B, 0x30, 0xA4, 0xF0, 0x84, 0x14, 0x72, 0x5C, 0xFF, 0xA4, 0xFB, 0x54,
 21	0x9D, 0x70, 0xE2, 0xFF, 0xBE, 0xE8, 0x24, 0x76, 0xE5, 0x15, 0xFB, 0x1A, 0xBC, 0x87, 0x02, 0x2A,
 22	0x58, 0x8F, 0x9A, 0x95, 0xBD, 0xAE, 0x8D, 0x0C, 0xA5, 0x4C, 0xF2, 0x5C, 0x7D, 0xAD, 0x51, 0xFB,
 23	0xB1, 0x22, 0x07, 0xE0, 0x29, 0x7C, 0xEB, 0x98, 0x14, 0xC6, 0x31, 0x97, 0xE4, 0x34, 0x8F, 0xCC,
 24	0x99, 0x56, 0x9F, 0x78, 0x43, 0x91, 0x85, 0x3F, 0xC2, 0xD0, 0xD1, 0x80, 0xD1, 0x77, 0xA7, 0xE2,
 25	0x43, 0x99, 0x1D, 0x2F, 0x8B, 0x6A, 0xE4, 0x66, 0x82, 0xF7, 0x2B, 0x0B, 0x65, 0x14, 0xC0, 0xC2,
 26	0x1D, 0x96, 0x78, 0x1C, 0xC4, 0xC3, 0xD2, 0xB1, 0x64, 0x07, 0xD7, 0x6F, 0x02, 0xE9, 0x44, 0x31,
 27	0xDB, 0x3C, 0xEB, 0x93, 0xED, 0x9A, 0x57, 0x05, 0xB9, 0x0E, 0xAF, 0x1F, 0x48, 0x11, 0xDC, 0x35,
 28	0x6C, 0xB8, 0xEE, 0x2A, 0x48, 0x2B, 0xBC, 0x89, 0x12, 0x59, 0xCB, 0xD1, 0x18, 0xEA, 0x72, 0x11,
 29	0x01, 0x75, 0x3B, 0xB5, 0x56, 0xF4, 0x8B, 0xA0, 0x41, 0x75, 0x86, 0x7B, 0x94, 0x12, 0x2D, 0x4C,
 30	0x0C, 0x22, 0xC9, 0x4A, 0xD8, 0xB1, 0x8D, 0xF0, 0x55, 0x2E, 0x77, 0x50, 0x1C, 0x64, 0x77, 0xAA,
 31	0x3E, 0xAC, 0xD3, 0x3D, 0xCE, 0x60, 0xCA, 0x5D, 0xA0, 0x92, 0x78, 0xC6, 0x51, 0xFE, 0xF9, 0x30
 32};
 33
 34static const uint8_t _par3T2[256] = {
 35	0xAA, 0xAF, 0xF0, 0x72, 0x90, 0xF7, 0x71, 0x27, 0x06, 0x11, 0xEB, 0x9C, 0x37, 0x12, 0x72, 0xAA,
 36	0x65, 0xBC, 0x0D, 0x4A, 0x76, 0xF6, 0x5C, 0xAA, 0xB0, 0x7A, 0x7D, 0x81,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 0x51, 0x9D, 0x3F, 0xF8,
 48	0x62, 0x11, 0x2E, 0x89, 0xD2, 0x9D, 0x35, 0xB1, 0xE4, 0x0A, 0x4D, 0x93, 0x01, 0xA7, 0xD1, 0x2D,
 49	0x00, 0x87, 0xE2, 0x2D, 0xA4, 0xE9, 0x0A, 0x06, 0x66, 0xF8, 0x1F, 0x44, 0x75, 0xB5, 0x6B, 0x1C,
 50	0xFC, 0x31, 0x09, 0x48, 0xA3, 0xFF, 0x92, 0x12, 0x58, 0xE9, 0xFA, 0xAE, 0x4F, 0xE2, 0xB4, 0xCC
 51};
 52static uint32_t _parAddr(uint32_t x) {
 53	return (x & 0xFFFFF) | ((x << 4) & 0x0F000000);
 54}
 55
 56static void _parEndBlock(struct GBACheatSet* cheats) {
 57	size_t size = GBACheatListSize(&cheats->list) - GBACheatListIndex(&cheats->list, cheats->currentBlock);
 58	if (cheats->currentBlock->repeat) {
 59		cheats->currentBlock->negativeRepeat = size - cheats->currentBlock->repeat;
 60	} else {
 61		cheats->currentBlock->repeat = size;
 62	}
 63	cheats->currentBlock = 0;
 64}
 65
 66static void _parElseBlock(struct GBACheatSet* cheats) {
 67	size_t size = GBACheatListSize(&cheats->list) - GBACheatListIndex(&cheats->list, cheats->currentBlock);
 68	cheats->currentBlock->repeat = size;
 69}
 70
 71static bool _addPAR3Cond(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) {
 72	enum GBAActionReplay3Condition condition = op1 & PAR3_COND;
 73	int width = 1 << ((op1 & PAR3_WIDTH) >> PAR3_WIDTH_BASE);
 74	if (width > 4) {
 75		// TODO: Always false conditions
 76		return false;
 77	}
 78	if ((op1 & PAR3_ACTION) == PAR3_ACTION_DISABLE) {
 79		// TODO: Codes that disable
 80		return false;
 81	}
 82
 83	struct GBACheat* cheat = GBACheatListAppend(&cheats->list);
 84	cheat->address = _parAddr(op1);
 85	cheat->width = width;
 86	cheat->operand = op2 & (0xFFFFFFFFU >> ((4 - width) * 8));
 87	cheat->addressOffset = 0;
 88	cheat->operandOffset = 0;
 89
 90	switch (op1 & PAR3_ACTION) {
 91	case PAR3_ACTION_NEXT:
 92		cheat->repeat = 1;
 93		cheat->negativeRepeat = 0;
 94		break;
 95	case PAR3_ACTION_NEXT_TWO:
 96		cheat->repeat = 2;
 97		cheat->negativeRepeat = 0;
 98		break;
 99	case PAR3_ACTION_BLOCK:
100		cheat->repeat = 0;
101		cheat->negativeRepeat = 0;
102		if (cheats->currentBlock) {
103			_parEndBlock(cheats);
104		}
105		cheats->currentBlock = cheat;
106		break;
107	}
108
109	switch (condition) {
110	case PAR3_COND_OTHER:
111		// We shouldn't be able to get here
112		GBALog(0, GBA_LOG_ERROR, "Unexpectedly created 'other' PARv3 code");
113		cheat->type = CHEAT_IF_LAND;
114		cheat->operand = 0;
115		break;
116	case PAR3_COND_EQ:
117		cheat->type = CHEAT_IF_EQ;
118		break;
119	case PAR3_COND_NE:
120		cheat->type = CHEAT_IF_NE;
121		break;
122	case PAR3_COND_LT:
123		cheat->type = CHEAT_IF_LT;
124		break;
125	case PAR3_COND_GT:
126		cheat->type = CHEAT_IF_GT;
127		break;
128	case PAR3_COND_ULT:
129		cheat->type = CHEAT_IF_ULT;
130		break;
131	case PAR3_COND_UGT:
132		cheat->type = CHEAT_IF_UGT;
133		break;
134	case PAR3_COND_LAND:
135		cheat->type = CHEAT_IF_LAND;
136		break;
137	}
138	return true;
139}
140
141static bool _addPAR3Special(struct GBACheatSet* cheats, uint32_t op2) {
142	struct GBACheat* cheat;
143	switch (op2 & 0xFF000000) {
144	case PAR3_OTHER_SLOWDOWN:
145		// TODO: Slowdown
146		return false;
147	case PAR3_OTHER_BUTTON_1:
148	case PAR3_OTHER_BUTTON_2:
149	case PAR3_OTHER_BUTTON_4:
150		// TODO: Button
151		GBALog(0, GBA_LOG_STUB, "GameShark button unimplemented");
152		return false;
153	// TODO: Fix overriding existing patches
154	case PAR3_OTHER_PATCH_1:
155		cheats->romPatches[0].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1);
156		cheats->romPatches[0].applied = false;
157		cheats->romPatches[0].exists = true;
158		cheats->incompletePatch = &cheats->romPatches[0];
159		break;
160	case PAR3_OTHER_PATCH_2:
161		cheats->romPatches[1].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1);
162		cheats->romPatches[1].applied = false;
163		cheats->romPatches[1].exists = true;
164		cheats->incompletePatch = &cheats->romPatches[1];
165		break;
166	case PAR3_OTHER_PATCH_3:
167		cheats->romPatches[2].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1);
168		cheats->romPatches[2].applied = false;
169		cheats->romPatches[2].exists = true;
170		cheats->incompletePatch = &cheats->romPatches[2];
171		break;
172	case PAR3_OTHER_PATCH_4:
173		cheats->romPatches[3].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1);
174		cheats->romPatches[3].applied = false;
175		cheats->romPatches[3].exists = true;
176		cheats->incompletePatch = &cheats->romPatches[3];
177		break;
178	case PAR3_OTHER_ENDIF:
179		if (cheats->currentBlock) {
180			_parEndBlock(cheats);
181			return true;
182		}
183		return false;
184	case PAR3_OTHER_ELSE:
185		if (cheats->currentBlock) {
186			_parElseBlock(cheats);
187			return true;
188		}
189		return false;
190	case PAR3_OTHER_FILL_1:
191		cheat = GBACheatListAppend(&cheats->list);
192		cheat->address = _parAddr(op2);
193		cheat->width = 1;
194		cheats->incompleteCheat = cheat;
195		break;
196	case PAR3_OTHER_FILL_2:
197		cheat = GBACheatListAppend(&cheats->list);
198		cheat->address = _parAddr(op2);
199		cheat->width = 2;
200		cheats->incompleteCheat = cheat;
201		break;
202	case PAR3_OTHER_FILL_4:
203		cheat = GBACheatListAppend(&cheats->list);
204		cheat->address = _parAddr(op2);
205		cheat->width = 3;
206		cheats->incompleteCheat = cheat;
207		break;
208	}
209	return true;
210}
211
212bool GBACheatAddProActionReplayRaw(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) {
213	if (cheats->incompletePatch) {
214		cheats->incompletePatch->newValue = op1;
215		cheats->incompletePatch = 0;
216		return true;
217	}
218	if (cheats->incompleteCheat) {
219		cheats->incompleteCheat->operand = op1 & (0xFFFFFFFFU >> ((4 - cheats->incompleteCheat->width) * 8));
220		cheats->incompleteCheat->addressOffset = op2 >> 24;
221		cheats->incompleteCheat->repeat = (op2 >> 16) & 0xFF;
222		cheats->incompleteCheat->addressOffset = (op2 & 0xFFFF) * cheats->incompleteCheat->width;
223		cheats->incompleteCheat = 0;
224		return true;
225	}
226
227	if (op2 == 0x001DC0DE) {
228		return true;
229	}
230
231	switch (op1) {
232	case 0x00000000:
233		return _addPAR3Special(cheats, op2);
234	case 0xDEADFACE:
235		GBACheatReseedGameShark(cheats->gsaSeeds, op2, _par3T1, _par3T2);
236		return true;
237	}
238
239	if (op1 >> 24 == 0xC4) {
240		if (cheats->hook) {
241			return false;
242		}
243		cheats->hook = malloc(sizeof(*cheats->hook));
244		cheats->hook->address = BASE_CART0 | (op1 & (SIZE_CART0 - 1));
245		cheats->hook->mode = MODE_THUMB;
246		cheats->hook->refs = 1;
247		cheats->hook->reentries = 0;
248		return true;
249	}
250
251	if (op1 & PAR3_COND) {
252		return _addPAR3Cond(cheats, op1, op2);
253	}
254
255	int width = 1 << ((op1 & PAR3_WIDTH) >> PAR3_WIDTH_BASE);
256	struct GBACheat* cheat = GBACheatListAppend(&cheats->list);
257	cheat->address = _parAddr(op1);
258	cheat->operandOffset = 0;
259	cheat->addressOffset = 0;
260	cheat->repeat = 1;
261
262	switch (op1 & PAR3_BASE) {
263	case PAR3_BASE_ASSIGN:
264		cheat->type = CHEAT_ASSIGN;
265		cheat->addressOffset = width;
266		if (width < 4) {
267			cheat->repeat = (op2 >> (width * 8)) + 1;
268		}
269		break;
270	case PAR3_BASE_INDIRECT:
271		cheat->type = CHEAT_ASSIGN_INDIRECT;
272		if (width < 4) {
273			cheat->addressOffset = (op2 >> (width * 8)) * width;
274		}
275		break;
276	case PAR3_BASE_ADD:
277		cheat->type = CHEAT_ADD;
278		break;
279	case PAR3_BASE_OTHER:
280		width = ((op1 >> 24) & 1) + 1;
281		cheat->type = CHEAT_ASSIGN;
282		cheat->address = BASE_IO | (op1 & OFFSET_MASK);
283		break;
284	}
285
286	cheat->width = width;
287	cheat->operand = op2 & (0xFFFFFFFFU >> ((4 - width) * 8));
288	return true;
289}
290
291bool GBACheatAddProActionReplay(struct GBACheatSet* set, uint32_t op1, uint32_t op2) {
292	uint32_t o1 = op1;
293	uint32_t o2 = op2;
294	char line[18] = "XXXXXXXX XXXXXXXX";
295	snprintf(line, sizeof(line), "%08X %08X", op1, op2);
296	GBACheatRegisterLine(set, line);
297
298	switch (set->gsaVersion) {
299	case 0:
300	case 1:
301		GBACheatSetGameSharkVersion(set, 3);
302	// Fall through
303	case 3:
304		GBACheatDecryptGameShark(&o1, &o2, set->gsaSeeds);
305		return GBACheatAddProActionReplayRaw(set, o1, o2);
306	}
307	return false;
308}
309
310bool GBACheatAddProActionReplayLine(struct GBACheatSet* cheats, const char* line) {
311	uint32_t op1;
312	uint32_t op2;
313	line = hex32(line, &op1);
314	if (!line) {
315		return false;
316	}
317	while (*line == ' ') {
318		++line;
319	}
320	line = hex32(line, &op2);
321	if (!line) {
322		return false;
323	}
324	return GBACheatAddProActionReplay(cheats, op1, op2);
325}