2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-25 11:18:27 +08:00

bit2bin: cleanup

This commit is contained in:
Robert Jördens 2017-11-09 12:52:36 +01:00
parent 76ddb063cf
commit 4880e4225d

View File

@ -17,38 +17,39 @@ def flip32(data):
def bit2bin(bit, bin, flip=False): def bit2bin(bit, bin, flip=False):
with open(bit, "rb") as bitfile: l, = struct.unpack(">H", bit.read(2))
l, = struct.unpack(">H", bitfile.read(2)) if l != 9:
if l != 9: raise ValueError("Missing <0009> header, not a bit file")
raise ValueError("Missing <0009> header, not a bit file") _ = bit.read(l) # unknown data
l, = struct.unpack(">H", bit.read(2))
if l != 1:
raise ValueError("Missing <0001> header, not a bit file")
bitfile.read(l) while True:
d = bitfile.read(*struct.unpack(">H", bitfile.read(2))) key = bit.read(1).decode()
if d != b"a": if not key:
raise ValueError("Missing <a> header, not a bit file") break
if key in "abcd":
d = bitfile.read(*struct.unpack(">H", bitfile.read(2))) d = bit.read(*struct.unpack(">H", bit.read(2)))
print("Design name:", d) assert d.endswith(b"\x00")
d = d.decode()
while True: name = {
key = bitfile.read(1) "a": "Design",
if not key: "b": "Part name",
break "c": "Date",
if key in b"bcd": "d": "Time"
d = bitfile.read(*struct.unpack(">H", bitfile.read(2))) }[key]
name = {b"b": "Partname", b"c": "Date", b"d": "Time"}[key] print("{}: {}".format(name, d))
print(name, d) elif key == "e":
elif key == b"e": l, = struct.unpack(">I", bit.read(4))
l, = struct.unpack(">I", bitfile.read(4)) print("Bitstream payload length: {:#x}".format(l))
print("found binary data length:", l) d = bit.read(l)
d = bitfile.read(l) if flip:
if flip: d = flip32(d)
d = flip32(d) bin.write(d)
with open(bin, "wb") as f: else:
f.write(d) d = bit.read(*struct.unpack(">H", bit.read(2)))
else: print("Unexpected key: {}: {}".format(key, d))
d = bitfile.read(*struct.unpack(">H", bitfile.read(2)))
print("Unexpected key: ", key, d)
if __name__ == "__main__": if __name__ == "__main__":
@ -64,4 +65,5 @@ if __name__ == "__main__":
help="Output bin file name") help="Output bin file name")
args = parser.parse_args() args = parser.parse_args()
bit2bin(args.bitfile, args.binfile, args.flip) with open(args.bitfile, "rb") as f, open(args.binfile, "wb") as g:
bit2bin(f, g, args.flip)