diff --git a/Makefile b/Makefile index b9eb4931..4aa93ae3 100755 --- a/Makefile +++ b/Makefile @@ -140,6 +140,7 @@ LOADER_FLAGS = -vwf FixPath = $(subst /,,$1) N64CRC = $(TOOLS_DIR)/n64crc +FIXCHECKSUMS = python3 $(TOOLS_DIR)/python/calc_func_checksums.py $(VERSION) TEXBUILDER = $(TOOLS_DIR)/dkr_texbuilder COMPRESS = $(TOOLS_DIR)/dkr_decompressor -c @@ -442,6 +443,7 @@ $(BUILD_DIR)/$(TARGET).bin: $(BUILD_DIR)/$(TARGET).elf | $(ALL_ASSETS_BUILT) $(BUILD_DIR)/$(TARGET).z64: $(BUILD_DIR)/$(TARGET).bin | $(ALL_ASSETS_BUILT) cp $< $@ + $(FIXCHECKSUMS) $(N64CRC) $@ sha1sum -c sha1/dkr.$(VERSION).sha1 diff --git a/src/unknown_001050.c b/src/unknown_001050.c index b4cdd6bd..581dec97 100644 --- a/src/unknown_001050.c +++ b/src/unknown_001050.c @@ -136,7 +136,8 @@ void audio_init(u32 arg0){ u32 tmp2; audioMgrConfig audConfig; - //*((s32*)0x803FFFFC) = 0xDEADBEEF; + // Uncomment this to test shiftibilty! + // *((s32*)0x803FFFFC) = 0xDEADBEEF; seq_max_len = 0; alHeapInit(&gALHeap, gBssSectionStart, 0x00029D88); diff --git a/src/unknown_043920.c b/src/unknown_043920.c index 2250ddcd..7c710334 100644 --- a/src/unknown_043920.c +++ b/src/unknown_043920.c @@ -99,6 +99,7 @@ s16 D_800DCDB0[16] = { 0x02FA, 0x02FE, 0x08F8, 0x03FD }; +// Checksum count for func_8003B4BC s32 D_800DCDD0 = 42391; s16 D_800DCDD4[4] = { diff --git a/tools/python/calc_func_checksums.py b/tools/python/calc_func_checksums.py new file mode 100644 index 00000000..f1a1565b --- /dev/null +++ b/tools/python/calc_func_checksums.py @@ -0,0 +1,75 @@ +import re +import os +import sys + +from file_util import FileUtil + +VERSION = sys.argv[1] + +BUILD_DIR = 'build/' + VERSION +MAP_FILEPATH = BUILD_DIR + '/dkr.map' +ROM_FILEPATH = BUILD_DIR + '/dkr.z64' + +FUNCTIONS_TO_CALC = [ + ('func_8003B4BC', 'D_800DCDD0'), + ('func_80068158', 'D_800DD320'), + ('D_80024D54', 'D_800DCEA0') +] + +def getMatches(string, regex): + out = [] + matches = re.finditer(regex, string, re.MULTILINE) + for matchNum, match in enumerate(matches, start=1): + start = match.start() + end = match.end() + out.append((string[start:end], match.groups(), (start, end))) + return out + +rom = FileUtil.get_bytes_from_file(ROM_FILEPATH) +mapFile = {} +RAM_TO_ROM = None + +def write_checksum_to_rom(romOffset, value): + global rom + rom[romOffset + 0] = (value >> 24) & 0xFF + rom[romOffset + 1] = (value >> 16) & 0xFF + rom[romOffset + 2] = (value >> 8) & 0xFF + rom[romOffset + 3] = value & 0xFF + +def calculate_checksum_for_function(funcLabel, varLabel): + if funcLabel not in mapFile: + raise Exception('Label "' + funcLabel + '" does not exist in the map file!') + if varLabel not in mapFile: + raise Exception('Label "' + varLabel + '" does not exist in the map file!') + funcEntry = mapFile[funcLabel] + funcAddress = funcEntry['value'] + funcLength = funcEntry['length'] + funcRomOffset = funcAddress - RAM_TO_ROM + count = 0 + for i in range(0, funcLength): + count += rom[funcRomOffset + i] + varEntry = mapFile[varLabel] + varAddress = varEntry['value'] + varRomOffset = varAddress - RAM_TO_ROM + write_checksum_to_rom(varRomOffset, count) + # print('The checksum count for "' + funcLabel + '" is: ' + hex(count)) + +def calculate_matches(): + global mapFile, RAM_TO_ROM + REGEX_MAP_GET_LABEL = r"[ ]*0x[0-9A-Fa-f]{8}([0-9A-Fa-f]{8})[ ]*([_A-Za-z0-9]+)" + mapMatches = getMatches(FileUtil.get_text_from_file(MAP_FILEPATH), REGEX_MAP_GET_LABEL) + for i in range(0, len(mapMatches) - 1): + match = mapMatches[i] + labelValue = match[1] + value = int(labelValue[0], 16) + length = int(mapMatches[i + 1][1][0], 16) - value + mapFile[labelValue[1]] = { "value": value, "length": length } + RAM_TO_ROM = mapFile['__RAM_TO_ROM']['value'] + +def main(): + calculate_matches() + for entry in FUNCTIONS_TO_CALC: + calculate_checksum_for_function(entry[0], entry[1]) + FileUtil.write_bytes_to_file(ROM_FILEPATH, rom) + +main() diff --git a/tools/python/file_util.py b/tools/python/file_util.py index 9545a9d0..e3f0c1a4 100644 --- a/tools/python/file_util.py +++ b/tools/python/file_util.py @@ -49,11 +49,21 @@ class FileUtil: with open(filename, 'r') as inFile: return inFile.read() + @staticmethod + def get_bytes_from_file(filename): + with open(filename, 'rb') as inFile: + return list(inFile.read()) + @staticmethod def write_text_to_file(filename, text): with open(filename, 'w') as outFile: outFile.write(text) + @staticmethod + def write_bytes_to_file(filename, binary): + with open(filename, 'wb') as outFile: + outFile.write(bytearray(binary)) + @staticmethod def delete_file(filename): remove(filename) diff --git a/tools/python/generate_ld.py b/tools/python/generate_ld.py index 220f4d04..2045ce0e 100644 --- a/tools/python/generate_ld.py +++ b/tools/python/generate_ld.py @@ -53,6 +53,9 @@ class GenerateLD: self.gen_open_block() self.gen_line('romPos = 0x0;') self.gen_boot_section() + self.gen_line('__RAM_START = 0x80000400;') + self.gen_line('__RAM_TO_ROM = __RAM_START - romPos;') + self.gen_newline() self.gen_main_section() self.gen_ucode_text_section() self.gen_data_section() @@ -75,7 +78,7 @@ class GenerateLD: self.gen_newline() def gen_main_section(self): - self.gen_line('.main 0x80000400 : AT(romPos) SUBALIGN(16)') + self.gen_line('.main __RAM_START : AT(romPos) SUBALIGN(16)') self.gen_open_block() for file in self.files: self.gen_line(file[0] + '(.text);')