SOURCE_FILES  := main.cpp  ../software_demo/SampleFilter.cpp
override HLS_CXX_FLAGS := $(HLS_CXX_FLAGS) -ffp-contract=fast -ffp-reassociate
CXX := i++
DEVICE := 5CSEBA6U23I7

VERBOSE := 1

override CXXFLAGS := $(CXXFLAGS) -I ../software_demo -lwave -L../wavelib/lib
#override CXXFLAGS := $(CXXFLAGS) -I ../software_demo 

# Any tools installed with HLS can be found relative to the location of i++
intelFPGA := /comelec/softs/opt/altera/current
#intelFPGA := /opt/comelec/intelFPGA/current
HLS_INSTALL_DIR := ${intelFPGA}/hls


# OS-dependant tools
RM  := rm -rf

# COMELEC
export PATH    := ${intelFPGA}/hls/bin:/comelec/softs/bin:${PATH}
export LD_LIBRARY_PATH := ${intelFPGA}/hls/host/linux64/lib:../wavelib/lib

.PHONY: help
help:
	@echo "make test-gpp                     : GPP  : Compile the component and testbench using g++"
	@echo "make gpp_gen_filtered_wave        : GPP  : run the test to filter the noisy wave file"
	@echo "make gpp_gen_short_filtered_wave  : GPP  : run the test to filter the SHORT noisy wave file"
	@echo "make test-fpga                    : FPGA : Synthesise the component to Verilog, and prepare simulation"
	@echo "make fpga_gen_short_filtered_wave : FPGA : run the test to filter the SHORT noisy wave file" 
	@echo "make show-waves                   : FPGA : Examine simulation waveforms"
	@echo "make show-syn-results             : FPGA : Examine the synthesis results"
	@echo "make test-fpga-quartus            : FPGA : Run Quartus synthesis for performance evaluation"
	@echo "make play_original_wave           : play the original wave file without sine noise."
	@echo "make play_noisy_wave              : play the wave file including a 8kHz sine wave."
	@echo "make play_gpp_filtered_wave       : GPP  : play the filtered file including a 8kHz sine wave."
	@echo "make play_fpga_filtered_wave      : FPGA : play the filtered file including a 8kHz sine wave."
	


play_original_wave: ../waves/piano2_ref.wav
	mpv $<

play_noisy_wave: ../waves/piano2_noisy.wav
	mpv $<

play_gpp_filtered_wave: ../waves/gpp_piano2_filtered.wav
	mpv $<

play_fpga_filtered_wave: ../waves/fpga_piano2_filtered.wav
	mpv $<

../waves/gpp_piano2_filtered.wav: test-gpp ../waves/piano2_noisy.wav
	./test-gpp  ../waves/piano2_noisy.wav $@

../waves/gpp_piano2_short_filtered.wav: test-gpp ../waves/piano2_short_noisy.wav
	./test-gpp  ../waves/piano2_short_noisy.wav $@

../waves/fpga_piano2_short_filtered.wav: test-fpga ../waves/piano2_short_noisy.wav
	./test-fpga  ../waves/piano2_short_noisy.wav $@
	@cp -f  test-fpga.prj/verification/vsim.wlf .

.PHONY: gpp-gen_filtered_wave
gpp_gen_filtered_wave: ../waves/gpp_piano2_filtered.wav

gpp_gen_short_filtered_wave: ../waves/gpp_piano2_short_filtered.wav

fpga_gen_short_filtered_wave: ../waves/fpga_piano2_short_filtered.wav


# Compile the component and testbench using g++ and run them as a regular program
test-gpp: CXX := g++
test-gpp: CXXFLAGS := $(CXXFLAGS) -std=c++17 -I"$(HLS_INSTALL_DIR)/include" -L"$(HLS_INSTALL_DIR)/host/linux64/lib" -lhls_emul -o test-gpp
test-gpp: $(SOURCE_FILES)
	$(CXX) $(SOURCE_FILES) $(CXXFLAGS)


# HLS Synthesis of the component and generation of a cosimulation program
# + patch for University Program IP compatibility bugs
ifeq ($(VERBOSE),1)
  test-fpga: CXXFLAGS := $(CXXFLAGS) -v
endif
test-fpga: CXXFLAGS := $(CXXFLAGS) --gcc-toolchain=/usr  $(HLS_CXX_FLAGS) -march=$(DEVICE) -o test-fpga -ghdl=1
test-fpga: $(SOURCE_FILES)
	$(CXX) $(SOURCE_FILES) $(CXXFLAGS)
	sed -i -e 's/firstSymbolInHighOrderBits 0/firstSymbolInHighOrderBits 1/g' ./test-fpga.prj/components/audio_filter/audio_filter_internal_hw.tcl

test-fpga.prj/reports/report.html: fpga_gen_short_filtered_wave

# Show the simulation waveforms
.PHONY: show-waves
show-waves: vsim.wlf
	@vsim -view $< 

# Perform a quartus synthesis in order to extract timing and area information
PHONY: test-fpga-quartus
test-fpga-quartus: .test-fpga-quartus
test-fpga.prj/quartus/quartus_compile.sv: test-fpga

.test-fpga-quartus: test-fpga.prj/quartus/quartus_compile.sv
	cd test-fpga.prj/quartus && \
	quartus_map --read_settings_files=on --write_settings_files=off quartus_compile -c quartus_compile && \
	quartus_cdb --read_settings_files=off --write_settings_files=off quartus_compile -c quartus_compile --merge=on && \
	quartus_fit --read_settings_files=off --write_settings_files=off quartus_compile -c quartus_compile && \
	quartus_sta quartus_compile -c quartus_compile && \
	quartus_sh -t generate_report.tcl -revision quartus_compile  quartus_compile
	touch $@

test-fpga.prj: test-fpga

# Show the synthesis results
.PHONY: show-syn-results
show-syn-results: ./test-fpga.prj/reports/report.html
	@chromium $< &


# Clean up temprary and delivered files
CLEAN_FILES := test-gpp \
               test-gpp.prj \
               test-fpga \
               test-fpga.prj \
			   .test-fpga-quartus \
			   transcript \
		       vsim.wlf  \
			   ../waves/gpp_piano2_filtered.wav \
			   ../waves/gpp_piano2_short_filtered.wav \
			   ../waves/fpga_piano2_short_filtered.wav 		   


clean:
	-$(RM) $(CLEAN_FILES)
