File: bin_to_coe.py

package info (click to toggle)
uhd 4.8.0.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 183,172 kB
  • sloc: cpp: 279,415; python: 109,850; ansic: 103,348; vhdl: 57,230; tcl: 20,007; xml: 8,581; makefile: 2,863; sh: 2,797; pascal: 230; javascript: 120; csh: 94; asm: 20; perl: 11
file content (27 lines) | stat: -rwxr-xr-x 787 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3

import binascii
import sys

if len(sys.argv) < 2:
    print("Usage: bin_to_coe.py <bin file> <coe file>")
    sys.exit(1)

bin_file = sys.argv[1]
coe_file = sys.argv[2]

# Read the binary file in binary mode
with open(bin_file, 'rb') as f:
    binary_data = f.read()
    padding_length = (8 - (len(binary_data) * 2) % 8) % 8  # Calculate necessary padding
    h = binascii.hexlify(binary_data) + b'0' * padding_length

# Convert binary to hexadecimal and format for COE file
coe_str = 'memory_initialization_radix=16;\n'
coe_str += 'memory_initialization_vector=\n'
coe_str += ',\n'.join(h[i:i+8].decode('ascii') for i in range(0, len(h), 8))
coe_str += ';'

# Write the COE formatted string to the output file
with open(coe_file, 'w') as f:
    f.write(coe_str)