curr_dir:=$(abspath $(dir $(lastword $(MAKEFILE_LIST))))
###################
### BLACKPARROT ###
###################
BLACKPARROT = ${root_dir}/../third_party/black_parrot
BLACKPARROT_PATCHES_DIR = ${curr_dir}/black_parrot_patches/
BLACKPARROT_CFG ?= e_bp_unicore_cfg
BLACKPARROT_UHDM = ${root_dir}/build/surelog/bp_tethered.${BLACKPARROT_CFG}.none.parse/out/slpp_unit/surelog.uhdm

export BP_TOOLS_DIR = $(BLACKPARROT)/../black_parrot_tools
export BP_SDK_DIR = $(BLACKPARROT)/../black_parrot_sdk

${BLACKPARROT}/.gitpatch:
	cd ${BLACKPARROT}/ && git apply ${BLACKPARROT_PATCHES_DIR}/*.patch && touch $@

${BLACKPARROT_UHDM}: clean-build | ${BLACKPARROT}/.gitpatch
	(export PATH=${root_dir}/../image/bin:${PATH} && \
		cd ${BLACKPARROT}/bp_top/syn/ && SURELOG_OPTS="-synth -DSYNTHESIS=1" $(MAKE) parse.surelog CFG=${BLACKPARROT_CFG} RESULTS_PATH=${root_dir}/build)

uhdm/yosys/synth-blackparrot-build: | ${BLACKPARROT_UHDM}
	(export PATH=${root_dir}/../image/bin:${PATH} && \
		cd ${root_dir}/build && \
		yosys -p "plugin -i systemverilog" \
                      -p "read_uhdm ${BLACKPARROT_UHDM}" \
                      -p "synth_xilinx -iopad -family xc7" \
                      -p "write_edif bp_${BLACKPARROT_CFG}.edif")
