1
0
Fork 0
mirror of https://github.com/notwa/mm synced 2024-11-05 00:29:02 -08:00

knock off TODOs

This commit is contained in:
Connor Olding 2015-03-01 19:52:12 -08:00
parent 6e9a37d8a0
commit 5c33550a0c
2 changed files with 37 additions and 8 deletions

17
n64.py
View file

@ -1,5 +1,9 @@
# Based on uCON64's N64 checksum algorithm by Andreas Sterbenz
from zlib import crc32
MAX32 = 0xFFFFFFFF
crc_seeds = {
6101: 0xF8CA4DDC,
6102: 0xF8CA4DDC,
@ -8,7 +12,13 @@ crc_seeds = {
6106: 0x1FEA617A,
}
MAX32 = 0xFFFFFFFF
bootcode_crcs = {
0x6170A4A1: 6101,
0x90BB6CB5: 6102,
0x0B050EE0: 6103,
0x98BC2C86: 6105,
0xACC8580A: 6106,
}
def ROL(i, b):
return ((i << b) | (i >> (32 - b))) & MAX32
@ -67,3 +77,8 @@ def crc(f, bootcode=6105):
crc1 = t6 ^ t4 ^ t3
crc2 = t5 ^ t2 ^ t1
return crc1 & MAX32, crc2 & MAX32
def bootcode_version(f):
f.seek(0x40)
return bootcode_crcs[crc32(f.read(0x1000 - 0x40)) & MAX32]

View file

@ -3,7 +3,8 @@
import os, os.path
import sys
import struct
import io
import struct, array
import hashlib
import n64
@ -105,15 +106,28 @@ def z_dump(f):
while z_dump_file(f, '{:05} '.format(i)):
i += 1
def swap_order(f, size='H'):
f.seek(0)
a = array.array(size, f.read())
a.byteswap()
f.seek(0)
f.write(a.tobytes())
def dump_rom(fn):
with open(fn, 'rb') as f:
data = f.read()
if data[:4] != b'\x80\x37\x12\x40':
# TODO: check if it's a .n64 (2 byte swap) and convert
with io.BytesIO(data) as f:
start = f.read(4)
if start == b'\x37\x80\x40\x12':
swap_order(f)
elif start != b'\x80\x37\x12\x40':
lament('not a .z64:', fn)
return
f.seek(0)
data = f.read()
outdir = hashlib.sha1(data).hexdigest()
del data
@ -123,8 +137,7 @@ def dump_rom(fn):
def z_read_file(path, fn=None):
if fn == None:
# TODO: infer from path
return False
fn = os.path.basename(path)
if len(fn) < 37:
return False
@ -196,8 +209,9 @@ def create_rom(d):
assert(f.tell() <= (pe or ve))
# fix makerom (n64 header)
# TODO: don't assume bootcode is 6105
crc1, crc2 = n64.crc(f)
bootcode = n64.bootcode_version(f)
lament('bootcode:', bootcode)
crc1, crc2 = n64.crc(f, bootcode)
lament('crcs: {:08X} {:08X}'.format(crc1, crc2))
f.seek(0x10)
f.write(W4(crc1))