###################################################################
# Makefile for template testcode in Modelsim
#
# Copyright (c) 2018 - 2020 Global Inkjet Systems
#
###################################################################

# User files to build, passed in to vproc makefile build
USERCODE           = VUserMain0.cpp utils.cpp

# Get the OS type
OSTYPE:=$(shell uname)

# Set the OS specific variable between Linux and Windows (MinGW)
# Change OSTYPE test to makeit the same a WSL. I have also to chage location of modelsim
# The reason why there are two modelsim paths is to allow to run the gui when the other is busy running simulations. 
$(info OSTYPE=$(OSTYPE))
ifeq (${OSTYPE}, Linux)
  MODELSIMDIR      = /opt/intelFPGA_lite/20.1/modelsim_ae
  MODELSIMDIRAUX   = /opt/intelFPGA_lite/20.1/modelsim_ase
  MAKE_EXE         = make
  VSIMEXE          = ${MODELSIMDIRAUX}/linuxaloem/vsim
  VSIMEXEAUX       = ${MODELSIMDIRAUX}/linuxaloem/vsim
  VSIMARGS         = -cpppath /usr/bin
  AUTOSCRIPT       = ./autogen.sh
  CMDSHELL         = bash
else
  MODELSIMDIR      = c:\altera\ver\modelsim_ae
  MODELSIMDIRAUX   = c:\altera\ver\modelsim_ase
  MAKE_EXE         = mingw32-make
  VSIMEXE          = ${MODELSIMDIR}\\win32aloem\\vsim
  VSIMEXEAUX       = ${MODELSIMDIRAUX}\\win32aloem\\vsim
  VSIMARGS         = 
  AUTOSCRIPT       = autogen.bat
  CMDSHELL         = cmd /c
endif

FWDEVTOOLS         = /mnt/c/Projects/firmware_tools/
VPROCDIR           = ${FWDEVTOOLS}/verification/vproc

# Define where the HAL directory is for the auto-generated code
HALDIR             = src/hal

# Pick single target files for two auto-generations---UUT and test. 
# The core.h file is always presentfor the UUT, and the test package
# for the test bench
TBAUTOFILE         = test_pkg_auto.vhd

LIBDOCDIR          = ${FWDEVTOOLS}/verification/lib/hdl/doc

# Add JSON files build is dependent on
JSONFILES          = doc/*.json               \
                     ${LIBDOCDIR}/*.json

# If no external code directory (e.g. a HAL) set this to local source directory, else set to code directory
EXTINCLDIR         = ${CURDIR}/src

HALCOPTS           = -DVPROC_VHDL

#------------------------------------------------------
# BUILD RULES
#------------------------------------------------------

# Build is dependent on generating the auto-gen file and 
# processing makefile in vproc
all: ${TBAUTOFILE} vproc

# Make test bench auto-gen code dependent on all the JSON files to 
# call the batch file if any have changed
${TBAUTOFILE}: ${JSONFILES}
	@${CMDSHELL} ${AUTOSCRIPT}

# Call the vproc make file for it to determine if anything
# needs building.
.PHONY : vproc
vproc:
	@${MAKE_EXE} -C ${VPROCDIR}               \
                 TESTDIR=${CURDIR}            \
                 USER_C="${USERCODE}"         \
                 MODELSIMDIR="${MODELSIMDIRAUX}" \
                 EXTINCLDIR="${EXTINCLDIR}"   \
                 HALCOPTS="${HALCOPTS}"

#------------------------------------------------------
# EXECUTION RULES
#------------------------------------------------------

run: all
	@rm -rf sim.log
	@${VSIMEXE} -c   -do sim.do ${VSIMARGS}

rungui: all
	@${VSIMEXE} -gui -do simg.do ${VSIMARGS}
    
runlog: all
	@awk -F" " '/add wave/{print "log " $$NF}' < wave.do > batch.do
	@${VSIMEXE} -c -do simlog.do ${VSIMARGS}
    
runfree: all
	@awk -F" " '/add wave/{print "log " $$NF}' < wave.do > batch.do
	@${VSIMEXEAUX} -c -do simlog.do ${VSIMARGS}
    
runwaves:
	@${VSIMEXEAUX} -view vsim.wlf -do wave.do
    
compile:
	@${VSIMEXE} -c   -do compile.do -do quit
    
autobuild:
	@${AUTOSCRIPT}

gui: rungui

sim: run

log: runlog

free: runfree

waves: runwaves

#------------------------------------------------------
# CLEANING RULES
#------------------------------------------------------

clean:
	@${MAKE_EXE} -C ${VPROCDIR} USER_C="${USERCODE}" TESTDIR="${CURDIR}" clean
	@rm -rf "wlf*"
	@rm -rf *.wlf
	@rm -rf batch.do
	@rm -rf  *_auto.vhd
	@rm -rf sim.log
	@rm -rf src/hal/*.h
	@vsim -c -do cleanvlib.do -do quit
	@rm -rf work
	@rm -rf tb_pkg
	@rm -rf transcript
