# Simple Makefile to build FDK example passthrough project
#
# We perform the following steps:
#  * Generate an qsys_application.sopcinfo from the qsys_application.qsys
#  * Run sopcinfo.py Python script to extract the memory map into HEX files
#  * Run Quartus synthesis on the project
#
# This Makefile assumes that Quartus and the QSys tools are on the path.
#
# The auto-extraction of the memory map information from the qsys_application.sopcinfo
# requires Python >= 2.5

FDK_ROOT := $(PWD)/..
PROJECT  := fdk_example
QSYS_APP := $(FDK_ROOT)/cognento/device/deployments/applications/$(PROJECT)/qsys/qsys_application.qsys
SOPCINFO := $(FDK_ROOT)/cognento/synthesis/altera/python/sopcinfo.py
LICENSE  := $(FDK_ROOT)/board_services_license.dat:$(LM_LICENSE_FILE)

all : $(PROJECT).asm.rpt $(PROJECT).sta.rpt

clean:
	-rm -rf *.rpt
	-rm -rf *.summary
	-rm -rf db
	-rm -rf *.sopcinfo
	-rm -rf *.mif
	-rm -rf *.hex

qsys_application.sopcinfo : $(QSYS_APP) ip-generate
	ip-generate --report-file=sopcinfo:$@ --component-file=$(QSYS_APP)

memory_map_rom.hex : qsys_application.sopcinfo
	$(SOPCINFO) -m qsys_application.sopcinfo

instance_name_rom.hex : memory_map_rom.hex

$(PROJECT).map.rpt : instance_name_rom.hex memory_map_rom.hex quartus_map
	LM_LICENSE_FILE=$(LICENSE) quartus_map $(PROJECT)

$(PROJECT).fit.rpt : $(PROJECT).map.rpt
	LM_LICENSE_FILE=$(LICENSE) quartus_fit $(PROJECT)

$(PROJECT).asm.rpt : $(PROJECT).fit.rpt
	LM_LICENSE_FILE=$(LICENSE) quartus_asm $(PROJECT)

$(PROJECT).sta.rpt : $(PROJECT).fit.rpt
	LM_LICENSE_FILE=$(LICENSE) quartus_sta $(PROJECT)

# Some rules to provide semi-useful error messages if Quartus is not correctly set-up on the path
HELPFUL_ERROR_MESSAGE := "\
\\n\\nSearched $(PATH).\\n\
\\n\\nAre you sure your PATH is correctly configured?\
\\n\\nUse the following example to correctly set your path:\\n\
\\texport QUARTUS_ROOTDIR=/path/to/quartus/install\\n\
\\texport PATH=\$$QUARTUS_ROOTDIR/nios2eds:\$$QUARTUS_ROOTDIR/quartus/bin:\$$QUARTUS_ROOTDIR/quartus/sopc_builder/bin:\$$PATH\\n\\n"


.PHONY: ip-generate
ip-generate :
	@if ! which $@ &>/dev/null; then echo -e "\ncommand \"ip-generate\" not found!\n$(HELPFUL_ERROR_MESSAGE)"; $@; fi


.PHONY: quartus_map
quartus_map :
	@if ! which $@ &>/dev/null; then echo -e "\ncommand \"ip-generate\" not found!\n$(HELPFUL_ERROR_MESSAGE)"; $@; fi

