# superresolution/demo -- zooming onto a car of the highway clip, the network on the unit (README.md)
#     make fetch            the clip and the network's float weights, downloaded once into ~/.cache/sr (curl; the sources below)
#     make prep             the host's part, once: the clip tracked, the windows, the network, the references (~/.cache/sr/highway)
#     make prep-rrdb        the second model on the host (RRDBNet, 64 MB of weights downloaded): sr_rrdb/ for the simulated page
#     make sr_bare.img      the bare-metal network with the blob behind it (every layer's reference): for the simulation and the board
#     make sr_ame           the same network as a Linux user program (build/sr_ame) and its blob (build/net1.blob, one reference map)
HB   ?= riscv32-buildroot-linux-musl-     # on PATH: linux/build/output/host/bin after ./build.sh
AME  := ../..
VM   := bare
DATA ?= $(HOME)/.cache/sr/highway
PY   ?= python3
VOP  ?= 1
BCF  := -O2 -march=rv32ima_zicsr -mabi=ilp32 -ffreestanding -fno-builtin -nostdlib -nostartfiles -static -fno-stack-protector -Wall -I$(AME) -DUSE_VOP=$(VOP)
# THE CLIP: "Jane M. Byrne Interchange Traffic", Wikimedia Commons, CC BY-SA 4.0 (Sea Cow)
#   https://commons.wikimedia.org/wiki/File:Jane_M._Byrne_Interchange_Traffic.webm
# THE WEIGHTS: realesr-general-x4v3 of Real-ESRGAN (BSD 3-Clause), the float network prep.py quantises
CLIP     ?= $(HOME)/.cache/sr/video/interchange.webm
CLIP_URL := https://upload.wikimedia.org/wikipedia/commons/9/90/Jane_M._Byrne_Interchange_Traffic.webm
PTH      ?= $(HOME)/.cache/sr/realesr-general-x4v3.pth
PTH_URL  := https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth
$(CLIP):
	mkdir -p $(dir $@)
	curl -L --fail -A "l8-tpu-demo (make fetch)" -o $@.part $(CLIP_URL) && mv $@.part $@
$(PTH):
	mkdir -p $(dir $@)
	curl -L --fail -o $@.part $(PTH_URL) && mv $@.part $@
# THE SECOND MODEL: RealESRGAN_x4plus (RRDBNet, BSD 3-Clause), for the simulated comparison (prep-rrdb)
PTH2     ?= $(HOME)/.cache/sr/RealESRGAN_x4plus.pth
PTH2_URL := https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth
$(PTH2):
	mkdir -p $(dir $@)
	curl -L --fail -o $@.part $(PTH2_URL) && mv $@.part $@
prep-rrdb: $(PTH2)
	$(PY) prep_rrdb.py --data $(DATA) --pth $(PTH2)
fetch: $(CLIP) $(PTH)
	@ls -l $(CLIP) $(PTH) | awk '{print "fetch:", $$5, $$9}'
prep: fetch
	$(PY) prep.py --out $(DATA)
build/sr_bare.bin: sr_bare.c sr_net.h $(VM)/start.S $(VM)/link.ld $(AME)/ame.h
	@mkdir -p build
	$(HB)gcc $(BCF) -T $(VM)/link.ld -o build/sr_bare.elf $(VM)/start.S sr_bare.c -lgcc
	$(HB)objcopy -O binary build/sr_bare.elf $@
sr_ame: build/sr_ame build/net1.blob
build/sr_ame: sr_ame.c sr_net.h ../../l8ex.h $(AME)/ame.h
	@mkdir -p build
	$(HB)gcc -O2 -static -Wall -I$(AME) -DUSE_VOP=$(VOP) -o $@ sr_ame.c
build/net1.blob: blob.py $(DATA)/net/cfg.txt
	@mkdir -p build
	python3 blob.py --data $(DATA) $@
build/net.blob: blob.py $(DATA)/net/cfg.txt
	@mkdir -p build
	python3 blob.py --data $(DATA) --refs $@
sr_bare.img: build/sr_bare.bin build/net.blob
	@test $$(stat -c %s build/sr_bare.bin) -lt 1048576
	{ cat build/sr_bare.bin; head -c $$((1048576 - $$(stat -c %s build/sr_bare.bin))) /dev/zero; cat build/net.blob; } > build/$@
	@ls -l build/$@ | awk '{print "sr_bare.img:", $$5, "bytes"}'
	$(MAKE) -C ../../../../test/verilated check-et-kx2 ET=$(abspath build/sr_bare.img)
clean:
	rm -rf build
