diff --git a/common.mk b/common.mk index 6679457e..43615a92 100644 --- a/common.mk +++ b/common.mk @@ -161,9 +161,7 @@ run-fast: run-asm-tests-fast run-bmark-tests-fast # helper rules to run simulator with fast loadmem via hex files ######################################################################################### $(binary_hex): $(output_dir) $(BINARY) - hex_bytes=$(shell readelf --segments --wide $(BINARY) | grep LOAD | tail -n 1 | tr -s [:space:] | cut -f4,6 -d' ' | tr -d x | tr [:lower:] [:upper:] | tr ' ' + | sed 's/0000000008/ibase=16;/' | bc);\ - power_2_bytes=`python -c "import math; print int(pow(2,math.ceil(math.log($$hex_bytes)/math.log(2))))"`;\ - elf2hex 64 $$power_2_bytes $(BINARY) $(shell echo "ibase=16;$(LOADMEM_ADDR)" | bc) > $(binary_hex) + $(base_dir)/scripts/smartelf2hex.sh $(BINARY) > $(binary_hex) run-binary-hex: $(output_dir) $(sim) $(binary_hex) run-binary-hex: run-binary diff --git a/scripts/smartelf2hex.sh b/scripts/smartelf2hex.sh new file mode 100755 index 00000000..782977ff --- /dev/null +++ b/scripts/smartelf2hex.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +# This script find the appropriate arguments to pass to elf2hex by inspecting the given RISC-V elf binary +# First and only argument is the binary to be converted. +# The output of this script should be redirected to a file (as with normal elf2hex). + +binary=$1 +segments=`readelf --segments --wide $binary` +entry_hex=`echo -e "$segments" | grep "Entry point" | cut -f3 -d' ' | sed 's/0x//' | tr [:lower:] [:upper:]` +entry_dec=`bc <<< "ibase=16;$entry_hex"` +length_hex=`echo "$segments" | grep LOAD | tail -n 1 | tr -s [:space:] | cut -f4,6 -d' '` +length_dec=`echo $length_hex | tr -d x | tr [:lower:] [:upper:] | tr ' ' + | sed 's/^/ibase=16;/' | sed "s/$/-$entry_hex/" | bc` +power_2_length=`echo "x=l($length_dec)/l(2); scale=0; 2^((x+1)/1)" | bc -l` +elf2hex 64 $power_2_length $binary $entry_dec diff --git a/variables.mk b/variables.mk index 61602507..d7eccb49 100644 --- a/variables.mk +++ b/variables.mk @@ -173,6 +173,7 @@ override SIM_FLAGS += +loadmem=$(LOADMEM) +loadmem_addr=$(LOADMEM_ADDR) endif VERBOSE_FLAGS ?= +verbose sim_out_name = $(output_dir)/$(subst $() $(),_,$(notdir $(basename $(BINARY)))) +binary_hex= $(sim_out_name).loadmem_hex ######################################################################################### # build output directory for compilation