"""Compare both movie timestamp patches with Unicorn and their lifted C. Requires Unicorn and a C compiler (--cc or CC). No game files or GPU are used. Generated harness files stay in build/test/movie_timeline. """ import argparse import ctypes import os from pathlib import Path import random import struct import subprocess import sys import unicorn as uc from unicorn import arm64_const as arm ROOT = Path(__file__).resolve().parents[1] sys.path[:0] = [str(ROOT), str(ROOT / 'recomp')] from lifter import Lifter from patches import load from tools.check_content import GENERATED_MARKER def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--cc', default=os.environ.get('CC', 'clang')) args = parser.parse_args() table = load(ROOT / 'recomp/patches-v111.txt', [('main', 0x7000000000, 0x4ea0000)]) sites = [0x3b5c7c4, 0x3b5c908] words = [] for site in sites: alternatives = table.sites['main'][site] assert len(alternatives) == 1 key, replacement, original = alternatives[0] assert table.sets[key] == ('ostep', 'NX2NAVI_OCARINA_REALTIME', 1) assert original == 0xb942fa88 words.append(replacement) assert words[0] == words[1] work = ROOT / 'build/test/movie_timeline' work.mkdir(parents=True, exist_ok=True) code = '// ' + GENERATED_MARKER + '\n#include "cpu.h"\n' export = '__declspec(dllexport)' if os.name == 'nt' else '' for i, sequence in enumerate(words): code += f'{export} void movie_tick_{i}(Cpu *c) {{\n' for j, word in enumerate(sequence): instruction = Lifter(0, simd='c').lift(word, sites[i] + j * 4) assert instruction.kind == 'op' code += instruction.code + '\n' code += '}\n' source = work / 'harness.c' library = work / ('harness.dll' if os.name == 'nt' else 'harness.so') source.write_text(code) subprocess.run([args.cc, '-shared', '-O2', '-fno-strict-aliasing', '-I', str(ROOT / 'runtime'), *([] if os.name == 'nt' else ['-fPIC']), str(source), '-o', str(library)], check=True) dll = ctypes.CDLL(str(library)) # One synthetic layer -> controller -> player -> video surface chain for both engines. arena = ctypes.create_string_buffer(0x4000) address = ctypes.addressof(arena) image = bytearray(0x4000) for at, target in [(0x10, address + 0x1000), (0x1000, address + 0x2000), (0x21e8, address + 0x3000)]: struct.pack_into('