# Makefile - Spectrum 512 image + YM-sample demo for the Atari ST
#
# Builds jan15.tos from the source assets in ./assets/, generating every
# incbin'd input on the way:
#
#   assets/image512.png --[tools/spec512]----------------> image.spu
#   assets/audio.mp3   \                                          (emit-grid:
#   assets/regions.csv  --[tools/downsample]-> audio.15000.wav     pre-warps to
#                       --[tools/compsample]-------------> audio.tab + audio.dat
#   (no input)          --[tools/genymtable]-------------> ymtable.i
#   assets/font6.png    --[tools/fontgen]----------------> font.dat (scroll font)
#   scroll.txt          (ASCII message, incbin'd as-is by scroller.s)
#
# then assembles main.s (which incbin's image.spu / audio.tab / audio.dat /
# font.dat / scroll.txt and include's ymtable.i).  audio.dat is incbin'd, so the
# demo ships as a single jan15.tos.
#
#   make          build the tools as needed, regenerate out-of-date assets,
#                 and assemble jan15.tos
#   make tools    just build the host tools
#   make clean    remove jan15.tos, every asset-generated file, and the tool
#                 build artifacts (all regenerated on the next make)
#
# Assembler notes:
#   -no-opt : cycle-counted code; do NOT let vasm rewrite/retime instructions.
#   (no -devpac): keep the extended DRI symbol table in jan15.tos for the
#                 debugger.  (Codegen is byte-identical either way.)

AS      = vasmm68k_mot
ASFLAGS = -Ftos -no-opt -showopt -nosym
TARGET  = jan15.tos
ASM     = main.s helpers.s spu.s audio.s scroller.s fade.s debug.s
GENSRC  = ymtable.i             # generated assembler include (fast to rebuild)

# ----- source assets -------------------------------------------------------
ASSETS  = assets
IMG_SRC = $(ASSETS)/image512.png
AUD_SRC = $(ASSETS)/audio.wav
#AUD_SRC = $(ASSETS)/audio_max.wav
#AUD_SRC = $(ASSETS)/audio_sane.wav
# Per-VBL emit grid (from tools/emitadjaudio).  DISABLED for the 15 kHz rebuild:
# the emit grid changed completely (300 emits/frame), so the existing
# regions.csv is stale.  Regenerate it against the new demo with emitadjaudio,
# then restore the $(if ...) line below to re-enable.  Until then downsample
# uses a plain uniform 15000 Hz resample.
ADJ_SRC = $(ASSETS)/regions.csv
ADJUST  = $(if $(wildcard $(ADJ_SRC)),--adjust $(ADJ_SRC))
FONT_SRC = $(ASSETS)/font6.png

# ----- host tools (built on demand by their own Makefiles) -----------------
SPEC512    = tools/spec512/spec512
DOWNSAMPLE = tools/downsample/downsample
COMPSAMPLE = tools/compsample/compsample
GENYMTABLE = tools/genymtable/genymtable
EMITADJ    = tools/emitadjaudio/emitadjaudio   # diagnostic; not in the build chain
FONTGEN    = tools/fontgen/fontgen

# Optional tweaks, e.g.  make DOWNSAMPLE_FLAGS="--duration 310"
#                        make DOWNSAMPLE_FLAGS="--frame-hz 50.056"
#                        make GENYMTABLE_FLAGS="--smooth 2"
DOWNSAMPLE_FLAGS ?=
SPEC512_FLAGS    ?=
GENYMTABLE_FLAGS ?=

AUDIO_WAV = audio.15000.wav          # downsampled 8-bit, 15000 Hz

# ----- intermediate + generated files --------------------------------------
GEN       = image.spu audio.tab audio.dat  # incbin'd into jan15.tos at assembly time

.PHONY: all tools clean

# audio.dat is incbin'd into jan15.tos now (see $(GEN)); nothing ships loose.
all: $(TARGET)

$(TARGET): $(ASM) $(GENSRC) font.dat scroll.txt $(GEN)
	$(AS) $(ASFLAGS) -o $(TARGET) main.s

# ----- image: PNG -> Spectrum 512 ------------------------------------------
image.spu: $(IMG_SRC) $(SPEC512)
	$(SPEC512) $(IMG_SRC) --spu -o . $(SPEC512_FLAGS)

# ----- audio: mp3 -> 15000 Hz 8-bit -> delta-table tab + dat ----------------
# Pre-warped to the demo's per-VBL emit grid via $(ADJUST) when regions.csv is
# present (a change to it re-renders the song).
audio.15000.wav: $(AUD_SRC) $(wildcard $(ADJ_SRC)) $(DOWNSAMPLE)
	$(DOWNSAMPLE) $(AUD_SRC) $@ $(ADJUST) $(DOWNSAMPLE_FLAGS)

# compsample writes BOTH audio.tab and audio.dat in one run; whichever target
# make builds first produces the other, so compsample runs once.
audio.tab audio.dat: $(AUDIO_WAV) $(COMPSAMPLE)
	$(COMPSAMPLE) $(AUDIO_WAV) audio.tab audio.dat

# ----- ymtable.i: YM2149 DAC volume table (from the chip model, no input) ---
ymtable.i: $(GENYMTABLE)
	$(GENYMTABLE) $@ $(GENYMTABLE_FLAGS)

# ----- font.dat: 6x6 scroll font (incbin'd by scroller.s) -------------------
font.dat: $(FONT_SRC) $(FONTGEN)
	$(FONTGEN) $(FONT_SRC) $@

# ----- tools: build if missing (each delegates to its own Makefile) --------
tools: $(SPEC512) $(DOWNSAMPLE) $(COMPSAMPLE) $(GENYMTABLE) $(EMITADJ) $(FONTGEN)

$(SPEC512):
	$(MAKE) -C tools/spec512
$(DOWNSAMPLE):
	$(MAKE) -C tools/downsample
$(COMPSAMPLE):
	$(MAKE) -C tools/compsample
$(GENYMTABLE):
	$(MAKE) -C tools/genymtable
$(EMITADJ):
	$(MAKE) -C tools/emitadjaudio
$(FONTGEN):
	$(MAKE) -C tools/fontgen

# ----- clean: jan15.tos, every asset-generated file + intermediate, and the
#        tool build artifacts (regenerated on the next make) -----------------
clean:
	rm -f $(TARGET) $(GENSRC) $(GEN) $(AUDIO_WAV) font.dat image.dat pals.dat
	$(MAKE) -C tools/spec512 clean
	$(MAKE) -C tools/downsample clean
	$(MAKE) -C tools/compsample clean
	$(MAKE) -C tools/genymtable clean
	$(MAKE) -C tools/emitadjaudio clean
	$(MAKE) -C tools/fontgen clean
