# Makefile -- the ATmega328P clone 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 prog-vivado          the same with Vivado's hardware manager and a Platform Cable on J1700 (scripts/jtag_prog.tcl)
#   make detect               list the JTAG chain (is the module's USB plugged in?)
#   make arduino-cli          build arduino-cli, serial-discovery and ctags from source into ~/opt/arduino-tools and
#                             set up the board package kx2:avr (scripts/arduino_setup.sh); needs go, gcc-avr, avr-libc, avrdude
#   make ports                the serial ports arduino-cli sees (the module's UART: /dev/ttyUSB1 when alone)
#   make blinky PORT=/dev/ttyUSBn    compile sketches/Blink (FQBN kx2:avr:uno) and upload it through optiboot
#   make upload SKETCH=dir PORT=...  the same for any sketch folder
#   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 C program, the debug ID register)
BIT     ?= bit/top.bit
SKETCH  ?= sketches/Blink
FQBN    ?= kx2:avr:uno
ARDUINO_TOOLS ?= $(HOME)/opt/arduino-tools
ACLI    := $(ARDUINO_TOOLS)/bin/arduino-cli --config-file $(ARDUINO_TOOLS)/arduino-cli.yaml
OUT     := $(CURDIR)/out
OFL     ?= openFPGALoader
OFL_ARGS ?= $(if $(SERIAL),--ftdi-serial $(SERIAL))
NOISE   := 'index\|Error initializing instance\|mdns-discovery'
# 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 prog-vivado detect arduino-cli ports compile blinky upload bit core-sim

help:
	@sed -n '2,11p' 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 chip runs; an upload resets it into optiboot by itself (auto-reset on avrdude's sync)"

prog-vivado:
	@[ -n "$(VIVADO)" ] || { echo "prog-vivado: no Vivado found (PATH, XILINX_VIVADO or VIVADO=...)"; exit 1; }
	@[ -s $(BIT) ] || { echo "prog-vivado: no $(BIT)"; exit 1; }
	$(VIVADO) -mode batch -nojournal -nolog -source scripts/jtag_prog.tcl -tclargs $(abspath $(BIT))

arduino-cli:
	sh scripts/arduino_setup.sh

ports:
	@[ -x $(ARDUINO_TOOLS)/bin/arduino-cli ] || { echo "ports: no arduino-cli: make arduino-cli first"; exit 1; }
	$(ACLI) board list 2>&1 | grep -v $(NOISE); true

compile:
	@[ -x $(ARDUINO_TOOLS)/bin/arduino-cli ] || { echo "compile: no arduino-cli: make arduino-cli first"; exit 1; }
	mkdir -p $(OUT)/$(notdir $(SKETCH))
	$(ACLI) compile -b $(FQBN) --output-dir $(OUT)/$(notdir $(SKETCH)) $(SKETCH) 2>&1 | grep -v $(NOISE); true
	@[ -s $(OUT)/$(notdir $(SKETCH))/$(notdir $(SKETCH)).ino.hex ] || { echo "compile: no hex written"; exit 1; }

upload: compile
	@[ -n "$(PORT)" ] || { echo "upload: PORT=/dev/ttyUSBn -- the module UART, /dev/ttyUSB1 when it is the only FTDI (make ports lists them)"; exit 1; }
	@[ -c "$(PORT)" ] || { echo "upload: $(PORT) is no serial device"; exit 1; }
	$(ACLI) upload -b $(FQBN) -p $(PORT) -l serial --input-dir $(OUT)/$(notdir $(SKETCH)) $(SKETCH)
	@echo "== uploaded $(notdir $(SKETCH)) through optiboot"

blinky:
	$(MAKE) upload SKETCH=sketches/Blink PORT=$(PORT)
	@echo "== LED0 (L, D13) blinks: one second on, one second off"

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: about 10 to 20 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 avr-gcc > /dev/null || { echo "core-sim: no avr-gcc (gcc-avr, avr-libc)"; exit 1; }
	$(MAKE) -C core/tb
