#! /bin/bash
# do_vivado_mmi_elf.sh  PascalB,    09-Jul-2020.

proj_name=$(basename $1 .bit)
lm32_wrpc_mmi=${proj_name}.mmi
lm32_wrpc_elf=$2
lm32_wrpc_instpath="lm32_wrpc_memory"

if not [ -f "$1" ]; then
    echo "$1 bit file not found"
    echo "Usage: do_vivado_mmi_elf.sh <bitfile>.bit <elffile>.elf"
    exit
fi
if not [ ${1: -4} == ".bit" ]; then
    echo "$1 bit file not found"
    echo "Usage: do_vivado_mmi_elf.sh <bitfile>.bit <elffile>.elf"
    exit
fi

if not [ -f "$2" ]; then
    echo "$1 elf file not found"
    echo "Usage: do_vivado_mmi_elf.sh <bitfile>.bit <elffile>.elf"
    exit
fi
if not [ ${2: -4} == ".elf" ]; then
    echo "$2 elf file not found"
    echo "Usage: do_vivado_mmi_elf.sh <bitfile>.bit <elffile>.elf"
    exit
fi

if not [ -f "$lm32_wrpc_mmi" ]; then
    echo "$1 mmi file not found, has it been generated?"
    exit
fi

# Cleanup old log files and stuff
rm vivado_mmi_elf.log 2>/dev/null
rm updatemem*.jou 2>/dev/null
rm updatemem*.log 2>/dev/null

updatemem -meminfo ${proj_name}.mmi -data $2 -bit $1 -proc $lm32_wrpc_instpath -out ${proj_name}_elf.bit -force >> vivado_mmi_elf.log
