
HEXAGON_TOOLS_INSTALL_DIR ?= /local/tools/hexagon/8.1.03


HEXAGON_TOOLS_EXAMPLES = $(HEXAGON_TOOLS_INSTALL_DIR)/Examples
HEXAGON_LIBNATIVE_INC = $(HEXAGON_TOOLS_INSTALL_DIR)/Tools/libnative/include
HEXAGON_LIBNATIVE_LIB = $(HEXAGON_TOOLS_INSTALL_DIR)/Tools/libnative/lib
HEXAGON_BIN_DIR = $(HEXAGON_TOOLS_INSTALL_DIR)/Tools/bin
HEXAGON_SIM = $(HEXAGON_BIN_DIR)/hexagon-sim 
HEXAGON_PROF = $(HEXAGON_BIN_DIR)/hexagon-profiler


HEX_UTILS_H ?= ../../../../hexagon/include
SQUISH_QDSP ?= ../../../../hexagon/util/squish_qdsp_asm.py
SRC = ..

# set tcm size to 128k
TCM_SIZE	= 0x20000
STACK_SIZE	= 0x4000
HEAP_SIZE	= 0x10000

TCM_BASE_ADDR = 0xd8000000

LINKFLAGS = -Wl,--defsym,TCM_BASE_ADDR=$(TCM_BASE_ADDR)


HEX_AS ?= $(HEXAGON_BIN_DIR)/hexagon-llvm-mc
HEX_CPP ?= $(HEXAGON_BIN_DIR)/hexagon-clang
HEX_CPPLD ?= $(HEXAGON_BIN_DIR)/hexagon-clang++ -mv65
BASE_HEX_CPP_OPTS = -O2 -fno-cxx-exceptions -DNDEBUG=1 -fno-exceptions -fno-use-cxa-atexit -fno-rtti  $(HH)

HH = -I .. -I$(HEX_UTILS_H)

GPP ?= g++
GPP_LN_OPTS = -O2 -Wall -Wno-unused-variable -Wno-unused-function $(HH) -I$(HEXAGON_LIBNATIVE_INC)
GPPLD ?= g++
GPPLD_OPTS = 

TWHVX2_OBJS = $(addsuffix .HVX2.o, test_warp warp_block8_hvx rectangle_copy) 
BASE_HEX_CPP_OPTS += -mv65

# -mhvx -mhvx-double should not both be there.
HEX_CPP_OPTS_HVX2 = $(BASE_HEX_CPP_OPTS) -mhvx-double

# Lines with double '#' are output by 'make help'.
##
## executable targets:
##      test_warp.HVX2.out  : hexagon exec for V128
##      test_warpLN2 :           libnative (for V128)
##
##       Note that each of the above writes a different ppm filename for the result:
##          tresult_hvx2.ppm  tresult_native2.ppm
##
##     binaries    -   builds both  of the executables.
##
## Targets which run things (after building the executables):
##           (you can use e.g: make TESTWARP_OPTS="-w2 -p8" runsim
##            to pass extra options to the executable)
##
##       runsim       -  run test_warp.HVX2.out in simulator (no timing). ppm output file will be stored
##
##       runsweep     -   run with timing, sweep across all warps and all row alignments, record runtime for each case in file.
##                                    results are in sweep2.log resp.
## 
##       prof128.html   - these create profile reports for a single warp. Writing the output file is skipped.
##
##
## The libnative build uses its own emulation of vgather.
## Results don't match exactly between V64/V128, but should match exactly between hexagon and libnative.
##
## The 'test_warp' program reads a raw 256x256 4:2:0 image from lena_nv12_256.raw, does 
## a warp operation, and then writes the 256x256 result as .ppm file. The cycle count is reported
## for the core warp operation (starting from when the source data is in vtcm, and ending when the
## complete 4:2:0 result is generated in the output buffer).
##
##  Command line parameters to test_warp (may be out of date; use "./test_warpLN -h" for latest):
##
## options are:
##   -q          quiet - reduce printing, and skip writing result.
##   -w0 -w1 -w2 -w3   select warp option - default is -w1 ; -w0 is a null warp
##   -pnnn          set row pitch skew to nnn (0..128; must be even)
##  (the remainder are ignored on libnative build:)
##   -t             only one thread
##   -s             sweep skew from 0 to 128 and log times to sweeprun.log
##
##
help:
	@grep '#''#' Makefile

binaries: test_warpLN2 test_warp.HVX2.out

test_warp.HVX2.out: $(TWHVX2_OBJS) 
	$(HEX_CPPLD) -o $@ $(LINKFLAGS) $(TWHVX2_OBJS) -Wl,-Map,test_warp.map  -lhexagon

#
# note: TESTWARP_OPTS is intended to be set on the make command line for these
# targets.
#
runsim: test_warp.HVX2.out
	$(HEXAGON_SIM) $< -- $(TESTWARP_OPTS)

#
# run a 'timing' test, sweeping across all pitch skew and all warps
# takes about 11 mins on my machine (about 3.3 seconds per case) for the HVX2.
#
#
runsweep: test_warp.HVX2.out
	$(HEXAGON_SIM) --timing  $< -- -q -s $(TESTWARP_OPTS) && mv sweeprun.log sweep2.log

#
# run a single warp with profiling
#

prof128.html: test_warp.HVX2.out
	$(HEXAGON_SIM) --timing --packet_analyze pa2.json $< -- -q $(TESTWARP_OPTS)
	$(HEXAGON_PROF) --packet_analyze --elf=$< --json=pa2.json -o prof128.html

runLN2: test_warpLN2
	./$<


warp_block8_hvx.HVX2.o: $(SRC)/warp_block8_hvx.cpp $(SRC)/hvx_warp_defs.hpp
	$(HEX_CPP) -o $@ -c $(HEX_CPP_OPTS_HVX2) $<
warp_block8_hvx.DBL.s: $(SRC)/warp_block8_hvx.cpp $(SRC)/hvx_warp_defs.hpp
	$(HEX_CPP) -o $@ -S $(HEX_CPP_OPTS_HVX2)  $<
	python $(SQUISH_QDSP) $@
warp_block8_hvx.LN2.o: $(SRC)/warp_block8_hvx.cpp $(SRC)/hvx_warp_defs.hpp
	$(GPP)  -o $@ -c  -D__HVXDBL__=1 $(GPP_LN_OPTS) $<


rectangle_copy.HVX2.o: $(SRC)/rectangle_copy.S
	$(HEX_CPP) -o $@ -c $(HEX_CPP_OPTS_HVX2) $<



test_warp.LN2.o: test_warp.cpp $(SRC)/hvx_warp_defs.hpp
	$(GPP) -o $@ -c $(GPP_LN_OPTS) -D__HVXDBL__=1 $<
test_warp.HVX2.o: test_warp.cpp $(SRC)/hvx_warp_defs.hpp
	$(HEX_CPP) -o $@ -c -I$(HEXAGON_TOOLS_EXAMPLES)/HVX/common/include  $(HEX_CPP_OPTS_HVX2) $<



test_warpLN2 : test_warp.LN2.o warp_block8_hvx.LN2.o
	$(GPPLD)  -o $@  -L$(HEXAGON_LIBNATIVE_LIB) test_warp.LN2.o warp_block8_hvx.LN2.o -lnative 

clean:
	rm -f *.o





