Skip to content

Commit 539ab3a

Browse files
committed
Upgrade bitfile parser to use a reader object
1 parent e19ddd8 commit 539ab3a

1 file changed

Lines changed: 34 additions & 27 deletions

File tree

bin2hpm/bitfile.py

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,59 @@
66

77
import sys
88

9-
def bs_read_n(inp, n):
10-
result, inp = \
11-
int.from_bytes(inp[0:n], byteorder='big'), inp[n:]
12-
return result, inp
9+
class BitfileReader():
10+
def __init__(self, input):
11+
self.inp = input
12+
13+
def read_n(self, n):
14+
val, self.inp = self.inp[:n], self.inp[n:]
15+
return val
1316

14-
def bs_read_u8(inp):
15-
return bs_read_n(inp, 1)
17+
def read_int(self, n):
18+
result, self.inp = \
19+
int.from_bytes(self.inp[0:n], byteorder='big'), self.inp[n:]
20+
return result
1621

17-
def bs_read_u16(inp):
18-
return bs_read_n(inp, 2)
22+
def read_u8(self):
23+
return self.read_int(1)
1924

20-
def bs_read_u32(inp):
21-
return bs_read_n(inp, 4)
25+
def read_u16(self):
26+
return self.read_int(2)
2227

23-
def bs_read_str(inp):
24-
n, inp = bs_read_u16(inp)
25-
val, inp = inp[:n], inp[n:]
26-
return val, inp
28+
def read_u32(self):
29+
return self.read_int(4)
2730

28-
def check_info(inp, section_expected, title):
29-
section_name, inp = bs_read_u8(inp)
31+
def read_str(self):
32+
n = self.read_u16()
33+
return self.read_n(n)
34+
35+
def check_info(bs, section_expected, title):
36+
section_name = bs.read_u8()
3037
if section_name != ord(section_expected):
3138
raise ValueError(f'Bitstream section {section_expected} missing')
3239

33-
section_info, inp = bs_read_str(inp)
40+
section_info = bs.read_str()
3441
section_info = section_info.decode('utf-8')
3542
print(f'{title}: {section_info}')
36-
return inp
3743

3844
def parse_bitfile(inp):
3945
print('Parsing bitfile...')
40-
prologue, inp = bs_read_str(inp)
41-
one, inp = bs_read_u16(inp)
46+
bs = BitfileReader(inp)
47+
prologue = bs.read_str()
48+
one = bs.read_u16()
4249
if len(prologue) != 9 or one != 1:
4350
raise ValueError('Bitstream header invalid')
4451

45-
inp = check_info(inp, 'a', ' Design info')
46-
inp = check_info(inp, 'b', ' Part name')
47-
inp = check_info(inp, 'c', ' File date')
48-
inp = check_info(inp, 'd', ' time')
52+
check_info(bs, 'a', ' Design info')
53+
check_info(bs, 'b', ' Part name')
54+
check_info(bs, 'c', ' File date')
55+
check_info(bs, 'd', ' time')
4956

50-
section_e, inp = bs_read_u8(inp)
57+
section_e = bs.read_u8()
5158
if section_e != ord('e'):
5259
raise ValueError('Bitstream section e missing')
5360

54-
payload_size, inp = bs_read_u32(inp)
61+
payload_size = bs.read_u32()
5562
print(f' Image size: 0x{payload_size:08x} ({int(payload_size/1024)}KiB)\n')
5663

57-
return inp[:payload_size]
64+
return bs.read_n(payload_size)

0 commit comments

Comments
 (0)