##############################################################################
# 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 stock Digital Research CP/M 2 and CP/M 3.
# 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.info/Cpm/software/linux.html>
#  >>  z80asm from z80pack: 
#      <ftp://ftp.mayn.de/pub/unix/software/Emulators/Computer/z80pack.tgz>
#  >>  GENCPM.COM from <http://www.cpm.z80.de/>
#  >>  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 -P
#
# Format for CP/M disc. Note that this requires cpmtools 1.6 or later,
# for support for the Amstrad superblock.
#
FMT = amstrad
#
# Delete at all costs
#
RM = rm -f
#
# PcW16 emulator (not necessary, just nice for testing purposes)
#
XANNE = xanne
#XANNE = /home/jce/C/joyce-2.4.0/bin/xanne
#
##############################################################################
#
# Names of the distribution files
#
BINARC = cpm_102.zip
SRCARC = cpm_102s.zip
#
CCP = DRI/ccp.com
#
# 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 \
        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/* DRI/*
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 inject1440.sh inject720.sh \
	contrib/* pcw1440.00 pcw720.00 profile.sub
BSRC = cpm2.sys xlogsea.com 
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    = a12cpm2.lbr
EML3    = a12cpm3.eml
#
# No. of directory entries required in the EML files (ALLENTRIES for the
# EMLALL file)
#
ENTRIES    = 12
ALLENTRIES = 16
#
##############################################################################
#
# Full build - two OSs, documentation, disc images
#
all:	cpm2 cpm3 anneldr.prg $(BINTXTS) disc
#
# Components for CP/M 2
#
cpm2:   common fidldr.sys cpm2.eml 
#
# Components for CP/M 3
#
cpm3:	common drcpm3.sys fidldr3.sys cpm3.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.prg:	bios.zsm annecpm.inc annelint bin2zsm version.inc
	anneasm -obios.prg -l -s -fb $<

#
# 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 is built from the Digital Research source:
#
cpm2.sys:	DRI/cpm2.sys
	cp $< $@

cpm2.dbi:	cpm2.sys bin2zsm
	./bin2zsm cpm2.sys $@
#
##############################################################################
#
# 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.
#

DISTFILES = DRI/pip.com DRI/date.com DRI/device.com DRI/dir.com DRI/dump.com \
	DRI/ed.com DRI/erase.com DRI/gencom.com DRI/gencpm.com DRI/get.com \
	DRI/help.com DRI/help.hlp \
	DRI/initdir.com DRI/patch.com DRI/put.com DRI/rename.com DRI/save.com \
	DRI/set.com DRI/setdef.com DRI/show.com DRI/sid.com DRI/submit.com \
	DRI/type.com \
	profile.sub \
	gencpm.dat desk.com disckit.com callanne.com callanne.zsm \
	bnkbios3.spr COPYING COPYING.LIB DRI/COPYING.DRI ChangeLog \
	cpm.txt crtfont.com \
	docs/keyboard.txt docs/userf.txt \
	Du55/du55.com Du55/du55.doc Du55/du55con.com Du55/anneboot.du5 \
	language.com loadkeys.com palette.com contrib/qt16-01c.lbr \
	set24x80.com setlst.com setsio.com xlogsea.com
	

disc:	pcw1440.raw pcw720.raw $(DISTFILES)
	

pcw1440.raw:	pcw1440.00 anneldr.prg cpm2.eml cpm3.eml inject1440.sh
	ls -l anneldr.prg
	-cp pcw1440.00 pcw1440.raw
	-mdel -i pcw1440.raw ::/pcw/folder01/pcw000.prg
	mcopy -i pcw1440.raw anneldr.prg ::/pcw/folder01/pcw000.prg
	-cpmrm -f amstrad pcw1440.raw \*.eml
	cpmcp -f amstrad pcw1440.raw cpm2.eml 0:$(EML2)
	cpmcp -f amstrad pcw1440.raw cpm3.eml 0:$(EML3)
	./inject1440.sh $(DISTFILES)

pcw720.raw:	pcw720.00 anneldr.prg cpm2.eml cpm3.eml inject720.sh
	ls -l anneldr.prg
	-cp pcw720.00 pcw720.raw
	-mdel -i pcw720.raw ::/pcw/folder01/pcw000.prg
	mcopy -i pcw720.raw anneldr.prg ::/pcw/folder01/pcw000.prg
	-cpmrm -f amstrad pcw720.raw \*.eml
	cpmcp -f amstrad pcw720.raw cpm2.eml 0:$(EML2)
	cpmcp -f amstrad pcw720.raw cpm3.eml 0:$(EML3)
	./inject720.sh $(DISTFILES)

run:	disc
	$(XANNE) -A pcw1440.raw
#
#############################################################################
#
# Create archives for distribution
#
zip:	binzip srczip

binzip:	pcw720.raw pcw1440.raw $(BINTXTS) $(GPL) 
	zip -j ${BINARC} pcw720.raw pcw1440.raw $(GPL) DRI/COPYING.DRI \
	$(BINTXTS) 

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:	DRI/bnkbdos3.spr DRI/resbdos3.spr bnkbios3.spr gencpm.dat
	cp DRI/bnkbdos3.spr .
	cp DRI/resbdos3.spr .
	zxcc DRI/gencpm.com	-AUTO -DISPLAY
	mv -f cpm3.sys drcpm3.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
	cp -f $(CCP) ccp.com
	${RM} cpm3.eml
	echo "${ENTRIES}" | lar u cpm3.eml cpm3.sys fidldr3.sys ${FIDS} ccp.com
	mv -f cpm3.sys drcpm3.sys

cpm2.eml:	fidldr.sys ${FIDS}
	${RM} cpm2.eml
	echo "${ENTRIES}" | lar u cpm2.eml fidldr.sys ${FIDS}

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

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