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 <mgba/internal/gba/cheats.h>
7
8#include "gba/cheats/gameshark.h"
9#include <mgba/internal/gba/gba.h>
10#include <mgba-util/string.h>
11
12const uint32_t GBACheatProActionReplaySeeds[4] = { 0x7AA9648F, 0x7FAE6994, 0xC0EFAAD5, 0x42712C57 };
13
14static const uint8_t _par3T1[256] = {
15 0xD0, 0xFF, 0xBA, 0xE5, 0xC1, 0xC7, 0xDB, 0x5B, 0x16, 0xE3, 0x6E, 0x26, 0x62, 0x31, 0x2E, 0x2A,
16 0xD1, 0xBB, 0x4A, 0xE6, 0xAE, 0x2F, 0x0A, 0x90, 0x29, 0x90, 0xB6, 0x67, 0x58, 0x2A, 0xB4, 0x45,
17 0x7B, 0xCB, 0xF0, 0x73, 0x84, 0x30, 0x81, 0xC2, 0xD7, 0xBE, 0x89, 0xD7, 0x4E, 0x73, 0x5C, 0xC7,
18 0x80, 0x1B, 0xE5, 0xE4, 0x43, 0xC7, 0x46, 0xD6, 0x6F, 0x7B, 0xBF, 0xED, 0xE5, 0x27, 0xD1, 0xB5,
19 0xD0, 0xD8, 0xA3, 0xCB, 0x2B, 0x30, 0xA4, 0xF0, 0x84, 0x14, 0x72, 0x5C, 0xFF, 0xA4, 0xFB, 0x54,
20 0x9D, 0x70, 0xE2, 0xFF, 0xBE, 0xE8, 0x24, 0x76, 0xE5, 0x15, 0xFB, 0x1A, 0xBC, 0x87, 0x02, 0x2A,
21 0x58, 0x8F, 0x9A, 0x95, 0xBD, 0xAE, 0x8D, 0x0C, 0xA5, 0x4C, 0xF2, 0x5C, 0x7D, 0xAD, 0x51, 0xFB,
22 0xB1, 0x22, 0x07, 0xE0, 0x29, 0x7C, 0xEB, 0x98, 0x14, 0xC6, 0x31, 0x97, 0xE4, 0x34, 0x8F, 0xCC,
23 0x99, 0x56, 0x9F, 0x78, 0x43, 0x91, 0x85, 0x3F, 0xC2, 0xD0, 0xD1, 0x80, 0xD1, 0x77, 0xA7, 0xE2,
24 0x43, 0x99, 0x1D, 0x2F, 0x8B, 0x6A, 0xE4, 0x66, 0x82, 0xF7, 0x2B, 0x0B, 0x65, 0x14, 0xC0, 0xC2,
25 0x1D, 0x96, 0x78, 0x1C, 0xC4, 0xC3, 0xD2, 0xB1, 0x64, 0x07, 0xD7, 0x6F, 0x02, 0xE9, 0x44, 0x31,
26 0xDB, 0x3C, 0xEB, 0x93, 0xED, 0x9A, 0x57, 0x05, 0xB9, 0x0E, 0xAF, 0x1F, 0x48, 0x11, 0xDC, 0x35,
27 0x6C, 0xB8, 0xEE, 0x2A, 0x48, 0x2B, 0xBC, 0x89, 0x12, 0x59, 0xCB, 0xD1, 0x18, 0xEA, 0x72, 0x11,
28 0x01, 0x75, 0x3B, 0xB5, 0x56, 0xF4, 0x8B, 0xA0, 0x41, 0x75, 0x86, 0x7B, 0x94, 0x12, 0x2D, 0x4C,
29 0x0C, 0x22, 0xC9, 0x4A, 0xD8, 0xB1, 0x8D, 0xF0, 0x55, 0x2E, 0x77, 0x50, 0x1C, 0x64, 0x77, 0xAA,
30 0x3E, 0xAC, 0xD3, 0x3D, 0xCE, 0x60, 0xCA, 0x5D, 0xA0, 0x92, 0x78, 0xC6, 0x51, 0xFE, 0xF9, 0x30
31};
32
33static const uint8_t _par3T2[256] = {
34 0xAA, 0xAF, 0xF0, 0x72, 0x90, 0xF7, 0x71, 0x27, 0x06, 0x11, 0xEB, 0x9C, 0x37, 0x12, 0x72, 0xAA,
35 0x65, 0xBC, 0x0D, 0x4A, 0x76, 0xF6, 0x5C, 0xAA, 0xB0, 0x7A, 0x7D, 0x81, 0xC1, 0xCE, 0x2F, 0x9F,
36 0x02, 0x75, 0x38, 0xC8, 0xFC, 0x66, 0x05, 0xC2, 0x2C, 0xBD, 0x91,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 0xA7, 0xD1, 0x2D,
48 0x00, 0x87, 0xE2, 0x2D, 0xA4, 0xE9, 0x0A, 0x06, 0x66, 0xF8, 0x1F, 0x44, 0x75, 0xB5, 0x6B, 0x1C,
49 0xFC, 0x31, 0x09, 0x48, 0xA3, 0xFF, 0x92, 0x12, 0x58, 0xE9, 0xFA, 0xAE, 0x4F, 0xE2, 0xB4, 0xCC
50};
51static uint32_t _parAddr(uint32_t x) {
52 return (x & 0xFFFFF) | ((x << 4) & 0x0F000000);
53}
54
55static void _parEndBlock(struct GBACheatSet* cheats) {
56 size_t size = mCheatListSize(&cheats->d.list) - cheats->currentBlock - 1;
57 struct mCheat* currentBlock = mCheatListGetPointer(&cheats->d.list, cheats->currentBlock);
58 if (currentBlock->repeat) {
59 currentBlock->negativeRepeat = size - currentBlock->repeat;
60 } else {
61 currentBlock->repeat = size;
62 }
63 cheats->currentBlock = COMPLETE;
64}
65
66static void _parElseBlock(struct GBACheatSet* cheats) {
67 size_t size = mCheatListSize(&cheats->d.list) - cheats->currentBlock - 1;
68 struct mCheat* currentBlock = mCheatListGetPointer(&cheats->d.list, cheats->currentBlock);
69 currentBlock->repeat = size;
70}
71
72static bool _addPAR3Cond(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) {
73 enum GBAActionReplay3Condition condition = op1 & PAR3_COND;
74 int width = 1 << ((op1 & PAR3_WIDTH) >> PAR3_WIDTH_BASE);
75 if (width > 4) {
76 // TODO: Always false conditions
77 return false;
78 }
79 if ((op1 & PAR3_ACTION) == PAR3_ACTION_DISABLE) {
80 // TODO: Codes that disable
81 return false;
82 }
83
84 struct mCheat* cheat = mCheatListAppend(&cheats->d.list);
85 cheat->address = _parAddr(op1);
86 cheat->width = width;
87 cheat->operand = op2 & (0xFFFFFFFFU >> ((4 - width) * 8));
88 cheat->addressOffset = 0;
89 cheat->operandOffset = 0;
90
91 switch (op1 & PAR3_ACTION) {
92 case PAR3_ACTION_NEXT:
93 cheat->repeat = 1;
94 cheat->negativeRepeat = 0;
95 break;
96 case PAR3_ACTION_NEXT_TWO:
97 cheat->repeat = 2;
98 cheat->negativeRepeat = 0;
99 break;
100 case PAR3_ACTION_BLOCK:
101 cheat->repeat = 0;
102 cheat->negativeRepeat = 0;
103 if (cheats->currentBlock != COMPLETE) {
104 _parEndBlock(cheats);
105 }
106 cheats->currentBlock = mCheatListIndex(&cheats->d.list, cheat);
107 break;
108 }
109
110 switch (condition) {
111 case PAR3_COND_OTHER:
112 // We shouldn't be able to get here
113 mLOG(CHEATS, ERROR, "Unexpectedly created 'other' PARv3 code");
114 cheat->type = CHEAT_IF_LAND;
115 cheat->operand = 0;
116 break;
117 case PAR3_COND_EQ:
118 cheat->type = CHEAT_IF_EQ;
119 break;
120 case PAR3_COND_NE:
121 cheat->type = CHEAT_IF_NE;
122 break;
123 case PAR3_COND_LT:
124 cheat->type = CHEAT_IF_LT;
125 break;
126 case PAR3_COND_GT:
127 cheat->type = CHEAT_IF_GT;
128 break;
129 case PAR3_COND_ULT:
130 cheat->type = CHEAT_IF_ULT;
131 break;
132 case PAR3_COND_UGT:
133 cheat->type = CHEAT_IF_UGT;
134 break;
135 case PAR3_COND_AND:
136 cheat->type = CHEAT_IF_AND;
137 break;
138 }
139 return true;
140}
141
142static bool _addPAR3Special(struct GBACheatSet* cheats, uint32_t op2) {
143 int romPatch = -1;
144 struct mCheat* cheat;
145 switch (op2 & 0xFF000000) {
146 case PAR3_OTHER_SLOWDOWN:
147 // TODO: Slowdown
148 mLOG(CHEATS, STUB, "Unimplemented PARv3 slowdown");
149 return false;
150 case PAR3_OTHER_BUTTON_1:
151 cheat = mCheatListAppend(&cheats->d.list);
152 cheat->type = CHEAT_IF_BUTTON;
153 cheat->repeat = 1;
154 cheat->negativeRepeat = 0;
155 cheat = mCheatListAppend(&cheats->d.list);
156 cheat->type = CHEAT_ASSIGN;
157 cheat->width = 1;
158 cheat->repeat = 1;
159 cheat->address = _parAddr(op2);
160 cheats->incompleteCheat = mCheatListIndex(&cheats->d.list, cheat);
161 break;
162 case PAR3_OTHER_BUTTON_2:
163 cheat = mCheatListAppend(&cheats->d.list);
164 cheat->type = CHEAT_IF_BUTTON;
165 cheat->repeat = 1;
166 cheat->negativeRepeat = 0;
167 cheat = mCheatListAppend(&cheats->d.list);
168 cheat->type = CHEAT_ASSIGN;
169 cheat->width = 2;
170 cheat->repeat = 1;
171 cheat->address = _parAddr(op2);
172 cheats->incompleteCheat = mCheatListIndex(&cheats->d.list, cheat);
173 break;
174 case PAR3_OTHER_BUTTON_4:
175 cheat = mCheatListAppend(&cheats->d.list);
176 cheat->type = CHEAT_IF_BUTTON;
177 cheat->repeat = 1;
178 cheat->negativeRepeat = 0;
179 cheat = mCheatListAppend(&cheats->d.list);
180 cheat->type = CHEAT_ASSIGN;
181 cheat->width = 4;
182 cheat->repeat = 1;
183 cheat->address = _parAddr(op2);
184 cheats->incompleteCheat = mCheatListIndex(&cheats->d.list, cheat);
185 break;
186 case PAR3_OTHER_PATCH_1:
187 romPatch = 0;
188 break;
189 case PAR3_OTHER_PATCH_2:
190 romPatch = 1;
191 break;
192 case PAR3_OTHER_PATCH_3:
193 romPatch = 2;
194 break;
195 case PAR3_OTHER_PATCH_4:
196 romPatch = 3;
197 break;
198 case PAR3_OTHER_ENDIF:
199 if (cheats->currentBlock != COMPLETE) {
200 _parEndBlock(cheats);
201 return true;
202 }
203 return false;
204 case PAR3_OTHER_ELSE:
205 if (cheats->currentBlock != COMPLETE) {
206 _parElseBlock(cheats);
207 return true;
208 }
209 return false;
210 case PAR3_OTHER_FILL_1:
211 cheat = mCheatListAppend(&cheats->d.list);
212 cheat->type = CHEAT_ASSIGN;
213 cheat->address = _parAddr(op2);
214 cheat->width = 1;
215 cheats->incompleteCheat = mCheatListIndex(&cheats->d.list, cheat);
216 break;
217 case PAR3_OTHER_FILL_2:
218 cheat = mCheatListAppend(&cheats->d.list);
219 cheat->type = CHEAT_ASSIGN;
220 cheat->address = _parAddr(op2);
221 cheat->width = 2;
222 cheats->incompleteCheat = mCheatListIndex(&cheats->d.list, cheat);
223 break;
224 case PAR3_OTHER_FILL_4:
225 cheat = mCheatListAppend(&cheats->d.list);
226 cheat->type = CHEAT_ASSIGN;
227 cheat->address = _parAddr(op2);
228 cheat->width = 4;
229 cheats->incompleteCheat = mCheatListIndex(&cheats->d.list, cheat);
230 break;
231 }
232 if (romPatch >= 0) {
233 while (cheats->romPatches[romPatch].exists) {
234 ++romPatch;
235 if (romPatch >= MAX_ROM_PATCHES) {
236 break;
237 }
238 }
239 cheats->romPatches[romPatch].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1);
240 cheats->romPatches[romPatch].applied = false;
241 cheats->romPatches[romPatch].exists = true;
242 cheats->incompletePatch = &cheats->romPatches[romPatch];
243 }
244 return true;
245}
246
247bool GBACheatAddProActionReplayRaw(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) {
248 if (cheats->incompletePatch) {
249 cheats->incompletePatch->newValue = op1;
250 cheats->incompletePatch = 0;
251 return true;
252 }
253 if (cheats->incompleteCheat != COMPLETE) {
254 struct mCheat* incompleteCheat = mCheatListGetPointer(&cheats->d.list, cheats->incompleteCheat);
255 incompleteCheat->operand = op1 & (0xFFFFFFFFU >> ((4 - incompleteCheat->width) * 8));
256 if (cheats->incompleteCheat > 0) {
257 struct mCheat* lastCheat = mCheatListGetPointer(&cheats->d.list, cheats->incompleteCheat - 1);
258 if (lastCheat->type == CHEAT_IF_BUTTON) {
259 cheats->incompleteCheat = COMPLETE;
260 return true;
261 }
262 }
263 incompleteCheat->operandOffset = op2 >> 24;
264 incompleteCheat->repeat = (op2 >> 16) & 0xFF;
265 incompleteCheat->addressOffset = (op2 & 0xFFFF) * incompleteCheat->width;
266 cheats->incompleteCheat = COMPLETE;
267 return true;
268 }
269
270 if (op2 == 0x001DC0DE) {
271 return true;
272 }
273
274 switch (op1) {
275 case 0x00000000:
276 return _addPAR3Special(cheats, op2);
277 case 0xDEADFACE:
278 GBACheatReseedGameShark(cheats->gsaSeeds, op2, _par3T1, _par3T2);
279 return true;
280 }
281
282 if (op1 >> 24 == 0xC4) {
283 if (cheats->hook) {
284 return false;
285 }
286 cheats->hook = malloc(sizeof(*cheats->hook));
287 cheats->hook->address = BASE_CART0 | (op1 & (SIZE_CART0 - 2));
288 cheats->hook->mode = MODE_THUMB;
289 cheats->hook->refs = 1;
290 cheats->hook->reentries = 0;
291 return true;
292 }
293
294 if (op1 & PAR3_COND) {
295 return _addPAR3Cond(cheats, op1, op2);
296 }
297
298 int width = 1 << ((op1 & PAR3_WIDTH) >> PAR3_WIDTH_BASE);
299 struct mCheat* cheat = mCheatListAppend(&cheats->d.list);
300 cheat->address = _parAddr(op1);
301 cheat->operandOffset = 0;
302 cheat->addressOffset = 0;
303 cheat->repeat = 1;
304
305 switch (op1 & PAR3_BASE) {
306 case PAR3_BASE_ASSIGN:
307 cheat->type = CHEAT_ASSIGN;
308 cheat->addressOffset = width;
309 if (width < 4) {
310 cheat->repeat = (op2 >> (width * 8)) + 1;
311 }
312 break;
313 case PAR3_BASE_INDIRECT:
314 cheat->type = CHEAT_ASSIGN_INDIRECT;
315 if (width < 4) {
316 cheat->addressOffset = (op2 >> (width * 8)) * width;
317 }
318 break;
319 case PAR3_BASE_ADD:
320 cheat->type = CHEAT_ADD;
321 break;
322 case PAR3_BASE_OTHER:
323 width = ((op1 >> 24) & 1) + 1;
324 cheat->type = CHEAT_ASSIGN;
325 cheat->address = BASE_IO | (op1 & OFFSET_MASK);
326 break;
327 }
328 if (op1 & 0x01000000 && (op1 & 0xFE000000) != 0xC6000000) {
329 return false;
330 }
331
332
333 cheat->width = width;
334 cheat->operand = op2 & (0xFFFFFFFFU >> ((4 - width) * 8));
335 return true;
336}
337
338bool GBACheatAddProActionReplay(struct GBACheatSet* set, uint32_t op1, uint32_t op2) {
339 uint32_t o1 = op1;
340 uint32_t o2 = op2;
341 char line[18] = "XXXXXXXX XXXXXXXX";
342 snprintf(line, sizeof(line), "%08X %08X", op1, op2);
343
344 switch (set->gsaVersion) {
345 default:
346 GBACheatSetGameSharkVersion(set, GBA_GS_PARV3);
347 // Fall through
348 case GBA_GS_PARV3:
349 GBACheatDecryptGameShark(&o1, &o2, set->gsaSeeds);
350 // Fall through
351 case GBA_GS_PARV3_RAW:
352 return GBACheatAddProActionReplayRaw(set, o1, o2);
353 }
354 return false;
355}
356
357bool GBACheatAddProActionReplayLine(struct GBACheatSet* cheats, const char* line) {
358 uint32_t op1;
359 uint32_t op2;
360 line = hex32(line, &op1);
361 if (!line) {
362 return false;
363 }
364 while (*line == ' ') {
365 ++line;
366 }
367 line = hex32(line, &op2);
368 if (!line) {
369 return false;
370 }
371 return GBACheatAddProActionReplay(cheats, op1, op2);
372}
373
374int GBACheatProActionReplayProbability(uint32_t op1, uint32_t op2) {
375 int probability = 0;
376 if (op2 == 0x001DC0DE) {
377 return 0x100;
378 }
379 if (op1 == 0xDEADFACE && !(op2 & 0xFFFF0000)) {
380 return 0x100;
381 }
382 if (!op1) {
383 probability += 0x40;
384 uint32_t address = _parAddr(op2);
385 switch (op2 & 0xFE000000) {
386 case PAR3_OTHER_FILL_1:
387 case PAR3_OTHER_FILL_2:
388 case PAR3_OTHER_FILL_4:
389 probability += GBACheatAddressIsReal(address);
390 break;
391 case PAR3_OTHER_PATCH_1:
392 case PAR3_OTHER_PATCH_2:
393 case PAR3_OTHER_PATCH_3:
394 case PAR3_OTHER_PATCH_4:
395 // TODO: Detect ROM address
396 break;
397 case PAR3_OTHER_END:
398 case PAR3_OTHER_SLOWDOWN:
399 case PAR3_OTHER_BUTTON_1:
400 case PAR3_OTHER_BUTTON_2:
401 case PAR3_OTHER_BUTTON_4:
402 case PAR3_OTHER_ENDIF:
403 case PAR3_OTHER_ELSE:
404 if (op2 & 0x01000000) {
405 probability -= 0x40;
406 }
407 break;
408 default:
409 probability -= 0x40;
410 break;
411 }
412 return probability;
413 }
414 int width = ((op1 & PAR3_WIDTH) >> (PAR3_WIDTH_BASE - 3));
415 if (op1 & PAR3_COND) {
416 probability += 0x20;
417 if (width >= 24) {
418 return 0;
419 }
420 if (op2 & ~((1 << width) - 1)) {
421 probability -= 0x10;
422 }
423 } else {
424 uint32_t address = _parAddr(op1);
425 probability += 0x20;
426 switch (op1 & PAR3_BASE) {
427 case PAR3_BASE_ADD:
428 if (op2 & ~((1 << width) - 1)) {
429 probability -= 0x10;
430 }
431 // Fall through
432 case PAR3_BASE_ASSIGN:
433 case PAR3_BASE_INDIRECT:
434 probability += GBACheatAddressIsReal(address);
435 if (op1 & 0x01000000) {
436 return 0;
437 }
438 break;
439 case PAR3_BASE_OTHER:
440 break;
441 }
442 }
443 return probability;
444}