-
Notifications
You must be signed in to change notification settings - Fork 676
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix binary file reading in bin_to_coe.py
Co-authored-by: Virendra Kakade <[email protected]>
- Loading branch information
Showing
1 changed file
with
22 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
#!/usr/bin/python | ||
#!/usr/bin/env python3 | ||
|
||
import sys | ||
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] | ||
|
||
if __name__ == '__main__': | ||
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 | ||
|
||
#parse bin file into hex lines | ||
h = binascii.hexlify(open(bin_file).read()) + '0'*7 | ||
d = [h[i*8:(i+1)*8] for i in range(len(h)/8)] | ||
# 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 output coe file | ||
out = open(coe_file, 'w') | ||
out.write('memory_initialization_radix=16;\n') | ||
out.write('memory_initialization_vector=\n') | ||
out.write(',\n'.join([h[i*8:(i+1)*8] for i in range(len(h)/8)]) + ';') | ||
# Write the COE formatted string to the output file | ||
with open(coe_file, 'w') as f: | ||
f.write(coe_str) |