# Makefile — generated by `l8i --emit vivado,mercury_kx2_pe1`.
# Vivado builds the bitstream; the board's own programmer loads it.
# Regenerate rather than edit.
NAME    = top
DESIGN  = tsac_kx2.v
PART    = xc7k160tffg676-2

# A VENDOR toolchain is FOUND, never fetched ([board-34]).  On PATH, where
# the vendor's own settings64.sh points XILINX_VIVADO, or where the
# installer actually put it: a person who has installed Vivado has done
# their part, and asking them to source a script before every build is a
# step to forget.  The newest version wins.
VIVADO ?= $(shell command -v vivado 2>/dev/null || \
            ([ -x "$(XILINX_VIVADO)/bin/vivado" ] && echo "$(XILINX_VIVADO)/bin/vivado") || \
            ls -1d /fast4/xilinx/*/Vivado/bin/vivado /opt/Xilinx/*/Vivado/bin/vivado \
                   /tools/Xilinx/*/Vivado/bin/vivado $(HOME)/Xilinx/*/Vivado/bin/vivado \
               2>/dev/null | sort -V | tail -1)
OSS_PREFIX ?= /home/eiselekd/git/l8/use/toolchain
ZTEX_SDK   ?= $(OSS_PREFIX)/ztex
FWLOADER   ?= $(ZTEX_SDK)/java/FWLoader/FWLoader.jar

need = @command -v $(1) >/dev/null || { echo "$(1) is not installed.  run: make toolchain"; exit 1; }
needvivado = @[ -n "$(VIVADO)" ] || { \
	  echo "vivado not found.  Install it and source its settings64.sh,"; \
	  echo "or run: make VIVADO=/path/to/Vivado/bin/vivado"; exit 1; }

# `synth` is what a person types, `build` the vendor's word ([board-33]).
all: synth
build: synth
synth: $(NAME).bit

$(NAME).bit: $(DESIGN) $(NAME).v $(NAME).xdc $(NAME).tcl
	$(needvivado)
	$(VIVADO) -mode batch -source $(NAME).tcl -nojournal -log $(NAME)_vivado.log

# PROBES ([debug-28]): the same build with a capture core per `probe`, then
# `make capture PROBE=name [TRIGGER=field] [DEPTH=n]` brings the rows back
# over JTAG into capture/<name>.csv.  An ordinary `synth` carries no core.
synth-probes:
	$(needvivado)
	L8_PROBES=1 $(VIVADO) -mode batch -source $(NAME).tcl -nojournal -log $(NAME)_vivado.log

PROBE ?=
TRIGGER ?=
DEPTH ?= 4096
capture:
	$(needvivado)
	@[ -s $(NAME).ltx ] || { echo "no $(NAME).ltx: run make synth-probes first"; exit 1; }
	$(VIVADO) -mode batch -source $(NAME)_capture.tcl -nojournal -nolog -tclargs "$(PROBE)" "$(TRIGGER)" "$(DEPTH)"

# The board's FTDI is a generic cable to openFPGALoader ([board_kx2-06]).
# SERIAL=… picks one FTDI when several are plugged in.
OFL ?= openFPGALoader
OFL_CABLE ?= ft2232
OFL_ARGS ?= $(if $(SERIAL),--ftdi-serial $(SERIAL))

detect:
	$(call need,$(OFL))
	$(OFL) -c $(OFL_CABLE) $(OFL_ARGS) --detect

prog: $(NAME).bit
	$(call need,$(OFL))
	$(OFL) -c $(OFL_CABLE) $(OFL_ARGS) $<

# What the TOOLS made, not what the compiler emitted ([board-33]).
clean:
	rm -rf $(NAME).bit $(NAME)*.dcp $(NAME)*.rpt $(NAME)_vivado.log \
	  top.cache top.hw top.ip_user_files top.runs top.sim top.xpr .Xil

toolchain:
	@PREFIX=$(OSS_PREFIX) sh get_toolchain.sh

toolchain-check:
	@PREFIX=$(OSS_PREFIX) sh get_toolchain.sh --check

.PHONY: all synth build prog prog-flash firmware devices detect clean toolchain toolchain-check
