diff options
| author | octorock <79596758+octorock@users.noreply.github.com> | 2021-03-05 23:39:40 +0100 |
|---|---|---|
| committer | octorock <79596758+octorock@users.noreply.github.com> | 2021-03-06 14:53:33 +0100 |
| commit | c83c3e60d373b29efef0688f622d061c6cde6a1b (patch) | |
| tree | 1cb29259857572b9497be120b0513ab51c3ccef5 /tools/script_disassembler | |
| parent | 6dce46458cb4011e824e16d19a6ae129f61481ca (diff) | |
Split scripts at more useful places
Diffstat (limited to 'tools/script_disassembler')
| -rw-r--r-- | tools/script_disassembler/incbin_parser.py | 104 | ||||
| -rw-r--r-- | tools/script_disassembler/script_disassembler.py | 179 |
2 files changed, 204 insertions, 79 deletions
diff --git a/tools/script_disassembler/incbin_parser.py b/tools/script_disassembler/incbin_parser.py index e0850f13..5d184b28 100644 --- a/tools/script_disassembler/incbin_parser.py +++ b/tools/script_disassembler/incbin_parser.py @@ -4,49 +4,73 @@ TMC_FOLDER='../..' import subprocess import sys -from script_disassembler import disassemble_script +from script_disassembler import disassemble_script, generate_macros -def main(): +ROM_OFFSET=0x08000000 +SCRIPTS_START=0x08008B5C +SCRIPTS_END=0x08016984 +# cat data/scripts.s | grep @ | cut -d " " -f 3 | sed -e 's/^/0x/' | sed -e 's/\\n/, /g' > labels.txt + +def read_baserom(): # read baserom data with open(f'{TMC_FOLDER}/baserom.gba', 'rb') as baserom: - baserom_data = bytearray(baserom.read()) - - # read scripts.s with incbins - with open(f'{TMC_FOLDER}/data/scripts.s', 'r') as scripts: - - while True: - line = scripts.readline() - - if not line: - # end of file - break - - if ':: @' in line: # this might be a label - incbin_line = scripts.readline() - - if '.incbin' in incbin_line: # found a label with incbin - label = line.split('::')[0] - (_, start, end) = incbin_line.split(',') - start = int(start, 16) - end = int(end, 16) - - # read data from rom - data = baserom_data[start:start+end] - - stdout = sys.stdout - with open(f'{TMC_FOLDER}/data/scripts/{label}.inc','w') as out: - sys.stdout = out - print(f'SCRIPT_START {label}') - disassemble_script(data) - sys.stdout = stdout - - # print new include label - print(f'.include "data/scripts/{label}.inc"') - else: - print(line, end='') - print(incbin_line, end='') - else: - print(line, end='') + return bytearray(baserom.read()) + +def get_label(addr): + return hex(addr).upper().replace('0X', 'gUnk_0') + +def main(): + baserom_data = read_baserom() + + script_start = SCRIPTS_START-ROM_OFFSET + + scripts = ''' .include "asm/macros.inc" + .include "constants/constants.inc" + + .include "asm/macros/scripts.inc" + + .syntax unified + + .text + + ''' + + while script_start < SCRIPTS_END-ROM_OFFSET: + label = get_label(script_start+ROM_OFFSET) + print(f"Disassembling \033[1;34m{label}\033[0m ({script_start} / { SCRIPTS_END-ROM_OFFSET} bytes converted)...") + # find end of the script signified by 0xffff0000 + script_end = baserom_data.index(b'\xff\xff\x00\x00', script_start) + 4 + + if script_end > SCRIPTS_END-ROM_OFFSET: + script_end = SCRIPTS_END-ROM_OFFSET + + script_start+ROM_OFFSET + # read data from rom + data = baserom_data[script_start:script_end] + + + scripts += f' .include "data/scripts/{label}.inc"\n' + stdout = sys.stdout + with open(f'{TMC_FOLDER}/data/scripts/{label}.inc','w') as out: + sys.stdout = out + print(f'SCRIPT_START {label}') + res = disassemble_script(data) + if res != 0: + # Script ended in the middle, need to create a new file + script_end = script_start + res + sys.stdout = stdout + + script_start = script_end + print('Writing scripts.s file...') + with open(f'{TMC_FOLDER}/data/scripts.s', 'w') as out: + out.write(scripts) + print('Generating asm macros...') + stdout = sys.stdout + with open(f'{TMC_FOLDER}/asm/macros/scripts.inc', 'w') as out: + sys.stdout = out + generate_macros() + sys.stdout = stdout + print('\033[1;92mDone\033[0m\n') if __name__ == '__main__': main()
\ No newline at end of file diff --git a/tools/script_disassembler/script_disassembler.py b/tools/script_disassembler/script_disassembler.py index 330d2a7d..ae8f3272 100644 --- a/tools/script_disassembler/script_disassembler.py +++ b/tools/script_disassembler/script_disassembler.py @@ -5,7 +5,7 @@ import struct # Input 'macros' to generate the macros for the script commands # Input the script bytes as hex to disassemble the script -# Build macros: echo "macros" | python script_disassembler.py > ~/git/tmc/github/asm/macros/script.inc +# Build macros: echo "macros" | python script_disassembler.py > ~/git/tmc/github/asm/macros/scripts.inc @dataclass class Context: @@ -33,25 +33,29 @@ def get_pointer(barray): integers = struct.unpack('I', barray) return 'sub_' + (struct.pack('>I', integers[0]-1).hex()).upper() +def get_data_pointer(barray): + integers = struct.unpack('I', barray) + return 'gUnk_' + (struct.pack('>I', integers[0]-1).hex()).upper() + commands = [ - {'fun': 'ScriptCommandNop', 'params': ''}, + {'fun': 'ScriptCommandNop', 'params': 'v'}, # TODO one version with length 33??? {'fun': 'ScriptCommand_StartScript', 'params': '', 'name': 'start executing scripts'}, {'fun': 'ScriptCommand_StopScript', 'params': '', 'name': 'stop executing scripts'}, {'fun': 'ScriptCommand_Jump', 'params': 's', 'name': 'jump by offset'}, {'fun': 'ScriptCommand_JumpIf', 'params': 's', 'name': 'jump if'}, {'fun': 'ScriptCommand_JumpIfNot', 'params': 's', 'name': 'jump if not'}, - {'fun': 'ScriptCommand_0807E078', 'params': 's'}, - {'fun': 'ScriptCommand_JumpAbsolute', 'params': 'w','name': 'abs jump' }, - {'fun': 'ScriptCommand_JumpAbsoluteIf', 'params': 'w', 'name': 'abs jump if'}, - {'fun': 'ScriptCommand_JumpAbsoluteIfNot', 'params': 'w', 'name': 'abs jump if not'}, - {'fun': 'ScriptCommand_0807E0E0', 'params': 'w'}, + {'fun': 'ScriptCommand_0807E078', 'params': 'v'}, + {'fun': 'ScriptCommand_JumpAbsolute', 'params': 'd','name': 'abs jump' }, + {'fun': 'ScriptCommand_JumpAbsoluteIf', 'params': 'd', 'name': 'abs jump if'}, + {'fun': 'ScriptCommand_JumpAbsoluteIfNot', 'params': 'd', 'name': 'abs jump if not'}, + {'fun': 'ScriptCommand_0807E0E0', 'params': 'dd'}, {'fun': 'ScriptCommand_Call', 'params':'p', 'name': 'Execute function via pointer'},# 'exec': ScriptCommand_Call}, - {'fun': 'ScriptCommand_CallWithArg', 'params': 'pw'}, - {'fun': 'ScriptCommand_LoadRoomEntityList', 'params': 'w'}, + {'fun': 'ScriptCommand_CallWithArg', 'params': 'pv'}, + {'fun': 'ScriptCommand_LoadRoomEntityList', 'params': 'd'}, {'fun': 'ScriptCommand_TestBit', 'params': 'w'}, {'fun': 'ScriptCommand_CheckInventory1', 'params': 's'}, {'fun': 'ScriptCommand_CheckInventory2', 'params': 's'}, - {'fun': 'ScriptCommand_HasRoomItemForSale', 'params': 's'}, + {'fun': 'ScriptCommand_HasRoomItemForSale', 'params': ''}, {'fun': 'ScriptCommand_CheckLocalFlag', 'params': 's'}, {'fun': 'ScriptCommand_CheckLocalFlagByOffset', 'params': 'ss'}, {'fun': 'ScriptCommand_CheckGlobalFlag', 'params': 's'}, @@ -68,7 +72,7 @@ commands = [ {'fun': 'ScriptCommand_0807E48C', 'params': 's'}, {'fun': 'ScriptCommand_0807E4CC', 'params': 'w'}, {'fun': 'ScriptCommand_0807E4EC', 'params': 'w'}, - {'fun': 'ScriptCommand_0807E51', 'params': 's'}, + {'fun': 'ScriptCommand_0807E514', 'params': 'w'}, {'fun': 'ScriptCommand_CheckPlayerFlags', 'params':'w'}, {'fun': 'ScriptCommand_0807E564', 'params': ''}, {'fun': 'ScriptCommand_EntityHasHeight', 'params': ''}, @@ -100,12 +104,12 @@ commands = [ {'fun': 'ScriptCommand_DoFade7', 'params': ''}, {'fun': 'ScriptCommand_0807E800', 'params': 'w'}, {'fun': 'ScriptCommand_0807E80C', 'params': 'w'}, - {'fun': 'ScriptCommand_0807E858', 'params': 's'}, + {'fun': 'ScriptCommand_0807E858', 'params': 'v'}, # TODO why are there both 0 and 1? {'fun': 'ScriptCommand_0807E864', 'params': ''}, {'fun': 'ScriptCommand_0807E878', 'params': ''}, {'fun': 'ScriptCommand_0807E888', 'params': ''}, {'fun': 'ScriptCommand_SetPlayerAction', 'params': 'w'}, - {'fun': 'ScriptCommand_StartPlayerScript', 'params': 'w'}, + {'fun': 'ScriptCommand_StartPlayerScript', 'params': 'd'}, {'fun': 'ScriptCommand_0807E8D4', 'params': 's'}, {'fun': 'ScriptCommand_0807E8E4_0', 'params': ''}, # duplicate {'fun': 'ScriptCommand_0807E8E4_1', 'params': ''}, # duplicate @@ -126,8 +130,8 @@ commands = [ {'fun': 'ScriptCommand_0807EA94', 'params': ''}, {'fun': 'ScriptCommand_TextboxNoOverlapFollow', 'params': 's'}, {'fun': 'ScriptCommand_TextboxNoOverlap', 'params': 's'}, - {'fun': 'ScriptCommand_TextboxNoOverlapFollowPos', 'params': 'ss'}, - {'fun': 'ScriptCommand_0807EAF0', 'params': 'w'}, + {'fun': 'ScriptCommand_TextboxNoOverlapFollowPos', 'params': 'v'}, # TODO 1 or two + {'fun': 'ScriptCommand_0807EAF0', 'params': 'v'}, # TODO {'fun': 'ScriptCommand_TextboxNoOverlapVar', 'params': ''}, {'fun': 'ScriptCommand_0807EB28', 'params': 's'}, {'fun': 'ScriptCommand_0807EB38', 'params': ''}, @@ -219,12 +223,30 @@ parameters = { 'expr': ' .word \w', 'read': lambda ctx: get_pointer(ctx.data[ctx.ptr+2:ctx.ptr+6]) }, - 'pw': { + 'd': { # Data pointer + 'length': 2, + 'param': 'w', + 'expr': ' .word \w', + 'read': lambda ctx: get_data_pointer(ctx.data[ctx.ptr+2:ctx.ptr+6]) + }, + 'pv': { + 'length': -2, + 'param': 'w', + 'expr': ' .word \w', + 'read': lambda ctx: '' + }, + 'dd': { 'length': 4, - 'param': 'a,b', + 'param': 'a, b', 'expr': ' .word \\a\n .word \\b', - 'read': lambda ctx: get_pointer(ctx.data[ctx.ptr+2:ctx.ptr+6]) + ', ' + barray_to_u32_hex(ctx.data[ctx.ptr+6:ctx.ptr+10])[0] + 'read': lambda ctx: get_data_pointer(ctx.data[ctx.ptr+2:ctx.ptr+6]) + ', ' + get_data_pointer(ctx.data[ctx.ptr+6:ctx.ptr+10]) }, + 'v': { # variable parameter count (TODO why?) + 'length': -1, + 'param': '', + 'expr': '', + 'read': lambda ctx: '' + } } # Remove the ScriptCommand_ prefix for the asm macros @@ -234,31 +256,50 @@ def build_script_command(name: str): return '_' + name return name +def print_rest_bytes(ctx): + print('\n'.join(['.byte ' + hex(x) for x in ctx.data[ctx.ptr:]])) + def ExecuteScriptCommandSet(ctx: Context): + # print(f'@{ctx.ptr}') print offsets to debug when manually inserting labels cmd = struct.unpack('H', ctx.data[ctx.ptr:ctx.ptr+2])[0] if cmd == 0: - return 0 + # this does not need to be the end of the script + print('.short 0x0000') + ctx.ptr += 2 + return 1 + if cmd == 0xffff: ctx.ptr += 2 print('SCRIPT_END') - return 0 - - unk_06 = cmd >> 0xA - if unk_06 == 0: + cmd = struct.unpack('H', ctx.data[ctx.ptr:ctx.ptr+2])[0] + if cmd == 0x0000: + # This is actually the end of the script + print('.short 0x0000') + ctx.ptr += 2 + return 2 + return 3 # There is a SCRIPT_END without 0x0000 afterwards, but still split into a new file, please + + commandSize = cmd >> 0xA + if commandSize == 0: + raise Exception(f'Zero commandSize') # TODO error return 0 - operationId = cmd & 0x3FF - if operationId >= len(commands): + commandId = cmd & 0x3FF + if commandId >= len(commands): + #print_rest_bytes(ctx) + print(f'.short {u16_to_hex(cmd)}') + ctx.ptr += 2 + #raise Exception(f'Invalid commandId {commandId} / {len(commands)} {cmd}') # TODO error - return 0 - command = commands[operationId] - param_length = unk_06 - 1 - if unk_06 > 1: - if ctx.ptr+2*unk_06 >= len(ctx.data): - # TODO raise Exception(f'Not enough data to fetch {unk_06-1} params') + return 1 + command = commands[commandId] + param_length = commandSize - 1 + if commandSize > 1: + if ctx.ptr+2*commandSize > len(ctx.data): + raise Exception(f'Not enough data to fetch {commandSize-1} params') return 0 - meta = struct.unpack( - 'H'*(unk_06-1), ctx.data[ctx.ptr+2:ctx.ptr+2*unk_06]) + #meta = struct.unpack( + # 'H'*(unk_06-1), ctx.data[ctx.ptr+2:ctx.ptr+2*unk_06]) #print('meta', meta) # Handle parameters @@ -266,9 +307,45 @@ def ExecuteScriptCommandSet(ctx: Context): raise Exception('Parameters not defined for ' + command['fun'] + ' Should be of length ' + str(param_length)) if not command['params'] in parameters: raise Exception('Parameter configuration ' + command['params'] + ' not defined') + + # TODO REMOVE fix pointers + if command['params'] == 'p': + command['params'] = 'w' + elif command['params'] == 'd': + command['params'] = 'w' + elif command['params'] == 'pv': + command['params'] = 'v' + elif command['params'] == 'dd': + command['params'] = 'ww' + params = parameters[command['params']] - if unk_06-1 != params['length']: - # raise Exception(f'Call {command["fun"]} with ' + str(unk_06-1) +' length, while length of ' + str(params['length'])+' defined') + + + + + # TODO REMOVE + if commandSize == 34: + print('@TODO FIX THIS COMMAND!') + commandSize = 13 + + + if params['length'] == -1: # variable parameter length + print(build_script_command(command['fun'])) + if commandSize > 1: + print('\n'.join(['.short ' + x for x in barray_to_u16_hex(ctx.data[ctx.ptr+2:ctx.ptr+commandSize*2])])) + print(f'@ End of {commandSize-1} parameters') + ctx.ptr += commandSize*2 + return 1 + elif params['length'] == -2: # point and var + print(build_script_command(command['fun']) + ' '+ get_pointer(ctx.data[ctx.ptr+2:ctx.ptr+6])) + if commandSize > 3: + print('\n'.join(['.short ' + x for x in barray_to_u16_hex(ctx.data[ctx.ptr+6:ctx.ptr+commandSize*2])])) + print(f'% End of {commandSize-3} parameters') + ctx.ptr += commandSize*2 + return 1 + + if commandSize-1 != params['length']: + raise Exception(f'Call {command["fun"]} with ' + str(commandSize-1) +' length, while length of ' + str(params['length'])+' defined') return 0 #with open('log.txt', 'a') as log: # log.write(f'Call {command["fun"]} with ' + str(unk_06-1) +' length, while length of ' + str(params['length'])+' defined\n') @@ -279,29 +356,53 @@ def ExecuteScriptCommandSet(ctx: Context): print(build_script_command(command['fun']) + ' ' + params['read'](ctx)) # Execute script - ctx.ptr += unk_06*2 + ctx.ptr += commandSize*2 return 1 +# TODO +# JumpAbsolute 0x08016384 +# JumpAbsoluteIf 0x08016384 +# JumpAbsoluteIfNot 0x08016384 + def disassemble_script(input_bytes): ctx = Context(0, input_bytes) + + foundEnd = False while True: if ctx.ptr >= len(ctx.data) - 1: # End of file (there need to be at least two bytes remaining for the next operation id) break - if not ExecuteScriptCommandSet(ctx): + res = ExecuteScriptCommandSet(ctx) + if res == 0: break + elif res == 2: + foundEnd = True + break + elif res == 3: + # End in the middle of the script, please create a new file + return ctx.ptr + + # Print rest (did not manage to get there) if ctx.ptr < len(ctx.data): if (len(ctx.data) - ctx.ptr) % 2 != 0: - print('\n'.join(['.byte ' + hex(x) for x in ctx.data[ctx.ptr:]])) + print_rest_bytes(ctx) # TODO error + raise Exception('DONT WANT EXTRA after '+ str(ctx.ptr) + ' / ' + str(len(ctx.data))) return print('\n'.join(['.short ' + x for x in barray_to_u16_hex(ctx.data[ctx.ptr:])])) - + raise Exception('DONT WANT EXTRA after '+ str(ctx.ptr) + ' / ' + str(len(ctx.data))) + + if not foundEnd: + # Seems to happen, sadly + return 0 + #print('\033[93mNo end found\033[0m') + #raise Exception('No end found') + return 0 def generate_macros(): print('@ All the macro functions for scripts') |
