generated from chipsalliance/chisel-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin2coe.py
31 lines (26 loc) · 1.05 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
import sys
def bin2coe(bin_file, coe_file):
try:
with open(bin_file, 'rb') as f:
binary_data = f.read()
words = []
for i in range(0, len(binary_data), 4):
word = binary_data[i:i+4]
# Ensure the word is 4 bytes long by padding with zeros if necessary
if len(word) < 4:
word = word.ljust(4, b'\x00')
# Convert the word to little-endian format
little_endian_word = word[::-1]
# Convert to hex string
words.append(little_endian_word.hex())
with open(coe_file, 'w') as f:
f.write("memory_initialization_radix=16;\n")
f.write("memory_initialization_vector=\n")
f.write("\n".join(words))
print(f"Conversion complete: {coe_file} generated successfully.")
except FileNotFoundError:
print(f"Error: The file {bin_file} was not found.")
except Exception as e:
print(f"An error occurred: {str(e)}")
if __name__ == "__main__":
bin2coe("random.bin", "random.coe")