Skip to content

Commit

Permalink
xilinx standalone synthesis fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tinebp committed Aug 19, 2024
1 parent 2762bd5 commit 1814ff6
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 259 deletions.
91 changes: 91 additions & 0 deletions hw/scripts/bin2coe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env python3

# Copyright © 2019-2023
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

g_memory = {}

def hex2bin(ch):
return int(ch, 16) if ch.isdigit() or ch in 'abcdefABCDEF' else 0

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 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

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")

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.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).")

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
)

write_coe(args.out, args.wordsize, depth, args.default)

if __name__ == "__main__":
main()
22 changes: 17 additions & 5 deletions hw/syn/xilinx/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ DPI_DIR := $(VORTEX_HOME)/hw/dpi
AFU_DIR := $(RTL_DIR)/afu/opae
SCRIPT_DIR := $(VORTEX_HOME)/hw/scripts

KERNEL ?= fibonacci

# include paths
FPU_INCLUDE = -I$(RTL_DIR)/fpu
ifneq (,$(findstring FPU_FPNEW,$(CONFIGS)))
Expand All @@ -32,17 +34,27 @@ CFLAGS += -DSTACK_BASE_ADDR=32\'hFF000

all: build

$(KERNEL).bin:
$(MAKE) -C $(ROOT_DIR)/kernel clean
STACK_BASE_ADDR=0xFF000 $(MAKE) -C $(ROOT_DIR)/kernel
$(MAKE) -C $(ROOT_DIR)/tests/kernel/$(KERNEL) clean
STARTUP_ADDR=0x8000 $(MAKE) -C $(ROOT_DIR)/tests/kernel/$(KERNEL)
cp $(ROOT_DIR)/tests/kernel/$(KERNEL)/$(KERNEL).bin $(KERNEL).bin

kernel.bin.coe: $(KERNEL).bin
$(SCRIPT_DIR)/bin2coe.py --out=$@ --binary=$(KERNEL).bin --binaddr=8192 --depth=16384 --wordsize=64

gen-sources: project_1/sources.txt
project_1/sources.txt:
mkdir -p project_1
$(SCRIPT_DIR)/gen_sources.sh $(CFLAGS) -P -Cproject_1/src -Oproject_1/sources.txt

build: project_1/vortex.xpr
project_1/vortex.xpr: project_1/sources.txt project.tcl
build: project_1/project_1.xpr
project_1/project_1.xpr: project_1/sources.txt kernel.bin.coe project.tcl
$(VIVADO) -mode batch -source project.tcl -tclargs project_1/sources.txt project_1/src $(SCRIPT_DIR)

run: project_1/vortex.xpr
$(VIVADO) project_1/vortex.xpr &
run: project_1/project_1.xpr
$(VIVADO) project_1/project_1.xpr &

clean:
rm -rf project_1
rm -rf project_1 $(KERNEL).bin kernel.bin.coe
51 changes: 0 additions & 51 deletions hw/syn/xilinx/test/kernel/Makefile

This file was deleted.

3 changes: 0 additions & 3 deletions hw/syn/xilinx/test/kernel/kernel.dat

This file was deleted.

36 changes: 0 additions & 36 deletions hw/syn/xilinx/test/kernel/main.c

This file was deleted.

23 changes: 0 additions & 23 deletions hw/syn/xilinx/test/kernel/start.S

This file was deleted.

2 changes: 1 addition & 1 deletion hw/syn/xilinx/test/project.tcl.in
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ set_property -dict [ list \
CONFIG.Assume_Synchronous_Clk {true} \
CONFIG.Byte_Size {8} \
CONFIG.Load_Init_File {true} \
CONFIG.Coe_File {@VORTEX_HOME@/hw/syn/xilinx/test/project_1_files/kernel.bin.coe} \
CONFIG.Coe_File {@VORTEX_HOME@/hw/syn/xilinx/test/kernel.bin.coe} \
CONFIG.EN_SAFETY_CKT {true} \
CONFIG.Enable_32bit_Address {true} \
CONFIG.Fill_Remaining_Memory_Locations {false} \
Expand Down
Loading

0 comments on commit 1814ff6

Please sign in to comment.