# DikuMUD NES - build build/diku.nes (MMC3, 512 KB PRG, 256 KB CHR, 8 KB battery WRAM) and the
# Text Edition build/dikutext.nes (MMC3, 512 KB PRG, 8 KB CHR-RAM, 8 KB battery WRAM)
CC65   = cc65
CA65   = ca65
LD65   = ld65
CFLAGS = -O -t none -I include -I build --cpu 6502 -g
AFLAGS = -I src -I . -g
PY     = tools/.venv/bin/python

CSRC = $(wildcard src/*.c)
ASRC = $(wildcard src/*.s)
# modules only one edition has: the graphic edition's scene, sprites, ring and menus; the Text
# Edition's interpreter and keyboard (each edition links stubs for the other's)
GFX_ONLY = scene actors ring ui ui2 gfx_stubs print
TEXT_ONLY = interp interp2 interp3 interp4 tedit temenu te_stubs print56 text56
# (text5: the MMC5 Text Edition's screen, in place of text56)
SHARED = $(filter-out $(GFX_ONLY) $(TEXT_ONLY) text5 main,$(patsubst src/%.c,%,$(CSRC)))
DATA = $(patsubst src/%.s,build/obj/%.o,$(filter-out src/text56a.s,$(ASRC))) build/obj/worlddata.o \
       build/obj/scenedata.o build/obj/sprdata.o
OBJ  = $(patsubst %,build/obj/%.o,$(SHARED) $(GFX_ONLY) main) $(DATA)
GEN  = build/chr.bin build/chr.h build/sprites.h build/world.h build/scenes.h build/text56.h build/text5.h

all: build/diku.nes build/dikutext.nes build/dikutext5.nes build/dikutext_web.nes

# the Text Edition: the shared objects, its own, and main.c built with TEXT_EDITION; the engine and
# the header assembled with TEXT56 (CHR-RAM, the 56-column bands); no CHR-ROM
TDATA = $(filter-out build/obj/engine.o build/obj/crt0.o build/obj/chr.o,$(DATA)) \
        build/obj/engine_t.o build/obj/crt0_t.o build/obj/text56data.o build/obj/text56a.o
TOBJ = $(patsubst %,build/obj/%.o,$(SHARED) $(TEXT_ONLY) main_text) $(TDATA)

# the Text Edition on MMC5 (build/dikutext5.nes): the same, with text5 (every letter pair in
# CHR-ROM, ExRAM extended attributes) for text56, and the engine, header and sound for MMC5
T5DATA = $(filter-out build/obj/engine.o build/obj/crt0.o build/obj/chr.o build/obj/sound.o,$(DATA)) \
         build/obj/engine_5.o build/obj/crt0_5.o build/obj/sound_5.o build/obj/text5data.o \
         build/obj/text5chr.o
T5OBJ = $(patsubst %,build/obj/%.o,$(SHARED) $(filter-out text56,$(TEXT_ONLY)) text5 main_text) $(T5DATA)

build/chr.bin build/chr.h build/sprites.h build/scenes.h build/scenedata.s build/sprdata.s: tools/gen_chr.py tools/font.py tools/scenes.py tools/sprites.py tools/art_sprites.py tools/classify.py
	$(PY) tools/gen_chr.py

build/world.s build/world.h: tools/gen_world.py tools/world.py tools/textpack.py tools/classify.py tools/automap.py tools/titles.py
	$(PY) tools/gen_world.py 24

# the Text Edition's 56-column screen: the 4x8 font, the static letter pairs, the keyboard
build/text56.s build/text56.h: tools/gen_text56.py tools/fonts/a2at_font.s tools/world.py build/chr.bin
	$(PY) tools/gen_text56.py

build/obj/text56data.o: build/text56.s | build/obj
	$(CA65) $(AFLAGS) -o $@ $<

build/obj/engine_t.o: src/engine.s src/nes.inc | build/obj
	$(CA65) $(AFLAGS) -D TEXT56 -o $@ $<

# the Text Edition for the web page (web/dikumud_nes, EmulatorJS's FCEUmm): the keyboard check
# lenient (src/engine.s FKB_LENIENT), the rest the same objects
build/obj/engine_tw.o: src/engine.s src/nes.inc | build/obj
	$(CA65) $(AFLAGS) -D TEXT56 -D FKB_LENIENT -o $@ $<

TWOBJ = $(subst build/obj/engine_t.o,build/obj/engine_tw.o,$(TOBJ))

build/obj/crt0_t.o: src/crt0.s src/nes.inc | build/obj
	$(CA65) $(AFLAGS) -D TEXT56 -o $@ $<

# the MMC5 Text Edition's letter pairs (CHR-ROM) and tables
build/text5.s build/text5.h build/text5_chr.s build/text5_chr.bin: tools/gen_text5.py tools/gen_text56.py tools/font6.py tools/font.py tools/fonts/a2at_font.s build/chr.bin
	$(PY) tools/gen_text5.py

build/obj/text5data.o: build/text5.s | build/obj
	$(CA65) $(AFLAGS) -o $@ $<

build/obj/text5chr.o: build/text5_chr.s build/text5_chr.bin | build/obj
	$(CA65) $(AFLAGS) -o $@ $<

build/obj/engine_5.o: src/engine.s src/nes.inc | build/obj
	$(CA65) $(AFLAGS) -D MMC5 -o $@ $<

build/obj/crt0_5.o: src/crt0.s src/nes.inc | build/obj
	$(CA65) $(AFLAGS) -D MMC5 -o $@ $<

build/obj/sound_5.o: src/sound.s build/music.inc src/nes.inc | build/obj
	$(CA65) $(AFLAGS) -D MMC5 -o $@ $<

build/obj/worlddata.o: build/world.s | build/obj
	$(CA65) $(AFLAGS) -o $@ $<

build/obj/scenedata.o: build/scenedata.s | build/obj
	$(CA65) $(AFLAGS) -o $@ $<

build/obj/sprdata.o: build/sprdata.s | build/obj
	$(CA65) $(AFLAGS) -o $@ $<

# the music and sound effects: one PRG data bank (src/sound.s reads it in R6 from the NMI)
SOUND_BANK = 58
build/music.inc build/music.json: tools/gen_music.py include/sound.h $(wildcard music/*.txt)
	$(PY) tools/gen_music.py $(SOUND_BANK)

build/obj/sound.o: src/sound.s build/music.inc src/nes.inc | build/obj
	$(CA65) $(AFLAGS) -o $@ $<

cfg/diku.cfg cfg/dikutext.cfg cfg/dikutext5.cfg: tools/gen_cfg.py
	python3 tools/gen_cfg.py

# C in the fixed banks keeps its variables in internal RAM; the banked modules' go to WRAM (MODBSS)
FIXEDC = main print print56 chars act misc actors
# the Text Edition's screen code runs for every line printed: static locals (-Cl: no C stack work;
# neither re-enters its functions)
STATICL = text56 text5 print56
build/obj/%.s: src/%.c include/*.h $(GEN) | build/obj
	$(CC65) $(CFLAGS) $(if $(filter $*,$(FIXEDC)),,--bss-name MODBSS) $(if $(filter $*,$(STATICL)),-Cl -Oirs,) -o $@ $<

build/obj/main_text.s: src/main.c include/*.h $(GEN) | build/obj
	$(CC65) $(CFLAGS) -DTEXT_EDITION -o $@ $<

build/obj/interp.s: src/cmdtab.h include/cmds.h

build/obj/%.o: build/obj/%.s
	$(CA65) $(AFLAGS) -o $@ $<

build/obj/%.o: src/%.s src/nes.inc $(GEN) | build/obj
	$(CA65) $(AFLAGS) -o $@ $<

build/obj:
	mkdir -p build/obj

build/diku.nes: $(OBJ) cfg/diku.cfg
	$(LD65) -C cfg/diku.cfg -o $@ -m build/diku.map --dbgfile build/diku.dbg $(OBJ) none.lib

build/dikutext5.nes: $(T5OBJ) cfg/dikutext5.cfg
	$(LD65) -C cfg/dikutext5.cfg -o $@ -m build/dikutext5.map --dbgfile build/dikutext5.dbg $(T5OBJ) none.lib

build/dikutext.nes: $(TOBJ) cfg/dikutext.cfg
	$(LD65) -C cfg/dikutext.cfg -o $@ -m build/dikutext.map --dbgfile build/dikutext.dbg $(TOBJ) none.lib

build/dikutext_web.nes: $(TWOBJ) cfg/dikutext.cfg
	$(LD65) -C cfg/dikutext.cfg -o $@ -m build/dikutext_web.map --dbgfile build/dikutext_web.dbg $(TWOBJ) none.lib

clean:
	rm -rf build/obj build/diku.* build/dikutext.* build/dikutext_web.*

.PRECIOUS: build/obj/%.s
.PHONY: all clean
