-
Notifications
You must be signed in to change notification settings - Fork 1
/
jedec2hex.py
56 lines (45 loc) · 1.65 KB
/
jedec2hex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from bitarray import bitarray
from bitarray.util import ba2hex
import re
import argparse
FUSES_PATTERN = re.compile(r'\*?L(\d+)\s(\d+)')
# GAL16v8 address space
# https://k1.spdns.de/Develop/Projects/GalAsm/info/galer/jedecfile.html
AND = 0000, 2047 # matrix of fuses (AND-array)
XOR = 2048, 2055 # XOR bits
SIG = 2056, 2119 # signature
AC1 = 2120, 2127 # AC1 bits
PE = 2128, 2191 # product term disable bits
SYN = 2192 # SYN bit
AC0 = 2193 # AC0 bit
parser = argparse.ArgumentParser(description='Converts GAL16V8 JEDEC to hex')
parser.add_argument('-i', dest='in_file', required=True)
parser.add_argument('-o', dest='out_file', required=True)
args = parser.parse_args()
with open(args.in_file, 'r') as f_in, open(args.out_file, 'w+') as f_out:
f_out.write('v2.0 raw\n')
p_addr = 0
data_len = 0
fuses_bin = bitarray()
for line in f_in:
fuses_match = FUSES_PATTERN.match(line)
if fuses_match:
addr, data = fuses_match.groups()
c_addr = int(addr)
d_addr = c_addr - p_addr - data_len
p_addr = c_addr
data_len = len(data)
# empty fuse address
if d_addr > 0:
fuses_bin += d_addr * bitarray('1')
fuses_bin += bitarray(data)
# ignore PE & SIG
del fuses_bin[PE[0]:PE[1]+1]
del fuses_bin[SIG[0]:SIG[1]+1]
# 32 bit padding
padding = 32 - len(fuses_bin) % 32
fuses_bin += padding * bitarray('1')
fuses_hex = ba2hex(fuses_bin)
# write 8 hex per line
for i in range(0, len(fuses_hex), 8):
f_out.write(fuses_hex[i:i+8] + '\n')