# Makefile for yaze-ag

# CC must be an ANSI-C compiler
CC            =	cc

#where you want the binaries and manual page
BINDIR	      = /usr/local/bin
MANDIR	      = /usr/local/man/man1
LIBDIR	      = /usr/local/lib/yaze
DSKDIR        = $(LIBDIR)/disks
DOCDIR        = $(LIBDIR)/doc
DOCHTMLDIR    = $(LIBDIR)/doc_html

#OPTIMIZE	= -g
#OPTIMIZE	= -g -save-temps
# OPTIMIZE	=
# OPTIMIZE	= -O1
OPTIMIZE	= -xO5 -native
# OPTIMIZE	= -O2 -save-temps

# optimizing with gcc 2.x.x
#OPTIMIZE	= -O3

# optimizing with gcc 3.x.x
# (please search for your processor in "man gcc")
# (tip: search first for "sparc")
#
# -mcpu=cpu-type  # refer the man page "man gcc" for -mcpu
#
#OPTIMIZE	= -O3 -mcpu=v9
#OPTIMIZE	= -O3

# -DUSE_GNU_READLINE for command recall/editing and filename completion
# -DBGii_BUG works around a problem in Backgrounder II
# -DBIOS to build a CP/M bios and monitor program into the emulator
#  (see also YAZE_OBJS, below)
#
# -- The following options are new:
#
# -DMMU compiles support for MMU-switched memory and CP/M 3.1 extensions
# -DMMUTABLES=<val> sets the no of MMU tables (default 16)
# -DYAZEPAGESIZE=<val> sets the page size for every page pointer (default 4 KB)
#		(The MMUPAGEPOINTERS are generated automatically)
# -DSHOWMMU shows at start the MMU tables (will be defined if MMUTEST is def.)
# -DMMUTEST setup pages for 3000h - BFFFh to pages above 64K
#	    (-DSHOWMMU will be set and the MMU-Table 7 will be selected)
#	    (Only used unter CP/M 2.2. See README-1.20 for details)
#	    (DO NOT USE IT IF YOU WANT TO RUN CP/M 3.1 - THIS IS ONLY FOR TEST!)
#
# -DMEMSIZE=<val> sets size of memory in KBytes (default 64KB, with MMU: 512KB)
#
# -DBOOTSYS loads yaze-cpm3.boot at 0100H  instead of  yaze.boot (CP/M 2.2).
#	    yaze-cpm3.boot is idendical with A:CPM3.COM. Inside there is
#	    the CP/M 3.1 loader BOOTSYS.COM + CPM3.SYS (see BOOTSYS.Z80
#	    on drive P:). You can generate CPM3.COM with
#		"pip cpm3.com=bootsys.com,cpm3.sys".
#	    If BOOTSYS is given you can boot CP/M 2.2 (SUPERDOS) with
#		"yaze -l -1 -b yaze.boot".
#
# -DSHOWDRV shows the entries in @dtbl, the DPHs and the DPBs when CP/M 3.1
#	    starts or "mount.com" or "sys mount" is running.
# -DMULTIO  Use Multi-I/O disk transfere
# -DRWDEBUG Set the debug mode for read/write operations. If MULTIO is defined
#	    and multi sector count is set by the bdos the multi sector read
#	    and write will be displayed with "r" and "w". The rest will be
#	    displayed with ".". Reads/writes without multi sector count will
#	    be displayed with "R" and "W".
#
# -DUCSD    Set mode if you want to use UCSD-pascal version for CP/M.
#	    It was neccessary because UCSD-Pascal-System does not use the
#	    register B when it gives Sektor or Track to the BIOS. With this
#	    Flag only the Register C of BC is used in SETTRK & SETSEC. It
#	    was neccessary because yaze-ag crashes.
#
# --
# solaris2, freebsd and cygwin needs -D__EXTENSIONS__
# linux needs -D_BSD_SOURCE
#
OPTIONS	= -DBIOS -D__EXTENSIONS__ -DMMU -DBOOTSYS -DMULTIO # -DSHOWDRV

# Link with CP/M BIOS support,
YAZE_OBJS     = yaze.o mem_mmu.o simz80.o ybios.o monitor.o
# or link as a naked Z80
#YAZE_OBJS    = yaze.o simz80.o io.o

# -lreadline -lcurses -liberty if you defined USE_GNU_READLINE
#  (you don't need -liberty on linux or BSD systems)
#
#LIBS	      = -lreadline -lncurses
LIBS	      =

# a bsd-like install program (/usr/ucb/install on Solaris2)
INSTALL	      = install


###### you should not need to change anything below this line ######
# CWARN	      = -ansi -pedantic -Wall -Wshadow \
#		-Wpointer-arith -Wnested-externs -Winline
#CWARN	      = 
# CWARN	      = -ansi -Wall 
CFLAGS        =	$(CWARN) $(OPTIMIZE) $(OPTIONS) -DLIBDIR=\"$(LIBDIR)/\"

SRCS	      = yaze.c mem_mmu.c simz80.c io.c ybios.c monitor.c cdm.c
DOC	      = COPYING yaze.1 cdm.1 cpm.1 z80.1 ChangeLog \
		doc docfiles.txt install_doc.sh \
		doc_html docfiles_html.txt doc_cpmhelp_html.txt \
		install_doc_html.sh

TEST	      =	test/prelim.z80 test/zexlax.pl test/zexall.z80 test/zexdoc.z80 \
		test/prelim.com test/zexall.com test/zexdoc.com \
		test/savage.pas test/savage.com test/sys.azm test/sys.com \
		test/timex.com \
		TODO test.c md5 md5_V1.10 di

DISTRIB       =	Makefile_linux Makefile_solaris_cc Makefile_solaris_gcc \
		Makefile_freebsd \
		Makefile Makefile_first \
		Makefile_cygwin gen_windows yaze.bat yaze-cygwin-cpm3.boot \
		mem_mmu.h simz80.h yaze.h ybios.h \
		simz80.pl .yazerc yaze.boot $(SRCS) $(DOC) \
		yaze.sh doc_ydskfiles.txt install_disks.sh \
		BIOS3.ydsk yaze-cpm3.boot \
		BOOT_UTILS.ydsk testdsk.ydsk \
		CPM3_SYS.ydsk Kermit_SZRZ.ydsk \
		ZINC.ydsk spl.ydsk\
		TEST-UTILS-1.10.ydsk TODO \
		UNIXLIKE.ydsk Turbo-Modula-2.ydsk \
		HI-C-Z280-Compiler.ydsk MMU-Utils.ydsk \
		disksort DISKSORT.ydsk # $(TEST)

all:		yaze_bin cdm

yaze_bin:	$(YAZE_OBJS)
		$(CC) $(CFLAGS) $(YAZE_OBJS) $(LIBS) -o $@
		ln -f -s yaze.sh yaze

simz80.c:	simz80.pl
		rm -f simz80.c
		perl -w simz80.pl >simz80.c
		chmod a-w simz80.c

cdm:		cdm.o
		$(CC) $(CFLAGS) cdm.o $(LIBS) -o $@

install:	all
		rm -rf $(LIBDIR)
		mkdir -p $(LIBDIR)
		mkdir -p $(MANDIR)
		$(INSTALL) -s -c -m 755 yaze_bin $(BINDIR)
		$(INSTALL) -s -c -m 755 cdm $(BINDIR)
		$(INSTALL) -c -m 755 yaze $(BINDIR)
		ln -f -s $(BINDIR)/yaze $(BINDIR)/cpm
		ln -f -s $(BINDIR)/yaze $(BINDIR)/z80
		$(INSTALL) -c -m 644 yaze-cpm3.boot $(LIBDIR)
		$(INSTALL) -c -m 644 yaze.boot $(LIBDIR)
		$(INSTALL) -c -m 644 yaze.1 $(MANDIR)
		$(INSTALL) -c -m 644 cdm.1 $(MANDIR)
		$(INSTALL) -c -m 644 cpm.1 $(MANDIR)
		$(INSTALL) -c -m 644 z80.1 $(MANDIR)
		/bin/sh install_disks.sh $(DSKDIR)
		/bin/sh install_doc.sh $(DOCDIR)
		/bin/sh install_doc_html.sh $(DOCHTMLDIR)
		(cd $(LIBDIR); \
		rm -f md5 /tmp/md5.yaze-ag; \
		find . -type f -exec md5sum -b {} >>/tmp/md5.yaze-ag \; ; \
		mv /tmp/md5.yaze-ag md5)

md5:		$(DISTRIB)
		(P=`pwd`; D=`basename $$P`; \
			rm -f md5_$$D; \
			ls md5_y* >md5.list; \
			md5sum -b $(DISTRIB) `cat md5.list` \
						`cat docfiles.txt` \
						`cat docfiles_html.txt` \
			| grep -v 'md5_$$D' >md5_$$D; )
		rm -f md5.list

tar:		$(DISTRIB) md5
		ls md5_y* >md5.list
		(P=`pwd`; D=`basename $$P`; cd ..; \
			for f in $(DISTRIB) `cat $$D/md5.list`; \
				do echo $$D/$$f; done \
			| tar -T - -cf - | gzip -9 >$$D/$$D.tar.gz)
		rm -f md5.list

clean:;		rm -f *.o *~ core
		rm -f *.i *.s
		rm -f yaze

yaze.o:		yaze.c simz80.h yaze.h mem_mmu.h  	 # Makefile
ybios.o:	ybios.c simz80.h yaze.h ybios.h mem_mmu.h # Makefile
mem_mmu.o:	mem_mmu.c mem_mmu.h simz80.h		 # Makefile
simz80.o:	simz80.c simz80.h mem_mmu.h		 # Makefile
monitor.o:	monitor.c yaze.h ybios.h mem_mmu.h	 # Makefile
