# Customer ID=7729; Build=0x71521; Copyright (c) 2005-2016 Cadence Design Systems, Inc.  ALL RIGHTS RESERVED.
# 
# These coded instructions, statements, and computer programs are the
# copyrighted works and confidential proprietary information of
# Cadence Design Systems, Inc.  They may be adapted and modified by bona fide
# purchasers for internal use, but neither the original nor any adapted
# or modified version may be disclosed or distributed to third parties
# in any manner, medium, or form, in whole or in part, without the prior
# written consent of Cadence Design Systems, Inc.



EXAMPLE_NAME       = multicore.standalone$(EXE)
HOST_OBJECTS       = sc_main.$(OBJ)

# Edit the following 6 macros as required to point to your Xtensa registry
# or registries, to identify your config or configs, and to point to your
# TDK  or TDK's (if any).
XTENSA0_SYSTEM     = C:/././build/build/RG-2017.5/ttiEng/vpawar/hifi3_hikey960/269583/RG-2017.5-win32/hifi3_hikey960/config
XTENSA0_CORE       = hifi3_hikey960
TIE0_FILE          = 
XTENSA1_SYSTEM     = $(XTENSA0_SYSTEM)
XTENSA1_CORE       = $(XTENSA0_CORE)
TIE1_FILE          = $(TIE0_FILE)

# Edit to identify your target program object files (which will be built
# from corresponding C source files).
TARGET0_OBJECTS    = target/core0.out
TARGET1_OBJECTS    = target/core1.out

# Edit as required for any desired target C compiler flags
TARGET0_CFLAGS     = -g 
TARGET1_CFLAGS     = $(TARGET0_CFLAGS)

# Edit as required for any desired target link libraries
TARGET0_LIBS       = -lm
TARGET1_LIBS       = $(TARGET0_LIBS)

# NOTE: Multiple variants of ISS/XTSC libraries, when available and installed, will be found inside XTTOOLS in
#       a directory that depends on the compiler and SystemC version used.  For example:
# lib\iss:                         directory contains 32-bit libraries compatible with MSVC 2010 using SystemC 2.3.0
# lib\iss-vc110:                   directory contains 32-bit libraries compatible with MSVC 2012 using SystemC 2.3.0
# lib\iss-vc120:                   directory contains 32-bit libraries compatible with MSVC 2013 using SystemC 2.3.0
# lib64\iss:                       directory contains 64-bit libraries compatible with MSVC 2010 using SystemC 2.3.0
# lib64\iss-vc120\systemc-2.3.1:   directory contains 64-bit libraries compatible with MSVC 2013 using SystemC 2.3.1

# NOTE: Each of the above directories contains a Release version of xtsc-run (called xtsc-run.exe) and a Debug 
#       version (called xtsc-rund.exe) linked against the corresponding Release and Debug XTSC libraries in that
#       directory.

# NOTE: When using this make file to compile sc_main, the iss or iss-vc110 directory is chosen based upon the cl compiler
#       version found on your path.  When using this make file to run xtsc-run, the xtsc-run version used is the one on
#       your path.

# NOTE: MS Windows finds shared libraries (*.dll) based upon your path.  You must ensure that your path is consistent 
#       with the choices you make here so that the correct versions of the ISS libraries and, when using a shared 
#       library version of XTSC, the XTSC and SystemC libraries are found at run time.  If the wrong libraries are
#       found first on your path then the application will typically fail to start.  To help debug problems here,
#       run 'xt-make info' and pay close attention to the values printed for LIBS and PATH.  If need be, contact
#       Tensilica Support and provide them with the output of 'xt-make info' along with a description of the 
#       problem.

# NOTE: Before using this make file to compile sc_main, ensure the environment (LIB, INCLUDE and path) is set up to
#       use a supported version of Microsoft Visual Studio (e.g., by running Microsoft's vsvars32.bat). The supported
#       versions are Microsoft Visual C++ 2010, 2012, and 2013.

# NOTE: By default, 32-bit binaries are built.  If you are on a 64-bit version of MS Windows or Linux and wish to
#       build 64-bit binaries, uncomment the following line to define XTSC_64BIT or pass XTSC_64BIT=1 to make.

# XTSC_64BIT   = 1

# NOTE: If desired, you can use SystemC 2.3.1 when compiling the simulator by defining
#       XTSC_USE_SYSTEMC_2_3_1.  If compiling one of the *.plugin examples and then
#       running it using xtsc-run then you must also ensure that the xtsc-run on your
#       path is from the appropriate systemc-2.3.1 sub-directory listed above.

# NOTE: If desired, you can use the shared library version of SystemC by defining
#       XTSC_USE_SHARED_LIBRARY.

# NOTE: If desired, you can use a static version of the original ASI/OSCI SystemC 2.3.1
#       or 2.3.0 library that you compiled (source available from www.accellera.org).  
#       To use your compiled version of one of the SystemC libraries, define
#       XTSC_USE_OSCI and set the SYSTEMC_INC, TLM2_INC, SYSTEMC_LIB_PATH, and 
#       SYSTEMC_LIBS macros according to your setup (also define or XTSC_USE_SYSTEMC_2_3_1
#       if applicable).  Do NOT define XTSC_USE_SHARED_LIBRARY.

# NOTE: See the "Getting Started" Chapter in the XTSC User's Guide (xtsc_ug.pdf).

# XTSC_USE_OSCI           = 1
# XTSC_USE_SYSTEMC_2_3_1  = 1
# XTSC_USE_SHARED_LIBRARY = 1

# NOTE: Using the -libdebug flag when building the example TDK makes TIE wires visible
#       to xt-gdb and Xplorer when running the Xtensa Instruction Set Simulator; however,
#       this can slow down simulation speed.  If desired, comment out the following line
#       to disable TIE wire visibility.
TCFLAGS            = -libdebug

SYSTEMC_INC        = 
TLM2_INC           = 
SYSTEMC_LIB_PATH   = 
SYSTEMC_LIBS       = 

XTSC_LIB_PATH      = /LIBPATH:$(XTTOOLSB)\$(LIBDIR)\$(ISSDIR)
ISS_SUFFIX         = -UNKNOWN_MSVC_VERSION
LIB_SUFFIX         = 
ifneq ($(findstring Compiler Version 14,$(shell cl 2>&1)),)
$(error Visual C++ 2005 is not supported)
endif
ifneq ($(findstring Compiler Version 15,$(shell cl 2>&1)),)
$(error Visual C++ 2008 is not supported)
endif
ifneq ($(findstring Compiler Version 16,$(shell cl 2>&1)),)
# Visual C++ 2010 uses Compiler (cl) Version 16
ISS_SUFFIX         = 
MSVCDIR            = msvc100
endif
ifneq ($(findstring Compiler Version 17,$(shell cl 2>&1)),)
# Visual C++ 2012 uses Compiler (cl) Version 17
ISS_SUFFIX         = -vc110
MSVCDIR            = msvc110
endif
ifneq ($(findstring Compiler Version 18,$(shell cl 2>&1)),)
# Visual C++ 2013 uses Compiler (cl) Version 18
ISS_SUFFIX         = -vc120
MSVCDIR            = msvc120
endif
ifdef XTSC_64BIT
ifeq ($(findstring for x64,$(shell cl 2>&1)),)
$(error XTSC_64BIT is defined but Microsoft compiler, cl, does not appear to be 64 bit)
endif
LIB_SUFFIX         = 64
endif

LIBDIR             = lib$(LIB_SUFFIX)
ISSDIR             = iss$(ISS_SUFFIX)
XTSC_RUN           = xtsc-run
ifeq ($(BUILD),Debug)
XTSC_RUN           = xtsc-rund
endif

ifeq ($(XTSC_USE_OSCI),) # {
SYSTEMC_INC        = /I $(XTTOOLSB)\include\systemc-2.3.0-static
TLM2_INC           =
SYSTEMC_LIB_PATH   = 
ifneq ($(XTSC_USE_SHARED_LIBRARY),) # {
SYSTEMC_SHR_EXT    = _sh
ADDITIONAL_DEFINES = /D SYSTEMC_DLL /D XTSC_DLL
SYSTEMC_INC        = /I $(XTTOOLSB)\include\systemc-2.3.0-shared
ifneq ($(XTSC_USE_SYSTEMC_2_3_1),) # {
SYSTEMC_INC        = /I $(XTTOOLSB)\include\systemc-2.3.1-shared
TLM2_INC           = /I $(XTTOOLSB)\include\tlm
XTSC_LIB_PATH      = /LIBPATH:$(XTTOOLSB)\$(LIBDIR)\$(ISSDIR)\systemc-2.3.1
endif # }
else # } {
ifneq ($(XTSC_USE_SYSTEMC_2_3_1),) # {
SYSTEMC_INC        = /I $(XTTOOLSB)\include\systemc-2.3.1-static
TLM2_INC           = /I $(XTTOOLSB)\include\tlm
XTSC_LIB_PATH      = /LIBPATH:$(XTTOOLSB)\$(LIBDIR)\$(ISSDIR)\systemc-2.3.1
endif # }
endif # }
ifeq ($(BUILD),Debug) # {
SYSTEMC_DBG_EXT    = d
endif # }
SYSTEMC_LIBS       = systemc$(SYSTEMC_SHR_EXT)$(SYSTEMC_DBG_EXT).lib
endif # }

XTTOOLS            = C:/././build/tree/RG-2017.5_kuma/tools/swtools-MSWin32-x86
TOOLS              = C:/././usr/xtensa/tools-7.0
TOOLSB             = C:\.\.\usr\xtensa\tools-7.0
XTTOOLSB           = C:\.\.\build\tree\RG-2017.5_kuma\tools\swtools-MSWin32-x86

ifdef RUNARGS
FWD_RUNARGS        =RUNARGS="$(RUNARGS)"
endif

ifdef XTSCRUNARGS
FWD_XTSCRUNARGS    =XTSCRUNARGS="$(XTSCRUNARGS)"
endif

TARGET_CXX         = $(XTTOOLS)/bin/xt-xcc

CXX                = cl
LINK               = link /nologo /subsystem:console /incremental:no /manifest
MT                 = mt -nologo
COMMON_CXXFLAGS    = /nologo /vmg /EHsc /GS- /Zi /c /W3 /wd4996 /wd4251
ifeq ($(BUILD),Debug) # {
COMMON_CXXFLAGS   += /MDd /Od /D _DEBUG
ifneq ($(XTSC_USE_SHARED_LIBRARY),) # {
XTSC_LIB_VER       = _shd
else # } {
XTSC_LIB_VER       = d
COMMON_CXXFLAGS   += /RTC1
endif # }
LIB_VER            = d
else # } {
COMMON_CXXFLAGS   += /MD /O2 /D NDEBUG 
ifneq ($(XTSC_USE_SHARED_LIBRARY),) # {
XTSC_LIB_VER       = _sh
endif # }
endif # }
LDFLAGS            = /DEBUG
LDOUT              = /OUT:
OBJ                = obj
EXE                = .exe

ISS_LIB_PATH       = /LIBPATH:$(XTTOOLSB)\$(LIBDIR)\$(ISSDIR)

INCLUDES           = \
        /I $(XTTOOLSB)\include \
        $(SYSTEMC_INC) \
        $(TLM2_INC)

XTENSA_LIBS        = \
        xtsc_comp$(XTSC_LIB_VER).lib \
        xtsc$(XTSC_LIB_VER).lib \
        xtmp$(LIB_VER).lib \
        log4xtensa$(LIB_VER).lib \
        xtlua$(LIB_VER).lib \
        xtparams-msvc$(LIB_VER).lib

D                  = /D

LIBS               = $(SYSTEMC_LIB_PATH) $(XTSC_LIB_PATH) $(ISS_LIB_PATH) $(XTENSA_LIBS) $(SYSTEMC_LIBS) advapi32.lib
DEFINES            = \
        $(D) CONFIG0_NAME=\"$(XTENSA0_CORE)\" \
        $(D) XTENSA0_REGISTRY=\"$(XTENSA0_SYSTEM)\" \
        $(D) TDK0_DIR=\"$(TDK0_DIR)\" \
        $(D) TARGET0_PROGRAM=\"$(TARGET0_OBJECTS)\" \
        $(D) CONFIG1_NAME=\"$(XTENSA1_CORE)\" \
        $(D) XTENSA1_REGISTRY=\"$(XTENSA1_SYSTEM)\" \
        $(D) TDK1_DIR=\"$(TDK1_DIR)\" \
        $(D) TARGET1_PROGRAM=\"$(TARGET1_OBJECTS)\" \
        $(ADDITIONAL_DEFINES)

CXXFLAGS           = $(INCLUDES) $(DEFINES) $(COMMON_CXXFLAGS)



.PHONY: all target run clean xtsc-run
BASE_NAME         := $(basename $(EXAMPLE_NAME))
ifeq ($(TERM),cygwin)
override \
RM                 = rm -f
RMDIR              = rm -rf
MV                 = mv -f
CP                 = cp -f -p
TEST               = TEST:\ \ \ 
else
RM                 = del /f/q
RMDIR              = cmd /c rd /s/q
MV                 = move /Y
CP                 = copy /Y
TEST               = TEST:   
SHELL              = cmd
endif

.PHONY: default

default: xtsc-run

all: target

target: $(TARGET0_OBJECTS)  $(TARGET1_OBJECTS) 

$(EXAMPLE_NAME): $(HOST_OBJECTS)
	$(LINK) $(LDFLAGS) $(LDOUT)$@ $(HOST_OBJECTS) $(LIBS)

$(TARGET0_OBJECTS): $(TARGET0_OBJECTS:.out=.c)
	$(TARGET_CXX) --xtensa-system=$(XTENSA0_SYSTEM) --xtensa-core=$(XTENSA0_CORE) --xtensa-params=$(TDK0_DIR) \
                      $(TARGET0_CFLAGS) -o $@ $< $(TARGET0_LIBS)

$(TARGET1_OBJECTS): $(TARGET1_OBJECTS:.out=.c)
	$(TARGET_CXX) --xtensa-system=$(XTENSA1_SYSTEM) --xtensa-core=$(XTENSA1_CORE) --xtensa-params=$(TDK1_DIR) \
                      $(TARGET1_CFLAGS) -o $@ $< $(TARGET1_LIBS)

%.$(OBJ): %.cpp
	$(CXX) $(CXXFLAGS) $<


EXAMPLE_DIR        = $(CURDIR)

VPATH              = $(EXAMPLE_DIR)

.PHONY: both
.PHONY: all.Debug all.Release run.Debug run.Release
.PHONY: all_debug all_release run_debug run_release

all_debug: all.Debug
all_release: all.Release
run_debug: run.Debug
run_release: run.Release

both: all.Debug all.Release
all: all.Debug all.Release

Debug Release:
	mkdir $@

all.Debug: Debug target
	cd Debug && $(MAKE) -f ../Makefile EXAMPLE_DIR=$(EXAMPLE_DIR) BUILD=Debug $(BASE_NAME).exe && \
	            $(MT) -manifest $(BASE_NAME).exe.manifest "-outputresource:$(BASE_NAME).exe;1" && \
	            $(CP) $(BASE_NAME).exe $(BASE_NAME)d.exe && $(MV) $(BASE_NAME)d.exe ..

all.Release: Release target
	cd Release && $(MAKE) -f ../Makefile EXAMPLE_DIR=$(EXAMPLE_DIR) BUILD=Release $(BASE_NAME).exe && \
	              $(MT) -manifest $(BASE_NAME).exe.manifest "-outputresource:$(BASE_NAME).exe;1" && \
	              $(CP) $(BASE_NAME).exe ..

run: run.Release

run.Debug:
	$(BASE_NAME)d.exe $(RUNARGS)

run.Release:
	$(BASE_NAME).exe $(RUNARGS)

clean:
	-$(RMDIR) Debug Release
	-$(RM) target\*.out
	-$(RM) *.log
	-$(RM) $(BASE_NAME).exe
	-$(RM) $(BASE_NAME).exe.manifest
	-$(RM) $(BASE_NAME)d.exe
	-$(RM) $(BASE_NAME)d.exe.manifest


info:
	-ver
	@echo CD: %CD%
	@echo PATH: %PATH%
	@echo LIBS=$(LIBS)
	@echo XTSC_USE_OSCI=$(XTSC_USE_OSCI)
	@echo XTSC_USE_SHARED_LIBRARY=$(XTSC_USE_SHARED_LIBRARY)
	@echo SYSTEMC_INC=$(SYSTEMC_INC)
	@echo TLM2_INC=$(TLM2_INC)
	@echo SYSTEMC_LIB_PATH=$(SYSTEMC_LIB_PATH)
	@echo SYSTEMC_LIBS=$(SYSTEMC_LIBS)
	@echo XTSC_LIB_PATH=$(XTSC_LIB_PATH)
	@echo XTTOOLS=$(XTTOOLS)
	@echo LIBDIR=$(LIBDIR)
	@echo ISSDIR=$(ISSDIR)
	@echo CXX=$(CXX)
	@echo CXXFLAGS=$(CXXFLAGS)
	@echo LINK=$(LINK)
	@echo LDFLAGS=$(LDFLAGS)
	@echo TARGET_CXX=$(TARGET_CXX)
	-$(CXX)


TDK0_DIR           = $(TIE0_FILE:.tie=.tdk)
TDK1_DIR           = $(TIE1_FILE:.tie=.tdk)

$(TARGET0_OBJECTS): $(TDK0_DIR)
$(TARGET1_OBJECTS): $(TDK1_DIR)

ifneq ($(TDK0_DIR),)
$(TDK0_DIR): $(TIE0_FILE)
	XTENSA_SYSTEM=$(XTENSA0_SYSTEM) XTENSA_CORE=$(XTENSA0_CORE) XTENSA_PARAMS=$(TDK0_DIR) \
	$(XTTOOLS)/bin/tc $(TCFLAGS)  -d $@ $<       
endif

ifneq ($(TDK1_DIR),)
$(TDK1_DIR): $(TIE1_FILE)
	XTENSA_SYSTEM=$(XTENSA1_SYSTEM) XTENSA_CORE=$(XTENSA1_CORE) XTENSA_PARAMS=$(TDK1_DIR) \
	$(XTTOOLS)/bin/tc $(TCFLAGS)  -d $@ $<       
endif

.PHONY: vclean
.PHONY: very.clean

vclean: very.clean
very.clean: clean
	-$(RMDIR) $(TDK0_DIR)
	-$(RMDIR) $(TDK1_DIR)

ifneq ($(strip $(XTENSA0_CORE)),$(strip $(XTENSA1_CORE)))
ifneq ($(strip $(TIE0_FILE)),)
ifeq ($(strip $(TIE0_FILE)),$(strip $(TIE1_FILE)))
$(error If XTENSA0_CORE is different than XTENSA1_CORE, then TIE0_FILE, if defined, must be different than TIE1_FILE)
endif
endif
endif

xtsc-run: target
	$(XTSC_RUN) -include=$(BASE_NAME).inc $(XTSCRUNARGS)

ifneq ($(XTSC_USE_OSCI),) # {
ifneq ($(XTSC_USE_SHARED_LIBRARY),) # {
$(error A shared library version of XTSC (including xtsc-run) may only be used with a shared library version of OSCI SystemC supplied by Tensilica.)
endif # }
endif # }

