##############################################################################
# Makefile for PCW16 CP/M 
#
# Origins of this code:
#
# The loader, BIOS, FID files and COM files are mine.
# The include files for the Rosanne OS are adapted from those in the SDK
# on Cliff Lawson's web page.
# The BDOS and CCP are SUPRBDOS and ZCPR-D&J, as supplied with YAZE. 
# The BDOS for CP/M 3 is Simeon Cran's ZPM3. I compile CPM3.SYS and ZPM3.SYS;
# distribute zpm3.sys if you build with a ZPM3 BDOS, or CPM3.SYS otherwise.
# The screen font is "S" as supplied with Slackware Linux.
#
# You will need: 
#  >> A C compiler, eg GNU C.
#  >> lar(1) and 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>
#  >>  GENCPM.COM from <http://cdl.uta.edu/cpm/>
#  >>  BNKBDOS3.SPR and RESBDOS3.SPR from a CP/M 3 or ZPM3 distribution.
#  >>  cpmtools(1) to write to CP/M discs:
#      <ftp://cantor.informatik.rwth-aachen.de/pub/cpmtools.tar.gz>
#
###############################################################################
#
# System options: 
# C compiler
#
CC  = gcc
#
# C preprocessor (preferably not excising comments)
#
CPP = gcc -E -C -P
#
# Floppy drive (720k 3.5" or 1440k 3.5", depending on format)
#
FD0 = /dev/fd0H720 
#FD0 = /dev/fd0H1440
#
# Format for CP/M disc. CF2DD for the standard 720k one, ANNEBOOT
# for the 720k bootable rootdisk, ANNEBTHD for the 1440k one.
#
#FMT = ANNEBTHD
#FMT = ANNEBOOT
FMT = CF2DD
#FMT = MF2HD
#
# Delete at all costs
#
RM = rm -f
#
##############################################################################
#
# Names of the distribution files
#
BINARC = cpm_100.zip
SRCARC = cpm_100s.zip
#
# The CP/M 3 CCP cannot be distributed. If you are building for distribution,
#comment out the top line and uncomment the lower one.
#
#CCP = ccp.com
CCP=
#
# Which font to use? 
#
#FONT = pc	# PC high-ASCII set
#FONT = joyce   # Old-style PCW high-ASCII set

FONT = joyce

# I hope you won't need to change anything below this line.
#
##############################################################################
#
# Dependencies for the loader program, which is fairly big
#
ANNELDR = anneldr.zsm annecrt.zsm annekbd.zsm \
	anneterm.dbi bios.dbi cpm2.dbi lomem.dbi cpm3ins.dbi tpaldr.dbi \
	annecpm.inc anneos.inc annecall.inc cpm2anne.inc getpars.inc \
	annelint
#
# FIDs to compile

FIDS    = adrive.fid mdrive.fid par.fid crtplus.fid sio.fid 
#
# Files that contain the version number
#
VERINC = ver.inc version.inc
#
# Files to be cleaned up by "make clean"
#
CLEAN = crtfont.com callanne.com desk.com fidldr.com fidldr3.com loadkeys.com \
        language.com set24x80.com palette.com disckit.com setsio.com \
	*.lis *.dbi *.0 *.1 *.eml *.prg *.fid *.prl *~ \
        fidldr.bin tpaldr.bin \
        callanne.zs1 font20.zsm font16.zsm fidldr.zsm bios.zsm \
        zpm3.sys drcpm3.sys cpm3.sys fidldr.sys fidldr3.sys org.inc scrrun.bin \
        xbm2zsm bin2zsm makeprl fidcsum unix2cpm \
	$(BINTXTS)
#
# Source to be archived
#
# XBMs; C; Z80 source; miscellaneous; Z80 binary data
#
XSRC = fonts/*.xbm
CSRC = bin2zsm.c fidcsum.c makeprl.c unix2cpm.c xbm2zsm.c
ZSRC1 = annecall.inc anneos.inc memlist.inc version.inc annecpm.inc \
       cpm2anne.inc opfcb.inc ver.inc getpars.inc fidmsg.inc debug.inc \
       debugl.inc \
       adrive.zsm desk.zsm tpaldr.zsm scrrun.zsm  \
       Du55/*
ZSRC2 = annecrt.zsm callanne.zsm loadkeys.zsm dkit1.zsm disckit.zsm \
       annekbd.zsm cpm3ins.zsm fidsup3.zsm lomem.zsm \
       anneldr.zsm crtplus.zsm fidsupp.zsm mdrive.zsm \
       anneterm.zsm crtfont.zsm set24x80.zsm palette.zsm \
       language.zsm setsio.zsm sio.zsm setlst.zsm \
       bios.h fidldr.h par.h
DSRC = gencpm.dat Makefile annelint diskdefs contrib/* 
BSRC = cpm2.sys nswp207.com nswp.doc nulu.com xlogsea.com \
       makecpm3.sub makecpm2.sub setccp.com zpm3.key
BINDOCS = cpm.doc docs/userf.doc docs/keyboard.doc
BINTXTS = cpm.txt docs/userf.txt docs/keyboard.txt
GPL     = COPYING COPYING.LIB
DOCS    = $(BINDOCS) docs/source.doc docs/STATUS COPYING COPYING.LIB
#
############################################################################
#
# The EMS (or, to be precise, EML) files. These change their names with
# each new version.
#
EML2    = a10cpm2.eml
EML3    = a10cpm3.eml
EMLZ3   = a10zpm3.eml
EMLZ3D  = zpm3.lbr	# "Dead" ZPM3 file
#
# No. of directory entries required in the EML files (ALLENTRIES for the
# EMLALL file)
#
ENTRIES    = 12
ALLENTRIES = 16
#
##############################################################################
#
# Full build - three OSs and documentation
#
all:	cpm2 cpm3 zpm3 $(BINTXTS)
#
# Components for CP/M 2
#
cpm2:   common fidldr.sys cpm2.eml 
#
# Components for CP/M 3
#
cpm3:	common drcpm3.sys fidldr3.sys cpm3.eml
#
# Components for ZPM3
#
zpm3:	common zpm3.sys fidldr3.sys zpm3.eml
#
# Common components
#
common:	anneldr.prg ${FIDS} utils 

utils:	desk.com crtfont.com palette.com language.com set24x80.com \
        callanne.com loadkeys.com disckit.com setsio.com \
	setlst.com
#
#############################################################################
#
# Some build tools. xbm2zsm converts fonts stored as X bitmaps to Z80 source.
#
xbm2zsm: xbm2zsm.c
	${CC} -o xbm2zsm xbm2zsm.c
#
# bin2zsm acts as a surrogate linker, by converting modules to a list of 
# DB instructions that can then be inserted into another source file.
#
bin2zsm: bin2zsm.c
	${CC} -o bin2zsm bin2zsm.c
#
# makeprl creates a Page Relocatable file from two memory images assembled at 
# different addresses.
#
makeprl: makeprl.c
	${CC} -o makeprl makeprl.c
#
# fidcsum inserts a correct checksum into .FID files.
#
fidcsum: fidcsum.c
	${CC} -o fidcsum fidcsum.c
#
# unix2cpm converts a text file from Unix to CP/M format (LF -> CR/LF)
#
unix2cpm: unix2cpm.c
	${CC} -o unix2cpm unix2cpm.c
#
##############################################################################
#
# The two console fonts
#
font16.zsm:	fonts/${FONT}font16.xbm xbm2zsm
	./xbm2zsm fonts/${FONT}font16.xbm >font16.zsm

font20.zsm:	fonts/${FONT}font20.xbm xbm2zsm
	./xbm2zsm fonts/${FONT}font20.xbm >font20.zsm
#
#############################################################################
#
# Modules that are inserted into source code. z80asm has no linker or 
# .phase directive, so code destined to be copied around in memory is 
# assembled for its final location, converted into a list of bytes (.dbi), and 
# then bodily included in another file.
#
%.dbi:	%.zsm bin2zsm annelint
	./annelint
	anneasm -o$*.prg -l -s -fb $<
	./bin2zsm $*.prg $@

anneterm.dbi:	anneterm.zsm annelint font16.zsm bin2zsm

bios.zsm:	bios.h
	${CPP} -DCPM2 bios.h >bios.zsm

bios.dbi:	bios.zsm annecpm.inc annelint bin2zsm version.inc

#
# The FID manager for CP/M 3 is completely different from that for CP/M 2; 
# therefore rather than generate them from one source as with the BIOS, keep
# two sets of source.
#
fidsupp.dbi:	fidsupp.zsm annecpm.inc getpars.inc annelint bin2zsm ${VERINC} \
                cpm2anne.inc fidmsg.inc

fidsup3.dbi:	fidsup3.zsm annecpm.inc getpars.inc annelint bin2zsm ${VERINC} \
                cpm2anne.inc fidmsg.inc
#
# CP/M 2: You can supply your own CPM2.SYS. It is a memory image of the BDOS
# and CCP, designed to load at 0E400h.
#
cpm2.dbi:	cpm2.sys bin2zsm
	./bin2zsm cpm2.sys cpm2.dbi
#
##############################################################################
#
# Files (such as FID files) in PRL format are produced by the somewhat brutal
# expedient of assembling twice at different origins. 
#
%.0:	%.zsm ${VERINC}
	echo " org 0" >org.inc
	anneasm -o$*.prg -l -s -fb $<
	mv -f $*.prg $@

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

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

%.fid:	%.prl fidcsum
	./fidcsum $< $@

crtplus.0:	crtplus.zsm anneterm.dbi font16.zsm font20.zsm ${VERINC}
crtplus.1:	crtplus.zsm anneterm.dbi font16.zsm font20.zsm ${VERINC}
par.zsm:	par.h ${VERINC}
	$(CPP) -DZ80ASM par.h >par.zsm
#
#############################################################################
#
# The device managers for CP/Ms 2 and 3. These share a common source file and
# use the C preprocessor to generate the two different versions.
#
fidldr.sys:	fidldr.h fidsupp.dbi
	${CPP} -DCPM2 fidldr.h >fidldr.zsm
	z80asm -ofidldr.com -l -s -fb fidldr.zsm
	cp fidldr.bin fidldr.sys
#
fidldr3.sys:	fidldr.h fidsup3.dbi scrrun.dbi
	${CPP} -DCPM3 fidldr.h >fidldr.zsm
	z80asm -ofidldr.com -l -s -fb fidldr.zsm
	cp fidldr.bin fidldr3.sys
#
##############################################################################
#
# The loader
#
anneldr.prg: ${ANNELDR} 
	./annelint
	anneasm -oanneldr.prg -l -s -fb anneldr.zsm
#
##############################################################################
#
# CP/M utilties - these are all built using the %.com:%.zsm rule below.
#
disckit.com:	disckit.zsm dkit1.dbi
crtfont.com:	crtfont.zsm opfcb.inc
loadkeys.com:	loadkeys.zsm opfcb.inc
#rdir.com:	rdir.zsm rfs.dbi rfs.inc

%.com: %.zsm
	z80asm -fb -l -s -o$*.bin $<
	mv -f $*.bin $@
#
# Documentation in CP/M format
#
%.txt:	%.doc unix2cpm
	./unix2cpm < $< > $@
#
# Source for CALLANNE - put it into CR/LF format...
#
callanne.zs1:   callanne.zsm unix2cpm
	./unix2cpm <callanne.zsm >callanne.zs1
#
##############################################################################
#
# Copying to discs
#
# anneldr.prg must be <16k, so display its size here. If it displays as 
# >16384, then it won't run.
#

%.com.copy:	%.com
	(cpmera -f $(FMT) $(FD0) $< ; true)
	cpmcp -f $(FMT) $(FD0) $< 0:

disc:	anneldr.prg
	ls -l anneldr.prg
	-mdel a:/pcw/folder01/pcw000.prg
	mcopy -noO anneldr.prg a:/pcw/folder01/pcw000.prg
#
# Make a disc that only boots CP/M 2
#
cpm2disc: cpm2.eml
	(cpmera -f $(FMT) ${FD0} ${EML2}; true)
	cpmcp -f $(FMT) ${FD0} cpm2.eml 0:${EML2}
#
# Make a disc that only boots CP/M 3
#
cpm3disc: cpm3.eml
	(cpmera -f $(FMT) ${FD0} ${EML3}; true)
	cpmcp -f $(FMT) ${FD0} cpm3.eml 0:${EML3}
#
# Make a disc that only boots ZPM3
#
zpm3disc: zpm3.eml
	(cpmera -f $(FMT) ${FD0} ${EMLZ3}; true)
	cpmcp -f $(FMT) ${FD0} zpm3.eml 0:${EMLZ3}
#
# Copy ZPM3 to a disc as a secondary boot file
#
zpm3dead: zpm3.eml
	(cpmera -f $(FMT) ${FD0} ${EMLZ3D}; true)
	cpmcp -f $(FMT) ${FD0} zpm3.eml 0:${EMLZ3D}
#
# ZPM3-only utilities
#
zpm3utils:	zpm3/autotog.com zpm3/clrhist.com zpm3/zpm3dist.txt
	(cpmera -f $(FMT) ${FD0} autotog.com; true)
	cpmcp -f $(FMT) ${FD0} zpm3/autotog.com 0:autotog.com
	(cpmera -f $(FMT) ${FD0} clrhist.com; true)
	cpmcp -f $(FMT) ${FD0} zpm3/clrhist.com 0:clrhist.com
	(cpmera -f $(FMT) ${FD0} zpm3dist.txt; true)
	cpmcp -f $(FMT) ${FD0} zpm3/zpm3dist.txt 0:zpm3dist.txt
#
# Documentation
#
copydocs:	$(BINTXTS) $(GPL)
	(cpmera -f $(FMT) ${FD0} userf.txt keyboard.txt cpm.txt copying copying.lib; true)
	cpmcp -f $(FMT) ${FD0} $(BINTXTS) $(GPL) 0: 

#
# Make a disc that will boot CP/M 2 or ZPM3.
#
cpmdisc: makecpm3.sub makecpm2.sub zpm3dead cpm2disc zpm3utils copydocs
	(cpmera -f $(FMT) ${FD0} nulu.com; true)
	cpmcp -f $(FMT) ${FD0} nulu.com 0:nulu.com
	(cpmera -f $(FMT) ${FD0} makecpm3.sub; true)
	cpmcp -f $(FMT) ${FD0} makecpm3.sub 0:makecpm3.sub	
	(cpmera -f $(FMT) ${FD0} makecpm2.sub; true)
	cpmcp -f $(FMT) ${FD0} makecpm2.sub 0:makecpm2.sub
	(cpmera -f $(FMT) ${FD0} setccp.com; true)
	cpmcp -f $(FMT) ${FD0} setccp.com 0:setccp.com
	(cpmera -f $(FMT) $(FD0) gencpm.dat; true)
	cpmcp -f $(FMT) $(FD0) gencpm.dat 0:gencpm.dat
	(cpmera -f $(FMT) $(FD0) bnkbios3.spr; true)
	cpmcp -f $(FMT) $(FD0) bnkbios3.spr 0:bnkbios3.spr
#
# Copy utilities to disc. These utilities are written by me...
#
copydesk:	desk.com.copy
copydisckit:	disckit.com.copy

copydu54:
	cd Du54; make
	(cpmera -f $(FMT) ${FD0} du55.com du55.doc du55con.com anneboot.du5; true)
	cpmcp -f $(FMT) ${FD0} Du54/du55.com Du54/du55.doc Du54/du55con.com Du54/anneboot.du5 0:

copydu55:
	cd Du55; make
	(cpmera -f $(FMT) ${FD0} du55.com du55.doc du55con.com anneboot.du5; true)
	cpmcp -f $(FMT) ${FD0} Du55/du55.com Du55/du55.doc Du55/du55con.com Du55/anneboot.du5 0:


utildisc:	desk.com.copy crtfont.com.copy set24x80.com.copy          \
                callanne.com.copy callanne.zs1 zpm3.key loadkeys.com.copy \
		palette.com.copy language.com.copy setsio.com.copy \
                xlogsea.com.copy setlst.com.copy # rdir.com.copy
	(cpmera -f $(FMT) ${FD0} callanne.zsm; true)
	cpmcp -f $(FMT) ${FD0} callanne.zs1 0:callanne.zsm
	(cpmera -f $(FMT) ${FD0} zpm3.key; true)
	cpmcp -f $(FMT) ${FD0} zpm3.key 0:zpm3.key
#
# These utilities are not written by me...
#
utils2:		nswp207.com nswp.doc contrib/qt16-01c.lbr
	(cpmera -f $(FMT) ${FD0} nswp.com; true)
	cpmcp -f $(FMT) ${FD0} nswp207.com 0:nswp.com
	(cpmera -f $(FMT) ${FD0} nswp.doc; true)
	cpmcp -f $(FMT) ${FD0} nswp.doc 0:nswp.doc
	(cpmera -f $(FMT) ${FD0} qt16-01c.lbr; true)
	cpmcp -f $(FMT) ${FD0} contrib/qt16-01c.lbr 0:qt16-01c.lbr 
#
#############################################################################
#
# Create disc images for distribution
#
discimage:	disc
	dd if=${FD0} of=pcwdisc.raw

cpmimage:	cpmdisc utildisc utils2 copydu55
	dd if=${FD0} of=cpmdisc.raw	
#
# Create archives for distribution
#
binzip:	pcw720.raw pcw1440.raw cpmdisc.raw $(BINTXTS) $(GPL) zpm3/zpm3dist.txt
	zip -j ${BINARC} pcw720.raw pcw1440.raw cpmdisc.raw $(GPL) $(BINTXTS) zpm3/zpm3dist.txt

zsrc1:	${ZSRC1}

zsrc2:	${ZSRC2}

src3:	${DSRC} ${BSRC} ${CSRC} 

src4:	${XSRC} ${DOCS}

srczip: zsrc1 zsrc2 src3 src4
	zip ${SRCARC} ${XSRC} 
	zip ${SRCARC} ${CSRC} 
	zip ${SRCARC} ${ZSRC1} 
	zip ${SRCARC} ${ZSRC2}
	zip ${SRCARC} ${DSRC} 
	zip ${SRCARC} ${BSRC} 
	zip ${SRCARC} ${DOCS}
#
#####################################################################
#
# Create CP/M 3
#
# Note: I am relying on my mini-emulator (ZXCC) to allow GENCPM to 
#       run.
#
bnkbios.0:	bios.h annecpm.inc annelint version.inc
	${CPP} -DORG0 -DCPM3 bios.h >bnkbios.zsm
	./annelint
	anneasm -obios.prg -l -s -fb -dCPM3 -dORG0 bnkbios.zsm
	mv -f bios.prg bnkbios.0

bnkbios.1:	bios.h annecpm.inc annelint version.inc
	${CPP} -DORG1 -DCPM3 bios.h >bnkbios.zsm
	./annelint
	anneasm -obios.prg -l -s -fb -dCPM3 -dORG1 bnkbios.zsm
	mv -f bios.prg bnkbios.1

bnkbios3.spr:	bnkbios.0 bnkbios.1 makeprl
	./makeprl bnkbios.1 bnkbios.0 /dev/zero bnkbios3.spr

drcpm3.sys:	cpm3/bnkbdos3.spr cpm3/resbdos3.spr bnkbios3.spr gencpm.dat
	cp cpm3/bnkbdos3.spr .
	cp cpm3/resbdos3.spr .
	zxcc gencpm.cpm	-AUTO -DISPLAY
	mv -f cpm3.sys drcpm3.sys

zpm3.sys:	zpm3/bnkbdos3.spr zpm3/resbdos3.spr bnkbios3.spr gencpm.dat
	cp zpm3/bnkbdos3.spr .
	cp zpm3/resbdos3.spr .
	zxcc gencpm.cpm -AUTO -DISPLAY
	mv -f cpm3.sys zpm3.sys
#
##############################################################################
#
# Boot files (in LBR format)
#
# When creating a new archive, lar asks how many directory entries to give it;
# this is the meaning of the 'echo', which tells it.
#
# CP/M 3 LBR: GENCPM always produces cpm3.sys, whether it is compiling
#             CP/M 3 or ZPM3. Therefore after the GENCPM, it is renamed to
#             ZPM3.SYS or DRCPM3.SYS. ZPM3.SYS is fine, but DRCPM3.SYS needs
#             to be renamed back before it is put in the library.
#
cpm3.eml:	drcpm3.sys fidldr3.sys ${FIDS} ${CCP}
	mv -f drcpm3.sys cpm3.sys
	${RM} cpm3.eml
	echo "${ENTRIES}" | lar u cpm3.eml cpm3.sys fidldr3.sys ${FIDS} ${CCP}
	mv -f cpm3.sys drcpm3.sys

zpm3.eml:	zpm3.sys fidldr3.sys ${FIDS}
	${RM} zpm3.eml
	echo "${ENTRIES}" | lar u zpm3.eml zpm3.sys fidldr3.sys ${FIDS} ${CCP}
	
cpm2.eml:	fidldr.sys ${FIDS}
	${RM} cpm2.eml
	echo "${ENTRIES}" | lar u cpm2.eml fidldr.sys ${FIDS}
#
# cpm.eml is a composite archive; if you put CCP.COM in it, then it runs 
# CP/M 3. If not, it runs CP/M 2.
#
cpm.eml:	zpm3.sys fidldr3.sys fidldr.sys ${FIDS}
	${RM} cpm.eml
	echo "${ALLENTRIES}" | lar u cpm.eml fidldr.sys fidldr3.sys zpm3.sys \
                                             ${FIDS}

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

#
#############################################################################
