|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import rabbitizer |
| 7 | +import mapfile_parser |
| 8 | + |
| 9 | + |
| 10 | +def decodeInstruction(bytesDiff: bytes, mapFile: mapfile_parser.MapFile) -> str: |
| 11 | + word = (bytesDiff[0] << 24) | (bytesDiff[1] << 16) | (bytesDiff[2] << 8) | (bytesDiff[3] << 0) |
| 12 | + instr = rabbitizer.Instruction(word) |
| 13 | + immOverride = None |
| 14 | + |
| 15 | + if instr.isJumpWithAddress(): |
| 16 | + # Instruction is a function call (jal) |
| 17 | + |
| 18 | + # Get the embedded address of the function call |
| 19 | + symAddress = instr.getInstrIndexAsVram() |
| 20 | + |
| 21 | + # Search for the address in the mapfile |
| 22 | + symInfo = mapFile.findSymbolByVramOrVrom(symAddress) |
| 23 | + if symInfo is not None: |
| 24 | + # Use the symbol from the mapfile instead of a raw value |
| 25 | + immOverride = symInfo.symbol.name |
| 26 | + |
| 27 | + return instr.disassemble(immOverride=immOverride, extraLJust=-20) |
| 28 | + |
| 29 | +def firstDiffMain(): |
| 30 | + parser = argparse.ArgumentParser(description="Find the first difference(s) between the built ROM and the base ROM.") |
| 31 | + |
| 32 | + parser.add_argument("-c", "--count", type=int, default=5, help="find up to this many instruction difference(s)") |
| 33 | + parser.add_argument("-a", "--add-colons", action='store_true', help="Add colon between bytes" ) |
| 34 | + |
| 35 | + args = parser.parse_args() |
| 36 | + |
| 37 | + buildFolder = Path("build") |
| 38 | + |
| 39 | + BUILTROM = buildFolder / f"pokestadium.z64" |
| 40 | + BUILTMAP = buildFolder / f"pokestadium.map" |
| 41 | + |
| 42 | + EXPECTEDROM = Path(f"baserom.z64") |
| 43 | + EXPECTEDMAP = "expected" / BUILTMAP |
| 44 | + |
| 45 | + mapfile_parser.frontends.first_diff.doFirstDiff(BUILTMAP, EXPECTEDMAP, BUILTROM, EXPECTEDROM, args.count, mismatchSize=True, addColons=args.add_colons, bytesConverterCallback=decodeInstruction) |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + firstDiffMain() |
0 commit comments