#!/usr/bin/env bash
# run_hello.sh - compile + run an ML-KEM hello_world hardware testbench.
#   ./run_hello.sh          single-instance protocol (default)
#   ./run_hello.sh two      two-instance (genenc + dec) protocol
set -e
cd "$(dirname "$0")"
source /opt/Xilinx/Vivado/2019.2/settings64.sh >/dev/null 2>&1
export LD_PRELOAD="/usr/lib64/libtinfo.so.5"
rm -rf xsim.dir .Xil

if [ "$1" = "two" ]; then
    TB=tb_mlkem_two_inst_xsim; SNAP=mlkem_two
else
    TB=tb_mlkem_hello_world_xsim; SNAP=mlkem_hello
fi

# compile the RTL (every non-TB xvlog line from the shared tcl)
grep -E '^xvlog ' sync_rtl/top/TB/xsim_run.tcl | grep -v 'TB/tb_' | while read -r cmd; do
    eval "$cmd" >/dev/null
done

xvlog -sv --relax sync_rtl/top/TB/$TB.v >/dev/null
xelab $TB -s $SNAP --timescale 1ns/1ps >/dev/null 2>&1
xsim $SNAP -R 2>/dev/null | grep -vE '^(#|INFO|Time resolution|run -all|exit|xsim|Vivado|====.*Simulator|SW Build|IP Build|Copyright|Tool Version|Start of|source |## )' | sed '/^$/N;/^\n$/D'

