all repos — mgba @ 8636b81f13514538200a7113304cce7fb87261c9

mGBA Game Boy Advance Emulator

GBA: Split cheats out into separate source files
Jeffrey Pfau jeffrey@endrift.com
Thu, 09 Apr 2015 00:41:00 -0700
commit

8636b81f13514538200a7113304cce7fb87261c9

parent

1dd82f21d915a3450c0254baff6fd2324c241af2

M CMakeLists.txtCMakeLists.txt

@@ -18,6 +18,7 @@ set(BUILD_STATIC OFF CACHE BOOL "Build a static library")

set(BUILD_SHARED ON CACHE BOOL "Build a shared library") file(GLOB ARM_SRC ${CMAKE_SOURCE_DIR}/src/arm/*.c) file(GLOB GBA_SRC ${CMAKE_SOURCE_DIR}/src/gba/*.c) +file(GLOB GBA_CHEATS_SRC ${CMAKE_SOURCE_DIR}/src/gba/cheats/*.c) file(GLOB GBA_RR_SRC ${CMAKE_SOURCE_DIR}/src/gba/rr/*.c) file(GLOB GBA_SV_SRC ${CMAKE_SOURCE_DIR}/src/gba/supervisor/*.c) file(GLOB UTIL_SRC ${CMAKE_SOURCE_DIR}/src/util/*.[cSs])

@@ -28,7 +29,7 @@ list(APPEND UTIL_SRC ${CMAKE_SOURCE_DIR}/src/platform/commandline.c)

set(VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-mem.c) source_group("ARM core" FILES ${ARM_SRC}) source_group("GBA board" FILES ${GBA_SRC} ${RENDERER_SRC} ${SIO_SRC}) -source_group("GBA supervisor" FILES ${GBA_SV_SRC} ${GBA_RR_SRC}) +source_group("GBA supervisor" FILES ${GBA_CHEATS_SRC} ${GBA_SV_SRC} ${GBA_RR_SRC}) source_group("Utilities" FILES ${UTIL_SRC}) include_directories(${CMAKE_SOURCE_DIR}/src/arm) include_directories(${CMAKE_SOURCE_DIR}/src)

@@ -303,6 +304,7 @@ # Binaries

set(CORE_SRC ${ARM_SRC} ${GBA_SRC} + ${GBA_CHEATS_SRC} ${GBA_RR_SRC} ${GBA_SV_SRC} ${DEBUGGER_SRC}
M src/gba/cheats.csrc/gba/cheats.c

@@ -5,8 +5,10 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this

* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "cheats.h" +#include "gba/cheats/gameshark.h" +#include "gba/cheats/parv3.h" #include "gba/gba.h" -#include "gba/io.h" +#include "util/string.h" #include "util/vfs.h" #define MAX_LINE_LENGTH 128

@@ -17,85 +19,6 @@ DEFINE_VECTOR(GBACheatList, struct GBACheat);

DEFINE_VECTOR(GBACheatSets, struct GBACheatSet*); DEFINE_VECTOR(StringList, char*); -static const uint32_t _gsa1S[4] = { 0x09F4FBBD, 0x9681884A, 0x352027E9, 0xF3DEE5A7 }; -static const uint32_t _par3S[4] = { 0x7AA9648F, 0x7FAE6994, 0xC0EFAAD5, 0x42712C57 }; - -static const uint8_t _gsa1T1[256] = { - 0x31, 0x1C, 0x23, 0xE5, 0x89, 0x8E, 0xA1, 0x37, 0x74, 0x6D, 0x67, 0xFC, 0x1F, 0xC0, 0xB1, 0x94, - 0x3B, 0x05, 0x56, 0x86, 0x00, 0x24, 0xF0, 0x17, 0x72, 0xA2, 0x3D, 0x1B, 0xE3, 0x17, 0xC5, 0x0B, - 0xB9, 0xE2, 0xBD, 0x58, 0x71, 0x1B, 0x2C, 0xFF, 0xE4, 0xC9, 0x4C, 0x5E, 0xC9, 0x55, 0x33, 0x45, - 0x7C, 0x3F, 0xB2, 0x51, 0xFE, 0x10, 0x7E, 0x75, 0x3C, 0x90, 0x8D, 0xDA, 0x94, 0x38, 0xC3, 0xE9, - 0x95, 0xEA, 0xCE, 0xA6, 0x06, 0xE0, 0x4F, 0x3F, 0x2A, 0xE3, 0x3A, 0xE4, 0x43, 0xBD, 0x7F, 0xDA, - 0x55, 0xF0, 0xEA, 0xCB, 0x2C, 0xA8, 0x47, 0x61, 0xA0, 0xEF, 0xCB, 0x13, 0x18, 0x20, 0xAF, 0x3E, - 0x4D, 0x9E, 0x1E, 0x77, 0x51, 0xC5, 0x51, 0x20, 0xCF, 0x21, 0xF9, 0x39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xCC, - 0x99, 0x56, 0x9F, 0x78, 0x43, 0x91, 0x85, 0x3F, 0xC2, 0xD0, 0xD1, 0x80, 0xD1, 0x77, 0xA7, 0xE2, - 0x43, 0x99, 0x1D, 0x2F, 0x8B, 0x6A, 0xE4, 0x66, 0x82, 0xF7, 0x2B, 0x0B, 0x65, 0x14, 0xC0, 0xC2, - 0x1D, 0x96, 0x78, 0x1C, 0xC4, 0xC3, 0xD2, 0xB1, 0x64, 0x07, 0xD7, 0x6F, 0x02, 0xE9, 0x44, 0x31, - 0xDB, 0x3C, 0xEB, 0x93, 0xED, 0x9A, 0x57, 0x05, 0xB9, 0x0E, 0xAF, 0x1F, 0x48, 0x11, 0xDC, 0x35, - 0x6C, 0xB8, 0xEE, 0x2A, 0x48, 0x2B, 0xBC, 0x89, 0x12, 0x59, 0xCB, 0xD1, 0x18, 0xEA, 0x72, 0x11, - 0x01, 0x75, 0x3B, 0xB5, 0x56, 0xF4, 0x8B, 0xA0, 0x41, 0x75, 0x86, 0x7B, 0x94, 0x12, 0x2D, 0x4C, - 0x0C, 0x22, 0xC9, 0x4A, 0xD8, 0xB1, 0x8D, 0xF0, 0x55, 0x2E, 0x77, 0x50, 0x1C, 0x64, 0x77, 0xAA, - 0x3E, 0xAC, 0xD3, 0x3D, 0xCE, 0x60, 0xCA, 0x5D, 0xA0, 0x92, 0x78, 0xC6, 0x51, 0xFE, 0xF9, 0x30 -}; - -static const uint8_t _par3T2[256] = { - 0xAA, 0xAF, 0xF0, 0x72, 0x90, 0xF7, 0x71, 0x27, 0x06, 0x11, 0xEB, 0x9C, 0x37, 0x12, 0x72, 0xAA, - 0x65, 0xBC, 0x0D, 0x4A, 0x76, 0xF6, 0x5C, 0xAA, 0xB0, 0x7A, 0x7D, 0x81, 0xC1, 0xCE, 0x2F, 0x9F, - 0x02, 0x75, 0x38, 0xC8, 0xFC, 0x66, 0x05, 0xC2, 0x2C, 0xBD, 0x91, 0xAD, 0x03, 0xB1, 0x88, 0x93, - 0x31,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A4, 0xE9, 0x0A, 0x06, 0x66, 0xF8, 0x1F, 0x44, 0x75, 0xB5, 0x6B, 0x1C, - 0xFC, 0x31, 0x09, 0x48, 0xA3, 0xFF, 0x92, 0x12, 0x58, 0xE9, 0xFA, 0xAE, 0x4F, 0xE2, 0xB4, 0xCC -}; - static int32_t _readMem(struct ARMCore* cpu, uint32_t address, int width) { switch (width) { case 1:

@@ -122,455 +45,10 @@ break;

} } -static int _hexDigit(char digit) { - switch (digit) { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - return digit - '0'; - - case 'a': - case 'b': - case 'c': - case 'd': - case 'e': - case 'f': - return digit - 'a' + 10; - - case 'A': - case 'B': - case 'C': - case 'D': - case 'E': - case 'F': - return digit - 'A' + 10; - - default: - return -1; - } -} - -static const char* _hex32(const char* line, uint32_t* out) { - uint32_t value = 0; - int i; - for (i = 0; i < 8; ++i, ++line) { - char digit = *line; - value <<= 4; - int nybble = _hexDigit(digit); - if (nybble < 0) { - return 0; - } - value |= nybble; - } - *out = value; - return line; -} - -static const char* _hex16(const char* line, uint16_t* out) { - uint16_t value = 0; - *out = 0; - int i; - for (i = 0; i < 4; ++i, ++line) { - char digit = *line; - value <<= 4; - int nybble = _hexDigit(digit); - if (nybble < 0) { - return 0; - } - value |= nybble; - } - *out = value; - return line; -} - -static void _registerLine(struct GBACheatSet* cheats, const char* line) { +void GBACheatRegisterLine(struct GBACheatSet* cheats, const char* line) { *StringListAppend(&cheats->lines) = strdup(line); } -// http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm -static void _decryptGameShark(uint32_t* op1, uint32_t* op2, const uint32_t* seeds) { - uint32_t sum = 0xC6EF3720; - int i; - for (i = 0; i < 32; ++i) { - *op2 -= ((*op1 << 4) + seeds[2]) ^ (*op1 + sum) ^ ((*op1 >> 5) + seeds[3]); - *op1 -= ((*op2 << 4) + seeds[0]) ^ (*op2 + sum) ^ ((*op2 >> 5) + seeds[1]); - sum -= 0x9E3779B9; - } -} - -static void _reseedGameShark(uint32_t* seeds, uint16_t params, const uint8_t* t1, const uint8_t* t2) { - int x, y; - int s0 = params >> 8; - int s1 = params & 0xFF; - for (y = 0; y < 4; ++y) { - for (x = 0; x < 4; ++x) { - uint8_t z = t1[(s0 + x) & 0xFF] + t2[(s1 + y) & 0xFF]; - seeds[y] <<= 8; - seeds[y] |= z; - } - } -} - -static void _setGameSharkVersion(struct GBACheatSet* cheats, int version) { - cheats->gsaVersion = 1; - switch (version) { - case 1: - memcpy(cheats->gsaSeeds, _gsa1S, 4 * sizeof(uint32_t)); - break; - case 3: - memcpy(cheats->gsaSeeds, _par3S, 4 * sizeof(uint32_t)); - break; - } -} - -static bool _addGSA1(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) { - enum GBAGameSharkType type = op1 >> 28; - struct GBACheat* cheat = 0; - - if (cheats->incompleteCheat) { - if (cheats->remainingAddresses > 0) { - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_ASSIGN; - cheat->width = 4; - cheat->address = op1; - cheat->operand = cheats->incompleteCheat->operand; - cheat->repeat = 1; - --cheats->remainingAddresses; - } - if (cheats->remainingAddresses > 0) { - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_ASSIGN; - cheat->width = 4; - cheat->address = op2; - cheat->operand = cheats->incompleteCheat->operand; - cheat->repeat = 1; - --cheats->remainingAddresses; - } - if (cheats->remainingAddresses == 0) { - cheats->incompleteCheat = 0; - } - return true; - } - - switch (type) { - case GSA_ASSIGN_1: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_ASSIGN; - cheat->width = 1; - cheat->address = op1 & 0x0FFFFFFF; - break; - case GSA_ASSIGN_2: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_ASSIGN; - cheat->width = 2; - cheat->address = op1 & 0x0FFFFFFF; - break; - case GSA_ASSIGN_4: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_ASSIGN; - cheat->width = 4; - cheat->address = op1 & 0x0FFFFFFF; - break; - case GSA_ASSIGN_LIST: - cheats->remainingAddresses = (op1 & 0xFFFF) - 1; - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_ASSIGN; - cheat->width = 4; - cheat->address = op2; - cheats->incompleteCheat = cheat; - break; - case GSA_PATCH: - cheats->romPatches[0].address = (op1 & 0xFFFFFF) << 1; - cheats->romPatches[0].newValue = op2; - cheats->romPatches[0].applied = false; - cheats->romPatches[0].exists = true; - return true; - case GSA_BUTTON: - // TODO: Implement button - GBALog(0, GBA_LOG_STUB, "GameShark button unimplemented"); - return false; - case GSA_IF_EQ: - if (op1 == 0xDEADFACE) { - _reseedGameShark(cheats->gsaSeeds, op2, _gsa1T1, _gsa1T2); - return true; - } - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_IF_EQ; - cheat->width = 2; - cheat->address = op1 & 0x0FFFFFFF; - break; - case GSA_IF_EQ_RANGE: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_IF_EQ; - cheat->width = 2; - cheat->address = op2 & 0x0FFFFFFF; - cheat->operand = op1 & 0xFFFF; - cheat->repeat = (op1 >> 16) & 0xFF; - return true; - case GSA_HOOK: - if (cheats->hook) { - return false; - } - cheats->hook = malloc(sizeof(*cheats->hook)); - cheats->hook->address = BASE_CART0 | (op1 & (SIZE_CART0 - 1)); - cheats->hook->mode = MODE_THUMB; - cheats->hook->refs = 1; - cheats->hook->reentries = 0; - return true; - default: - return false; - } - cheat->operand = op2; - cheat->repeat = 1; - return true; -} - -static uint32_t _parAddr(uint32_t x) { - return (x & 0xFFFFF) | ((x << 4) & 0x0F000000); -} - -static void _parEndBlock(struct GBACheatSet* cheats) { - size_t size = GBACheatListSize(&cheats->list) - GBACheatListIndex(&cheats->list, cheats->currentBlock); - if (cheats->currentBlock->repeat) { - cheats->currentBlock->negativeRepeat = size - cheats->currentBlock->repeat; - } else { - cheats->currentBlock->repeat = size; - } - cheats->currentBlock = 0; -} - -static void _parElseBlock(struct GBACheatSet* cheats) { - size_t size = GBACheatListSize(&cheats->list) - GBACheatListIndex(&cheats->list, cheats->currentBlock); - cheats->currentBlock->repeat = size; -} - -static bool _addPAR3Cond(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) { - enum GBAActionReplay3Condition condition = op1 & PAR3_COND; - int width = 1 << ((op1 & PAR3_WIDTH) >> PAR3_WIDTH_BASE); - if (width > 4) { - // TODO: Always false conditions - return false; - } - if ((op1 & PAR3_ACTION) == PAR3_ACTION_DISABLE) { - // TODO: Codes that disable - return false; - } - - struct GBACheat* cheat = GBACheatListAppend(&cheats->list); - cheat->address = _parAddr(op1); - cheat->width = width; - cheat->operand = op2 & (0xFFFFFFFFU >> ((4 - width) * 8)); - cheat->addressOffset = 0; - cheat->operandOffset = 0; - - switch (op1 & PAR3_ACTION) { - case PAR3_ACTION_NEXT: - cheat->repeat = 1; - cheat->negativeRepeat = 0; - break; - case PAR3_ACTION_NEXT_TWO: - cheat->repeat = 2; - cheat->negativeRepeat = 0; - break; - case PAR3_ACTION_BLOCK: - cheat->repeat = 0; - cheat->negativeRepeat = 0; - if (cheats->currentBlock) { - _parEndBlock(cheats); - } - cheats->currentBlock = cheat; - break; - } - - switch (condition) { - case PAR3_COND_OTHER: - // We shouldn't be able to get here - GBALog(0, GBA_LOG_ERROR, "Unexpectedly created 'other' PARv3 code"); - cheat->type = CHEAT_IF_LAND; - cheat->operand = 0; - break; - case PAR3_COND_EQ: - cheat->type = CHEAT_IF_EQ; - break; - case PAR3_COND_NE: - cheat->type = CHEAT_IF_NE; - break; - case PAR3_COND_LT: - cheat->type = CHEAT_IF_LT; - break; - case PAR3_COND_GT: - cheat->type = CHEAT_IF_GT; - break; - case PAR3_COND_ULT: - cheat->type = CHEAT_IF_ULT; - break; - case PAR3_COND_UGT: - cheat->type = CHEAT_IF_UGT; - break; - case PAR3_COND_LAND: - cheat->type = CHEAT_IF_LAND; - break; - } - return true; -} - -static bool _addPAR3Special(struct GBACheatSet* cheats, uint32_t op2) { - struct GBACheat* cheat; - switch (op2 & 0xFF000000) { - case PAR3_OTHER_SLOWDOWN: - // TODO: Slowdown - return false; - case PAR3_OTHER_BUTTON_1: - case PAR3_OTHER_BUTTON_2: - case PAR3_OTHER_BUTTON_4: - // TODO: Button - GBALog(0, GBA_LOG_STUB, "GameShark button unimplemented"); - return false; - // TODO: Fix overriding existing patches - case PAR3_OTHER_PATCH_1: - cheats->romPatches[0].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1); - cheats->romPatches[0].applied = false; - cheats->romPatches[0].exists = true; - cheats->incompletePatch = &cheats->romPatches[0]; - break; - case PAR3_OTHER_PATCH_2: - cheats->romPatches[1].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1); - cheats->romPatches[1].applied = false; - cheats->romPatches[1].exists = true; - cheats->incompletePatch = &cheats->romPatches[1]; - break; - case PAR3_OTHER_PATCH_3: - cheats->romPatches[2].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1); - cheats->romPatches[2].applied = false; - cheats->romPatches[2].exists = true; - cheats->incompletePatch = &cheats->romPatches[2]; - break; - case PAR3_OTHER_PATCH_4: - cheats->romPatches[3].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1); - cheats->romPatches[3].applied = false; - cheats->romPatches[3].exists = true; - cheats->incompletePatch = &cheats->romPatches[3]; - break; - case PAR3_OTHER_ENDIF: - if (cheats->currentBlock) { - _parEndBlock(cheats); - return true; - } - return false; - case PAR3_OTHER_ELSE: - if (cheats->currentBlock) { - _parElseBlock(cheats); - return true; - } - return false; - case PAR3_OTHER_FILL_1: - cheat = GBACheatListAppend(&cheats->list); - cheat->address = _parAddr(op2); - cheat->width = 1; - cheats->incompleteCheat = cheat; - break; - case PAR3_OTHER_FILL_2: - cheat = GBACheatListAppend(&cheats->list); - cheat->address = _parAddr(op2); - cheat->width = 2; - cheats->incompleteCheat = cheat; - break; - case PAR3_OTHER_FILL_4: - cheat = GBACheatListAppend(&cheats->list); - cheat->address = _parAddr(op2); - cheat->width = 3; - cheats->incompleteCheat = cheat; - break; - } - return true; -} - -static bool _addPAR3(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) { - if (cheats->incompletePatch) { - cheats->incompletePatch->newValue = op1; - cheats->incompletePatch = 0; - return true; - } - if (cheats->incompleteCheat) { - cheats->incompleteCheat->operand = op1 & (0xFFFFFFFFU >> ((4 - cheats->incompleteCheat->width) * 8)); - cheats->incompleteCheat->addressOffset = op2 >> 24; - cheats->incompleteCheat->repeat = (op2 >> 16) & 0xFF; - cheats->incompleteCheat->addressOffset = (op2 & 0xFFFF) * cheats->incompleteCheat->width; - cheats->incompleteCheat = 0; - return true; - } - - if (op2 == 0x001DC0DE) { - return true; - } - - switch (op1) { - case 0x00000000: - return _addPAR3Special(cheats, op2); - case 0xDEADFACE: - _reseedGameShark(cheats->gsaSeeds, op2, _par3T1, _par3T2); - return true; - } - - if (op1 >> 24 == 0xC4) { - if (cheats->hook) { - return false; - } - cheats->hook = malloc(sizeof(*cheats->hook)); - cheats->hook->address = BASE_CART0 | (op1 & (SIZE_CART0 - 1)); - cheats->hook->mode = MODE_THUMB; - cheats->hook->refs = 1; - cheats->hook->reentries = 0; - return true; - } - - if (op1 & PAR3_COND) { - return _addPAR3Cond(cheats, op1, op2); - } - - int width = 1 << ((op1 & PAR3_WIDTH) >> PAR3_WIDTH_BASE); - struct GBACheat* cheat = GBACheatListAppend(&cheats->list); - cheat->address = _parAddr(op1); - cheat->operandOffset = 0; - cheat->addressOffset = 0; - cheat->repeat = 1; - - switch (op1 & PAR3_BASE) { - case PAR3_BASE_ASSIGN: - cheat->type = CHEAT_ASSIGN; - cheat->addressOffset = width; - if (width < 4) { - cheat->repeat = (op2 >> (width * 8)) + 1; - } - break; - case PAR3_BASE_INDIRECT: - cheat->type = CHEAT_ASSIGN_INDIRECT; - if (width < 4) { - cheat->addressOffset = (op2 >> (width * 8)) * width; - } - break; - case PAR3_BASE_ADD: - cheat->type = CHEAT_ADD; - break; - case PAR3_BASE_OTHER: - width = ((op1 >> 24) & 1) + 1; - cheat->type = CHEAT_ASSIGN; - cheat->address = BASE_IO | (op1 & OFFSET_MASK); - break; - } - - cheat->width = width; - cheat->operand = op2 & (0xFFFFFFFFU >> ((4 - width) * 8)); - return true; -} - static void _addBreakpoint(struct GBACheatDevice* device, struct GBACheatSet* cheats) { if (!device->p || !cheats->hook) { return;

@@ -708,237 +186,35 @@ _unpatchROM(device, cheats);

_removeBreakpoint(device, cheats); } -bool GBACheatAddCodeBreaker(struct GBACheatSet* cheats, uint32_t op1, uint16_t op2) { - char line[14] = "XXXXXXXX XXXX"; - snprintf(line, sizeof(line), "%08X %04X", op1, op2); - _registerLine(cheats, line); - - enum GBACodeBreakerType type = op1 >> 28; - struct GBACheat* cheat = 0; - - if (cheats->incompleteCheat) { - cheats->incompleteCheat->repeat = op1 & 0xFFFF; - cheats->incompleteCheat->addressOffset = op2; - cheats->incompleteCheat->operandOffset = 0; - cheats->incompleteCheat = 0; - return true; - } - - switch (type) { - case CB_GAME_ID: - // TODO: Run checksum - return true; - case CB_HOOK: - if (cheats->hook) { - return false; - } - cheats->hook = malloc(sizeof(*cheats->hook)); - cheats->hook->address = BASE_CART0 | (op1 & (SIZE_CART0 - 1)); - cheats->hook->mode = MODE_THUMB; - cheats->hook->refs = 1; - cheats->hook->reentries = 0; - return true; - case CB_OR_2: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_OR; - cheat->width = 2; - break; - case CB_ASSIGN_1: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_ASSIGN; - cheat->width = 1; - break; - case CB_FILL: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_ASSIGN; - cheat->width = 2; - cheats->incompleteCheat = cheat; - break; - case CB_FILL_8: - GBALog(0, GBA_LOG_STUB, "[Cheat] CodeBreaker code %08X %04X not supported", op1, op2); - return false; - case CB_AND_2: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_AND; - cheat->width = 2; - break; - case CB_IF_EQ: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_IF_EQ; - cheat->width = 2; - break; - case CB_ASSIGN_2: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_ASSIGN; - cheat->width = 2; - break; - case CB_ENCRYPT: - GBALog(0, GBA_LOG_STUB, "[Cheat] CodeBreaker encryption not supported"); - return false; - case CB_IF_NE: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_IF_NE; - cheat->width = 2; - break; - case CB_IF_GT: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_IF_GT; - cheat->width = 2; - break; - case CB_IF_LT: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_IF_LT; - cheat->width = 2; - break; - case CB_IF_SPECIAL: - switch (op1 & 0x0FFFFFFF) { - case 0x20: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_IF_AND; - cheat->width = 2; - cheat->address = BASE_IO | REG_JOYSTAT; - cheat->operand = op2; - cheat->repeat = 1; - return true; - default: - GBALog(0, GBA_LOG_STUB, "[Cheat] CodeBreaker code %08X %04X not supported", op1, op2); - return false; - } - case CB_ADD_2: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_ADD; - cheat->width = 2; - break; - case CB_IF_AND: - cheat = GBACheatListAppend(&cheats->list); - cheat->type = CHEAT_IF_AND; - cheat->width = 2; - break; - } - - cheat->address = op1 & 0x0FFFFFFF; - cheat->operand = op2; - cheat->repeat = 1; - cheat->negativeRepeat = 0; - return true; -} - -bool GBACheatAddCodeBreakerLine(struct GBACheatSet* cheats, const char* line) { - uint32_t op1; - uint16_t op2; - line = _hex32(line, &op1); - if (!line) { - return false; - } - while (*line == ' ') { - ++line; - } - line = _hex16(line, &op2); - if (!line) { - return false; - } - return GBACheatAddCodeBreaker(cheats, op1, op2); -} - -bool GBACheatAddGameShark(struct GBACheatSet* set, uint32_t op1, uint32_t op2) { - uint32_t o1 = op1; - uint32_t o2 = op2; - char line[18] = "XXXXXXXX XXXXXXXX"; - snprintf(line, sizeof(line), "%08X %08X", op1, op2); - _registerLine(set, line); - - switch (set->gsaVersion) { - case 0: - _setGameSharkVersion(set, 1); - // Fall through - case 1: - _decryptGameShark(&o1, &o2, set->gsaSeeds); - return _addGSA1(set, o1, o2); - } - return false; -} - -bool GBACheatAddGameSharkLine(struct GBACheatSet* cheats, const char* line) { - uint32_t op1; - uint32_t op2; - line = _hex32(line, &op1); - if (!line) { - return false; - } - while (*line == ' ') { - ++line; - } - line = _hex32(line, &op2); - if (!line) { - return false; - } - return GBACheatAddGameShark(cheats, op1, op2); -} - -bool GBACheatAddProActionReplay(struct GBACheatSet* set, uint32_t op1, uint32_t op2) { - uint32_t o1 = op1; - uint32_t o2 = op2; - char line[18] = "XXXXXXXX XXXXXXXX"; - snprintf(line, sizeof(line), "%08X %08X", op1, op2); - _registerLine(set, line); - - switch (set->gsaVersion) { - case 0: - _setGameSharkVersion(set, 3); - // Fall through - case 1: - _decryptGameShark(&o1, &o2, set->gsaSeeds); - return _addPAR3(set, o1, o2); - } - return false; -} - -bool GBACheatAddProActionReplayLine(struct GBACheatSet* cheats, const char* line) { - uint32_t op1; - uint32_t op2; - line = _hex32(line, &op1); - if (!line) { - return false; - } - while (*line == ' ') { - ++line; - } - line = _hex32(line, &op2); - if (!line) { - return false; - } - return GBACheatAddProActionReplay(cheats, op1, op2); -} - bool GBACheatAddAutodetect(struct GBACheatSet* set, uint32_t op1, uint32_t op2) { uint32_t o1 = op1; uint32_t o2 = op2; char line[18] = "XXXXXXXX XXXXXXXX"; snprintf(line, sizeof(line), "%08X %08X", op1, op2); - _registerLine(set, line); + GBACheatRegisterLine(set, line); switch (set->gsaVersion) { case 0: // Try to detect GameShark version - _decryptGameShark(&o1, &o2, _gsa1S); + GBACheatDecryptGameShark(&o1, &o2, GBACheatGameSharkSeeds); if ((o1 & 0xF0000000) == 0xF0000000 && !(o2 & 0xFFFFFCFE)) { - _setGameSharkVersion(set, 1); - return _addGSA1(set, o1, o2); + GBACheatSetGameSharkVersion(set, 1); + return GBACheatAddGameSharkRaw(set, o1, o2); } o1 = op1; o2 = op2; - _decryptGameShark(&o1, &o2, _par3S); + GBACheatDecryptGameShark(&o1, &o2, GBACheatProActionReplaySeeds); if ((o1 & 0xFE000000) == 0xC4000000 && !(o2 & 0xFFFF0000)) { - _setGameSharkVersion(set, 3); - return _addPAR3(set, o1, o2); + GBACheatSetGameSharkVersion(set, 3); + return GBACheatAddProActionReplayRaw(set, o1, o2); } break; case 1: - _decryptGameShark(&o1, &o2, set->gsaSeeds); - return _addGSA1(set, o1, o2); + GBACheatDecryptGameShark(&o1, &o2, set->gsaSeeds); + return GBACheatAddGameSharkRaw(set, o1, o2); case 3: - _decryptGameShark(&o1, &o2, set->gsaSeeds); - return _addPAR3(set, o1, o2); + GBACheatDecryptGameShark(&o1, &o2, set->gsaSeeds); + return GBACheatAddProActionReplayRaw(set, o1, o2); } return false; }

@@ -946,14 +222,14 @@

bool GBACheatAutodetectLine(struct GBACheatSet* cheats, const char* line) { uint32_t op1; uint32_t op2; - line = _hex32(line, &op1); + line = hex32(line, &op1); if (!line) { return false; } while (*line == ' ') { ++line; } - line = _hex32(line, &op2); + line = hex32(line, &op2); if (!line) { return false; }

@@ -994,7 +270,7 @@ }

if (set && !reset) { GBACheatSetCopyProperties(newSet, set); } else { - _setGameSharkVersion(newSet, gsaVersion); + GBACheatSetGameSharkVersion(newSet, gsaVersion); } reset = false; set = newSet;

@@ -1023,7 +299,7 @@ set = malloc(sizeof(*set));

GBACheatSetInit(set, 0); set->enabled = !nextDisabled; nextDisabled = false; - _setGameSharkVersion(set, gsaVersion); + GBACheatSetGameSharkVersion(set, gsaVersion); } GBACheatAddLine(set, cheat); break;

@@ -1087,21 +363,21 @@ bool GBACheatAddLine(struct GBACheatSet* cheats, const char* line) {

uint32_t op1; uint16_t op2; uint16_t op3; - line = _hex32(line, &op1); + line = hex32(line, &op1); if (!line) { return false; } while (isspace(line[0])) { ++line; } - line = _hex16(line, &op2); + line = hex16(line, &op2); if (!line) { return false; } if (!line[0] || isspace(line[0])) { return GBACheatAddCodeBreaker(cheats, op1, op2); } - line = _hex16(line, &op3); + line = hex16(line, &op3); if (!line) { return false; }
A src/gba/cheats/cheats-private.h

@@ -0,0 +1,13 @@

+/* Copyright (c) 2013-2015 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#ifndef GBA_CHEATS_PRIVATE_H +#define GBA_CHEATS_PRIVATE_H + +#include "gba/cheats.h" + +void GBACheatRegisterLine(struct GBACheatSet* set, const char* line); + +#endif
A src/gba/cheats/codebreaker.c

@@ -0,0 +1,143 @@

+/* Copyright (c) 2013-2015 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "gba/cheats.h" + +#include "gba/cheats/cheats-private.h" +#include "gba/gba.h" +#include "gba/io.h" +#include "util/string.h" + +bool GBACheatAddCodeBreaker(struct GBACheatSet* cheats, uint32_t op1, uint16_t op2) { + char line[14] = "XXXXXXXX XXXX"; + snprintf(line, sizeof(line), "%08X %04X", op1, op2); + GBACheatRegisterLine(cheats, line); + + enum GBACodeBreakerType type = op1 >> 28; + struct GBACheat* cheat = 0; + + if (cheats->incompleteCheat) { + cheats->incompleteCheat->repeat = op1 & 0xFFFF; + cheats->incompleteCheat->addressOffset = op2; + cheats->incompleteCheat->operandOffset = 0; + cheats->incompleteCheat = 0; + return true; + } + + switch (type) { + case CB_GAME_ID: + // TODO: Run checksum + return true; + case CB_HOOK: + if (cheats->hook) { + return false; + } + cheats->hook = malloc(sizeof(*cheats->hook)); + cheats->hook->address = BASE_CART0 | (op1 & (SIZE_CART0 - 1)); + cheats->hook->mode = MODE_THUMB; + cheats->hook->refs = 1; + cheats->hook->reentries = 0; + return true; + case CB_OR_2: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_OR; + cheat->width = 2; + break; + case CB_ASSIGN_1: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_ASSIGN; + cheat->width = 1; + break; + case CB_FILL: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_ASSIGN; + cheat->width = 2; + cheats->incompleteCheat = cheat; + break; + case CB_FILL_8: + GBALog(0, GBA_LOG_STUB, "[Cheat] CodeBreaker code %08X %04X not supported", op1, op2); + return false; + case CB_AND_2: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_AND; + cheat->width = 2; + break; + case CB_IF_EQ: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_IF_EQ; + cheat->width = 2; + break; + case CB_ASSIGN_2: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_ASSIGN; + cheat->width = 2; + break; + case CB_ENCRYPT: + GBALog(0, GBA_LOG_STUB, "[Cheat] CodeBreaker encryption not supported"); + return false; + case CB_IF_NE: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_IF_NE; + cheat->width = 2; + break; + case CB_IF_GT: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_IF_GT; + cheat->width = 2; + break; + case CB_IF_LT: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_IF_LT; + cheat->width = 2; + break; + case CB_IF_SPECIAL: + switch (op1 & 0x0FFFFFFF) { + case 0x20: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_IF_AND; + cheat->width = 2; + cheat->address = BASE_IO | REG_JOYSTAT; + cheat->operand = op2; + cheat->repeat = 1; + return true; + default: + GBALog(0, GBA_LOG_STUB, "[Cheat] CodeBreaker code %08X %04X not supported", op1, op2); + return false; + } + case CB_ADD_2: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_ADD; + cheat->width = 2; + break; + case CB_IF_AND: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_IF_AND; + cheat->width = 2; + break; + } + + cheat->address = op1 & 0x0FFFFFFF; + cheat->operand = op2; + cheat->repeat = 1; + cheat->negativeRepeat = 0; + return true; +} + +bool GBACheatAddCodeBreakerLine(struct GBACheatSet* cheats, const char* line) { + uint32_t op1; + uint16_t op2; + line = hex32(line, &op1); + if (!line) { + return false; + } + while (*line == ' ') { + ++line; + } + line = hex16(line, &op2); + if (!line) { + return false; + } + return GBACheatAddCodeBreaker(cheats, op1, op2); +}
A src/gba/cheats/gameshark.c

@@ -0,0 +1,224 @@

+/* Copyright (c) 2013-2015 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "gameshark.h" + +#include "gba/cheats/cheats-private.h" +#include "gba/cheats/parv3.h" +#include "gba/gba.h" +#include "util/string.h" + +const uint32_t GBACheatGameSharkSeeds[4] = { 0x09F4FBBD, 0x9681884A, 0x352027E9, 0xF3DEE5A7 }; + +static const uint8_t _gsa1T1[256] = { + 0x31, 0x1C, 0x23, 0xE5, 0x89, 0x8E, 0xA1, 0x37, 0x74, 0x6D, 0x67, 0xFC, 0x1F, 0xC0, 0xB1, 0x94, + 0x3B, 0x05, 0x56, 0x86, 0x00, 0x24, 0xF0, 0x17, 0x72, 0xA2, 0x3D, 0x1B, 0xE3, 0x17, 0xC5, 0x0B, + 0xB9, 0xE2, 0xBD, 0x58, 0x71, 0x1B, 0x2C, 0xFF, 0xE4, 0xC9, 0x4C, 0x5E, 0xC9, 0x55, 0x33, 0x45, + 0x7C, 0x3F, 0xB2, 0x51, 0xFE, 0x10, 0x7E, 0x75, 0x3C, 0x90, 0x8D, 0xDA, 0x94, 0x38, 0xC3, 0xE9, + 0x95, 0xEA, 0xCE, 0xA6, 0x06, 0xE0, 0x4F, 0x3F, 0x2A, 0xE3, 0x3A, 0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+ 0x7A, 0x1D, 0x02, 0x55, 0x09, 0xD1, 0xFF, 0x55, 0x5E, 0x17, 0xA0, 0x56, 0xF4, 0xC9, 0x6B, 0x90, + 0xB4, 0x80, 0xA5, 0x07, 0x22, 0xFB, 0x22, 0x0D, 0xD9, 0xC0, 0x5B, 0x08, 0x35, 0x05, 0xC1, 0x75, + 0x4F, 0xD0, 0x51, 0x2D, 0x2E, 0x5E, 0x69, 0xE7, 0x3B, 0xC2, 0xDA, 0xFF, 0xF6, 0xCE, 0x3E, 0x76, + 0xE8, 0x36, 0x8C, 0x39, 0xD8, 0xF3, 0xE9, 0xA6, 0x42, 0xE6, 0xC1, 0x4C, 0x05, 0xBE, 0x17, 0xF2, + 0x5C, 0x1B, 0x19, 0xDB, 0x0F, 0xF3, 0xF8, 0x49, 0xEB, 0x36, 0xF6, 0x40, 0x6F, 0xAD, 0xC1, 0x8C +}; + +// http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm +void GBACheatDecryptGameShark(uint32_t* op1, uint32_t* op2, const uint32_t* seeds) { + uint32_t sum = 0xC6EF3720; + int i; + for (i = 0; i < 32; ++i) { + *op2 -= ((*op1 << 4) + seeds[2]) ^ (*op1 + sum) ^ ((*op1 >> 5) + seeds[3]); + *op1 -= ((*op2 << 4) + seeds[0]) ^ (*op2 + sum) ^ ((*op2 >> 5) + seeds[1]); + sum -= 0x9E3779B9; + } +} + +void GBACheatReseedGameShark(uint32_t* seeds, uint16_t params, const uint8_t* t1, const uint8_t* t2) { + int x, y; + int s0 = params >> 8; + int s1 = params & 0xFF; + for (y = 0; y < 4; ++y) { + for (x = 0; x < 4; ++x) { + uint8_t z = t1[(s0 + x) & 0xFF] + t2[(s1 + y) & 0xFF]; + seeds[y] <<= 8; + seeds[y] |= z; + } + } +} + +void GBACheatSetGameSharkVersion(struct GBACheatSet* cheats, int version) { + cheats->gsaVersion = 1; + switch (version) { + case 1: + memcpy(cheats->gsaSeeds, GBACheatGameSharkSeeds, 4 * sizeof(uint32_t)); + break; + case 3: + memcpy(cheats->gsaSeeds, GBACheatProActionReplaySeeds, 4 * sizeof(uint32_t)); + break; + } +} + +bool GBACheatAddGameSharkRaw(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) { + enum GBAGameSharkType type = op1 >> 28; + struct GBACheat* cheat = 0; + + if (cheats->incompleteCheat) { + if (cheats->remainingAddresses > 0) { + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_ASSIGN; + cheat->width = 4; + cheat->address = op1; + cheat->operand = cheats->incompleteCheat->operand; + cheat->repeat = 1; + --cheats->remainingAddresses; + } + if (cheats->remainingAddresses > 0) { + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_ASSIGN; + cheat->width = 4; + cheat->address = op2; + cheat->operand = cheats->incompleteCheat->operand; + cheat->repeat = 1; + --cheats->remainingAddresses; + } + if (cheats->remainingAddresses == 0) { + cheats->incompleteCheat = 0; + } + return true; + } + + switch (type) { + case GSA_ASSIGN_1: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_ASSIGN; + cheat->width = 1; + cheat->address = op1 & 0x0FFFFFFF; + break; + case GSA_ASSIGN_2: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_ASSIGN; + cheat->width = 2; + cheat->address = op1 & 0x0FFFFFFF; + break; + case GSA_ASSIGN_4: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_ASSIGN; + cheat->width = 4; + cheat->address = op1 & 0x0FFFFFFF; + break; + case GSA_ASSIGN_LIST: + cheats->remainingAddresses = (op1 & 0xFFFF) - 1; + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_ASSIGN; + cheat->width = 4; + cheat->address = op2; + cheats->incompleteCheat = cheat; + break; + case GSA_PATCH: + cheats->romPatches[0].address = (op1 & 0xFFFFFF) << 1; + cheats->romPatches[0].newValue = op2; + cheats->romPatches[0].applied = false; + cheats->romPatches[0].exists = true; + return true; + case GSA_BUTTON: + // TODO: Implement button + GBALog(0, GBA_LOG_STUB, "GameShark button unimplemented"); + return false; + case GSA_IF_EQ: + if (op1 == 0xDEADFACE) { + GBACheatReseedGameShark(cheats->gsaSeeds, op2, _gsa1T1, _gsa1T2); + return true; + } + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_IF_EQ; + cheat->width = 2; + cheat->address = op1 & 0x0FFFFFFF; + break; + case GSA_IF_EQ_RANGE: + cheat = GBACheatListAppend(&cheats->list); + cheat->type = CHEAT_IF_EQ; + cheat->width = 2; + cheat->address = op2 & 0x0FFFFFFF; + cheat->operand = op1 & 0xFFFF; + cheat->repeat = (op1 >> 16) & 0xFF; + return true; + case GSA_HOOK: + if (cheats->hook) { + return false; + } + cheats->hook = malloc(sizeof(*cheats->hook)); + cheats->hook->address = BASE_CART0 | (op1 & (SIZE_CART0 - 1)); + cheats->hook->mode = MODE_THUMB; + cheats->hook->refs = 1; + cheats->hook->reentries = 0; + return true; + default: + return false; + } + cheat->operand = op2; + cheat->repeat = 1; + return true; +} + +bool GBACheatAddGameShark(struct GBACheatSet* set, uint32_t op1, uint32_t op2) { + uint32_t o1 = op1; + uint32_t o2 = op2; + char line[18] = "XXXXXXXX XXXXXXXX"; + snprintf(line, sizeof(line), "%08X %08X", op1, op2); + GBACheatRegisterLine(set, line); + + switch (set->gsaVersion) { + case 0: + GBACheatSetGameSharkVersion(set, 1); + // Fall through + case 1: + GBACheatDecryptGameShark(&o1, &o2, set->gsaSeeds); + return GBACheatAddGameSharkRaw(set, o1, o2); + } + return false; +} + +bool GBACheatAddGameSharkLine(struct GBACheatSet* cheats, const char* line) { + uint32_t op1; + uint32_t op2; + line = hex32(line, &op1); + if (!line) { + return false; + } + while (*line == ' ') { + ++line; + } + line = hex32(line, &op2); + if (!line) { + return false; + } + return GBACheatAddGameShark(cheats, op1, op2); +}
A src/gba/cheats/gameshark.h

@@ -0,0 +1,18 @@

+/* Copyright (c) 2013-2015 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#ifndef GBA_CHEATS_GAMESHARK_H +#define GBA_CHEATS_GAMESHARK_H + +#include "gba/cheats.h" + +extern const uint32_t GBACheatGameSharkSeeds[4]; + +void GBACheatDecryptGameShark(uint32_t* op1, uint32_t* op2, const uint32_t* seeds); +void GBACheatReseedGameShark(uint32_t* seeds, uint16_t params, const uint8_t* t1, const uint8_t* t2); +void GBACheatSetGameSharkVersion(struct GBACheatSet* cheats, int version); +bool GBACheatAddGameSharkRaw(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2); + +#endif
A src/gba/cheats/parv3.c

@@ -0,0 +1,324 @@

+/* Copyright (c) 2013-2015 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "parv3.h" + +#include "gba/cheats/cheats-private.h" +#include "gba/cheats/gameshark.h" +#include "gba/gba.h" +#include "util/string.h" + +const uint32_t GBACheatProActionReplaySeeds[4] = { 0x7AA9648F, 0x7FAE6994, 0xC0EFAAD5, 0x42712C57 }; + +static const uint8_t _par3T1[256] = { + 0xD0, 0xFF, 0xBA, 0xE5, 0xC1, 0xC7, 0xDB, 0x5B, 0x16, 0xE3, 0x6E, 0x26, 0x62, 0x31, 0x2E, 0x2A, + 0xD1, 0xBB, 0x4A, 0xE6, 0xAE, 0x2F, 0x0A, 0x90, 0x29, 0x90, 0xB6, 0x67, 0x58, 0x2A, 0xB4, 0x45, + 0x7B, 0xCB, 0xF0, 0x73, 0x84, 0x30, 0x81, 0xC2, 0xD7, 0xBE, 0x89, 0xD7, 0x4E, 0x73, 0x5C, 0xC7, + 0x80, 0x1B, 0xE5, 0xE4, 0x43, 0xC7, 0x46, 0xD6, 0x6F, 0x7B, 0xBF, 0xED, 0xE5, 0x27, 0xD1, 0xB5, + 0xD0, 0xD8, 0xA3, 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 0xA0, 0x92, 0x78, 0xC6, 0x51, 0xFE, 0xF9, 0x30 +}; + +static const uint8_t _par3T2[256] = { + 0xAA, 0xAF, 0xF0, 0x72, 0x90, 0xF7, 0x71, 0x27, 0x06, 0x11, 0xEB, 0x9C, 0x37, 0x12, 0x72, 0xAA, + 0x65, 0xBC, 0x0D, 0x4A, 0x76, 0xF6, 0x5C, 0xAA, 0xB0, 0x7A, 0x7D, 0x81, 0xC1, 0xCE, 0x2F, 0x9F, + 0x02, 0x75, 0x38, 0xC8, 0xFC, 0x66, 0x05, 0xC2, 0x2C, 0xBD, 0x91, 0xAD, 0x03, 0xB1, 0x88, 0x93, + 0x31, 0xC6, 0xAB, 0x40, 0x23, 0x43, 0x76, 0x54, 0xCA, 0xE7, 0x00, 0x96, 0x9F, 0xD8, 0x24, 0x8B, + 0xE4, 0xDC, 0xDE, 0x48, 0x2C, 0xCB, 0xF7, 0x84, 0x1D, 0x45, 0xE5, 0xF1, 0x75, 0xA0, 0xED, 0xCD, + 0x4B, 0x24, 0x8A, 0xB3, 0x98, 0x7B, 0x12, 0xB8, 0xF5, 0x63, 0x97, 0xB3, 0xA6, 0xA6, 0x0B, 0xDC, + 0xD8, 0x4C, 0xA8, 0x99, 0x27, 0x0F, 0x8F, 0x94, 0x63, 0x0F, 0xB0, 0x11, 0x94, 0xC7, 0xE9, 0x7F, + 0x3B, 0x40, 0x72, 0x4C, 0xDB, 0x84, 0x78, 0xFE, 0xB8, 0x56, 0x08, 0x80, 0xDF, 0x20, 0x2F, 0xB9, + 0x66, 0x2D, 0x60, 0x63, 0xF5, 0x18, 0x15, 0x1B, 0x86, 0x85, 0xB9, 0xB4, 0x68, 0x0E, 0xC6, 0xD1, + 0x8A, 0x81, 0x2B, 0xB3, 0xF6, 0x48, 0xF0, 0x4F, 0x9C, 0x28, 0x1C, 0xA4, 0x51, 0x2F, 0xD7, 0x4B, + 0x17, 0xE7, 0xCC, 0x50, 0x9F, 0xD0, 0xD1, 0x40, 0x0C, 0x0D, 0xCA, 0x83, 0xFA, 0x5E, 0xCA, 0xEC, + 0xBF, 0x4E, 0x7C, 0x8F, 0xF0, 0xAE, 0xC2, 0xD3, 0x28, 0x41, 0x9B, 0xC8, 0x04, 0xB9, 0x4A, 0xBA, + 0x72, 0xE2, 0xB5, 0x06, 0x2C, 0x1E, 0x0B, 0x2C, 0x7F, 0x11, 0xA9, 0x26, 0x51, 0x9D, 0x3F, 0xF8, + 0x62, 0x11, 0x2E, 0x89, 0xD2, 0x9D, 0x35, 0xB1, 0xE4, 0x0A, 0x4D, 0x93, 0x01, 0xA7, 0xD1, 0x2D, + 0x00, 0x87, 0xE2, 0x2D, 0xA4, 0xE9, 0x0A, 0x06, 0x66, 0xF8, 0x1F, 0x44, 0x75, 0xB5, 0x6B, 0x1C, + 0xFC, 0x31, 0x09, 0x48, 0xA3, 0xFF, 0x92, 0x12, 0x58, 0xE9, 0xFA, 0xAE, 0x4F, 0xE2, 0xB4, 0xCC +}; +static uint32_t _parAddr(uint32_t x) { + return (x & 0xFFFFF) | ((x << 4) & 0x0F000000); +} + +static void _parEndBlock(struct GBACheatSet* cheats) { + size_t size = GBACheatListSize(&cheats->list) - GBACheatListIndex(&cheats->list, cheats->currentBlock); + if (cheats->currentBlock->repeat) { + cheats->currentBlock->negativeRepeat = size - cheats->currentBlock->repeat; + } else { + cheats->currentBlock->repeat = size; + } + cheats->currentBlock = 0; +} + +static void _parElseBlock(struct GBACheatSet* cheats) { + size_t size = GBACheatListSize(&cheats->list) - GBACheatListIndex(&cheats->list, cheats->currentBlock); + cheats->currentBlock->repeat = size; +} + +static bool _addPAR3Cond(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) { + enum GBAActionReplay3Condition condition = op1 & PAR3_COND; + int width = 1 << ((op1 & PAR3_WIDTH) >> PAR3_WIDTH_BASE); + if (width > 4) { + // TODO: Always false conditions + return false; + } + if ((op1 & PAR3_ACTION) == PAR3_ACTION_DISABLE) { + // TODO: Codes that disable + return false; + } + + struct GBACheat* cheat = GBACheatListAppend(&cheats->list); + cheat->address = _parAddr(op1); + cheat->width = width; + cheat->operand = op2 & (0xFFFFFFFFU >> ((4 - width) * 8)); + cheat->addressOffset = 0; + cheat->operandOffset = 0; + + switch (op1 & PAR3_ACTION) { + case PAR3_ACTION_NEXT: + cheat->repeat = 1; + cheat->negativeRepeat = 0; + break; + case PAR3_ACTION_NEXT_TWO: + cheat->repeat = 2; + cheat->negativeRepeat = 0; + break; + case PAR3_ACTION_BLOCK: + cheat->repeat = 0; + cheat->negativeRepeat = 0; + if (cheats->currentBlock) { + _parEndBlock(cheats); + } + cheats->currentBlock = cheat; + break; + } + + switch (condition) { + case PAR3_COND_OTHER: + // We shouldn't be able to get here + GBALog(0, GBA_LOG_ERROR, "Unexpectedly created 'other' PARv3 code"); + cheat->type = CHEAT_IF_LAND; + cheat->operand = 0; + break; + case PAR3_COND_EQ: + cheat->type = CHEAT_IF_EQ; + break; + case PAR3_COND_NE: + cheat->type = CHEAT_IF_NE; + break; + case PAR3_COND_LT: + cheat->type = CHEAT_IF_LT; + break; + case PAR3_COND_GT: + cheat->type = CHEAT_IF_GT; + break; + case PAR3_COND_ULT: + cheat->type = CHEAT_IF_ULT; + break; + case PAR3_COND_UGT: + cheat->type = CHEAT_IF_UGT; + break; + case PAR3_COND_LAND: + cheat->type = CHEAT_IF_LAND; + break; + } + return true; +} + +static bool _addPAR3Special(struct GBACheatSet* cheats, uint32_t op2) { + struct GBACheat* cheat; + switch (op2 & 0xFF000000) { + case PAR3_OTHER_SLOWDOWN: + // TODO: Slowdown + return false; + case PAR3_OTHER_BUTTON_1: + case PAR3_OTHER_BUTTON_2: + case PAR3_OTHER_BUTTON_4: + // TODO: Button + GBALog(0, GBA_LOG_STUB, "GameShark button unimplemented"); + return false; + // TODO: Fix overriding existing patches + case PAR3_OTHER_PATCH_1: + cheats->romPatches[0].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1); + cheats->romPatches[0].applied = false; + cheats->romPatches[0].exists = true; + cheats->incompletePatch = &cheats->romPatches[0]; + break; + case PAR3_OTHER_PATCH_2: + cheats->romPatches[1].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1); + cheats->romPatches[1].applied = false; + cheats->romPatches[1].exists = true; + cheats->incompletePatch = &cheats->romPatches[1]; + break; + case PAR3_OTHER_PATCH_3: + cheats->romPatches[2].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1); + cheats->romPatches[2].applied = false; + cheats->romPatches[2].exists = true; + cheats->incompletePatch = &cheats->romPatches[2]; + break; + case PAR3_OTHER_PATCH_4: + cheats->romPatches[3].address = BASE_CART0 | ((op2 & 0xFFFFFF) << 1); + cheats->romPatches[3].applied = false; + cheats->romPatches[3].exists = true; + cheats->incompletePatch = &cheats->romPatches[3]; + break; + case PAR3_OTHER_ENDIF: + if (cheats->currentBlock) { + _parEndBlock(cheats); + return true; + } + return false; + case PAR3_OTHER_ELSE: + if (cheats->currentBlock) { + _parElseBlock(cheats); + return true; + } + return false; + case PAR3_OTHER_FILL_1: + cheat = GBACheatListAppend(&cheats->list); + cheat->address = _parAddr(op2); + cheat->width = 1; + cheats->incompleteCheat = cheat; + break; + case PAR3_OTHER_FILL_2: + cheat = GBACheatListAppend(&cheats->list); + cheat->address = _parAddr(op2); + cheat->width = 2; + cheats->incompleteCheat = cheat; + break; + case PAR3_OTHER_FILL_4: + cheat = GBACheatListAppend(&cheats->list); + cheat->address = _parAddr(op2); + cheat->width = 3; + cheats->incompleteCheat = cheat; + break; + } + return true; +} + +bool GBACheatAddProActionReplayRaw(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2) { + if (cheats->incompletePatch) { + cheats->incompletePatch->newValue = op1; + cheats->incompletePatch = 0; + return true; + } + if (cheats->incompleteCheat) { + cheats->incompleteCheat->operand = op1 & (0xFFFFFFFFU >> ((4 - cheats->incompleteCheat->width) * 8)); + cheats->incompleteCheat->addressOffset = op2 >> 24; + cheats->incompleteCheat->repeat = (op2 >> 16) & 0xFF; + cheats->incompleteCheat->addressOffset = (op2 & 0xFFFF) * cheats->incompleteCheat->width; + cheats->incompleteCheat = 0; + return true; + } + + if (op2 == 0x001DC0DE) { + return true; + } + + switch (op1) { + case 0x00000000: + return _addPAR3Special(cheats, op2); + case 0xDEADFACE: + GBACheatReseedGameShark(cheats->gsaSeeds, op2, _par3T1, _par3T2); + return true; + } + + if (op1 >> 24 == 0xC4) { + if (cheats->hook) { + return false; + } + cheats->hook = malloc(sizeof(*cheats->hook)); + cheats->hook->address = BASE_CART0 | (op1 & (SIZE_CART0 - 1)); + cheats->hook->mode = MODE_THUMB; + cheats->hook->refs = 1; + cheats->hook->reentries = 0; + return true; + } + + if (op1 & PAR3_COND) { + return _addPAR3Cond(cheats, op1, op2); + } + + int width = 1 << ((op1 & PAR3_WIDTH) >> PAR3_WIDTH_BASE); + struct GBACheat* cheat = GBACheatListAppend(&cheats->list); + cheat->address = _parAddr(op1); + cheat->operandOffset = 0; + cheat->addressOffset = 0; + cheat->repeat = 1; + + switch (op1 & PAR3_BASE) { + case PAR3_BASE_ASSIGN: + cheat->type = CHEAT_ASSIGN; + cheat->addressOffset = width; + if (width < 4) { + cheat->repeat = (op2 >> (width * 8)) + 1; + } + break; + case PAR3_BASE_INDIRECT: + cheat->type = CHEAT_ASSIGN_INDIRECT; + if (width < 4) { + cheat->addressOffset = (op2 >> (width * 8)) * width; + } + break; + case PAR3_BASE_ADD: + cheat->type = CHEAT_ADD; + break; + case PAR3_BASE_OTHER: + width = ((op1 >> 24) & 1) + 1; + cheat->type = CHEAT_ASSIGN; + cheat->address = BASE_IO | (op1 & OFFSET_MASK); + break; + } + + cheat->width = width; + cheat->operand = op2 & (0xFFFFFFFFU >> ((4 - width) * 8)); + return true; +} + +bool GBACheatAddProActionReplay(struct GBACheatSet* set, uint32_t op1, uint32_t op2) { + uint32_t o1 = op1; + uint32_t o2 = op2; + char line[18] = "XXXXXXXX XXXXXXXX"; + snprintf(line, sizeof(line), "%08X %08X", op1, op2); + GBACheatRegisterLine(set, line); + + switch (set->gsaVersion) { + case 0: + GBACheatSetGameSharkVersion(set, 3); + // Fall through + case 1: + GBACheatDecryptGameShark(&o1, &o2, set->gsaSeeds); + return GBACheatAddProActionReplayRaw(set, o1, o2); + } + return false; +} + +bool GBACheatAddProActionReplayLine(struct GBACheatSet* cheats, const char* line) { + uint32_t op1; + uint32_t op2; + line = hex32(line, &op1); + if (!line) { + return false; + } + while (*line == ' ') { + ++line; + } + line = hex32(line, &op2); + if (!line) { + return false; + } + return GBACheatAddProActionReplay(cheats, op1, op2); +}
A src/gba/cheats/parv3.h

@@ -0,0 +1,15 @@

+/* Copyright (c) 2013-2015 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#ifndef GBA_CHEATS_PARV3_H +#define GBA_CHEATS_PARV3_H + +#include "gba/cheats.h" + +extern const uint32_t GBACheatProActionReplaySeeds[4]; + +bool GBACheatAddProActionReplayRaw(struct GBACheatSet* cheats, uint32_t op1, uint32_t op2); + +#endif
M src/util/string.csrc/util/string.c

@@ -197,3 +197,71 @@ }

newUTF8[utf8Length] = '\0'; return newUTF8; } + +int hexDigit(char digit) { + switch (digit) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return digit - '0'; + + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + return digit - 'a' + 10; + + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + return digit - 'A' + 10; + + default: + return -1; + } +} + +const char* hex32(const char* line, uint32_t* out) { + uint32_t value = 0; + int i; + for (i = 0; i < 8; ++i, ++line) { + char digit = *line; + value <<= 4; + int nybble = hexDigit(digit); + if (nybble < 0) { + return 0; + } + value |= nybble; + } + *out = value; + return line; +} + +const char* hex16(const char* line, uint16_t* out) { + uint16_t value = 0; + *out = 0; + int i; + for (i = 0; i < 4; ++i, ++line) { + char digit = *line; + value <<= 4; + int nybble = hexDigit(digit); + if (nybble < 0) { + return 0; + } + value |= nybble; + } + *out = value; + return line; +}
M src/util/string.hsrc/util/string.h

@@ -18,4 +18,8 @@

int utfcmp(const uint16_t* utf16, const char* utf8, size_t utf16Length, size_t utf8Length); char* utf16to8(const uint16_t* utf16, size_t length); +int hexDigit(char digit); +const char* hex32(const char* line, uint32_t* out); +const char* hex16(const char* line, uint16_t* out); + #endif