mirror of
https://github.com/EttusResearch/uhd.git
synced 2026-10-25 17:36:10 +00:00
Fix binary file reading in bin_to_coe.py
Co-authored-by: Virendra Kakade <virendra.kakade@ni.com>
This commit is contained in:
parent
31e40f8a25
commit
89cb249553
@ -1,18 +1,27 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import sys
|
|
||||||
import binascii
|
import binascii
|
||||||
|
import sys
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if len(sys.argv) < 2:
|
||||||
bin_file = sys.argv[1]
|
print("Usage: bin_to_coe.py <bin file> <coe file>")
|
||||||
coe_file = sys.argv[2]
|
sys.exit(1)
|
||||||
|
|
||||||
#parse bin file into hex lines
|
bin_file = sys.argv[1]
|
||||||
h = binascii.hexlify(open(bin_file).read()) + '0'*7
|
coe_file = sys.argv[2]
|
||||||
d = [h[i*8:(i+1)*8] for i in range(len(h)/8)]
|
|
||||||
|
|
||||||
#write output coe file
|
# Read the binary file in binary mode
|
||||||
out = open(coe_file, 'w')
|
with open(bin_file, 'rb') as f:
|
||||||
out.write('memory_initialization_radix=16;\n')
|
binary_data = f.read()
|
||||||
out.write('memory_initialization_vector=\n')
|
padding_length = (8 - (len(binary_data) * 2) % 8) % 8 # Calculate necessary padding
|
||||||
out.write(',\n'.join([h[i*8:(i+1)*8] for i in range(len(h)/8)]) + ';')
|
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)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user