ls2: add support for the Nexys Video board
[ls2.git] / scripts / bin2hex.py
1 #!/usr/bin/python3
2
3 import sys
4 import subprocess
5 import struct
6
7 if len(sys.argv) == 3:
8 wordlen = int(sys.argv[2]) // 8
9 else:
10 wordlen = 8
11
12 with open(sys.argv[1], "rb") as f:
13 while True:
14 word = f.read(wordlen)
15 if len(word) == 8:
16 print("%016x" % struct.unpack('<Q', word));
17 elif len(word) == 4:
18 if wordlen == 8:
19 print("00000000%08x" % struct.unpack('<I', word));
20 else:
21 print("%08x" % struct.unpack('<I', word));
22 elif len(word) == 0:
23 exit(0);
24 else:
25 raise Exception("Bad length %d" % (len(word)))