# Makefile -- the PIC16F877A on a PICDEM 2 Plus, on the Mercury+ KX2 / PE1 (README.md).  `make` alone prints this list.
#   make prog                 load bit/top.bit into the FPGA (openFPGALoader -c ft2232, volatile; SERIAL=... picks one FTDI)
#   make detect               list the JTAG chain (is the module's USB plugged in?)
#   make programs             assemble the example programs (programs/*.asm, gpasm from gputils) -> out/*.hex
#   make gpsim                each example on gpsim, checked against its expectations (programs/gpsim/*.stc)
#   make upload HEX=f.hex [PORT=/dev/ttyUSB1]   a program through the bootloader, over the PE1's micro-USB serial, J603 (press
#                             PE1 button 0, MCLR, when it asks): no Vivado, no reload of the FPGA
#   make host-lcd [IP=192.168.77.3]   the LCD's screen on the PC, the pot from the file $$PD2_POT (over Ethernet)
#   make image HEX=f.hex      the bootloader + a program into the design's program memory (build/, scripts/hex2v.py)
#   make bit                  rebuild the bitstream from build/ with Vivado -> build/top.bit (then: make prog BIT=build/top.bit)
#   make core-sim             the core alone under Verilator: core/tb (a program through TXREG, the debug ID register)
BIT     ?= bit/top.bit
OUT     := $(CURDIR)/out
OFL     ?= openFPGALoader
OFL_ARGS ?= $(if $(SERIAL),--ftdi-serial $(SERIAL))
GPASM   ?= gpasm
GPSIM   ?= gpsim
DESIGN  := build/p877a_kx2_picdem2_img.v
PROGS   := $(patsubst programs/%.asm,%,$(wildcard programs/*.asm))
# Vivado: on the PATH, XILINX_VIVADO (its settings64.sh), or where the installer put it (the newest wins)
VIVADO  ?= $(shell command -v vivado 2>/dev/null || \
             ([ -x "$(XILINX_VIVADO)/bin/vivado" ] && echo "$(XILINX_VIVADO)/bin/vivado") || \
             ls -1d /opt/Xilinx/*/Vivado/bin/vivado /tools/Xilinx/*/Vivado/bin/vivado $(HOME)/Xilinx/*/Vivado/bin/vivado \
                    2>/dev/null | sort -V | tail -1)
.PHONY: help prog detect programs gpsim image bit run core-sim boot upload host-lcd

help:
	@sed -n '2,12p' Makefile

detect:
	@command -v $(OFL) > /dev/null || { echo "detect: no openFPGALoader (apt install openfpgaloader, or build it)"; exit 1; }
	$(OFL) -c ft2232 $(OFL_ARGS) --detect

prog:
	@command -v $(OFL) > /dev/null || { echo "prog: no openFPGALoader (apt install openfpgaloader, or build it)"; exit 1; }
	@[ -s $(BIT) ] || { echo "prog: no $(BIT)"; exit 1; }
	@echo "== loading $(BIT) into the KX2 (volatile: gone at power-off; the module's USB J602, FT2232H port A)"
	$(OFL) -c ft2232 $(OFL_ARGS) $(BIT)
	@echo "== loaded: the program runs from reset (PE1 button 0 = MCLR); its text at 9600 8N1 on the PE1's micro-USB serial J603 (/dev/ttyUSB1)"

programs: $(patsubst %,$(OUT)/%.hex,$(PROGS))
$(OUT)/%.hex: programs/%.asm $(wildcard programs/*.inc)
	@command -v $(GPASM) > /dev/null || { echo "programs: no gpasm (apt install gputils)"; exit 1; }
	mkdir -p $(OUT) && $(GPASM) -p p16f877a -I programs -o $@ $< > $(OUT)/$*.log 2>&1 || { cat $(OUT)/$*.log; exit 1; }

gpsim: programs
	@command -v $(GPSIM) > /dev/null || { echo "gpsim: no gpsim (apt install gpsim)"; exit 1; }
	@for n in $(PROGS); do \
	  if sh programs/gpsim/run.sh $(GPSIM) $(OUT)/$$n.hex programs/gpsim/$$n.stc > $(OUT)/$$n.gpsim.log 2>&1; then echo "ok    $$n  $$(tail -1 $(OUT)/$$n.gpsim.log)"; \
	  else echo "FAIL  $$n  ($(OUT)/$$n.gpsim.log)"; fi; done

PORT    ?= /dev/ttyUSB1
IP      ?= 192.168.77.3
boot: $(OUT)/pd2boot.hex
$(OUT)/pd2boot.hex: boot/pd2boot.asm
	@command -v $(GPASM) > /dev/null || { echo "boot: no gpasm (apt install gputils)"; exit 1; }
	mkdir -p $(OUT) && $(GPASM) -p p16f877a -o $@ $< > $(OUT)/pd2boot.log 2>&1 || { cat $(OUT)/pd2boot.log; exit 1; }
upload:
	@[ -n "$(HEX)" ] || { echo "upload: HEX=prog.hex (make programs builds the examples into out/)"; exit 1; }
	python3 scripts/pd2boot.py upload --port $(PORT) --wait 60 $(HEX)
host-lcd:
	mkdir -p $(OUT) && c++ -O2 -std=c++17 -Ihost host/system.cpp host/adapter_udp.cpp -o $(OUT)/host_lcd
	$(OUT)/host_lcd $(IP)
image: boot
	@[ -n "$(HEX)" ] || { echo "image: HEX=prog.hex (make programs builds the examples into out/)"; exit 1; }
	python3 scripts/pd2boot.py image $(OUT)/pd2boot.hex $(HEX) $(OUT)/image.list --hex $(OUT)/image.hex
	python3 scripts/hex2v.py $(OUT)/image.hex $(DESIGN)

bit:
	@[ -n "$(VIVADO)" ] || { echo "bit: no vivado (source its settings64.sh, or VIVADO=/path/to/vivado)"; exit 1; }
	@echo "== Vivado on build/ (synthesis, place, route: a few minutes; log build/top_vivado.log)"
	cd build && $(VIVADO) -mode batch -source top.tcl -nojournal -log top_vivado.log > vivado.out 2>&1 || \
	  { grep -m5 '^ERROR\|^CRITICAL' top_vivado.log; exit 1; }
	@grep -m1 -A2 'WNS(ns)' build/top_timing.rpt | tail -1 | awk '{print "== build/top.bit written; WNS " $$1 " ns, failing endpoints " $$3}'
	@echo "   load it with: make prog BIT=build/top.bit"


core-sim:
	@command -v verilator > /dev/null || { echo "core-sim: no verilator"; exit 1; }
	@command -v $(GPASM) > /dev/null || { echo "core-sim: no gpasm (apt install gputils)"; exit 1; }
	$(MAKE) -C core/tb
