# Makefile -- the White Rabbit sync demo on the Mercury+ KX2 / PE1 (the receiver's side; README.md)
#   make check            the images' checksums (images/SHA256SUMS)
#   make load NAME=s1|s2  load an image over JTAG -- VOLATILE (power-off loses it; the flash is never touched)
#   make prog IMAGE=f     load a bitstream by path, without .bit (IMAGE=verilog/s2/top: your own build)
#   make bit NAME=s1|s2   Vivado: synthesis, placement, routing of verilog/NAME -> verilog/NAME/top.bit
#   make proxy            the JTAG proxy (holds the cable; run it in the background while reading the words)
#   make watch | status   the design's words every second / once (wr_demo.py --jtag)
#   make loop-on | loop-off   the loop of STAGE 2 on / off (control word 0 bit 0)
#   make map              the word map, nothing contacted
# Vivado: on the PATH (Vivado's settings64.sh), or VIVADO=/path/to/vivado -- nothing else is searched.
# HW_SERVER: Vivado's hardware server (host:port) for load / prog, default the local one.  The proxy uses the local cable.
NAME      ?= s2
IMAGE     ?=
HW_SERVER ?=
VIVADO    ?= vivado
PYTHON    ?= python3
DEMO      = $(PYTHON) host/l8/communication/wr/host/wr_demo.py
STEM      = images/wr_standalone_kx2_
.PHONY: help check images load prog bit proxy watch status loop-on loop-off map
help:
	@echo "The White Rabbit sync demo -- two clocks on one Mercury+ KX2 / PE1, one locked to the other (README.md)"
	@echo ""
	@echo "  make check                  the images' checksums"
	@echo "  make images                 the images this archive carries"
	@echo "  make load NAME=s1|s2        load an image over JTAG -- volatile (power-off loses it)"
	@echo "  make prog IMAGE=f           load f.bit (IMAGE=verilog/s2/top: your own build)"
	@echo "  make bit NAME=s1|s2         rebuild verilog/NAME/top.bit with Vivado"
	@echo "  make proxy                  the JTAG proxy, holding the cable (run it with &; kill %1 stops it)"
	@echo "  make watch | make status    the words every second / once"
	@echo "  make loop-on | loop-off     STAGE 2: the loop on / off"
	@echo "  make map                    the word map (no board)"
	@echo ""
	@echo "  options: VIVADO=/path/to/vivado (default: vivado on the PATH)   HW_SERVER=host:port (load / prog)"
	@echo ""
	@echo "  example -- STAGE 1, the clocks alone (no Ethernet cable):"
	@echo "      make check; make load NAME=s1; make proxy & make watch"
	@echo "  example -- STAGE 2 (stop the proxy first: kill %1), the cable Eth0 <-> Eth1 plugged in:"
	@echo "      make load NAME=s2; make proxy &"
	@echo "      make watch                (the loop off: the phase walks)"
	@echo "      make loop-on; make watch | tee run-loop-on.log    (LOCKED; the monitor's ppm 0, the phase still)"
	@echo ""
images:
	@[ -f images/SHA256SUMS ] || { echo "images: none in this archive (make bit NAME=s1 builds one)"; exit 0; }; \
	 for f in $(STEM)*.bit; do n=$$(basename $$f .bit); echo "  $${n#wr_standalone_kx2_}   $$f"; done
check:
	@[ -f images/SHA256SUMS ] || { echo "check: no images in this archive"; exit 0; }; cd images && sha256sum -c SHA256SUMS
load: check
	@[ -f $(STEM)$(NAME).bit ] || { echo "load: no image $(NAME) (make images); make bit NAME=$(NAME) then make prog IMAGE=verilog/$(NAME)/top"; exit 1; }
	$(MAKE) --no-print-directory prog IMAGE=$(STEM)$(NAME)
prog:
	@[ -n "$(IMAGE)" ] || { echo "prog: IMAGE=path without .bit (make load NAME=s1|s2 for the archive's images)"; exit 1; }
	@[ -f $(IMAGE).bit ] || { echo "prog: no $(IMAGE).bit"; exit 1; }
	@command -v $(VIVADO) >/dev/null 2>&1 || { echo "prog: no vivado (its settings64.sh, or VIVADO=/path/to/vivado)"; exit 1; }
	$(if $(HW_SERVER),sed 's|localhost:3121|$(HW_SERVER)|' scripts/jtag_prog.tcl > scripts/.prog.tcl,cp scripts/jtag_prog.tcl scripts/.prog.tcl)
	$(VIVADO) -mode batch -nojournal -nolog -source scripts/.prog.tcl -tclargs $(IMAGE).bit
	@rm -f scripts/.prog.tcl
bit:
	@[ -f verilog/$(NAME)/top.tcl ] || { echo "bit: no verilog/$(NAME)/top.tcl -- the sets:"; ls verilog; exit 1; }
	$(MAKE) -C verilog/$(NAME) synth VIVADO=$(VIVADO) NAME=top    # NAME there is the flow's file stem (top.tcl), not ours
proxy:
	cd host/l8/net/host && $(PYTHON) -m l8net.jtag_proxy --lag auto $(if $(filter-out vivado,$(VIVADO)),--vivado $(VIVADO))
watch:
	$(DEMO) --jtag watch
status:
	$(DEMO) --jtag status
loop-on:
	$(DEMO) --jtag set loop_enable 1
loop-off:
	$(DEMO) --jtag set loop_enable 0
map:
	$(DEMO) --dry-run
