##############################################################################
# Makefile for the JOYCE utilities
#
# You will need:
#
# # You will need: 
#  >> A C compiler, eg GNU C.
#  >> zxcc(1) from my web page:
#      <http://www.seasip.demon.co.uk/Cpm/software/linux.html>
#  >> z80asm from z80pack: 
#      <ftp://ftp.mayn.de/pub/unix/software/Emulators/Computer/z80pack.tgz>
#  >> cpmtools compiled with LibDsk - see
#      <http://www.seasip.demon.co.uk/Unix/LibDsk/>
#
###############################################################################
#
# System options: 
# C compiler
#
CC  = gcc
#
# C preprocessor (preferably not excising comments)
#
CPP = gcc -E -C -P
#
# Delete at all costs
#
RM = rm -f
#
# You should not need to change anything below this line
#
##############################################################################
#
# This is the version number of the package
#
PACKVER=2.5.5
#
# These files contain the version number
#
VERINC=ver.inc version.inc built.inc
#
# Intermediate files
#
CLEAN = *.lis *.0 *.1 *.fid *.prl *.com *.rsx *.bin *~ \
	built.inc bootfile.emj 
#
# Target files
#
ACOMS = dosdir.com export.com import.com quit.com vgacom.com guest.com gdump.com
JCOMS = cenport.com dosdir.com export.com import.com quit.com rtc.com \
	vgacom.com xpalette.com xlogin.com pckey.com guest.com gdump.com
AFIDS = annedrv.fid  annevga.fid 
JFIDS = joycedrv.fid vga.fid jfastcrt.fid jfastuc1.fid   \
	ddjoyce.prl xlogin.fid xloginp3.fid xloginpw.fid emulios.rsx \
	ddjoypcw.prl ddjoypdf.prl ddjoypng.prl

EMJ = bootfile.emj
LIBS =  impexp.lib  print.lib   ucreate.lib  uopfcb.lib \
	pnum.lib    ubytin.lib  udparse.lib  uwildex.lib

USER1 = user1/*

SOURCE=Makefile COPYING README $(LIBS) user1 *.zsm *.z80 *.a86 *.inc *.c \
	pcw720s.dsk 1512font.psf joyce.spc utils.txt

#
# Build targets
#
all:	utils.dsk utils16.dsk $(EMJ)

utils.dsk:	$(JFIDS) $(JCOMS) $(USER1) utils.txt
	mkp3fs -720 $@ $(JFIDS) $(JCOMS) utils.txt 
# XXX	cpmcp -f cf2dd utils.dsk $(USER1) 1:

utils16.dsk:	$(AFIDS) $(ACOMS) $(USER1) utils.txt
	cp pcw720s.dsk utils16.dsk
	cpmcp -f amstrad utils16.dsk $(AFIDS) $(ACOMS) utils.txt 0:
	cpmcp -f amstrad utils16.dsk $(USER1) 1:

tar:	$(SOURCE)
	mkdir joyce-z80-$(PACKVER)
	cp -a $(SOURCE) joyce-z80-$(PACKVER)
	tar zcvf joyce-z80-$(PACKVER).tar.gz joyce-z80-$(PACKVER)/*
	rm -rf joyce-z80-$(PACKVER)

#
# Building the boot file. Two code files, plus the screen font and the
# welcome graphic
#
bootfile.emj:	emshdr.bin emsdata.bin 1512font.psf joyce.spc
	cat emshdr.bin emsdata.bin 1512font.psf joyce.spc > $@
#
# makeprl creates a Page Relocatable file from two memory images assembled at 
# different addresses.
#
makeprl: makeprl.c
	${CC} -o $@ $<
#
# fidcsum inserts a correct checksum into .FID files.
#
fidcsum: fidcsum.c
	${CC} -o $@ $<
#
# bin2zsm converts a file into Z80 source so it can be bodily included in 
# another file
#
bin2zsm: bin2zsm.c
	${CC} -o $@ $<
#
#############################################################################
#
# Special build instructions
#
pckey.com:	pckey.zsm pckeyfid.inc $(VERINC)

xlogin.rsx:	xloginrx.prl
	mv -f $< $@

xlogin.com:	xlogincm.com xlogin.rsx
	zxcc gencom xlogincm xlogin
	mv -f xlogincm.com xlogin.com	

pckeyfid.inc:	pckeyfid.bin bin2zsm
	./bin2zsm <$< >$@	
#
#############################################################################
#
# Building COM files
#
%.com:	%.zsm $(VERINC) $(LIBS)
	z80asm -o$*.bin -l -s -fb $<
	mv -f $*.bin $@

%.com:	%.z80 $(VERINC) $(LIBS)
	zxcc m80 -= +$< 
	zxcc link -`basename $< .z80`

#
# Building the EMJ file
#
%.bin:	%.zsm $(VERINC) 
	z80asm -o$*.bin -l -s -fb $<
#
#############################################################################
#
# Building relocatable files. These are assembled twice at different offsets
# and then compared to produce the relocation map.
#
jfastuc1.0:    jfastcrt.zsm ${VERINC}
	echo " org 0" >org.inc
	echo "OPTIONS equ 0" >uc1.inc
	z80asm -o$*.bin -l -s -fb $<
	mv -f $*.bin $@

jfastuc1.1:    jfastcrt.zsm ${VERINC}
	echo " org 0100h " >org.inc
	echo "OPTIONS equ 0" >uc1.inc
	z80asm -o$*.bin -l -s -fb $<
	mv -f $*.bin $@

jfastcrt.0:    jfastcrt.zsm ${VERINC}
	echo " org 0" >org.inc
	echo "OPTIONS equ 1" >uc1.inc
	z80asm -o$*.bin -l -s -fb $<
	mv -f $*.bin $@

jfastcrt.1:    jfastcrt.zsm ${VERINC}
	echo " org 0100h " >org.inc
	echo "OPTIONS equ 1" >uc1.inc
	z80asm -o$*.bin -l -s -fb $<
	mv -f $*.bin $@


%.0:    %.zsm ${VERINC}
	echo " org 0" >org.inc
	z80asm -o$*.bin -l -s -fb $<
	mv -f $*.bin $@

%.1:    %.zsm ${VERINC}
	echo " org 0100h " >org.inc
	z80asm -o$*.bin -l -s -fb $<
	mv -f $*.bin $@

%.rsx:  %.0 %.1 makeprl
	./makeprl $*.0 $*.1 /dev/zero $@

%.prl:  %.0 %.1 makeprl
	./makeprl $*.0 $*.1 /dev/zero $@

%.fid:  %.prl fidcsum
	./fidcsum $< $@
#
# Create a file with build date
#
built.inc:
	date "+	defb	'%d/%m/%Y'" > built.inc


#
#############################################################################
#
# Cleaning up after the party
#
clean:
	${RM} ${CLEAN}

