Super Mario 64s source code (from a leak on 4chan so be careful)
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.

158 lines
4.9 KiB

6 years ago
  1. #!/usr/bin/env python3
  2. import sys
  3. import os.path
  4. lang = None
  5. for arg in sys.argv:
  6. if arg == '-j':
  7. lang = 'jp'
  8. if arg == '-u':
  9. lang = 'us'
  10. if lang is None:
  11. lang = 'us'
  12. best = 0
  13. for path in ['build/us/sm64.u.z64', 'build/jp/sm64.j.z64']:
  14. try:
  15. if os.path.getmtime(path) > best:
  16. lang = path.split('/')[1]
  17. except Exception:
  18. pass
  19. print("Assuming language " + lang)
  20. baserom = 'baserom' if lang == 'jp' else 'baseromus'
  21. baseimg = baserom + '.z64'
  22. basemap = 'sm64.' + lang[0] + '.map'
  23. myimg = 'build/' + lang + '/sm64.' + lang[0] + '.z64'
  24. mymap = 'build/' + lang + '/sm64.map'
  25. if os.path.isfile('expected/' + mymap):
  26. basemap = 'expected/' + mymap
  27. required_files = [baseimg, myimg, mymap]
  28. if any(not os.path.isfile(path) for path in required_files):
  29. print(', '.join(required_files[:-1]) + " and " + required_files[-1] + " must exist.")
  30. exit(1)
  31. mybin = open(myimg, 'rb').read()
  32. basebin = open(baseimg, 'rb').read()
  33. if len(mybin) != len(basebin):
  34. print("Modified ROM has different size...")
  35. exit(1)
  36. def search_map(rom_addr):
  37. ram_offset = None
  38. last_ram = 0
  39. last_rom = 0
  40. last_fn = '<start of rom>'
  41. last_file = '<no file>'
  42. with open(mymap) as f:
  43. for line in f:
  44. if 'load address' in line:
  45. # Example: ".boot 0x0000000004000000 0x1000 load address 0x0000000000000000"
  46. ram = int(line[16:16+18], 0)
  47. rom = int(line[59:59+18], 0)
  48. ram_offset = ram - rom
  49. continue
  50. if ram_offset is None or '=' in line or '*fill*' in line or ' 0x' not in line:
  51. continue
  52. ram = int(line[16:16+18], 0)
  53. rom = ram - ram_offset
  54. fn = line.split()[-1]
  55. if '0x' in fn:
  56. ram_offset = None
  57. continue
  58. if rom > rom_addr:
  59. return 'in {} (ram 0x{:08x}, rom 0x{:x}, {})'.format(last_fn, last_ram, last_rom, last_file)
  60. last_ram = ram
  61. last_rom = rom
  62. last_fn = fn
  63. if '/' in fn:
  64. last_file = fn
  65. return 'at end of rom?'
  66. def parse_map(fname):
  67. ram_offset = None
  68. cur_file = '<no file>'
  69. syms = {}
  70. prev_sym = None
  71. with open(fname) as f:
  72. for line in f:
  73. if 'load address' in line:
  74. ram = int(line[16:16+18], 0)
  75. rom = int(line[59:59+18], 0)
  76. ram_offset = ram - rom
  77. continue
  78. if ram_offset is None or '=' in line or '*fill*' in line or ' 0x' not in line:
  79. continue
  80. ram = int(line[16:16+18], 0)
  81. rom = ram - ram_offset
  82. fn = line.split()[-1]
  83. if '0x' in fn:
  84. ram_offset = None
  85. elif '/' in fn:
  86. cur_file = fn
  87. else:
  88. syms[fn] = (rom, cur_file, prev_sym)
  89. prev_sym = fn
  90. return syms
  91. def map_diff():
  92. map1 = parse_map(mymap)
  93. map2 = parse_map(basemap)
  94. min_ram = None
  95. found = None
  96. for sym, addr in map1.items():
  97. if sym not in map2:
  98. continue
  99. if addr[0] != map2[sym][0]:
  100. if min_ram is None or addr[0] < min_ram:
  101. min_ram = addr[0]
  102. found = (sym, addr[1], addr[2])
  103. if min_ram is None:
  104. return False
  105. else:
  106. print()
  107. print("Map appears to have shifted just before {} ({}) -- in {}?".format(found[0], found[1], found[2]))
  108. if found[2] is not None and found[2] not in map2:
  109. print()
  110. print("(Base map file {} out of date due to renamed symbols, so result may be imprecise.)".format(basemap))
  111. return True
  112. def hexbytes(bs):
  113. return ":".join("{:02x}".format(c) for c in bs)
  114. found_first = False
  115. diffs = 0
  116. shift_cap = 1000
  117. for i in range(24, len(mybin), 4):
  118. # (mybin[i:i+4] != basebin[i:i+4], but that's slightly slower in CPython...)
  119. if 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]:
  120. if not found_first:
  121. print("First difference at ROM addr " + hex(i) + ", " + search_map(i))
  122. print("Bytes:", hexbytes(mybin[i:i+4]), 'vs', hexbytes(basebin[i:i+4]))
  123. found_first = True
  124. diffs += 1
  125. if diffs > shift_cap:
  126. break
  127. if not found_first:
  128. print("No differences!")
  129. exit()
  130. definite_shift = (diffs > shift_cap)
  131. if not definite_shift:
  132. print(str(diffs) + " differing word(s).")
  133. if diffs > 100:
  134. if not os.path.isfile(basemap):
  135. if definite_shift:
  136. print("Tons of differences, must be a shifted ROM.")
  137. print("To find ROM shifts, copy a clean .map file to " + basemap + " and rerun this script.")
  138. exit()
  139. if not map_diff():
  140. print("No ROM shift{}.".format(" (!?)" if definite_shift else ""))