# *****************************************************************************
#                 Cadence C-to-Silicon Compiler
# 
# Copyright notice: Copyright 2006-2012 Cadence Design Systems, Inc. All
# rights reserved worldwide.
#
# The code contained herein is provided to Cadence's customer and may be used 
# in accordance with a previously executed license agreement between Cadence 
# and that customer. This code is provided as an example for educational 
# purposes and is not intended for use in a production design.
#
# Patent notice: Protected by U.S. Patent 7472361; other U.S. patents pending.
# ******************************************************************************
#
# This makefile runs simulations from the CtoS GUI or as a stand-alone
# makefile to run simulations in batch mode.
# 
# The following variables can be set in the command line as follows: 
# 
# CTOS_EXE  - Specify path to CtoS executable if it is not in your path
#
# TEST_FILE - Specify the path for the test pattern file, relative to the 
#             directory where this Makefile is located, if you wish to use any 
#             other than the default patterns/base.txt
#
# USER_ARGS - Specify additional arguments to the simulator
# 
# SIM_GUI - Bring up SimVision
#
# CDS_TR - Set to false to turn off the transaction recording with Cadence IES
#          The default is true.
#
#
# *****************************************************************************
# ***** CtoS Section *****
# *****************************************************************************
CTOS_EXE		?= $(shell echo `which ctos`)
CTOS_DIR        := $(shell echo `dirname ${CTOS_EXE}`)
CTOS_ROOT       := /comelec/softs/opt/Cadence/2015/CTOS142
DESIGN_NAME		 = DUT
TOP_MODULE_NAME		 = DUT
DESIGN_SRC_FILES	 = src/*.cpp 
DESIGN_TB_FILES		 = tb/*.cpp
DESIGN_INCLUDES  	 = -I./$(MODEL_DIR) -I./src -I./tb
MODEL_DIR		 ?= model
CTOS_INCLUDES		 := -I$(CTOS_ROOT)/share/ctos/include \
			 -I$(CTOS_ROOT)/share/ctos/include/ctos_fx \
			 -I$(CTOS_ROOT)/share/ctos/include/ctos_tlm \
			 -I$(CTOS_ROOT)/share/ctos/include/ctos_flex_channels
CTOS_MODELS  := $(MODEL_DIR)/$(DESIGN_NAME)_post_build.v \
                $(MODEL_DIR)/$(DESIGN_NAME)_final.v \
                $(MODEL_DIR)/$(DESIGN_NAME)_rtl.v

# *****************************************************************************
# ***** Simulator Section *****
# *****************************************************************************
SIM_EXE			 = irun

SIM_GUI_ARGS		 = -layout cdebug -ncelab_args,-scCreateViewables \
			-linedebug +access+rwc
CDS_TR			 ?= true
SIM_STANDARD_ARGS	 = -gnu -SC_MAIN -ncsc_runargs "-SINGLE_LOG"
SIM_CONFIG_ARGS		 := 

SIM_INCLUDES		 ?= $(CTOS_INCLUDES)
ORIG_ARGS		 := $(SIM_INCLUDES) $(DESIGN_INCLUDES) $(SIM_STANDARD_ARGS) \
			$(SIM_CONFIG_ARGS)  \
			$(DESIGN_SRC_FILES) $(DESIGN_TB_FILES)
LOG_DIR			 ?= log

ORIG_ARGS	 := $(ORIG_ARGS)

ifneq ($(origin USER_ARGS), undefined)
ORIG_ARGS	 := $(ORIG_ARGS) $(USER_ARGS)
endif

ifneq ($(origin SIM_GUI), undefined)
ORIG_ARGS	 := $(ORIG_ARGS) $(SIM_GUI_ARGS)
endif

SIM_ARGS		 := $(ORIG_ARGS)
WORK_LIBS		 := work_orig_sim work_post_build_sim\
			    work_rtl_sim  work_final_sim \
			    work_tlm_sim  work_current_sim

RUN_LIBS		 := run 


# *****************************************************************************
# ***** Rules and Targets Section *****
# *****************************************************************************
all: setup tlm_sim orig_sim post_build_sim final_sim rtl_sim synth_gates

setup:	info clean
	mkdir -p $(LOG_DIR)

info:
	@echo Test run at `date` on `hostname`
	@echo Test run in `pwd`

clean:
	rm -rf core* DUT_* $(LOG_DIR) $(WORK_LIBS) model rc_work *.log run run_*_sim 


ctos_found:
	@if [ ! ${CTOS_EXE} ]; then \
		echo ""; \
		echo "============================="; \
		echo "Error: ctos not found in PATH"; \
		echo "============================="; \
		echo "Two ways to specify location of ctos:"; \
		echo "1. Run 'make CTOS_EXE=<ctos_install_path>/tools/bin/ctos' OR"; \
		echo "2. Add <ctos_install_path>/tools/bin to your PATH"; \
		echo ""; \
		exit 1; \
	fi;

	@if [ ! -e ${CTOS_EXE} ]; then \
		echo ""; \
		echo "================================================="; \
		echo "Error: Value of CTOS_EXE is not executable file"; \
		echo "================================================="; \
		echo "Check that the value of CTOS_EXE specified is an executable."; \
		echo ""; \
		exit 1; \
	fi;

sim_found:	ctos_found
	@if [ ! $(shell echo `which irun`) ]; then \
		echo ""; \
		echo "=================================="; \
		echo "Error: irun not found in PATH"; \
		echo "=================================="; \
		echo "Add <ius_install_path>/tools/bin to your PATH"; \
		echo ""; \
		exit 1; \
	fi;

tlm_sim:	sim_found
	@echo "======================================"
	@echo "Simulating the original SystemC model"
	@echo "with FlexChannels at transaction level"
	@echo "======================================"
	rm -rf run_tlm_sim/*
	mkdir -p $(LOG_DIR) work_tlm_sim run_tlm_sim
	$(SIM_EXE) -DTLM_SIM  $(ORIG_ARGS) \
	        -nclibdirpath work_tlm_sim -l $(LOG_DIR)/tlm_sim.log
	grep '^PASS: Simulation completed.' $(LOG_DIR)/tlm_sim.log > /dev/null


orig_sim: 	sim_found
	@echo "======================================"
	@echo "Simulating the original SystemC model"
	@echo "with FlexChannels at signal level"
	@echo "======================================"
	rm -rf run_orig_sim/*
	mkdir -p $(LOG_DIR) work_orig_sim run_orig_sim
	$(SIM_EXE) $(ORIG_ARGS) \
		-nclibdirpath work_orig_sim \
		-l $(LOG_DIR)/orig_sim.log
	grep '^PASS: Simulation completed.' $(LOG_DIR)/orig_sim.log > /dev/null

post_build_sim: sim_found
	@echo "================================="
	@echo "Simulating the post-build model "
	@echo "================================="
	export CTOS_TARGET_NAME=post_build_sim; export CTOS_MODEL=post_build; \
	rm -rf run_post_build_sim/*
	mkdir -p $(LOG_DIR) work_post_build_sim run_post_build_sim
	$(SIM_EXE) $(SIM_ARGS) \
		$(MODEL_DIR)/*_post_build.v \
		-DCTOS_post_build -DCTOS_MODEL=post_build \
		-nclibdirpath work_post_build_sim \
		-l $(LOG_DIR)/post_build_sim.log
	grep '^PASS: Simulation completed.' $(LOG_DIR)/post_build_sim.log > /dev/null

current_sim: 	sim_found
	@echo "========================="
	@echo "Simulating the current model "
	@echo "========================="
	export CTOS_TARGET_NAME=current_sim; export CTOS_MODEL=current; \
	rm -rf run_orig_sim/*
	mkdir -p $(LOG_DIR) work_current_sim run_current_sim
	$(SIM_EXE) $(SIM_ARGS) \
		$(MODEL_DIR)/*_current.v \
		-DCTOS_current -DCTOS_MODEL=current \
		-nclibdirpath work_current_sim \
		-l $(LOG_DIR)/current_sim.log
	grep '^PASS: Simulation completed.' $(LOG_DIR)/current_sim.log > /dev/null

final_sim: sim_found
	@echo "========================="
	@echo "Simulating the final model "
	@echo "========================="
	export CTOS_TARGET_NAME=final_sim; export CTOS_MODEL=final; \
	rm -rf run_final_sim/*
	mkdir -p $(LOG_DIR) work_final_sim run_final_sim
	$(SIM_EXE) $(SIM_ARGS) \
		$(MODEL_DIR)/*_final.v \
		-DCTOS_final -DCTOS_MODEL=final \
		-nclibdirpath work_final_sim \
		-l $(LOG_DIR)/final_sim.log
	grep '^PASS: Simulation completed.' $(LOG_DIR)/final_sim.log > /dev/null

rtl_sim: sim_found
	@echo "========================="
	@echo "Simulating the RTL model "
	@echo "========================="
	export CTOS_TARGET_NAME=rtl_sim; export CTOS_MODEL=rtl; \
	rm -rf run_rtl_sim/*
	mkdir -p $(LOG_DIR) work_rtl_sim run_rtl_sim
	$(SIM_EXE) $(SIM_ARGS) \
		$(MODEL_DIR)/*_rtl.v \
		-DCTOS_rtl -DCTOS_MODEL=rtl \
		-nclibdirpath work_rtl_sim \
		-l $(LOG_DIR)/rtl_sim.log
	grep '^PASS: Simulation completed.' $(LOG_DIR)/rtl_sim.log > /dev/null

$(CTOS_MODELS): ctos_found $(DESIGN_SRC_FILES) ctos.tcl
	@echo "=========================================="
	@echo "Creating sim models using CTOS "
	@echo "=========================================="
	$(CTOS_EXE) ctos.tcl -log $(LOG_DIR)/ctos.log


synth_gates:
	cd run_synth_gates; \
	make -f Makefile.synth synth_gates

.PHONY: setup sim_found ctos_found info clean tlm_sim orig_sim post_build_sim current_sim final_sim rtl_sim synth_gates

