CC65 = cc65
CA65 = ca65
LD65 = ld65
CL65 = cl65
PYTHON = python3
B = build
IMAGE = releases/tunix-0.5.0.dsk

.PHONY: all
all: $(IMAGE)

$(B) releases:
	mkdir -p $@

HEADERS = $(wildcard *.h kernel/*.h)
CFLAGS = -Os -t none --cpu 6502

$(B)/%.s: %.c $(HEADERS) Makefile | $(B)
	$(CC65) $(CFLAGS) $(SEGFLAGS) -o $@ $<

KERNEL_C = tbfile tbsource tbconfirm os account editor alarm password picker chess chess_engine message mailstore help
$(addprefix $(B)/,$(addsuffix .s,$(KERNEL_C))): $(B)/%.s: kernel/%.c $(HEADERS) Makefile | $(B)
	$(CC65) $(CFLAGS) $(SEGFLAGS) -o $@ $<

# Auxiliary language-card functions use main pointers and share the C stack.
# Only their definitions and imports across physical banks need distinct names.
auxdefs = $(foreach name,$(1),-D$(name)=aux_$(name))
maindefs = $(foreach name,$(1),-D$(name)=main_$(name))
ACCOUNT = account_valid account_load account_find account_select account_deselect account_create account_set_password account_verify account_needs_password account_name account_uid account_home account_owner_name
EDITOR = editor_open editor_draw editor_key editor_close
ALARM = alarm_open alarm_step alarm_draw alarm_key alarm_target
ACCOUNT_FS = fs_account_read_slot fs_account_write_slot fs_cd fs_free_slots fs_get_user fs_home_root fs_set_user fs_user_home
EDITOR_FS = fs_abort_write fs_close_read fs_close_write fs_error fs_open_read fs_open_write fs_put fs_read
AUXFLAGS = --code-name AUXCODE --rodata-name AUXCODE
$(B)/shell.s: SEGFLAGS = -Dshell_run=bank1_shell_run -Dos_error=mail_os_error -Dos_spawn=mail_os_spawn
$(B)/fs.s: SEGFLAGS = --code-name BANKCODE --bss-name LOWBSS
$(B)/account.s: SEGFLAGS = --code-name AUXCODE $(call auxdefs,$(ACCOUNT) password_wipe) $(call maindefs,$(ACCOUNT_FS))
$(B)/editor.s: SEGFLAGS = $(AUXFLAGS) --bss-name LOWBSS $(call auxdefs,$(EDITOR)) $(call maindefs,$(EDITOR_FS))
$(B)/alarm.s: SEGFLAGS = $(AUXFLAGS) $(call auxdefs,$(ALARM)) $(call maindefs,shell_clock_text)
$(B)/password.s: SEGFLAGS = $(AUXFLAGS) --bss-name LOWBSS $(call auxdefs,password_wipe)
$(B)/picker.s: SEGFLAGS = --code-name AUXONE --rodata-name AUXONE -Dpicker_draw=bank1_picker_draw

# Mailbox code uses ordinary main RAM for its pointers. Stubs restore bank 1
# after filesystem/account calls, including routines that briefly map firmware.
MAIL_SERVICES = fs_get_user fs_set_user fs_open_read fs_read fs_close_read fs_open_write fs_put fs_close_write fs_abort_write fs_rm fs_update_page account_uid account_owner_name
$(B)/mailstore.s: SEGFLAGS = --code-name BANKONE -Dmessage_gateway=bank1_message_gateway -Dmail_counts=bank1_mail_counts $(foreach name,$(MAIL_SERVICES),-D$(name)=mail_$(name))
TB_GATES = tb_open tb_getline tb_write tb_commit tb_error tb_materialize tb_prepare_source tb_switch_program
TB_SERVICES = fs_open_read fs_read fs_close_read fs_open_write fs_put fs_close_write fs_error os_task_error
$(B)/tbfile.s $(B)/tbsource.s $(B)/tbconfirm.s: SEGFLAGS = --code-name TBENGINE $(foreach name,$(TB_GATES),-D$(name)=basic_$(name)) $(foreach name,$(TB_SERVICES),-D$(name)=mail_$(name))
$(B)/help.s: SEGFLAGS = --code-name HELPCODE --rodata-name HELPINFO

$(B)/%.o: $(B)/%.s
	$(CA65) -t none -o $@ $<

$(B)/start.o $(B)/tbclock.o $(B)/loader.o $(B)/gates.o $(B)/chess_entry.o $(B)/console.o $(B)/clock.o $(B)/banks.o $(B)/power.o $(B)/video.o $(B)/banner.o $(B)/sha256.o $(B)/demo.o: $(B)/%.o: kernel/%.s | $(B)
	$(CA65) -t none -o $@ $<

$(B)/disk.o: boot/disk.s | $(B)
	$(CA65) -o $@ $<

$(B)/boot.o: boot/boot.s | $(B)
	$(CA65) -o $@ $<

$(B)/boot.bin: $(B)/boot.o boot/boot.cfg
	$(LD65) -C boot/boot.cfg -o $@ $<

$(B)/tbasic.o $(B)/tbasic_runtime.o: $(B)/%.o: cmd/%.s cmd/native.inc cmd/tbasic_ops.inc $(wildcard cmd/tbasic*.inc) | $(B)
	$(CA65) -I cmd -o $@ $<

$(B)/tbasic.bin $(B)/tbasic_runtime.bin: $(B)/%.bin: $(B)/%.o cmd/native.cfg
	$(LD65) -C cmd/native.cfg -o $@ $<

$(B)/tbparse.o: cmd/tbparse.s cmd/tbasic.s cmd/native.inc cmd/tbasic_ops.inc | $(B)
	$(CA65) -I cmd -o $@ $<
$(B)/tbparse.bin: $(B)/tbparse.o cmd/tbparse.cfg
	$(LD65) -C cmd/tbparse.cfg -o $@ $<

$(B)/pulse.o: cmd/pulse.s cmd/native.inc | $(B)
	$(CA65) -I cmd -o $@ $<

$(B)/pulse.bin: $(B)/pulse.o cmd/native.cfg
	$(LD65) -C cmd/native.cfg -o $@ $<

OBJS = $(addprefix $(B)/,start.o loader.o gates.o console.o disk.o clock.o banks.o power.o video.o banner.o account.o tbclock.o tbsource.o tbconfirm.o tbfile.o os.o shell.o fs.o editor.o alarm.o password.o sha256.o demo.o picker.o mailstore.o)
$(B)/kernel.bin: $(OBJS) kernel/kernel.cfg
	$(CL65) -t none --no-target-lib -C kernel/kernel.cfg -m $(B)/kernel.map -Ln $(B)/kernel.lbl -o $@ $(OBJS) none.lib

$(B)/chess.bin: $(B)/chess_entry.o $(B)/chess.o $(B)/chess_engine.o $(B)/message.o $(B)/help.o kernel/chess.cfg
	$(CL65) -t none --no-target-lib -C kernel/chess.cfg -m $(B)/chess.map -o $@ $(B)/chess_entry.o $(B)/chess.o $(B)/chess_engine.o $(B)/message.o $(B)/help.o none.lib

$(IMAGE): tools/build_disk.py tools/mkimage.py tools/mkfs.py $(B)/boot.bin $(B)/kernel.bin $(B)/pulse.bin $(B)/chess.bin $(B)/tbasic.bin $(B)/tbasic_runtime.bin $(B)/tbparse.bin | releases
	$(PYTHON) tools/build_disk.py $(B)/boot.bin $(B)/kernel.bin $(B)/pulse.bin $@

.PHONY: bundle test
bundle: $(IMAGE)
	$(PYTHON) tools/bundle.py

$(B)/chess-engine-test: tests/chess_engine_test.c kernel/chess_engine.c kernel/chess_engine.h | $(B)
	$(CC) -std=c99 -O2 -o $@ $<

$(B)/mailstore-test: tests/mailstore_test.c fs.c fs.h kernel/mailstore.c kernel/mailstore.h kernel/message.h | $(B)
	$(CC) -std=c99 -O2 -o $@ tests/mailstore_test.c fs.c

test: $(IMAGE) $(B)/chess-engine-test $(B)/mailstore-test
	$(B)/chess-engine-test
	$(B)/mailstore-test
	cd tests/fullmachine && TUNIX_IMAGE="$(abspath $(IMAGE))" go test -count=1 -v
