diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/progress.py | 179 | ||||
-rw-r--r-- | tools/script_extractor.py | 245 |
2 files changed, 315 insertions, 109 deletions
diff --git a/tools/progress.py b/tools/progress.py new file mode 100644 index 0000000..69ae64b --- /dev/null +++ b/tools/progress.py @@ -0,0 +1,179 @@ +#!/usr/bin/env python3# +import subprocess +import math + +# TODO - +# -- reportINCROMs -- + # create switch + # create optional grep location + # add in percentage +# -- reportUnnamedSymbols -- + # create switch + # take sym file as an argument + +# this is not the best way to do it, by any means +def reportINCROMs(): + grepProc = subprocess.Popen(['grep', '-r', 'INCROM', '..'], stdout=subprocess.PIPE) + targetLines = grepProc.communicate()[0].decode().split('\n') + banks = 0x40 + incromBytes = [0]*banks + incromByteTotal = 0 + for line in targetLines: + line = line.lower() # ignore case + + # ignore the actual definition of the macro + if 'macro' in line: + continue + + # ignore anything in tools + if '/tools/' in line: + continue + + # ignore binary files in case swp's exist + if 'binary file' in line: + continue + + # find the last two hex location values + splitLine = line.split("$") + + # not sure what the line is, but it's not working so skip + if len(splitLine) < 3: + continue + + incEnd = int(splitLine[-1],16) + incStart = int(splitLine[-2].split(",",1)[0],16) + incBank = math.floor(incStart / 0x4000) + diff = incEnd - incStart + incromBytes[incBank] += diff + incromByteTotal += diff + print("Total: " + str(incromByteTotal) + " bytes") + print("Made up of the following: ") + for i in range(0,banks): + if incromBytes[i] == 0: + continue + + bankName= "bank" + format(i,"02x") + ": " + if i == 0: + bankName = "home: " + bytesString = str(incromBytes[i]) + formattingStrings = " "*(8-len(bytesString)) + print(bankName + bytesString + formattingStrings + "bytes") + + +# reads sym files and looks for instances of tcgdisasm's automatic symbols +def reportUnnamedSymbols(): + with open("../tcg.sym") as symfile: + data = symfile.read().split("\n") + + # format [ [ "type" : number ], ... ] + typeCounts = [] + + # to cut back on for loops I'll manually list the super common ones, such as Func + funcCount = 0 + branchCount = 0 + wramCount = 0 + sramCount = 0 + hramCount = 0 + + labelTotal = 0 + localLabelTotal = 0 + unnamedLocalLabelTotal = 0 + unnamedLabelTotal = 0 + + # expecting all lines to be formated as `bank:addr name` + for line in data: + + splitline = line.split(":") + + # line not formatted as expected + if len(splitline) < 2: + continue + + # at this point it's probably some form of label + if "." in line: + localLabelTotal += 1 + else: + labelTotal += 1 + + bank = int(splitline[0], 16) + splitline = splitline[1].split(" ") + + # line not formatted as expected + if len(splitline) < 2: + continue + + localAddr = int(splitline[0], 16) + name = splitline[1] + + globalAddr = bank*0x4000 + localAddr + if bank > 0: + globalAddr -= 0x4000 + + globalAddrString = format(globalAddr,"04x") + if name.endswith(globalAddrString): + + # don't pay as much attention to local labels + if "." in line: + unnamedLocalLabelTotal += 1 + continue + else: + unnamedLabelTotal += 1 + + labelType = name[0:len(globalAddrString)*-1] + + # take care of the common ones before looping + if labelType == "Func_": + funcCount += 1 + continue + elif labelType == "Branch_": + branchCount += 1 + continue + elif labelType == "w": + wramCount += 1 + continue + elif labelType in ["s0","s1","s2","s3"]: # all that are listed in sram.asm + sramCount += 1 + continue + elif labelType == "h": + hramCount += 1 + continue + + foundType = False + for tc in typeCounts: + if tc[0] == labelType: + tc[1] += 1 + foundType = True + + if not foundType: + typeCounts.append([labelType,1]) + + + # there are so many that I did them manually, but they're a misc type + typeCounts.append(["Branch_", branchCount]) + + # do some sorting. + typeCounts = sorted(typeCounts, key = lambda x: x[1], reverse = True) + + namedLabelTotal = labelTotal - unnamedLabelTotal + namedLabelPercent = round((namedLabelTotal / labelTotal)*100, 3) + namedLocalLabelTotal = localLabelTotal - unnamedLocalLabelTotal + namedLocalLabelPercent = round((namedLocalLabelTotal / localLabelTotal)*100, 3) + + print("Named Labels: " + str(namedLabelTotal) + "/" + str(labelTotal) + " (" + str(namedLabelPercent) + "%)") + print("Named Local Labels: " + str(namedLocalLabelTotal) + "/" + str(localLabelTotal) + " (" + str(namedLocalLabelPercent) + "%)") + print() + print("Func count: " + str(funcCount)) + print("wram count: " + str(wramCount)) + print("sram count: " + str(sramCount)) + print("hram count: " + str(hramCount)) + print() + print("Additional types:") + + for tc in typeCounts: + spaces = " " * (30 - len(tc[0])) + print(tc[0] + spaces + "x" + format(tc[1],"02")) + +reportINCROMs() +print() +print() +reportUnnamedSymbols() diff --git a/tools/script_extractor.py b/tools/script_extractor.py index 49d5e58..7f80b01 100644 --- a/tools/script_extractor.py +++ b/tools/script_extractor.py @@ -9,10 +9,18 @@ ###### Script list is a work in progress. The following arguments are ###### ###### accepted and accounted for. ###### ###### b - byte w - word j - jump (within script) t - text (tx) ###### +###### f - flag d - direction i - decimal byte ###### ###### q - Used when the script's arguments have not been determined yet ###### ############################################################################### import argparse +# Quit Types +DO_NOT_QUIT = 0 +QUIT_CONTINUE_CODE = 1 +QUIT_JUMP = 2 +QUIT_SPECIAL = 3 +QUIT_DEBUG = -1 + def decodeLine(scriptList, game_data, loc, ignore_broken, branchList): currLine = scriptList[game_data[loc]] ret = "\trun_script " + currLine[0] + "\n" @@ -22,6 +30,9 @@ def decodeLine(scriptList, game_data, loc, ignore_broken, branchList): if c == "b": ret += "\tdb $" + format(game_data[loc],"02x") + "\n" loc += 1 + elif c == "i": + ret += "\tdb " + str(game_data[loc]) + "\n" + loc += 1 elif c == "w": ret += "\tdw $" + format((game_data[loc] + (game_data[loc+1]<<8)),"04x") + "\n" loc += 2 @@ -34,12 +45,23 @@ def decodeLine(scriptList, game_data, loc, ignore_broken, branchList): branchList.append(wordLoc) loc += 2 elif c == "t": - ret += "\ttx Text" + format((game_data[loc] + (game_data[loc+1]<<8)),"04x") + "\n" + addr = (game_data[loc] + (game_data[loc+1]<<8)) + if addr == 0: + ret += "\tdw $0000\n" + else: + ret += "\ttx Text" + format(addr,"04x") + "\n" loc += 2 + elif c == "f": + ret += "\tdb EVENT_FLAG_" + format(game_data[loc],"02X") + "\n" + loc += 1 + elif c == "d": + dir_list = ["NORTH","EAST","SOUTH","WEST"] + ret += "\tdb " + dir_list[game_data[loc]] + "\n" + loc += 1 elif c == "q": print("haven't updated data for this yet") if not ignore_broken: - quit = True + quit = QUIT_DEBUG else: print("UNACCEPTED CHARACTER: " + c) return (loc, ret, quit) @@ -69,6 +91,7 @@ def main(): end = False ignore_broken = True while (len(branchList) > 0): + branchList.sort() # export parts in order somewhat loc = branchList.pop(0) + 0x8000 printScript(game_data, loc, auto, end, ignore_broken, scriptList,\ branchList, exploredList) @@ -90,123 +113,127 @@ def printScript(game_data, loc, auto, end, ignore_broken, scriptList, \ print("OWSequence_" + ls + ": ; " + ls + " (3:" + lsa + ")" ) loc += 1 print("\tstart_script") - while not end: + while end == DO_NOT_QUIT: loc, outstr, end = decodeLine(scriptList,game_data,loc,ignore_broken,branchList) outstr = outstr[:-1] # [:-1] strips the newline at the end if auto: print(outstr) else: input(outstr) - print("; " + hex(loc)) - + warning = "" + if end == QUIT_CONTINUE_CODE: + warning = " WARNING: There is probably regular assembly here" + #elif end == QUIT_SPECIAL: + # warning = "\nWARNING: This will return to the place that began this Sequence" + print("; " + hex(loc) + warning) def createList(): # this is a func just so all this can go at the bottom # name, arg list, is an ender return [ - ("OWScript_EndScriptLoop1", "", True), - ("OWScript_CloseAdvancedTextBox", "", False), - ("OWScript_PrintTextString", "t", False), - ("Func_ccdc", "bb", False), - ("OWScript_AskQuestionJump", "tj", False), # more complex behavior too (jumping) - ("OWScript_StartBattle", "bbb", False), - ("OWScript_PrintVariableText", "tt", False), - ("Func_cda8", "bbbb", False), - ("OWScript_PrintTextCloseBox", "t", True), - ("Func_cdcb", "", False), - ("Func_ce26", "bb", False), - ("OWScript_CloseTextBox", "", False), - ("OWScript_GiveBoosterPacks", "bbb", False), - ("Func_cf0c", "bbb", False), # more complex behavior too (jumping) - ("Func_cf12", "bbb", False), - ("OWScript_GiveCard", "b", False), - ("OWScript_TakeCard", "b", False), - ("Func_cf53", "w", False), # more complex behavior too (jumping) - ("Func_cf7b", "", False), - ("Func_cf2d", "bbbb", False), # more complex behavior too (jumping + ??) - ("Func_cf96", "w", False), # only jumps? still needs args to do that though - ("Func_cfc6", "b", False), - ("Func_cfd4", "", False), - ("Func_d00b", "", False), # includes something with random and extra data - ("Func_d025", "w", False), # possibly only jumps, still needs args - ("Func_d032", "w", False), # see above - ("Func_d03f", "", False), - ("OWScript_Jump", "j", True), # jumps to d - ("Func_d04f", "", False), - ("Func_d055", "b", False), - ("OWScript_MovePlayer", "bb", False), - ("Func_cee2", "b", False), - ("OWScript_SetDialogName", "b", False), - ("Func_d088", "bj", False), - ("Func_d095", "bbb", False), - ("Func_d0be", "bb", False), - ("OWScript_DoFrames", "b", False), - ("Func_d0d9", "bbw", False), # jumps but still needs args - ("Func_d0f2", "bbw", False), # jumps but still needs args - ("Func_ce4a", "bb", False), - ("Func_ceba", "q", False), - ("Func_d103", "q", False), - ("Func_d125", "b", False), - ("Func_d135", "b", False), - ("Func_d16b", "q", False), - ("Func_cd4f", "q", False), - ("Func_cd94", "q", False), - ("Func_ce52", "q", False), - ("Func_cdd8", "q", False), - ("Func_cdf5", "q", False), - ("Func_d195", "q", False), - ("Func_d1ad", "", False), - ("Func_d1b3", "q", False), - ("OWScript_EndScriptCloseText", "", True), # it calls inc twice but it ends anyway? - ("Func_d244", "q", False), - ("Func_d24c", "q", False), - ("OWScript_OpenDeckMachine", "q", False), - ("Func_d271", "q", False), - ("Func_d36d", "bbbbb", False), - ("Func_ce6f", "bbb", False), - ("Func_d209", "q", False), - ("Func_d38f", "q", False), - ("Func_d396", "q", False), - ("Func_cd76", "q", False), - ("Func_d39d", "q", False), - ("Func_d3b9", "q", False), - ("OWScript_GivePCPack", "b", False), - ("OWScript_nop", "", False), - ("Func_d3d4", "q", False), - ("Func_d3e0", "", False), - ("Func_d3fe", "q", False), - ("Func_d408", "q", False), - ("Func_d40f", "q", False), - ("Func_d416", "q", False), - ("Func_d423", "q", False), - ("Func_d429", "q", False), - ("Func_d41d", "", False), - ("Func_d42f", "q", False), - ("Func_d435", "b", False), - ("Func_cce4", "tj", False), - ("Func_d2f6", "q", False), - ("Func_d317", "", False), - ("Func_d43d", "q", False), - ("OWScript_EndScriptLoop2", "q", True), - ("OWScript_EndScriptLoop3", "q", True), - ("OWScript_EndScriptLoop4", "q", True), - ("OWScript_EndScriptLoop5", "q", True), - ("OWScript_EndScriptLoop6", "q", True), - ("OWScript_SetFlagValue", "bb", False), - ("OWScript_JumpIfFlagZero1", "q", False), - ("OWScript_JumpIfFlagNonzero1", "q", False), - ("OWScript_JumpIfFlagEqual", "bbj", False), # also capable of jumping - ("OWScript_JumpIfFlagNotEqual", "bbj", False), # jumps - ("OWScript_JumpIfFlagNotLessThan", "bbj", False), - ("OWScript_JumpIfFlagLessThan", "bbj", False), - ("OWScript_MaxOutFlagValue", "b", False), - ("OWScript_ZeroOutFlagValue", "q", False), - ("OWScript_JumpIfFlagNonzero2", "bj", False), - ("OWScript_JumpIfFlagZero2", "bj", False), - ("OWScript_IncrementFlagValue", "b", False), - ("OWScript_EndScriptLoop7", "q", True), - ("OWScript_EndScriptLoop8", "q", True), - ("OWScript_EndScriptLoop9", "q", True), - ("OWScript_EndScriptLoop10", "q", True) + ("OWScript_EndScriptLoop1", "", QUIT_CONTINUE_CODE), + ("OWScript_CloseAdvancedTextBox", "", DO_NOT_QUIT), + ("OWScript_PrintTextString", "t", DO_NOT_QUIT), + ("Func_ccdc", "bb", DO_NOT_QUIT), + ("OWScript_AskQuestionJump", "tj", DO_NOT_QUIT), # more complex behavior too (jumping) + ("OWScript_StartBattle", "bbb", DO_NOT_QUIT), + ("OWScript_PrintVariableText", "tt", DO_NOT_QUIT), + ("Func_cda8", "bbbb", DO_NOT_QUIT), + ("OWScript_PrintTextCloseBox", "t", QUIT_CONTINUE_CODE), + ("Func_cdcb", "", DO_NOT_QUIT), + ("Func_ce26", "bb", DO_NOT_QUIT), + ("OWScript_CloseTextBox", "", DO_NOT_QUIT), + ("OWScript_GiveBoosterPacks", "bbb", DO_NOT_QUIT), + ("Func_cf0c", "bbb", DO_NOT_QUIT), # more complex behavior too (jumping) + ("Func_cf12", "bbb", DO_NOT_QUIT), + ("OWScript_GiveCard", "b", DO_NOT_QUIT), + ("OWScript_TakeCard", "b", DO_NOT_QUIT), + ("Func_cf53", "w", DO_NOT_QUIT), # more complex behavior too (jumping) + ("Func_cf7b", "", DO_NOT_QUIT), + ("Func_cf2d", "bbbb", DO_NOT_QUIT), # more complex behavior too (jumping + ??) + ("Func_cf96", "w", DO_NOT_QUIT), # only jumps? still needs args to do that though + ("Func_cfc6", "b", DO_NOT_QUIT), + ("Func_cfd4", "", DO_NOT_QUIT), + ("Func_d00b", "", DO_NOT_QUIT), # includes something with random and extra data + ("Func_d025", "w", DO_NOT_QUIT), # possibly only jumps, still needs args + ("Func_d032", "w", DO_NOT_QUIT), # see above + ("Func_d03f", "", DO_NOT_QUIT), + ("OWScript_Jump", "j", QUIT_JUMP), # jumps to d + ("Func_d04f", "", DO_NOT_QUIT), + ("OWScript_SetPlayerDirection", "d", DO_NOT_QUIT), + ("OWScript_MovePlayer", "db", DO_NOT_QUIT), + ("OWScript_ShowCardReceivedScreen", "b", DO_NOT_QUIT), + ("OWScript_SetDialogName", "b", DO_NOT_QUIT), + ("OWScript_SetNextNPCandOWSequence", "bj", DO_NOT_QUIT), + ("Func_d095", "bbb", DO_NOT_QUIT), + ("Func_d0be", "bb", DO_NOT_QUIT), + ("OWScript_DoFrames", "b", DO_NOT_QUIT), + ("Func_d0d9", "bbw", DO_NOT_QUIT), # jumps but still needs args + ("OWScript_JumpIfPlayerCoordMatches", "iij", DO_NOT_QUIT), # jumps but still needs args + ("Func_ce4a", "bb", DO_NOT_QUIT), + ("OWScript_GiveOneOfEachTrainerBooster", "", DO_NOT_QUIT), + ("Func_d103", "q", DO_NOT_QUIT), + ("Func_d125", "b", DO_NOT_QUIT), + ("Func_d135", "b", DO_NOT_QUIT), + ("Func_d16b", "q", DO_NOT_QUIT), + ("Func_cd4f", "q", DO_NOT_QUIT), + ("Func_cd94", "q", DO_NOT_QUIT), + ("Func_ce52", "q", DO_NOT_QUIT), + ("Func_cdd8", "q", DO_NOT_QUIT), + ("Func_cdf5", "q", DO_NOT_QUIT), + ("Func_d195", "q", DO_NOT_QUIT), + ("Func_d1ad", "", DO_NOT_QUIT), + ("Func_d1b3", "q", DO_NOT_QUIT), + ("OWScript_QuitScriptFully", "", QUIT_SPECIAL), # it calls inc twice but it ends anyway? + ("Func_d244", "q", DO_NOT_QUIT), + ("Func_d24c", "q", DO_NOT_QUIT), + ("OWScript_OpenDeckMachine", "q", DO_NOT_QUIT), + ("Func_d271", "q", DO_NOT_QUIT), + ("OWScript_EnterMap", "bbood", DO_NOT_QUIT), + ("Func_ce6f", "bbb", DO_NOT_QUIT), + ("Func_d209", "q", DO_NOT_QUIT), + ("Func_d38f", "q", DO_NOT_QUIT), + ("Func_d396", "q", DO_NOT_QUIT), + ("Func_cd76", "q", DO_NOT_QUIT), + ("Func_d39d", "q", DO_NOT_QUIT), + ("Func_d3b9", "q", DO_NOT_QUIT), + ("OWScript_GivePCPack", "b", DO_NOT_QUIT), + ("OWScript_nop", "", DO_NOT_QUIT), + ("Func_d3d4", "q", DO_NOT_QUIT), + ("Func_d3e0", "", DO_NOT_QUIT), + ("Func_d3fe", "q", DO_NOT_QUIT), + ("Func_d408", "b", DO_NOT_QUIT), + ("Func_d40f", "q", DO_NOT_QUIT), + ("Func_d416", "q", DO_NOT_QUIT), + ("Func_d423", "q", DO_NOT_QUIT), + ("Func_d429", "q", DO_NOT_QUIT), + ("Func_d41d", "", DO_NOT_QUIT), + ("Func_d42f", "q", DO_NOT_QUIT), + ("Func_d435", "b", DO_NOT_QUIT), + ("OWScript_AskQuestionJumpDefaultYes", "tj", DO_NOT_QUIT), + ("Func_d2f6", "q", DO_NOT_QUIT), + ("Func_d317", "", DO_NOT_QUIT), + ("Func_d43d", "q", DO_NOT_QUIT), + ("OWScript_EndScriptLoop2", "q", QUIT_CONTINUE_CODE), + ("OWScript_EndScriptLoop3", "q", QUIT_CONTINUE_CODE), + ("OWScript_EndScriptLoop4", "q", QUIT_CONTINUE_CODE), + ("OWScript_EndScriptLoop5", "q", QUIT_CONTINUE_CODE), + ("OWScript_EndScriptLoop6", "q", QUIT_CONTINUE_CODE), + ("OWScript_SetFlagValue", "fb", DO_NOT_QUIT), + ("OWScript_JumpIfFlagZero1", "q", DO_NOT_QUIT), + ("OWScript_JumpIfFlagNonzero1", "q", DO_NOT_QUIT), + ("OWScript_JumpIfFlagEqual", "fbj", DO_NOT_QUIT), # also capable of jumping + ("OWScript_JumpIfFlagNotEqual", "fbj", DO_NOT_QUIT), # jumps + ("OWScript_JumpIfFlagNotLessThan", "fbj", DO_NOT_QUIT), + ("OWScript_JumpIfFlagLessThan", "fbj", DO_NOT_QUIT), + ("OWScript_MaxOutFlagValue", "f", DO_NOT_QUIT), + ("OWScript_ZeroOutFlagValue", "q", DO_NOT_QUIT), + ("OWScript_JumpIfFlagNonzero2", "fj", DO_NOT_QUIT), + ("OWScript_JumpIfFlagZero2", "fj", DO_NOT_QUIT), + ("OWScript_IncrementFlagValue", "f", DO_NOT_QUIT), + ("OWScript_EndScriptLoop7", "q", QUIT_CONTINUE_CODE), + ("OWScript_EndScriptLoop8", "q", QUIT_CONTINUE_CODE), + ("OWScript_EndScriptLoop9", "q", QUIT_CONTINUE_CODE), + ("OWScript_EndScriptLoop10", "q", QUIT_CONTINUE_CODE) ] main() |