### Common definitions

# Compile flags.
CAFLAGS = --target apple2 --list-bytes 0 --warnings-as-errors -W2
LDFLAGS = --config src/asm.cfg --warnings-as-errors

# Build into an 'out'  dir:
OUTDIR = out

# Headers
HEADERS = src/macros.inc

CHUNKS = $(OUTDIR)/1_4000-5fff $(OUTDIR)/2_f600-fbff $(OUTDIR)/3_d300-f3ff $(OUTDIR)/4_0200-25ff $(OUTDIR)/5_6000-b3df

TARGETS = $(OUTDIR)/complete.built $(OVLBINS)

# Scenery-file overlays: sections of the FS2.1 file that the game
# loads over $A7E0..$BFFF at run time (edit mode, WW1 mode, Ctrl+E,
# the boot page). Each src/ovlXX.s rebuilds orig/ovl_XX_*.bin exactly.
OVLS    = 25 2d 42 2b
OVLBINS = $(foreach o,$(OVLS),$(OUTDIR)/ovl$(o).bin)

.PHONY: clean all chunks validate
all: $(OUTDIR) $(TARGETS)

$(OUTDIR):
	mkdir -p $(OUTDIR)

clean:
	rm -f $(OUTDIR)/*.o
	rm -f $(OUTDIR)/*.built
	rm -f $(OUTDIR)/*.list
	rm -f $(OUTDIR)/?_????-????
	rm -f $(OUTDIR)/*.rev
	rm -f $(OUTDIR)/*.pak
	rm -f $(OUTDIR)/fs2\#062499
	rm -f $(OUTDIR)/loader.system\#ff2000

# Target that builds all the chunks at once as a single output; this
# eases sharing definitions across chunks.
$(OUTDIR)/complete.built: $(OUTDIR)/complete.o src/asm.cfg
	ld65 $(LDFLAGS) -o $@ $<

$(OUTDIR)/complete.o: src/complete.s src/chunk2.s src/chunk3.s src/chunk4.s src/chunk5.s $(HEADERS)
	ca65 $(CAFLAGS) --listing $(basename $@).list -o $@ $<

# Targets for individual "chunks", sliced out of the single output.
# These are used for validating the chunks and creating a binary.
$(OUTDIR)/1_4000-5fff: res/loading_panel.bin
	cp $< $@
$(OUTDIR)/2_f600-fbff: $(OUTDIR)/complete.built
	dd status=none if=$< of=$@ bs=1 skip=0 count=1536
$(OUTDIR)/3_d300-f3ff: $(OUTDIR)/complete.built
	dd status=none if=$< of=$@ bs=1 skip=1536 count=8448
$(OUTDIR)/4_0200-25ff: $(OUTDIR)/complete.built
	dd status=none if=$< of=$@ bs=1 skip=9984 count=9216
$(OUTDIR)/5_6000-b3df: $(OUTDIR)/complete.built
	dd status=none if=$< of=$@ bs=1 skip=19200 count=21472

$(OUTDIR)/ovl%.o: src/ovl%.s src/ovlPrelude.inc
	ca65 $(CAFLAGS) --listing $(basename $@).list -o $@ $<
$(OUTDIR)/ovl%.bin: $(OUTDIR)/ovl%.o src/ovl.cfg
	ld65 --config src/ovl.cfg --warnings-as-errors -o $@ $<

# "Phony" target that verifies that built chunks exactly match the
# original chunks of the @qkumba's ProDOS port, and that the overlay
# sources rebuild the FS2.1 file sections.
validate: $(CHUNKS) $(OVLBINS)
	@diff -q orig/1_4000-5fff $(OUTDIR)/1_4000-5fff > /dev/null || ( echo "Chunk 1 mismatch" && false )
	@diff -q orig/2_f600-fbff $(OUTDIR)/2_f600-fbff > /dev/null || ( echo "Chunk 2 mismatch" && false )
	@diff -q orig/3_d300-f3ff $(OUTDIR)/3_d300-f3ff > /dev/null || ( echo "Chunk 3 mismatch" && false )
	@diff -q orig/4_0200-25ff $(OUTDIR)/4_0200-25ff > /dev/null || ( echo "Chunk 4 mismatch" && false )
	@diff -q orig/5_6000-b3df $(OUTDIR)/5_6000-b3df > /dev/null || ( echo "Chunk 5 mismatch" && false )
	@diff -q orig/ovl_25_edit_a7e0-bfdf.bin $(OUTDIR)/ovl25.bin > /dev/null || ( echo "Overlay 25 mismatch" && false )
	@diff -q orig/ovl_2d_ww1_a7e0-b3df.bin $(OUTDIR)/ovl2d.bin > /dev/null || ( echo "Overlay 2D mismatch" && false )
	@diff -q orig/ovl_42_logdisk_a7e0-abdf.bin $(OUTDIR)/ovl42.bin > /dev/null || ( echo "Overlay 42 mismatch" && false )
	@diff -q orig/ovl_2b_boot_a7e0-abdf.bin $(OUTDIR)/ovl2b.bin > /dev/null || ( echo "Overlay 2B mismatch" && false )

# Target that creates a FS2 binary using @qkumba's ProRWTS2, with
# custom code for loading FS2 chunks.
binary: $(OUTDIR)/fs2\#062499 $(OUTDIR)/loader.system\#ff2000

$(OUTDIR)/fs2\#062499: $(CHUNKS) loader/PRORWTS2.S
	cd $(OUTDIR) && ../loader/pack.py
	cd $(OUTDIR) && acme --color --report prorwts2.list ../loader/PRORWTS2.S
	cd $(OUTDIR) && ../loader/movebytes.py
	@echo Successfully created: $@

$(OUTDIR)/loader.system\#ff2000: $(OUTDIR)/loader.system.o
	ld65 $(LDFLAGS) -o $@ $<

$(OUTDIR)/loader.system.o: loader/loader.system.s
	ca65 $(CAFLAGS) --listing $(basename $@).list -o $@ $<
