mirror of
https://github.com/vortexgpgpu/vortex.git
synced 2025-06-28 17:43:24 -04:00
adding test coverage for xilinx synthesis
This commit is contained in:
parent
ca3499f3df
commit
e4bfa47895
22 changed files with 415 additions and 18320 deletions
|
@ -14,78 +14,83 @@
|
|||
# limitations under the License.
|
||||
|
||||
import argparse
|
||||
import os
|
||||
|
||||
g_memory = {}
|
||||
def parse_binfile_option(option):
|
||||
addr, path = option.split(':')
|
||||
return int(addr, 0), path
|
||||
|
||||
def hex2bin(ch):
|
||||
return int(ch, 16) if ch.isdigit() or ch in 'abcdefABCDEF' else 0
|
||||
def parse_value_option(option):
|
||||
addr, value = option.split(':')
|
||||
return int(addr, 0), value
|
||||
|
||||
def process_binary(binfname, wordsize, binaddr):
|
||||
with open(binfname, 'rb') as f:
|
||||
buffer = list(f.read())
|
||||
g_memory[binaddr] = buffer
|
||||
return (len(buffer) + wordsize - 1) // wordsize
|
||||
def load_binary_data(addr, path, word_size, memory, little_endian):
|
||||
with open(path, 'rb') as f:
|
||||
binary_data = f.read()
|
||||
|
||||
def process_data(datfname, wordsize):
|
||||
offset, buffer = 0, []
|
||||
with open(datfname, 'r') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line.startswith("#"):
|
||||
continue
|
||||
if line.startswith("@"):
|
||||
if buffer:
|
||||
g_memory[offset] = buffer
|
||||
offset = int(line[1:], 16)
|
||||
buffer = []
|
||||
else:
|
||||
for i in range(0, len(line), 2):
|
||||
byte = hex2bin(line[i]) << 4 | hex2bin(line[i+1])
|
||||
buffer.append(byte)
|
||||
if len(buffer) % wordsize:
|
||||
buffer.extend([0] * (wordsize - len(buffer) % wordsize))
|
||||
offset += 1
|
||||
if buffer:
|
||||
g_memory[offset] = buffer
|
||||
return offset
|
||||
word_count = len(binary_data) // word_size
|
||||
if len(binary_data) % word_size != 0:
|
||||
word_count += 1
|
||||
|
||||
def write_coe(outfname, wordsize, depth, defval):
|
||||
with open(outfname, 'w') as f:
|
||||
f.write("MEMORY_INITIALIZATION_RADIX=16;\nMEMORY_INITIALIZATION_VECTOR=\n")
|
||||
i = 0
|
||||
for addr in sorted(g_memory):
|
||||
while i < addr:
|
||||
f.write(f"{defval},\n")
|
||||
i += 1
|
||||
data = g_memory[addr]
|
||||
for j in range(0, len(data), wordsize):
|
||||
f.write(",".join([f"{byte:02x}" for byte in data[j:j+wordsize][::-1]]) + ",\n")
|
||||
i += 1
|
||||
while i < depth:
|
||||
f.write(f"{defval},\n")
|
||||
i += 1
|
||||
f.seek(f.tell() - 2, 0) # Remove the last comma
|
||||
f.write(";\n")
|
||||
for i in range(word_count):
|
||||
word_data = binary_data[i * word_size: (i + 1) * word_size]
|
||||
if little_endian:
|
||||
word_data = word_data[::-1] # Reverse the byte order for little-endian
|
||||
hex_value = word_data.hex().zfill(word_size * 2)
|
||||
memory[addr + i] = hex_value
|
||||
|
||||
def add_value_data(addr, value, memory, word_size):
|
||||
value = value.zfill(word_size * 2)
|
||||
memory[addr] = value
|
||||
|
||||
def binary_to_coe(output_file, word_size, depth, default_value, memory):
|
||||
if depth == 0:
|
||||
depth = max(memory.keys()) + 1
|
||||
|
||||
with open(output_file, 'w') as coe_file:
|
||||
coe_file.write("; This file was generated from binary blobs and/or values\n")
|
||||
coe_file.write("memory_initialization_radix=16;\n")
|
||||
coe_file.write("memory_initialization_vector=\n")
|
||||
|
||||
for addr in range(depth):
|
||||
hex_value = memory.get(addr, default_value)
|
||||
coe_file.write(f"{hex_value},\n")
|
||||
|
||||
coe_file.seek(coe_file.tell() - 2)
|
||||
coe_file.write(";\n")
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Binary to Xilinx COE File Converter")
|
||||
parser.add_argument("--binary", help="Input binary file.")
|
||||
parser.add_argument("--data", help="Input data file.")
|
||||
parser = argparse.ArgumentParser(description="Convert binaries and values to a Xilinx COE file.")
|
||||
parser.add_argument("--binfile", action='append', help="Binary file with starting address in the format <addr>:<path>")
|
||||
parser.add_argument("--value", action='append', help="Hex value with starting address in the format <addr>:<value>")
|
||||
parser.add_argument("--out", default="output.coe", help="Output file (optional).")
|
||||
parser.add_argument("--wordsize", type=int, default=4, help="Word size in bytes (default 4).")
|
||||
parser.add_argument("--depth", type=int, default=0, help="Address size (optional).")
|
||||
parser.add_argument("--binaddr", type=int, default=0, help="Binary address (optional).")
|
||||
parser.add_argument("--default", default="00", help="Default hex value as string (optional).")
|
||||
parser.add_argument("--little_endian", action='store_true', help="Interpret binary files as little-endian (default is big-endian).")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
depth = max(
|
||||
process_binary(args.binary, args.wordsize, args.binaddr) if args.binary else 0,
|
||||
process_data(args.data, args.wordsize) if args.data else 0,
|
||||
args.depth
|
||||
)
|
||||
if args.binfile is None and args.value is None:
|
||||
raise ValueError("At least one --binfile or --value must be provided.")
|
||||
|
||||
write_coe(args.out, args.wordsize, depth, args.default)
|
||||
# Initialize memory dictionary
|
||||
memory = {}
|
||||
|
||||
# Process binary files
|
||||
if args.binfile:
|
||||
for option in args.binfile:
|
||||
addr, path = parse_binfile_option(option)
|
||||
load_binary_data(addr, path, args.wordsize, memory, args.little_endian)
|
||||
|
||||
# Process individual values
|
||||
if args.value:
|
||||
for option in args.value:
|
||||
addr, value = parse_value_option(option)
|
||||
add_value_data(addr, value, memory, args.wordsize)
|
||||
|
||||
# Generate the COE file
|
||||
binary_to_coe(args.out, args.wordsize, args.depth, args.default.zfill(args.wordsize * 2), memory)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue