curr_dir:=$(abspath $(dir $(lastword $(MAKEFILE_LIST))))

##############################
###  OPENTITAN_9d82960888  ###
##############################
VENV_OT_9d82960888 = ${root_dir}/venv-opentitan_9d82960888
OPENTITAN_9d82960888 = ${root_dir}/../third_party/opentitan_9d82960888

${OPENTITAN_9d82960888}/.gitpatch:
	cd ${OPENTITAN_9d82960888} && git apply ${curr_dir}/0001_Add_opentitan_patch_for_uhdm.patch && touch $@

${VENV_OT_9d82960888}:
	virtualenv ${VENV_OT_9d82960888}
	. ${VENV_OT_9d82960888}/bin/activate && \
	pip install -r ${OPENTITAN_9d82960888}/python-requirements.txt && \
	pip install git+https://github.com/antmicro/edalize@svplugin_support

uhdm/yosys/synth-opentitan-build: clean-build | ${OPENTITAN_9d82960888}/.gitpatch ${curr_dir}/boot_rom_fpga_nexysvideo.32.vmem ${VENV_OT_9d82960888}
	(export PATH=${root_dir}/../image/bin:${PATH} && \
		. ${VENV_OT_9d82960888}/bin/activate && \
		fusesoc --cores-root=${OPENTITAN_9d82960888} run --flag=fileset_ip --build --tool vivado --target=synth lowrisc:systems:top_earlgrey_nexysvideo --BootRomInitFile="${curr_dir}/boot_rom_fpga_nexysvideo.32.vmem")

#################
### OPENTITAN ###
#################
VENV_OT_SYNTH = ${root_dir}/venv-opentitan-synth
OPENTITAN_SYNTH_PATCHES_DIR = ${curr_dir}/opentitan_synth_patches
OPENTITAN = ${root_dir}/../third_party/opentitan

TOP := lowrisc:systems_custom_tiny:chip_custom_tiny_nexysvideo:0.1
TOP_HW_DIR := ${OPENTITAN}/hw/top_custom_tiny
TOP_HJSON := ${OPENTITAN}/hw/top_custom_tiny/data/top_custom_tiny.hjson

${OPENTITAN}/.gitpatch:
	cd ${OPENTITAN} && git apply ${curr_dir}/0001_Add_core_files_for_processing_Earlgrey_in_Yosys.patch && git apply ${curr_dir}/0002_use_Edalize_fork_for_SystemVerilog_in_Yosys.patch && touch $@

${OPENTITAN}/.gitpatch_synth:
	cd ${OPENTITAN} \
		&& git apply ${OPENTITAN_SYNTH_PATCHES_DIR}/*.patch \
		&& touch $@

${VENV_OT_SYNTH}: | ${OPENTITAN}/.gitpatch_synth
	virtualenv ${VENV_OT_SYNTH}
	(. ${VENV_OT_SYNTH}/bin/activate && \
		pip install -r ${OPENTITAN}/python-requirements.txt && \
		pip install git+https://github.com/antmicro/edalize@svplugin_support)

uhdm/yosys/synth-opentitan-build-tiny: clean-build | ${OPENTITAN}/.gitpatch_synth ${VENV_OT_SYNTH}
	(export PATH=${root_dir}/../image/bin:${PATH} && \
		. ${VENV_OT_SYNTH}/bin/activate && \
		fusesoc --cores-root=${OPENTITAN} run --flag=fileset_top --flag=ot_is_custom_tiny --build --tool yosys --target=synth ${TOP})

