#!/bin/bash
# check for warnings in Verilog code
# The verilator lint tool is faster and better than Questa so it is best to run this first.

export PATH=$PATH:/usr/local/bin/
verilator=`which verilator`

basepath=$(dirname $0)/..
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
fails=0

if [ "$1" == "--nightly" ]; then
    configs=(rv32e rv64gc rv32gc rv32imc rv32i rv64i) # fdqh_rv64gc
    derivconfigs=`ls $WALLY/config/deriv`
    for entry in $derivconfigs
    do
        if [[ $entry != *"syn_sram"* ]]; then  # ignore syn_sram* configs that contain undefined module
            configs[${#configs[@]}]=$entry
        fi
    done
else
    configs=(rv32e rv64gc rv32gc rv32imc rv32i rv64i ) # add fdqh_rv64gc when working
fi

for config in ${configs[@]}; do
#    echo "$config linting..."
    if !($verilator --lint-only --quiet --top-module wallywrapper "-I$basepath/config/shared" "-I$basepath/config/$config" "-I$basepath/config/deriv/$config" $basepath/src/cvw.sv $basepath/testbench/wallywrapper.sv $basepath/src/*/*.sv $basepath/src/*/*/*.sv --relative-includes ); then
        if [ "$1" == "-nightly" ]; then
            echo -e "${RED}$config failed lint${NC}"
            fails=$((fails+1))
        else
            echo -e "${RED}$config fails with lint errors or warnings"
            exit 1
        fi
    else
        echo -e "${GREEN}$config passed lint${NC}"
    fi
done
if [ $fails -gt 0 ]; then
    echo -e "${RED}Linting failed for $fails of ${#configs[@]} configurations"
    exit 1
fi
echo -e "${GREEN}All ${#configs[@]} lints run with no errors or warnings"

# --lint-only just runs lint rather than trying to compile and simulate
# -I points to the include directory where files such as `include config.vh  are found

# For more exhaustive (and sometimes spurious) warnings, add --Wall to the Verilator command
# Unfortunately, this produces a bunch of UNUSED and UNDRIVEN signal warnings in blocks that are configured to not exist.
