# NOTE: On some systems, you may need:
#   export LD_PRELOAD=/usr/lib64/libtinfo.so.5
# before running this script.

# xsim_run.tcl - Vivado xsim compilation and simulation for sample_ntt_sync
#
# Compiles sample_ntt_sync RTL + SHA3 dependencies + testbench and runs simulation.
# Run from the project root: ~/Dev/mlkem/
#
# Prerequisites:
#   source /opt/Xilinx/Vivado/2019.2/settings64.sh
#
# Usage:
#   xsim -runall sync_rtl/sample_ntt/TB/xsim_run.tcl
#
#   # Or step-by-step:
#   vivado -mode batch -source sync_rtl/sample_ntt/TB/xsim_run.tcl

# ================================================================
# Configuration
# ================================================================
set SRC_DIR      sync_rtl/sample_ntt
set SHA3_DIR     sync_rtl/sha3
set COMMON_DIR   sync_rtl/common
set TB_DIR       sync_rtl/sample_ntt/TB

# ================================================================
# Step 1: Compile all source files (xvlog)
# ================================================================
puts "=== Compiling RTL sources ==="

# Keccak round (combinational, used by keccak_core)
xvlog -sv --relax -i . sync_rtl/sha3/keccak_round.v

# Keccak core (24-round sequential core, used by sample_ntt_sync)
xvlog -sv --relax -i . sync_rtl/sha3/keccak_core.v

# sample_ntt_sync (DUT) — uses `include "sync_rtl/common/defines.vh"
xvlog -sv --relax -i . sync_rtl/sample_ntt/sample_ntt_sync.v

# ================================================================
# Step 2: Compile testbench
# ================================================================
puts "=== Compiling testbench ==="
xvlog -sv --relax sync_rtl/sample_ntt/TB/tb_sample_ntt_xsim.v

# ================================================================
# Step 3: Elaborate snapshot (xelab)
# ================================================================
puts "=== Elaborating snapshot ==="
xelab tb_sample_ntt_xsim -s tb_sample_ntt_xsim --timescale 1ns/1ps

# ================================================================
# Step 4: Run simulation
# ================================================================
puts "=== Running simulation ==="
xsim tb_sample_ntt_xsim -R

puts ""
puts "=== sample_ntt simulation complete ==="
