sources = reset.S monitor.S
targets = fw.bin debug.txt

DD = $(shell which dd)
ifeq ($(DD),/usr/bin/dd)
dd_args = status=none
endif

all: $(targets)
	@ls -lh $^

# firmware
fw.bin: $(sources:.S=) 
	dd if=reset of=$@.gen bs=1024 skip=248 seek=248 $(dd_args)
	dd if=monitor of=$@.gen bs=1024 conv=notrunc $(dd_args)
	python3 checksum.py $@.gen
	mv $@.gen $@

# exports symbols as constants
reset.sym: reset
	nasm -DNOMAP -Wno-all -f elf reset.S
	objdump -t reset.o | awk '($$2 == "g"){print "\\s"$$5"$$"}' > reset.glob
	grep -f reset.glob reset.map | \
		awk 'BEGIN{print ";; autogenerated"}{print $$3,"equ","0x"$$1}' > $@

debug.txt: reset
	grep -e '^\s*say_what' -e '^[a-z_\.]*:$$' reset.S > $@

# assembly
%: %.S
	nasm $< -o $@

clean:
	rm -f $(targets) $(sources:.S=.d) $(sources:.S=) reset.o reset.glob reset.map

# deps
%.d: %.S
	nasm -DPRE -M $^ > $@
include $(sources:.S=.d)

.PHONY: all clean
