# Cfish - classic AmigaOS (68k) build
#
# No multithreading (this toolchain has no threading library at all),
# no tablebase support (too costly for classic Amiga storage/hardware),
# no NUMA/LargePages (meaningless on this platform) - see project notes.
# numa.o is still built for now but compiles to essentially nothing
# since NUMA is never defined below.
#
# CPU variants: pick one with ARCH=020/030/040/060 (default: 020), or
# use the matching per-CPU shortcut target (make 020/030/040/060).
# 68000 is not covered yet - see project notes.
#
#   make 020               (equivalent to: make build ARCH=020)
#   make 030
#   make 040
#   make 060
#   make all                (all four at once, each with its own
#                             recommended settings - see below)
#
# No FPU variants: floating point in this codebase is confined to
# time-management code called a handful of times per move, never in the
# search's hot path, so generating code for a hardware FPU (68881/882,
# or 68040/060's built-in one) would not give any measurable benefit -
# see project notes. -msoft-float is used unconditionally for all of
# these targets.
#
# Optimization level (-O2 vs -O3) was benchmarked per CPU (see project
# notes for methodology and results): -O3 measured a consistent,
# repeatable win on 68020, and an even clearer one on 68040 (likely
# because its real cache can absorb -O3's larger code from more
# aggressive inlining/unrolling, unlike 68020/030's little-to-none).
# 68030 showed no repeatable difference either way, so it defaults to
# the more conservative -O2. 68060 wasn't tested directly (WinUAE
# limitation) but is grouped with the other real-cache CPUs. Defaults
# below reflect this; still overridable explicitly if you want to
# experiment further (make build ARCH=030 OPT=O3).

ARCH ?= 020

ifeq ($(origin OPT),undefined)
  ifeq ($(ARCH),030)
    OPT = O2
  else
    OPT = O3
  endif
endif

ifeq ($(ARCH),020)
  MCPU = -m68020
else ifeq ($(ARCH),030)
  MCPU = -m68030
else ifeq ($(ARCH),040)
  MCPU = -m68040
else ifeq ($(ARCH),060)
  MCPU = -m68060
else
  $(error Unknown ARCH '$(ARCH)' - use 020, 030, 040, or 060)
endif

ifneq ($(OPT),O2)
ifneq ($(OPT),O3)
  $(error Unknown OPT '$(OPT)' - use O2 or O3)
endif
endif

EXE = cfish$(ARCH)

CC = m68k-amigaos-gcc

OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
	material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
	search.o thread.o timeman.o tt.o uci.o ucioption.o \
	numa.o settings.o polybook.o

# CPU_NAME feeds into the "CFish 68020"-style engine identification
# string printed at startup and over UCI (see misc.c) - not just a
# cosmetic label, it's how the user tells apart which variant they're
# actually running.
CFLAGS = -Wall -std=c11 $(MCPU) -msoft-float -noixemul -DNDEBUG -$(OPT) \
	-DCPU_NAME=\"68$(ARCH)\"
LDFLAGS = -noixemul -lm

# We tried the standard libnix stack-enlargement mechanism (a global
# "__stack" variable linked with swapstack.o) but found it defective on
# this toolchain (crashes on exit - see main.c). The executable instead
# requires the caller to raise the stack manually before launching it
# ("stack 500000", or a Workbench icon tooltype of "STACK=500000").

.PHONY: help build 020 030 040 060 all initdebug clean clean-obj

default: build

help:
	@echo ""
	@echo "Cfish for classic AmigaOS (68k). Just run:"
	@echo ""
	@echo "  make 020              (builds cfish020, recommended settings)"
	@echo "  make 030              (builds cfish030, recommended settings)"
	@echo "  make 040              (builds cfish040, recommended settings)"
	@echo "  make 060              (builds cfish060, recommended settings)"
	@echo "  make all              (builds all four of the above)"
	@echo "  make build ARCH=030 OPT=O3   (override the recommended OPT,"
	@echo "                                 e.g. to experiment further)"
	@echo "  make initdebug        (extra timing output during startup)"
	@echo "  make clean"
	@echo ""

build: $(EXE)

020:
	$(MAKE) clean-obj; $(MAKE) build ARCH=020

030:
	$(MAKE) clean-obj; $(MAKE) build ARCH=030

040:
	$(MAKE) clean-obj; $(MAKE) build ARCH=040

060:
	$(MAKE) clean-obj; $(MAKE) build ARCH=060

all: 020 030 040 060

initdebug:
	$(MAKE) clean
	$(MAKE) CFLAGS="$(CFLAGS) -DINIT_DEBUG" build

$(EXE): $(OBJS)
	$(CC) -o $@ $(OBJS) $(LDFLAGS)

clean:
	rm -f cfish020 cfish030 cfish040 cfish060 *.o

clean-obj:
	rm -f *.o
