# Makefile -- House of cards, the soundcard card on the Mercury+ KX2 / PE1 (the receiver's side; README.md)
#   make check     the images' checksums (baseline/ and images/ SHA256SUMS), when the card carries images
#   make bit       synthesis, placement, routing of verilog/$(NAME) with Vivado: verilog/$(NAME)/top.bit (an hour or less)
#   make prog      load IMAGE (the baseline / demo image) over JTAG (volatile; IMAGE=verilog/$(NAME)/top: your own build)
#   make flash     write IMAGE.mcs into the KX2's QSPI flash (S25FL512S), the flash read back to
#                  flash_backup.bin first -- the card must be in the flash when the PC's BIOS scans the bus
#   (scripts/flash_baseline.sh NAME and scripts/jtag_load.sh IMAGE.bit do the same by name; docs/)
# 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), default the local one.
IMAGE   ?= images/soundcard_kx2_avb_g2
NAME    ?= avb_g2
HW_SERVER ?=
VIVADO  ?= vivado
.PHONY: help check images baseline baseline-dry wireguardsetup load bit prog flash
help:
	@echo "House of cards -- the soundcard card on the Mercury+ KX2 / PE1 (README.md; docs/FLASH.md)"
	@echo ""
	@echo "  make check                       the images' checksums"
	@echo "  make images                      the images this archive carries (baseline/, images/)"
	@echo "  make load NAME=n                 load image n over JTAG -- volatile (a warm reboot keeps it, power-off loses it)"
	@echo "  make baseline [NAME=n] [PEERS=f] write baseline n (default $(NAME)) into the KX2's flash (backup first)"
	@echo "                                   PEERS=f (WireGuard): f is MADE by a few questions if it does not exist"
	@echo "                                   the card's PUBLIC key is printed at the end (CARD_KEY=1: the card makes"
	@echo "                                   its key itself at its first boot, the key then only on its console)"
	@echo "  make baseline-dry NAME=n [PEERS=f]  the same as a dry run: checksums, areas, backups -- nothing written"
	@echo "  make wireguardsetup [PEERS=f]    print the WireGuard setup commands again (peer, card settings, PC)"
	@echo "  make bit [NAME=n]                rebuild verilog/$(NAME)/top.bit with Vivado (an hour or less)"
	@echo "  make prog | make flash [IMAGE=f]  load / flash a file by path (default $(IMAGE); IMAGE=verilog/$(NAME)/top: your build)"
	@echo ""
	@echo "  options: HW_SERVER=host:port (only when the Platform Cable sits on another machine; default the local one)"
	@echo "           VIVADO=/path/to/vivado (default: vivado on the PATH -- Vivado's settings64.sh)"
	@echo ""
	@[ ! -f baseline/network_kx2_gd2.mcs ] || { echo "  example -- the gigabit network card into the flash (the default), then a full power cycle of the PC:"; \
	   echo "      make baseline        (= make baseline NAME=gd2)"; echo ""; }
	@[ ! -f baseline/network_kx2_wg100.manifest ] || { echo "  example -- the WireGuard appliance: the bitstream, the card's firmware and its key store in one go"; \
	   echo "  (wg-peers.conf is made by questions if it does not exist: the card's IP, each peer's public key, ...;"; \
	   echo "   the configuration secret is shown once, the card's public key at the end):"; \
	   echo "      make baseline NAME=wg100 PEERS=wg-peers.conf"; echo ""; }
	@[ ! -d images ] || { echo "  example -- load the demo image over JTAG while the PC runs Linux:"; \
	   echo "      make load NAME=$$(ls images/*.bit 2>/dev/null | head -1 | sed 's|images/||; s|\.bit$$||; s|^[a-z]*_kx2_||')"; echo ""; }
images:
	@for d in baseline images; do [ -f $$d/SHA256SUMS ] || continue; for f in $$d/*.bit; do n=$$(basename $$f .bit); \
	   printf "  %-34s %s\n" "$$(echo $$n | sed 's/^[a-z]*_kx2_//')" "$$( [ -f $$d/$$n.manifest ] && echo '+ firmware + key store (PEERS=...)' )"; done; done
baseline:
	@[ -n "$(NAME)" ] || { echo "baseline: NAME=... (make images)"; exit 1; }
	VIVADO=$(VIVADO) sh scripts/flash_baseline.sh $(NAME) $(HW_SERVER) $(if $(PEERS),--peers $(PEERS)) $(if $(CARD_KEY),--card-key)
baseline-dry:
	@[ -n "$(NAME)" ] || { echo "baseline-dry: NAME=... (make images)"; exit 1; }
	VIVADO=$(VIVADO) sh scripts/flash_baseline.sh --dry-run $(NAME) $(HW_SERVER) $(if $(PEERS),--peers $(PEERS)) $(if $(CARD_KEY),--card-key)
wireguardsetup:
	@python3 host/l8/bus/pcie/designs/network/host/wgks_provision.py setup-cmds --peers $(or $(PEERS),wg-peers.conf) $(if $(PUB),--pub $(PUB))
load:
	@[ -n "$(NAME)" ] || { echo "load: NAME=... (make images)"; exit 1; }
	@b=$$(ls baseline/*_$(NAME).bit images/*_$(NAME).bit 2>/dev/null | head -1); [ -n "$$b" ] || { echo "load: no image $(NAME)"; exit 1; }; \
	 echo VIVADO=$(VIVADO) sh scripts/jtag_load.sh $$b $(HW_SERVER); VIVADO=$(VIVADO) sh scripts/jtag_load.sh $$b $(HW_SERVER)
check:
	@n=0; for d in baseline images; do [ -f $$d/SHA256SUMS ] || continue; n=1; (cd $$d && sha256sum -c SHA256SUMS) || exit 1; done; \
	 [ $$n = 1 ] || echo "check: no image for this card yet (make bit builds one)"
bit:
	@[ -n "$(VIVADO)" ] || { echo "bit: no vivado (its settings64.sh, or VIVADO=/path/to/vivado)"; exit 1; }
	@[ -f verilog/$(NAME)/top.tcl ] || { echo "bit: no verilog/$(NAME)/top.tcl -- the Verilog 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
prog: check
	@[ -n "$(IMAGE)" ] || { echo "prog: say which image -- make load NAME=n (make images lists them), or make prog IMAGE=verilog/$(NAME)/top (your own build)"; exit 1; }
	@[ -n "$(VIVADO)" ] || { echo "prog: no 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
flash: check
	@[ -n "$(IMAGE)" ] || { echo "flash: say which image -- make baseline NAME=n (make images lists them), or make flash IMAGE=baseline/<file without .mcs>"; exit 1; }
	@[ -n "$(VIVADO)" ] || { echo "flash: no vivado"; exit 1; }
	$(if $(HW_SERVER),L8_HW_SERVER=$(HW_SERVER)) $(VIVADO) -mode batch -nojournal -nolog -source scripts/flash_kx2.tcl \
	  -tclargs $(IMAGE).mcs flash_backup.bin
