# https://sourceforge.net/projects/acme-crossass/
ACME=acme

# https://github.com/einar-saukas/ZX0
ZX0=zx0

BUILDDIR=build
SOURCES=$(wildcard src/*.a)
T00=$(BUILDDIR)/mr.bin
ATTRACT=$(BUILDDIR)/mrattract.bin
T22=$(ATTRACT).zx0
BUILDDISK=$(BUILDDIR)/mini-replay.dsk

.PHONY: burgertime digdug hardhatmack mariobros moonpatrol sneakers spyhunter wavynavy compress sizes clean mount dist all

$(BUILDDISK): $(T00) $(T22)
# each block is stored in increasing logical sector order
# so block 'a' starts on T00,S00, 'b' starts on T01,S00, 'c' starts on T04,S01, &c.
# fastloader scatter-reads for optimal loading
#
#   |0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  2  2  2
#   |0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F  0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F  0  1  2
# --+-------------------------------------------------------------------------------------------------------
# 00|a  b  b  b  b  c  c  c  c  d  d  d  d  d  e  e  e  e  e  f  f  f  f  g  g  g  g  h  h  h  i  i  i  i  j
# 01|a  b  b  b  bc c  c  c  c  d  d  d  d  d  e  e  e  e  e  f  f  f  f  g  g  g  g  h  h  h  i  i  i  i  j
# 02|a  b  b  b  c  c  c  c  c  d  d  d  d  d  e  e  e  e  e  f  f  f  f  g  g  g  gh h  h  h  i  i  i  i  j
# 03|a  b  b  b  c  c  c  c  c  d  d  d  d  d  e  e  e  e  e  f  f  f  f  g  g  g  h  h  h  h  i  i  i  i  j
# 04|a  b  b  b  c  c  c  c  c  d  d  d  d  d  e  e  e  e  ef f  f  f  f  g  g  g  h  h  h  h  i  i  i  i  j
# 05|a  b  b  b  c  c  c  c  c  d  d  d  d  d  e  e  e  e  f  f  f  f  f  g  g  g  h  h  h  h  i  i  i  i  j
# 06|a  b  b  b  c  c  c  c  c  d  d  d  d  d  e  e  e  e  f  f  f  f  fg g  g  g  h  h  h  h  i  i  i  i  j
# 07|a  b  b  b  c  c  c  c  c  d  d  d  d  d  e  e  e  e  f  f  f  f  g  g  g  g  h  h  h  h  i  i  i  i  j
# 08|a  b  b  b  c  c  c  c  c  d  d  d  d  d  e  e  e  e  f  f  f  f  g  g  g  g  h  h  h  h  i  i  i  i  j
# 09|a  b  b  b  c  c  c  c  c  d  d  d  d  de e  e  e  e  f  f  f  f  g  g  g  g  h  h  h  h  i  i  i  i  j
# 0A|a  b  b  b  c  c  c  c  c  d  d  d  d  e  e  e  e  e  f  f  f  f  g  g  g  g  h  h  h  hi i  i  i  i  j
# 0B|a  b  b  b  c  c  c  c  cd d  d  d  d  e  e  e  e  e  f  f  f  f  g  g  g  g  h  h  h  i  i  i  i  i  j
# 0C|a  b  b  b  c  c  c  c  d  d  d  d  d  e  e  e  e  e  f  f  f  f  g  g  g  g  h  h  h  i  i  i  i  i  j
# 0D|a  b  b  b  c  c  c  c  d  d  d  d  d  e  e  e  e  e  f  f  f  f  g  g  g  g  h  h  h  i  i  i  i  i  j
# 0E|a  b  b  b  c  c  c  c  d  d  d  d  d  e  e  e  e  e  f  f  f  f  g  g  g  g  h  h  h  i  i  i  i  i  j
# 0F|a  b  b  b  c  c  c  c  d  d  d  d  d  e  e  e  e  e  f  f  f  f  g  g  g  g  h  h  h  i  i  i  i  i  j

# Block 'a': boot code + menu
	cp "$(T00)" "$(BUILDDISK)"

# Block 'b': Dig Dug
	cat "res/dig-dug/DIG.DUG.TITLE.zx0" >> "$(BUILDDISK)"
	cat "res/dig-dug/DIG.DUG.8000.zx0" >> "$(BUILDDISK)"
# next byte is T04,S01,$7B

# Block 'c': Spy Hunter
	cat "res/spy-hunter/SPY.HUNTER.5F00.zx0" >> "$(BUILDDISK)"
	cat "res/spy-hunter/SPY.HUNTER.0800.zx0" >> "$(BUILDDISK)"
	cat "res/spy-hunter/SPY.HUNTER.0283.zx0" >> "$(BUILDDISK)"
# next byte is T08,S0B,$82

# Block 'd': BurgerTime
	cat "res/burgertime/BURGERTIME.0C00.zx0" >> "$(BUILDDISK)"
	cat "res/burgertime/BURGERTIME.INPUT.zx0" >> "$(BUILDDISK)"
	cat "res/burgertime/BURGERTIME.TITLE.zx0" >> "$(BUILDDISK)"
	cat "res/burgertime/BURGERTIME.6000.zx0" >> "$(BUILDDISK)"
# next byte is T0D,S09,$2F

# Block 'e': Hard Hat Mack
	cat "res/hard-hat-mack/HARD.HAT.MACK.2000.zx0" >> "$(BUILDDISK)"
	cat "res/hard-hat-mack/HARD.HAT.MACK.0800.zx0" >> "$(BUILDDISK)"
# next byte is T12,S04,$4D

# Block 'f': Moon Patrol
	cat "res/moon-patrol/MOON.PATROL.0800.zx0" >> "$(BUILDDISK)"
# next byte is T16,S05,$F8

# Block 'g': Sneakers
	cat "res/sneakers/SNEAKERS.0800.zx0" >> "$(BUILDDISK)"
# next byte is T1A,S02,$3B

# Block 'h': Mario Bros.
	cat "res/mario-bros/MARIO.BROS.0A00.zx0" >> "$(BUILDDISK)"
# next byte is T1D,S09,$E5

# Block 'i': Wavy Navy
	cat "res/wavy-navy/WAVY.NAVY.0800.zx0" >> "$(BUILDDISK)"
# next byte is T21,S0F,$B3

# Block 'j': attract mode
# occupies T22,S00-T22,S0F regardless of where previous block ends
	truncate -s 136K "$(BUILDDISK)"
	cat "$(T22)" >> "$(BUILDDISK)"
	truncate -s 140K "$(BUILDDISK)"


$(T00): $(SOURCES) | $(BUILDDIR)
	$(ACME) -r "$(BUILDDIR)/mr0boot.lst" -l "$(BUILDDIR)/mr0boot-symbols.a" -o "$(BUILDDIR)/mr0boot.bin" "src/mr0boot.a"
	$(ACME) -r "$(BUILDDIR)/mrboot.lst" -l "$(BUILDDIR)/mrboot-symbols.a" -o "$(BUILDDIR)/mrboot.bin" "src/mrboot.a"
	$(ACME) -r "$(BUILDDIR)/dzx0.lst" -l "$(BUILDDIR)/dzx0-symbols.a" -o "$(BUILDDIR)/dzx0.bin" "src/dzx0.a"
	$(ACME) -r "$(BUILDDIR)/mrmenu.lst" -l "$(BUILDDIR)/mrmenu-symbols.a" -o "$(BUILDDIR)/mrmenu.bin" "src/mrmenu.a"
	$(ZX0) -f "$(BUILDDIR)/mrmenu.bin" "$(BUILDDIR)/mrmenu.bin.zx0"
	$(ACME) -r "$(BUILDDIR)/mr.lst" -o "$@" "src/mr.a"

$(ATTRACT): $(T00)
	$(ACME) -r "$(BUILDDIR)/mrattract.lst" -l "$(BUILDDIR)/mrattract-symbols.a" -o "$@" "src/mr.attract.a"

$(T22): $(ATTRACT)
	$(ZX0) -f "$(ATTRACT)" "$@"

$(BUILDDIR):
	mkdir -p "$@"

mount: $(BUILDDISK)
	mame apple2ee -debug -maximize -gameio joy -flop1 "$(BUILDDISK)" &

clean:
	rm -rf "$(BUILDDIR)"

burgertime: $(BUILDDIR)
	$(ZX0) -f "res/burgertime/6400-7eaf-input" "res/burgertime/BURGERTIME.INPUT.zx0"
	$(ZX0) -f "res/burgertime/0c00-1fff" "res/burgertime/BURGERTIME.0C00.zx0"
	$(ZX0) -f "res/burgertime/2000-3fff" "res/burgertime/BURGERTIME.TITLE.zx0"
	$(ZX0) -f "res/burgertime/6000-bdc0" "res/burgertime/BURGERTIME.6000.zx0"

digdug: $(BUILDDIR)
	$(ZX0) -f "res/dig-dug/4000-5fff" "res/dig-dug/DIG.DUG.TITLE.zx0"
	$(ZX0) -f "res/dig-dug/8000-be93" "res/dig-dug/DIG.DUG.8000.zx0"

hardhatmack: $(BUILDDIR)
	$(ZX0) -f "res/hard-hat-mack/0800-3fff" "res/hard-hat-mack/HARD.HAT.MACK.0800.zx0"
	$(ZX0) -f "res/hard-hat-mack/2000-93ff" "res/hard-hat-mack/HARD.HAT.MACK.2000.zx0"

mariobros: $(BUILDDIR)
	cat \
		"res/mario-bros/0a00-137f" \
		"res/mario-bros/1380-19ff-blank" \
		"res/mario-bros/1a00-7fff" \
		> "$(BUILDDIR)/mario-0a00"
	$(ZX0) -f "$(BUILDDIR)/mario-0a00" "res/mario-bros/MARIO.BROS.0A00.zx0"

moonpatrol: $(BUILDDIR)
	$(ZX0) -f "res/moon-patrol/4000-5fff-background" "res/moon-patrol/MOON.PATROL.BACKGROUND.zx0"
	$(ACME) -r "$(BUILDDIR)/loader.moon.patrol.lst" -o "res/moon-patrol/7800-7fff" "src/loader.moon.patrol.a"
	cat \
		"res/moon-patrol/0800-77ff" \
		"res/moon-patrol/7800-7fff" \
		"res/moon-patrol/8000-83ff" \
		"res/moon-patrol/8400-84ff-blank" \
		"res/moon-patrol/8500-90ff" \
		> "$(BUILDDIR)/moon-0800"
	$(ZX0) -f "$(BUILDDIR)/moon-0800" "res/moon-patrol/MOON.PATROL.0800.zx0"

sneakers: $(BUILDDIR)
	$(ACME) -r "$(BUILDDIR)/loader.sneakers.lst" -o "res/sneakers/9e00" "src/loader.sneakers.a"
	cat \
		"res/sneakers/0600-07ff" \
		"res/sneakers/0a00-97ff" \
		"res/sneakers/0000-00ff" \
		"res/sneakers/0200-02ff" \
		"res/sneakers/0400-05ff" \
		"res/sneakers/0800-09ff" \
		"res/sneakers/9e00" \
		> "$(BUILDDIR)/sneakers-0800"
	$(ZX0) -f "$(BUILDDIR)/sneakers-0800" "res/sneakers/SNEAKERS.0800.zx0"

spyhunter: $(BUILDDIR)
	$(ZX0) -f "res/spy-hunter/0283-07ff" "res/spy-hunter/SPY.HUNTER.0283.zx0"
	cat "res/spy-hunter/0800-23ff" "res/spy-hunter/2400-27ff-blank" "res/spy-hunter/2800-2cff" > "$(BUILDDIR)/spy-0800"
	$(ZX0) -f "$(BUILDDIR)/spy-0800" "res/spy-hunter/SPY.HUNTER.0800.zx0"
	$(ZX0) -f "res/spy-hunter/5f00-bdff" "res/spy-hunter/SPY.HUNTER.5F00.zx0"

wavynavy: $(BUILDDIR)
	$(ZX0) -f "res/wavy-navy/0800-baff" "res/wavy-navy/WAVY.NAVY.0800.zx0"

compress: burgertime digdug hardhatmack mariobros moonpatrol sneakers spyhunter wavynavy

sizes:
	@for f in res/**/*.zx0; do printf '%04x %s\n' $$(wc --bytes < "$$f") "$$f"; done

all: clean mount

dist: $(BUILDDIR)
	rm -rf "$(BUILDDIR)/mini-replay-src.zip"
	cd .. && 7z a mini-replay/build/mini-replay-src.zip mini-replay/Makefile mini-replay/src mini-replay/res mini-replay/LICENSE

.NOTPARALLEL:
