###################################################################
# Makefile for SG1024 testcode in Modelsim
#
# Copyright (c) 2018, 2019 Global Inkjet Systems
#
###################################################################

# User files to build, including basename of common tests, passed in to vproc makefile build
USERCODE           = VUserMain0.cpp         \
                     utils.cpp              \
                     runTest.cpp            \
                     zeroGapTests.cpp       \
                     bmpUtils.cpp           \
                     swim2Tests.cpp         \
                     fpgaCfg5Test.cpp       \
                     shtdnMon0Test.cpp

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

# Set the OS specific variable between Linux and Windows (MinGW)
ifeq (${OSTYPE}, Linux)
  MODELSIMDIR      = /opt/altera_18_1_linux/ver/modelsim_ae
  MAKE_EXE         = make
  VSIMEXE          = ${MODELSIMDIR}/linuxaloem/vsim
  VSIMARGS         = -cpppath /usr/bin
  AUTOSCRIPT       = autobuild.sh
  CMDSHELL         = bash
else
  MODELSIMDIR      = c:\altera\ver\modelsim_ae
  MAKE_EXE         = mingw32-make
  VSIMEXE          = ${MODELSIMDIR}\\win32aloem\\vsim
  VSIMARGS         =
  AUTOSCRIPT       = autobuild.bat
  CMDSHELL         = cmd /c
endif

FWDEVTOOLS         = ../../../../../fw_development_tools
VPROCDIR           = ${FWDEVTOOLS}/verification/vproc

# 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

ELGIPDIR           = ../../../generic_ip/
LIBDOCDIR          = ${FWDEVTOOLS}/verification/lib/hdl/doc
# Files used in auto-generating core file lists for simulation.
# compile.do uses the generated Tcl files.
COREHWTCLFILE      = ../core_hw.tcl
COREVHDLTCLFILE    = files_core_auto.tcl
COREVLOGTCLFILE    = files_core_vlog_auto.tcl

JSONFILES          = doc/*.json                                    \
                     ../../../print_ctrl/doc/*.json                \
                     ../../../print_streamer/doc/*.json            \
                     ../../../print_unit/doc/*.json                \
                     ../../../top/doc/core.json                    \
                     ${LIBDOCDIR}/*.json                           \
                     ${ELGIPDIR}/shutdown_monitor/0/doc/*.json     \
                     ${ELGIPDIR}/enc_pd_emul/1/doc/*.json          \
                     ${ELGIPDIR}/fire_pulse_manager/3/doc/*.json   \
                     ${ELGIPDIR}/generic_delay_queue/1/doc/*.json  \
                     ${ELGIPDIR}/generic_pio/1/doc/*.json          \
                     ${ELGIPDIR}/generic_sequencer/15/doc/*.json   \
                     ${ELGIPDIR}/hmb_fpga_cfg/5/doc/*.json         \
                     ${ELGIPDIR}/pulse_generator/1/doc/*.json      \
                     ${ELGIPDIR}/signal_sequencer/10/doc/*.json    \
                     ${ELGIPDIR}/swim/2/doc/*.json                 \
                     ${ELGIPDIR}/uart_1MBd/3/doc/*.json

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

# Add -DVPROC_VHDL to this variable if using the VHDL models for VProc and memory model
HALCOPTS           = -DVPROC_VHDL

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

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

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

# Generate the local list of VHDL core files from the QSYS core tcl file
${COREVHDLTCLFILE}: ${COREHWTCLFILE}
	@awk '/VHDL/{print "../" $$5}' $< > $@
# Generate the local list of Verilog core files from the QSYS core tcl file
${COREVLOGTCLFILE}: ${COREHWTCLFILE}
	@awk '/VERILOG/{print "../" $$5}' $< > $@
# 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="${MODELSIMDIR}" \
                  EXTINCLDIR="${EXTINCLDIR}"   \
                  HALCOPTS="${HALCOPTS}"

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

# For run, delete the simulation log befoere execution so that on
# jenkins a compilation failure will not get a false positive on
# an old successful log
run: all
	@rm -rf sim.log
	@${VSIMEXE} -c -do sim.do ${VSIMARGS}

rungui: all
	@${VSIMEXE} -gui -do simg.do ${VSIMARGS}

runnc: all
	@${VSIMEXE} -c -do sim_nocomb.do ${VSIMARGS}

runncgui: all
	@${VSIMEXE} -gui -do sim_nocomb_gui.do ${VSIMARGS}

runser: all
	@${VSIMEXE} -c -do sim_serdes.do ${VSIMARGS}

compile:
	@${VSIMEXE} -c -do compile.do -do quit

autobuild:
	@${CMDSHELL} ${AUTOSCRIPT}

gui: rungui

sim: run

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

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

