#
# Makefile for Transaction Generator 2 (SCTG2)
#

export

PWD = $(shell pwd)

#
TG2_INC         = transaction_generator_2/include
MESH_INC        = hw_lib/mesh_2d/systemc
PKT_CODEC_INC   = hw_lib/packet_codec/systemc
FIFO_INC        = hw_lib/fifo/systemc
ADAPTER_INC     = hw_lib/adapters/systemc
SIMPLE_BUS_INC  = hw_lib/simple_bus/systemc
NOC_FACTORY_INC = hw_lib/noc_factory/systemc


INCLUDES = -I$(PWD)/$(TG2_INC) \
 -I$(PWD)/$(BOOST_INC) \
 -I$(PWD)/$(ADAPTER_INC) \
 -I$(PWD)/$(MESH_INC) \
 -I$(PWD)/$(PKT_CODEC_INC) \
 -I$(PWD)/$(FIFO_INC) \
 -I$(PWD)/$(SIMPLE_BUS_INC) \
 -I$(PWD)/$(TLM_INC) \
 -I$(PWD)/$(SC_INC) \
 -I$(PWD)/$(NOC_FACTORY_INC) \
 -I$(PWD)/$(OCP_TLM_INC)


MODELSIM_INCS = -I$(TG2_INC) \
 -I$(BOOST_INC) \
 -I$(MESH_INC) \
 -I$(ADAPTER_INC) \
 -I$(PKT_CODEC_INC) \
 -I$(FIFO_INC) \
 -I$(SIMPLE_BUS_INC) \
 -I$(NOC_FACTORY_INC) \
 -I$(OCP_TLM_INC)

TARGET = sctg

CXXFLAGS = $(OPT_LEVEL) -DSC_INCLUDE_DYNAMIC_PROCESSES $(ASIO_FLAGS) $(USE_EXECMON) \
           $(ADD_CXXFLAGS) $(USE_OCP_TLM_KIT)

SCCOM_OPTS = $(MODELSIM_INCS) $(OPT_LEVEL) -DSC_INCLUDE_DYNAMIC_PROCESSES -incr \
             $(USE_EXECMON) -DSCTG_MIXED_LANGUAGE_SIM -DNDEBUG $(ADD_SCCOM) \
             $(USE_OCP_TLM_KIT)

VCOM = vcom -check_synthesis

all: Makefile.env subs $(TARGET)

Makefile.env:
	@echo
	@echo " Copy or rename Makefile.env.tmp as Makefile.env"
	@echo " and modify it to set paths"
	@echo
	stopping_here_on_purpose

include Makefile.env

ifdef USE_EXECMON
ASIO = $(ASIO_LINK)
else
ASIO =
endif

ifdef USE_EXECMON
all: bin/execution_monitor
sccom: bin/execution_monitor

bin/execution_monitor:
	cd java_tool_installer; ant -Dprefix=.. configure_all
	cd java_tool_installer; ant build_all
	cd java_tool_installer; ant install_all
endif

ifndef SC_LIB
SC_LIB = .
endif

#
# Call make on these subdirectories
# 
NOC_DIRS = hw_lib/mesh_2d/systemc \
           hw_lib/simple_bus/systemc

.PHONY: subs
subs:
	@for dir in $(NOC_DIRS); do $(MAKE) -C $$dir; done
	cd hw_lib/noc_factory/systemc; $(MAKE)
	cd transaction_generator_2; $(MAKE)

#
# Factory object to be linked with SCTG2
#
FACTORIES = \
          hw_lib/mesh_2d/systemc/sc_rtl_1/noc_factory.o \
          hw_lib/mesh_2d/systemc/sc_rtl_2/noc_factory.o \
          hw_lib/mesh_2d/systemc/sc_tlm_1/tlm_1_factory.o \
          hw_lib/mesh_2d/systemc/mesh_factory.o \
          hw_lib/simple_bus/systemc/sbus_factory.o \
          hw_lib/noc_factory/systemc/noc_factory.o

ifdef USE_OCP_TLM_KIT
FACTORIES += hw_lib/mesh_2d/systemc/sc_ocp_tl3_1/ocp_tl3_factory.o
endif

$(TARGET): $(FACTORIES) \
          transaction_generator_2/libsctg.a

$(TARGET):
	$(CXX) -o $(TARGET) $(ADD_LDFLAGS) $(CXXFLAGS) $(INCLUDES) \
          $(FACTORIES) \
          -L$(SC_LIB) \
          -lsystemc \
          -Ltransaction_generator_2 \
          -lsctg \
          -L$(BOOST_LIBS) \
          -lboost_program_options \
          $(ASIO)

.PHONY: clean
clean:
	@for dir in $(NOC_DIRS); do $(MAKE) clean -C $$dir; done
	cd hw_lib/noc_factory/systemc; $(MAKE) clean
	cd transaction_generator_2; $(MAKE) clean
	rm -f *.o *.exe $(TARGET) *~
	rm -rf work_libs/*

ifdef USE_EXECMON
clean: clean_javas
endif

.PHONY: clean_javas
clean_javas:
	cd java_tool_installer; ant clean_all
	cd java_tool_installer; ant uninstall_all
	cd java_tool_installer; ant uninstall_libs

.PHONY: modelsim_libs
modelsim_libs:
	vlib work_libs/lib_vhd
	vlib work_libs/lib_sctg
	vlib work_libs/lib_1
	vlib work_libs/lib_2

VHDL_FILES = \
	hw_lib/fifo/vhd/fifo.vhd \
	hw_lib/fifo/vhd/multiclk_fifo.vhd \
	hw_lib/packet_codec/vhd/addr_lut_pkg.vhd \
	hw_lib/packet_codec/vhd/addr_lut.vhd \
	hw_lib/packet_codec/vhd/pkt_counter.vhd \
	hw_lib/packet_codec/vhd/pkt_dec.vhd \
	hw_lib/packet_codec/vhd/pkt_enc.vhd \
	hw_lib/packet_codec/vhd/pkt_enc_dec.vhd \
	hw_lib/packet_codec/vhd/enc_dec_1d.vhd \
	hw_lib/mesh_2d/vhd/mesh_router.vhd \
	hw_lib/mesh_2d/vhd/mesh_2d.vhd \
	hw_lib/mesh_2d/vhd/mesh_2d_with_pkt_codec_top.vhd

.PHONY: vcom
vcom: $(VHDL_FILES)
	$(VCOM) -work work_libs/lib_vhd $(VHDL_FILES)

SCCOM_SCTG_FILES = \
        transaction_generator_2/src/common.cc \
        transaction_generator_2/src/buffer.cc \
        transaction_generator_2/src/configuration.cc \
        transaction_generator_2/src/measure.cc \
        transaction_generator_2/src/event.cc \
        transaction_generator_2/src/trigger.cc \
        transaction_generator_2/src/task.cc \
        transaction_generator_2/src/resource.cc	\
        transaction_generator_2/src/processing_element.cc \
        hw_lib/mesh_2d/systemc/mesh_factory.cc \
        hw_lib/simple_bus/systemc/sbus_factory.cc \
        hw_lib/noc_factory/systemc/noc_factory.cc \
        transaction_generator_2/src/main.cc 

SCCOM_LIB_1_FILES = \
        hw_lib/mesh_2d/systemc/sc_rtl_1/noc_factory.cc

SCCOM_LIB_2_FILES = \
        hw_lib/mesh_2d/systemc/sc_rtl_2/noc_factory.cc \
        hw_lib/mesh_2d/systemc/sc_tlm_1/tlm_1_factory.cc

SCCOM_LIB_VHD_FILES = \
	hw_lib/mesh_2d/systemc/vhd/noc_factory.cc


.PHONY: sccom_sctg
sccom_sctg: $(SCCOM_SCTG_FILES)
	sccom $(SCCOM_OPTS) -work work_libs/lib_sctg $(SCCOM_SCTG_FILES)

.PHONY: sccom_lib_vhd
sccom_lib_vhd: $(SCCOM_LIB_VHD_FILES)
	sccom $(SCCOM_OPTS) -work work_libs/lib_vhd $(SCCOM_LIB_VHD_FILES)

.PHONY: sccom_lib_1
sccom_lib_1: $(SCCOM_LIB_1_FILES)
	sccom $(SCCOM_OPTS) -work work_libs/lib_1 $(SCCOM_LIB_1_FILES)

.PHONY: sccom_lib_2
sccom_lib_2: $(SCCOM_LIB_2_FILES)
	sccom $(SCCOM_OPTS) -work work_libs/lib_2 $(SCCOM_LIB_2_FILES)

.PHONY: sccom
sccom: work_libs/cost_fuction.o sccom_sctg sccom_lib_vhd sccom_lib_1 sccom_lib_2

work_libs/cost_fuction.o: transaction_generator_2/src/cost_function.cc
	$(CXX) -c -fPIC -o work_libs/cost_function.o $(CPPFLAGS) $(CXXFLAGS) $(INCLUDES) transaction_generator_2/src/cost_function.cc

link:
	sccom -link -lib work_libs/lib_vhd -lib work_libs/lib_sctg -lib work_libs/lib_1 \
              -lib work_libs/lib_2 -work work_libs/lib_sctg work_libs/cost_function.o

sim_mesh:
	time vsim -novopt -lib work_libs/lib_sctg -L work_libs/lib_vhd -sc_arg "examples/test_mesh.xml" \
             -t 1fs -c -do "run 100ms; quit -f" sc_main

sim_sbus:
	time vsim -lib work_libs/lib_sctg -L work_libs/lib_vhd -sc_arg "examples/test_sbus.xml" \
             -t 1fs -c -do "run 100ms; quit -f" sc_main
