|
6 | 6 |
|
7 | 7 | import sys |
8 | 8 |
|
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 |
13 | 16 |
|
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 |
16 | 21 |
|
17 | | -def bs_read_u16(inp): |
18 | | - return bs_read_n(inp, 2) |
| 22 | + def read_u8(self): |
| 23 | + return self.read_int(1) |
19 | 24 |
|
20 | | -def bs_read_u32(inp): |
21 | | - return bs_read_n(inp, 4) |
| 25 | + def read_u16(self): |
| 26 | + return self.read_int(2) |
22 | 27 |
|
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) |
27 | 30 |
|
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() |
30 | 37 | if section_name != ord(section_expected): |
31 | 38 | raise ValueError(f'Bitstream section {section_expected} missing') |
32 | 39 |
|
33 | | - section_info, inp = bs_read_str(inp) |
| 40 | + section_info = bs.read_str() |
34 | 41 | section_info = section_info.decode('utf-8') |
35 | 42 | print(f'{title}: {section_info}') |
36 | | - return inp |
37 | 43 |
|
38 | 44 | def parse_bitfile(inp): |
39 | 45 | 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() |
42 | 49 | if len(prologue) != 9 or one != 1: |
43 | 50 | raise ValueError('Bitstream header invalid') |
44 | 51 |
|
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') |
49 | 56 |
|
50 | | - section_e, inp = bs_read_u8(inp) |
| 57 | + section_e = bs.read_u8() |
51 | 58 | if section_e != ord('e'): |
52 | 59 | raise ValueError('Bitstream section e missing') |
53 | 60 |
|
54 | | - payload_size, inp = bs_read_u32(inp) |
| 61 | + payload_size = bs.read_u32() |
55 | 62 | print(f' Image size: 0x{payload_size:08x} ({int(payload_size/1024)}KiB)\n') |
56 | 63 |
|
57 | | - return inp[:payload_size] |
| 64 | + return bs.read_n(payload_size) |
0 commit comments