# z80mcp — Z80 machine emulator exposed as an MCP 2.0 stdio server.
# See SPEC.md. Build: make ; Test: make test

CC      ?= cc
CSTD    = -std=c17
WARN    = -Wall -Wextra -Werror
DBG     = -g -fno-omit-frame-pointer -fsanitize=address,undefined

PREFIX ?= $(if $(filter 0,$(shell id -u)),/usr/local,$(HOME)/.local)
BINDIR  = $(DESTDIR)$(PREFIX)/bin
BINDIR  = $(DESTDIR)$(PREFIX)/bin
LIBDIR  = $(DESTDIR)$(PREFIX)/lib
INCDIR  = $(DESTDIR)$(PREFIX)/include/z80mcp

SRC     = src/core/desc.c src/core/memmap.c src/core/machine.c \
          src/core/device.c src/core/registry.c \
          src/machines/z80_cpu.c src/machines/zx81_devices.c \
          src/machines/zx81_ploader.c src/machines/cpm_term.c src/machines/cpm_loader.c src/machines/cpm_fdc.c \
          src/core/png_writer.c \
          src/core/base64.c
COREOBJ = $(SRC:.c=.o)
VENDORSRC = vendor/cJSON/cJSON.c vendor/z80emu/z80emu.c
VENDOROBJ = $(VENDORSRC:.c=.o)

# Object-linked builds: vendor/z80emu needs its own flags (see Z80EMU_FLAGS),
# so library and test binary link prebuilt objects instead of recompiling.
CJSON_OBJ = vendor/cJSON/cJSON.o
Z80EMU_OBJ = vendor/z80emu/z80emu.o

# The vendored z80emu core gets OUR z80user.h (src/machines/z80_cpu_user.h):
# -I order + -D__Z80USER_INCLUDED__ skips the vendor example hooks.
Z80EMU_FLAGS = -Isrc -Isrc/core -Isrc/machines -D_POSIX_C_SOURCE=200809L \
               -D__Z80USER_INCLUDED__ -DZ80_CATCH_HALT -include src/machines/z80_cpu_user.h $(CSTD) -Wall -Wno-unused-but-set-variable -Wno-maybe-uninitialized $(DBG)  # no -Wextra: vendored third-party code
# -DZ80_CATCH_HALT (2026-12-01): with the vendor default (flags commented out
# in z80config.h), HALT silently burns the rest of the slice budget and status
# stays 0 - our HALT display-walk model (stall until R-wrap INT, ROM L0038)
# never engaged. With the catch, z80emu returns Z80_STATUS_HALT and PC points
# AT the HALT; z80_cpu.c advances PC past it and stalls until the R-wrap INT.

CPPFLAGS += -Isrc -Isrc/core -Isrc/machines -Isrc/server -I../mcp-core/src/core -Ivendor/cJSON -Ivendor/unity/src -Ivendor/z80emu \
            -D_POSIX_C_SOURCE=200809L -DUNITY_INCLUDE_DOUBLE
CFLAGS   += $(CSTD) $(WARN) $(DBG) $(CPPFLAGS)

LIB      = bin/libz80mcp.a
TESTBIN  = bin/test-z80mcp
SERVER   = bin/z80mcp

MCP_LIB = ../mcp-core/bin/libmcpcore.a

.PHONY: all test clean install uninstall probe zip

all: $(LIB) $(SERVER) $(MCP_LIB)

$(MCP_LIB):
	$(MAKE) -C ../mcp-core all

bin:
	mkdir -p bin

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

vendor/cJSON/cJSON.o: vendor/cJSON/cJSON.c
	$(CC) $(CFLAGS) -c $< -o $@

vendor/z80emu/z80emu.o: vendor/z80emu/z80emu.c vendor/z80emu/tables.h
	$(CC) $(Z80EMU_FLAGS) -c $< -o $@

vendor/z80emu/tables.h: vendor/z80emu/maketables.c
	$(CC) $(CSTD) $(WARN) vendor/z80emu/maketables.c -o vendor/z80emu/maketables
	vendor/z80emu/maketables > vendor/z80emu/tables.h

SERVEROBJ = src/server/server_tools.o src/server/main.o

$(SERVER): src/server/server_tools.c src/server/cpmfs.c src/server/main.c \
           $(COREOBJ) $(CJSON_OBJ) $(Z80EMU_OBJ) ../mcp-core/bin/libmcpcore.a | bin
	$(CC) $(CFLAGS) src/server/server_tools.c src/server/cpmfs.c src/server/main.c \
		$(COREOBJ) $(CJSON_OBJ) $(Z80EMU_OBJ) \
		../mcp-core/bin/libmcpcore.a -o $@.tmp
	test -x $@.tmp
	mv -f $@.tmp $@

$(LIB): $(COREOBJ) $(VENDOROBJ) vendor/z80emu/tables.h | bin
	ar rcs $@.tmp $(COREOBJ) $(VENDOROBJ)
	mv -f $@.tmp $@

test: $(TESTBIN)
	ASAN_OPTIONS=detect_leaks=0 ./$(TESTBIN)

$(TESTBIN): tests/test_main.c $(wildcard tests/*.c) $(COREOBJ) $(CJSON_OBJ) \
            $(Z80EMU_OBJ) vendor/unity/src/unity.c | bin
	$(CC) $(CFLAGS) tests/test_main.c vendor/unity/src/unity.c \
		$(COREOBJ) $(CJSON_OBJ) $(Z80EMU_OBJ) -o $@.tmp
	test -x $@.tmp
	mv -f $@.tmp $@

bin/boot-probe: src/tools/boot_probe.c $(COREOBJ) $(CJSON_OBJ) \
                $(Z80EMU_OBJ) | bin
	$(CC) $(CFLAGS) $< $(COREOBJ) $(CJSON_OBJ) $(Z80EMU_OBJ) -o $@.tmp
	test -x $@.tmp
	mv -f $@.tmp $@

probe: bin/boot-probe
	ASAN_OPTIONS=detect_leaks=0 ./bin/boot-probe

clean:
	rm -f $(COREOBJ) $(VENDOROBJ) $(LIB) $(TESTBIN)
	rm -rf bin

install: $(LIB) $(SERVER)
	mkdir -p $(LIBDIR) $(INCDIR)
	cp $(LIB) $(LIBDIR)/
	cp src/core/desc.h src/core/memmap.h src/core/machine.h $(INCDIR)/
	cp $(SERVER) $(BINDIR)/

uninstall:
	rm -f $(LIBDIR)/libz80mcp.a
	rm -rf $(INCDIR)

# Release zip: git archive from HEAD after a forced clean; project folder
# as archive prefix; build binaries excluded. Output: ../z80mcp.zip
# (override with ZIP_OUT=).
ZIP_OUT ?= ../z80mcp.zip

zip: clean
	git archive -o $(ZIP_OUT) --prefix=z80mcp/ HEAD
