
# The system-independent files:

SRC_CPSKER = cpsker.asm cpsdef.asm cpsmit.asm cpscom.asm cpspk1.asm \
	cpspk2.asm cpsrem.asm cpsser.asm cpstt.asm cpscpm.asm \
	cpswld.asm cpscmd.asm cpsutl.asm cpsdat.asm cpxlnk.asm

# The system-dependent files:

SRC_SYSDEP = cpxtyp.asm cpxlnk.asm cpxcom.asm cpxswt.asm cpxvdu.asm

# One of:

SRC_SYSTEM = cpxsys.asm cpxtor.asm cpxnor.asm cpxmrl.asm cpxsb.asm \
	cpxcif.asm cpxhea.asm cpxapp.asm cpxpcw.asm cpxbbi.asm cpxbee.asm \
	cpxsyo.asm cpxtm4.asm cpxgni.asm cpxpro.asm cpxz80.asm cpxac.asm


OVLADR = 7000

# Create assembler listing by default
AS_FLAGS = /L

# CP/M emulator
#CPMEMU = aliados
CPMEMU = zxcc

ifeq "$(OS)" "Windows_NT"
  PLATFORM=win32
else
  PLATFORM=Linux
endif

#Location of M80/L80
ifeq ($(PLATFORM),Linux)
  CPMBIN = /usr/local/lib/cpm/bin80
else
  CPMBIN = C:/cpmbin
endif

AS	= $(CPMEMU) $(CPMBIN)/m80.com
LINK	= $(CPMEMU) $(CPMBIN)/l80.com

ECHO	= /bin/echo -e
RM	= rm -f

comma	:= ,
empty	:=
space	:= $(empty) $(empty)
commasep = $(subst $(space),$(comma),$(strip $(1)))
ccpline  = $(CPMEMU) $(1) -$(call commasep, $(2))

do_as	= \
	@COMMAND="$(AS) -=$<$(AS_FLAGS)"; \
	OUTPUT=$$(mktemp); echo $${COMMAND}; \
	$${COMMAND} | tee $${OUTPUT}; \
	grep -q 'No Fatal error(s).$$' $${OUTPUT}; ERROR=$$? ; \
	if [ "$${ERROR}" != "0" ]; then cat $${OUTPUT}; $(RM) $@; fi ; \
	$(RM) $${OUTPUT}; \
	exit $${ERROR}


.SUFFIXES:
.PHONY: all


all:	kerm411.com

kerm411.com: cpsker.hex cpxtyp.hex
	$(CPMEMU) mload.com -$@=$(call commasep, $^)

cpsker.hex: LOADADR = 100
cpxtyp.hex: LOADADR = $(OVLADR)


cpxtyp.hex: $(SRC_SYSDEP) $(SRC_SYSTEM)
cpsker.hex: $(SRC_CPSKER)
#cpxtyp.prn: $(SRC_SYSDEP) $(SRC_SYSTEM)
#cpsker.prn: $(SRC_CPSKER)

kerm411.com: | mload.com

.SECONDARY: mload.com
.INTERMEDIATE: mload.rel tmp.asm
mload.rel: AS_FLAGS = /	# No assembler listing
mload.rel: tmp.asm
tmp.asm: mload.asm
	$(ECHO) 'aseg\r' >tmp.asm
	cat mload.asm >>tmp.asm


%.rel: %.asm
	$(do_as)


%.hex: %.rel
	$(LINK) -/P:$(LOADADR),$(call commasep, $< $@)/N/X/E


%.com: %.rel
	$(LINK) -/P:100,$(call commasep, $< $@)/N/E


.PHONY: clean

clean:
	$(RM) *.hex *.prn
