Reference pointers directly

This commit is contained in:
octorock
2021-03-06 00:24:24 +01:00
parent 9441dac477
commit b65f5e1bd0
359 changed files with 2774 additions and 3074 deletions
+21 -12
View File
@@ -12,7 +12,10 @@ SCRIPTS_END=0x08016984
# Create labels for these additional script instructions
# Currently done by splitting the script at that point
LABEL_BREAKS=[ 0x0800B41C, 0x08012F0C, 0x080142B0, 0x08014A80]
LABEL_BREAKS=[0x0800A088, 0x0800ACE0, 0x0800AD54, 0x0800B41C, 0x0800B7C4, 0x0800C8C8, 0x0800D190, 0x800D3EC, 0x0800E9F4, 0x0800FD80, 0x08012AC8, 0x08012F0C, 0x080130E4, 0x08013B70, 0x080142B0, 0x080147DC, 0x08014A80, 0x08014B10,0x0801635C, 0x08016384, 0x080165D8]
# Generate a version of the script that is annotated with the byte offset to the beginning of the script
GENERATE_REF=False
def read_baserom():
# read baserom data
@@ -61,25 +64,31 @@ def main():
scripts += f' .include "data/scripts/{label}.inc"\n'
stdout = sys.stdout
with open(f'{TMC_FOLDER}/data/scripts/{label}.inc','w') as out:
with open(f'{TMC_FOLDER}/data/scripts/{label}.ref' if GENERATE_REF else f'{TMC_FOLDER}/data/scripts/{label}.inc','w') as out:
sys.stdout = out
print(f'SCRIPT_START {label}')
res = disassemble_script(data)
if GENERATE_REF:
res = disassemble_script(data, True)
else:
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
if not GENERATE_REF:
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__':
@@ -28,14 +28,29 @@ def barray_to_u32_hex(barray):
integers = struct.unpack('I'*count, barray)
return [u32_to_hex(x) for x in integers]
# Functions that have already been renamed
POINTER_MAP = {
'sub_08095458': 'nullsub_527',
'sub_0805EBCC': 'DeleteAllEnemies',
'sub_0806C23C': 'Simon_CreateChest',
'sub_0801637C': '0x0801637D', # TODO disassembly assembly code between scripts
'sub_08016383': '0x08016384', # TODO points to the end of the previous function?
'sub_0806C598': 'FUN_0806c598',
'sub_080A2138': 'Windcrest_Unlock',
'sub_080A29BC': 'CreateDust'
}
# tries to directly reference the function this is pointing to
def get_pointer(barray):
integers = struct.unpack('I', barray)
return 'sub_' + (struct.pack('>I', integers[0]-1).hex()).upper()
pointer = 'sub_' + (struct.pack('>I', integers[0]-1).hex()).upper()
if pointer in POINTER_MAP:
return POINTER_MAP[pointer]
return pointer
def get_data_pointer(barray):
integers = struct.unpack('I', barray)
return 'gUnk_' + (struct.pack('>I', integers[0]-1).hex()).upper()
return 'gUnk_' + (struct.pack('>I', integers[0]).hex()).upper()
commands = [
{'fun': 'ScriptCommandNop', 'params': 'v'}, # TODO one version with length 33???
@@ -51,7 +66,7 @@ commands = [
{'fun': 'ScriptCommand_0807E0E0', 'params': 'dd'},
{'fun': 'ScriptCommand_Call', 'params':'p', 'name': 'Execute function via pointer'},# 'exec': ScriptCommand_Call},
{'fun': 'ScriptCommand_CallWithArg', 'params': 'pv'},
{'fun': 'ScriptCommand_LoadRoomEntityList', 'params': 'd'},
{'fun': 'ScriptCommand_LoadRoomEntityList', 'params': 'w'}, # TODO return to d and create labels for them
{'fun': 'ScriptCommand_TestBit', 'params': 'w'},
{'fun': 'ScriptCommand_CheckInventory1', 'params': 's'},
{'fun': 'ScriptCommand_CheckInventory2', 'params': 's'},
@@ -259,8 +274,9 @@ def build_script_command(name: str):
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
def ExecuteScriptCommandSet(ctx: Context, add_annotations=False):
if add_annotations:
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:
# this does not need to be the end of the script
@@ -308,27 +324,10 @@ def ExecuteScriptCommandSet(ctx: Context):
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']]
# TODO REMOVE
if commandSize == 34:
print('@TODO FIX THIS COMMAND!')
commandSize = 13
if params['length'] == -1: # variable parameter length
print(f'.short {u16_to_hex(cmd)} @ {build_script_command(command["fun"])} with {commandSize-1} parameters')
if commandSize > 1:
@@ -337,12 +336,12 @@ def ExecuteScriptCommandSet(ctx: Context):
ctx.ptr += commandSize*2
return 1
elif params['length'] == -2: # point and var
print(f'.short {u16_to_hex(cmd)} @ {build_script_command(command["fun"])} with parameters:')
print(f'.short {u16_to_hex(cmd)} @ {build_script_command(command["fun"])} with {commandSize-3} parameters')
print('.word'+ get_pointer(ctx.data[ctx.ptr+2:ctx.ptr+6]))
print('.word '+ 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')
print(f'@ End of parameters')
ctx.ptr += commandSize*2
return 1
@@ -367,7 +366,7 @@ def ExecuteScriptCommandSet(ctx: Context):
# JumpAbsoluteIf 0x08016384
# JumpAbsoluteIfNot 0x08016384
def disassemble_script(input_bytes):
def disassemble_script(input_bytes, add_annotations=False):
ctx = Context(0, input_bytes)
@@ -376,7 +375,7 @@ def disassemble_script(input_bytes):
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
res = ExecuteScriptCommandSet(ctx)
res = ExecuteScriptCommandSet(ctx, add_annotations)
if res == 0:
break
elif res == 2: