Skip to content

Instantly share code, notes, and snippets.

@plantvsbirds
Last active February 4, 2019 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plantvsbirds/037ceaeb4f62f225acdf61596bf6b892 to your computer and use it in GitHub Desktop.
Save plantvsbirds/037ceaeb4f62f225acdf61596bf6b892 to your computer and use it in GitHub Desktop.
load compiled risc-v object to modelsim-readable memory file
#!/bin/bash
# somebody at ece411 made this and i just changed it, cred cred cred stuff, license license license stuff, yeah.
# Settings
DEFAULT_TARGET=$HOME/ece411/netid/mp1/simulation/modelsim/memory.lst
ASSEMBLER=/class/ece411/software/riscv-tools/bin/riscv32-unknown-elf-gcc
OBJCOPY=/class/ece411/software/riscv-tools/bin/riscv32-unknown-elf-objcopy
OBJDUMP=/class/ece411/software/riscv-tools/bin/riscv32-unknown-elf-objdump
ADDRESSABILITY=1
# Command line parameters
ASM_FILE=$1
TARGET_FILE=${2:-$DEFAULT_TARGET}
ADDRESSABILITY=${3:-$ADDRESSABILITY}
# Print usage
if [[ "$#" -lt 1 ]]; then
echo "Write compiled risc-v object to a memory file for simulation."
echo "Usage: $0 <obj-file> [memory-file]"
exit 0
fi
OBJ_FILE=$1
BIN_FILE="/tmp/rv.bin"
## display, is all
"$OBJDUMP" -D "$OBJ_FILE"
"$OBJCOPY" -O binary "$OBJ_FILE" "$BIN_FILE"
hexdump $BIN_FILE
# Fail if binary file doesn't exist or has no memory content
if [[ ! -e "$BIN_FILE" || "$(cat "$BIN_FILE" | wc -c)" -le "1" ]]; then
echo "Error binarizing $OBJ_FILE, not generating memory file" >&2
exit 3
fi
# Fail if the target directory doesn't exist
if [[ ! -d "$(dirname "$TARGET_FILE")" ]]; then
echo "Directory $(dirname "$TARGET_FILE") does not exist." >&2
echo "Did you specify the correct target path? Aborting." >&2
exit 4
fi
# Ask if user wants to overwrite target
if [ -e "$TARGET_FILE" ]; then
echo "Target file $TARGET_FILE exists."
read -p "Overwrite? [y/N] " CONF
shopt -s nocasematch
if [[ "${CONF}" != "y" && "${CONF}" != "yes" ]]; then
echo "Aborting." >&2
exit 0
fi
shopt -u nocasematch
fi
# Write memory to file
function log2 {
local x=0
for (( y=$1-1 ; $y > 0; y >>= 1 )) ; do
let x=$x+1
done
echo $x
}
z=$( log2 $ADDRESSABILITY )
hex="0x00000000" # ?
result=$(( hex >> $z ))
mem_start=$(printf "@%08x\n" $result)
{
echo $mem_start
hexdump -ve $ADDRESSABILITY'/1 "%02X " "\n"' "$BIN_FILE" \
| awk '{for (i = NF; i > 0; i--) printf "%s", $i; print ""}'
} > "$TARGET_FILE"
echo "Assembled $ASM_FILE and wrote memory contents to $TARGET_FILE"
rm BIN_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment