Super Mario 64 OpenGL port for PC. Mirror of https://github.com/sm64pc/sm64pc https://github.com/sm64pc/sm64pc
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

203 lines
6.4 KiB

  1. #!/usr/bin/env python3
  2. import sys
  3. import os.path
  4. lang = None
  5. # TODO: -S argument for shifted ROMs
  6. args = []
  7. for arg in sys.argv[1:]:
  8. if arg == '-j':
  9. lang = 'jp'
  10. elif arg == '-u':
  11. lang = 'us'
  12. elif arg == '-e':
  13. lang = 'eu'
  14. elif arg == '-s':
  15. lang = 'sh'
  16. else:
  17. args.append(arg)
  18. if lang is None:
  19. lang = 'us'
  20. best = 0
  21. for path in ['build/us/sm64.us.z64', 'build/jp/sm64.jp.z64', 'build/eu/sm64.eu.z64', 'build/sh/sm64.sh.z64']:
  22. try:
  23. mtime = os.path.getmtime(path)
  24. if mtime > best:
  25. best = mtime
  26. lang = path.split('/')[1]
  27. except Exception:
  28. pass
  29. print("Assuming language " + lang)
  30. baseimg = 'baserom.' + lang + '.z64'
  31. basemap = 'sm64.' + lang + '.map'
  32. myimg = 'build/' + lang + '/sm64.' + lang + '.z64'
  33. mymap = 'build/' + lang + '/sm64.' + lang + '.map'
  34. if os.path.isfile('expected/' + mymap):
  35. basemap = 'expected/' + mymap
  36. required_files = [baseimg, myimg, mymap]
  37. if any(not os.path.isfile(path) for path in required_files):
  38. print(', '.join(required_files[:-1]) + " and " + required_files[-1] + " must exist.")
  39. exit(1)
  40. mybin = open(myimg, 'rb').read()
  41. basebin = open(baseimg, 'rb').read()
  42. if len(mybin) != len(basebin):
  43. print("Modified ROM has different size...")
  44. exit(1)
  45. if mybin == basebin:
  46. print("No differences!")
  47. exit(0)
  48. def search_map(rom_addr):
  49. ram_offset = None
  50. last_ram = 0
  51. last_rom = 0
  52. last_fn = '<start of rom>'
  53. last_file = '<no file>'
  54. prev_line = ''
  55. with open(mymap) as f:
  56. for line in f:
  57. if 'load address' in line:
  58. # Example: ".boot 0x0000000004000000 0x1000 load address 0x0000000000000000"
  59. if 'noload' in line or 'noload' in prev_line:
  60. ram_offset = None
  61. continue
  62. ram = int(line[16:16+18], 0)
  63. rom = int(line[59:59+18], 0)
  64. ram_offset = ram - rom
  65. continue
  66. prev_line = line
  67. if ram_offset is None or '=' in line or '*fill*' in line or ' 0x' not in line:
  68. continue
  69. ram = int(line[16:16+18], 0)
  70. rom = ram - ram_offset
  71. fn = line.split()[-1]
  72. if '0x' in fn:
  73. ram_offset = None
  74. continue
  75. if rom > rom_addr or (rom_addr & 0x80000000 and ram > rom_addr):
  76. return 'in {} (ram 0x{:08x}, rom 0x{:x}, {})'.format(last_fn, last_ram, last_rom, last_file)
  77. last_ram = ram
  78. last_rom = rom
  79. last_fn = fn
  80. if '/' in fn:
  81. last_file = fn
  82. return 'at end of rom?'
  83. def parse_map(fname):
  84. ram_offset = None
  85. cur_file = '<no file>'
  86. syms = {}
  87. prev_sym = None
  88. prev_line = ''
  89. with open(fname) as f:
  90. for line in f:
  91. if 'load address' in line:
  92. if 'noload' in line or 'noload' in prev_line:
  93. ram_offset = None
  94. continue
  95. ram = int(line[16:16+18], 0)
  96. rom = int(line[59:59+18], 0)
  97. ram_offset = ram - rom
  98. continue
  99. prev_line = line
  100. if ram_offset is None or '=' in line or '*fill*' in line or ' 0x' not in line:
  101. continue
  102. ram = int(line[16:16+18], 0)
  103. rom = ram - ram_offset
  104. fn = line.split()[-1]
  105. if '0x' in fn:
  106. ram_offset = None
  107. elif '/' in fn:
  108. cur_file = fn
  109. else:
  110. syms[fn] = (rom, cur_file, prev_sym, ram)
  111. prev_sym = fn
  112. return syms
  113. def map_diff():
  114. map1 = parse_map(mymap)
  115. map2 = parse_map(basemap)
  116. min_ram = None
  117. found = None
  118. for sym, addr in map1.items():
  119. if sym not in map2:
  120. continue
  121. if addr[0] != map2[sym][0]:
  122. if min_ram is None or addr[0] < min_ram:
  123. min_ram = addr[0]
  124. found = (sym, addr[1], addr[2])
  125. if min_ram is None:
  126. return False
  127. else:
  128. print()
  129. print("Map appears to have shifted just before {} ({}) -- in {}?".format(found[0], found[1], found[2]))
  130. if found[2] is not None and found[2] not in map2:
  131. print()
  132. print("(Base map file {} out of date due to renamed symbols, so result may be imprecise.)".format(basemap))
  133. return True
  134. def hexbytes(bs):
  135. return ":".join("{:02x}".format(c) for c in bs)
  136. # For convenience, allow `./first-diff.py <ROM addr | RAM addr | function name>`
  137. # to do a symbol <-> address lookup. This should really be split out into a
  138. # separate script...
  139. if args:
  140. try:
  141. addr = int(args[0], 0)
  142. print(args[0], "is", search_map(addr))
  143. except ValueError:
  144. m = parse_map(mymap)
  145. try:
  146. print(args[0], "is at position", hex(m[args[0]][0]), "in ROM,", hex(m[args[0]][3]), "in RAM")
  147. except KeyError:
  148. print("function", args[0], "not found")
  149. exit()
  150. found_instr_diff = None
  151. diffs = 0
  152. shift_cap = 1000
  153. for i in range(24, len(mybin), 4):
  154. # (mybin[i:i+4] != basebin[i:i+4], but that's slightly slower in CPython...)
  155. if diffs <= shift_cap and (mybin[i] != basebin[i] or mybin[i+1] != basebin[i+1] or mybin[i+2] != basebin[i+2] or mybin[i+3] != basebin[i+3]):
  156. if diffs == 0:
  157. print("First difference at ROM addr " + hex(i) + ", " + search_map(i))
  158. print("Bytes:", hexbytes(mybin[i:i+4]), 'vs', hexbytes(basebin[i:i+4]))
  159. diffs += 1
  160. if found_instr_diff is None and mybin[i] >> 2 != basebin[i] >> 2:
  161. found_instr_diff = i
  162. if diffs == 0:
  163. print("No differences!")
  164. exit()
  165. definite_shift = (diffs > shift_cap)
  166. if not definite_shift:
  167. print(str(diffs) + " differing word(s).")
  168. if diffs > 100:
  169. if found_instr_diff is not None:
  170. i = found_instr_diff
  171. print("First instruction difference at ROM addr " + hex(i) + ", " + search_map(i))
  172. print("Bytes:", hexbytes(mybin[i:i+4]), 'vs', hexbytes(basebin[i:i+4]))
  173. if lang == 'sh':
  174. print("Shifted ROM, as expected.")
  175. else:
  176. if not os.path.isfile(basemap):
  177. if definite_shift:
  178. print("Tons of differences, must be a shifted ROM.")
  179. print("To find ROM shifts, copy a clean .map file to " + basemap + " and rerun this script.")
  180. exit()
  181. if not map_diff():
  182. print("No ROM shift{}.".format(" (!?)" if definite_shift else ""))