-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin2coe.py
64 lines (45 loc) · 1.85 KB
/
bin2coe.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
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import sys
import os
import click
import coe
import numpy as np
def error(msg):
print(f'Error: {msg}')
exit(1)
@click.command()
@click.argument('input', type=click.Path(exists=True))
@click.argument('output', type=click.Path())
@click.option('-f', '--force', is_flag=True, help='allow overriding of output file')
@click.option('-w', '--word-width', type=click.INT, default=32, help='bit width of each word in each file, -1 for one pixel per word')
@click.option('-r', '--dump-radix', type=click.Choice(['HEX', 'BIN']), default='HEX', help='radix to use when dumping data')
def process(input, output, force, word_width, dump_radix):
if word_width % 8 != 0:
error('word width must be a multiple of 8')
word_length = word_width // 8
# print parameters
print('======Parameters======')
print(f'Input file: {input}')
print(f'Output file: {output}')
print(f'Word width: {word_width} bits ({word_length} bytes)')
print('=======Output=========')
if os.path.exists(output) and not force:
error('output file existed, use --force to overwrite')
mem = np.fromfile(input, dtype=np.uint8)
mem_size = mem.shape[0] # in bytes
print(f'Input file size: {mem_size} bytes')
if mem_size % word_length != 0:
pad_bytes = word_length - mem_size % word_length
print(f'Padding bytes: {pad_bytes}')
mem = np.append(mem, np.zeros(shape=(pad_bytes,), dtype=np.uint8))
mem_size = mem.shape[0]
word_count = mem_size // word_length
print(f'Memory size: {mem_size} bytes')
print(f'Depth (word count): {word_count}')
# reshape to (address, word)
mem = mem.reshape((word_count, word_length))
with open(output, 'w') as f:
coe.dump(mem, f, packed=True, data_radix=dump_radix)
print('Dump succeeded!')
if __name__ == '__main__':
process()