FIND_PACKAGE(Perl REQUIRED)

IF(VSIM_ROOT)
	SET(VSIM_VSIM_EXECUTABLE "${VSIM_ROOT}/bin/vsim" CACHE PATH "path to vsim")
	SET(VSIM_VLOG_EXECUTABLE "${VSIM_ROOT}/bin/vlog" CACHE PATH "path to vlog")
	SET(VSIM_VLIB_EXECUTABLE "${VSIM_ROOT}/bin/vlib" CACHE PATH "path to vlib")
	SET(VSIM_VMAP_EXECUTABLE "${VSIM_ROOT}/bin/vmap" CACHE PATH "path to vlib")
    SET(VSIM_VPI_INCLUDE_DIR "${VSIM_ROOT}/include" CACHE PATH "path to veriuser.h file")
ELSE()
    SET(VSIM_ROOT "" CACHE PATH "path to Verilog simulator")
ENDIF()

SET(VSIM_TIMESCALE "1ns/1ps" CACHE STRING "Timescale directive for Verilog")

FUNCTION(VSIM_ADD_LIBRARY LIBNAME)
    EXECUTE_PROCESS(COMMAND ${VSIM_VLIB_EXECUTABLE} ${LIBNAME}
	WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
ENDFUNCTION()

FUNCTION(VSIM_MAP_LIBRARY LIBNAME PATH)
    EXECUTE_PROCESS(COMMAND ${VSIM_VMAP_EXECUTABLE} ${LIBNAME} ${PATH}
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        )
ENDFUNCTION()

#FUNCTION(ADD_VERILOG_TESTCASE PACKAGE TESTCASE)
#	FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/VerilogTest)
#	ADD_CUSTOM_COMMAND(
#		OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/VerilogTest/mkTB_${TESTCASE}.v ${CMAKE_CURRENT_BINARY_DIR}/VerilogTest/mkTB_${TESTCASE}.v.log
#		COMMAND bsc -verilog ${BLUESPEC_COMPILE_OPTS} -g mkTB_${TESTCASE} -bdir ${CMAKE_BINARY_DIR} -vdir ${CMAKE_CURRENT_BINARY_DIR}/VerilogTest ${CMAKE_CURRENT_SOURCE_DIR}/${PACKAGE}.bsv
#		COMMAND ${VLOG_COMMAND} -work work -l ${CMAKE_CURRENT_BINARY_DIR}/VerilogTest/mkTB_${TESTCASE}.v.log -timescale ${VERILOG_TIMESCALE} ${BLUESPEC_ASSIGNMENT_DELAY_ARG} ${CMAKE_CURRENT_BINARY_DIR}/VerilogTest/mkTB_${TESTCASE}.v
#		WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
## Creates the Modelsim script
##		COMMAND bsc -verilog ${BLUESPEC_COMPILE_OPTS} -e mkTB_${TESTCASE}
#		DEPENDS ${CMAKE_BINARY_DIR}/${PACKAGE}.bo)
#
#	ADD_TEST(NAME "BSV|${PACKAGE}:mkTB_${TESTCASE}.v"
#		COMMAND ${VSIM_COMMAND} -c -do "force CLK -drive 1'b0, 1'b1 @ 2 -repeat 4; force RST_N -drive 1'b0, 1'b1 @ 4; run -all" -t 1ns -lib work -onfinish exit -L altera_mf_ver work.mkTB_${TESTCASE}
#		WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
##${VSIM_LIBS})
#
## vsim -c -do "command" -gGenericName=Value -l logfile -lib <work> -onfinish exit -L <libs> -pli
## -quiet -> suppress loading messages
#
#	ADD_CUSTOM_TARGET(Test_Verilog_${TESTCASE} DEPENDS ${PACKAGE} ${CMAKE_CURRENT_BINARY_DIR}/VerilogTest/mkTB_${TESTCASE}.v)
#ENDFUNCTION()


#FUNCTION(ADD_VERILOG_SOURCE FNPFX LIB)
#	ADD_CUSTOM_COMMAND(
#		OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${FNPFX}.v.log
#		COMMAND ${VLOG_COMMAND} -work work -l ${CMAKE_CURRENT_BINARY_DIR}/${FNPFX}.v.log -timescale ${VERILOG_TIMESCALE} ${BLUESPEC_ASSIGNMENT_DELAY_ARG} ${CMAKE_CURRENT_SOURCE_DIR}/${FNPFX}.v
#		WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
#		DEPENDS ${FNPFX}.v)
#
#	ADD_CUSTOM_TARGET(${FNPFX} DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${FNPFX}.v.log)
#ENDFUNCTION()

SET(VSIM_FOUND ON)
