#
# 5799-WZQ (C) COPYRIGHT IBM CORPORATION 1986,1987,1988
# LICENSED MATERIALS - PROPERTY OF IBM
# REFER TO COPYRIGHT INSTRUCTIONS FORM NUMBER G120-2083
#
#$Header:Makefile 12.0$
#$ACIS:Makefile 12.0$
#$Source: /ibm/acis/usr/sys/standca/RCS/Makefile,v $
# prf.o is the old standalone printf, and printf.o is the full blown
# version.
DESTDIR=
HOST=ws	
LIBSA=libsa.a
LIBC=-lc
LD=ld
R=0
CC=hc -O -Hvolatile
CPP=/lib/cpp
RM=rm
HD_BOOT=boot.c format.c cat.c ls.c dump.c copy.c
SAUTIL=$(HD_BOOT) minidisk.c nvram.c dosboot.c install.c
BOOT_RELOC=180000
SAUTIL_RELOC=180000
# for generating symrt0 for generating symbol tables for .dbg programs
SYMSTART=180
SYMSIZE=9216
# OTHER=keyboard_atr.o
OTHER=
OBJS = printf.o sys.o sys2.o sys3.o monotty.o gets.o conf.o parse.o rtt.o \
	premain.o keyboard.o hfetch.o ior.o init.o debug.o unasm.o mtsr.o \
	startup.o hd.o debugger.o initvec.o atox.o setvec.o trace.o \
	fd.o display.o dma.o lps.o screen_conf.o aedtty.o ibm_emul.o \
	std_emul.o apa8tty.o apa16tty.o apa8ctty.o apa_fontblt.o \
	hard_delay.o callabs.o aton.o crcgen.o time.o vtop.o perror.o \
	${OTHER} com.o egatty.o mpeltty.o lptty.o utils.o \
	err.o dkpart.o

.SUFFIXES: .ws .nm .dbg

.out.nm:
	nm -ng $*.out >$*.nm

.out.ws:
	rm -f $*.ws; ln $*.out $*.ws

.s.o:
	./samake "CPP=$(CPP)" $*.o

.c.o:
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" $*.o

.c.s:
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" $*.s

.o.out:
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" $*.out LIB=$(LIB) "LIBC=$(LIBC)"

# make utility with debugger present
.o.dbg:
	touch $*.o;	# force the make to be done
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" $*.out CRT0=symrt0 LIB=$(LIB) LIBC=$(LIBC)
	mergesym $*.out ${SYMSTART} ${SYMSIZE}
	rm -f $*.dbg; ln $*.out $*.dbg

.c.out:
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" $*.o LIB=$(LIB)
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" $*.out LIB=$(LIB) LIBC=$(LIBC)


ALL=$(LIBSA) srt0.o relsrt0.o symrt0.o boothd bootfd boot.out debug minidisk
all: $(ALL) out ws 

allobjs: $(ALL)

out: rdb.out boot.out

ws: rdb.ws 


$(LIBSA):	$(OBJS)
	ar rv $@ $?
	ranlib $@

codes.h:	codes codes.awk
	rm -f codes.h	# get rid of it in case it was RCS'ed
	awk -f codes.awk NAME=codes.h codes

keyboard.o: codes.h keyboard.c
	rm -f keyboard.o
	FLAGS="-DDEBOUNCE -DSECURE" ./samake "CC=$(CC)" keyboard.o

clean:
	rm -f *.lis *.out *.o *.ws codes.h *.S $(LIBSA) *.dbg debug minidisk boot[fh]d boot[fh]d.c cmds.h conf[fh]d.c
	a="`ls *.c *.s | sed 's;\.c;.s;' | uniq -d `"; echo $$a ; rm -f $$a
	make -f Makefile atr_dummy

atr_dummy:

copy.out: copy.o $(LIBSA) srt0.o
	touch copy.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" copy.out LIB=$(LIB) LIBC=$(LIBC) 

format.out: format.o $(LIBSA) 
	touch format.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" format.out  LIB=$(LIB) LIBC=$(LIBC) 

dump.out: dump.o $(LIBSA) srt0.o
	touch dump.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" dump.out LIB=$(LIB) LIBC=$(LIBC) 

wterm.out: wterm.o $(LIBSA) relsrt0.o
	touch wterm.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" wterm.out CRT0=relsrt0 RELOC=70000 LIB=$(LIB) LIBC=$(LIBC)

dkpart.o: ../machine/dkpart.c
	cp ../machine/dkpart.c dkpart.c
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" dkpart.o
	rm -f dkpart.c

crcgen.o: ../machine/crcgen.c
	cp ../machine/crcgen.c crcgen.c
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" crcgen.o
	rm -f crcgen.c

monotty.o: monotty.c mono.h monotty.h screen_conf.h consdefs.h mono_tcap.h sa.h
	rm -f monotty.o
	FLAGS=-DIBM3101 ./samake "CC=$(CC)" monotty.o

egatty.o: egatty.c mono_tcap.h sa.h ega.h
	rm -f egatty.o
	FLAGS=-DIBM3101 ./samake "CC=$(CC)" egatty.o

aedtty.o: aedtty.c aedtty.h aed.h
	rm -f aedtty.o
	FLAGS=-DIBM3101 ./samake "CC=$(CC)" aedtty.o

lptty.o: lptty.c screen_conf.h sa.h
	rm -f lptty.o
	FLAGS=-DIBM3101 ./samake "CC=$(CC)" lptty.o

ibm_emul.o: ibm_emul.c screen_conf.h consdefs.h mono_tcap.h sa.h mono.h
	rm -f ibm_emul.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" ibm_emul.o

std_emul.o: std_emul.c std_emul.h consdefs.h screen_conf.h mono.h sa.h aed.h
	rm -f std_emul.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" std_emul.o

hard_delay.o: hard_delay.c 
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" hard_delay.o

mpeltty.o:	mpeltty.c mpeltty.h screen_conf.h \
	mpel.h 
	rm -f mpeltty.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" mpeltty.o

mpeltty_boot.o: mpeltty.c mpeltty.h screen_conf.h \
	mpel.h mpeltty_font.h
	mv mpeltty.o mpeltty.o.save
	FLAGS="${FLAGS}" ./samake "CC=$(CC) -DLOAD_UCODE" mpeltty.o
	mv mpeltty.o mpeltty_boot.o
	mv mpeltty.o.save mpeltty.o

apa16tty.o:	apa16tty.c apa16tty.h screen_conf.h apa_structs.h  \
	apasixteen.h apaeight.h apaeightc.h apa16tty_font.h
	rm -f apa16tty.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" apa16tty.o

apa16tty_boot.o: apa16tty.c apa16tty.h screen_conf.h apa_structs.h \
	apasixteen.h apaeight.h apaeightc.h apa8tty_font.h
	mv apa16tty.o apa16tty.o.save
	FLAGS="${FLAGS}" ./samake "CC=$(CC) -DUSE_APA8_FONT" apa16tty.o
	mv apa16tty.o apa16tty_boot.o
	mv apa16tty.o.save apa16tty.o

apa8tty.o:	apa8tty.c apa8tty.h apa8tty_font.h screen_conf.h \
	apa_structs.h apasixteen.h apaeight.h apaeightc.h
	rm -f apa8tty.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" apa8tty.o

apa8ctty.o:	apa8ctty.c apa8ctty.h apa8tty_font.h screen_conf.h \
	apa_structs.h apasixteen.h apaeight.h apaeightc.h
	rm -f apa8ctty.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" apa8ctty.o

apa_fontblt.o:	apa_fontblt.c apa_structs.h apasixteen.h apaeight.h apaeightc.h
	rm -f apa_fontblt.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" apa_fontblt.o

#	special version of apa16tty.o is needed for boot to save space 
#	special version of mpeltty.o to load the micro code
boot boot.out:	sautil.o $(LIBSA) apa16tty_boot.o mpeltty_boot.o mpeltty_mcode.o relsrt0.o $(LIBSA)
	touch sautil.o
	./samake sautil.out CRT0=relsrt0 RELOC=$(SAUTIL_RELOC) LIB="apa16tty_boot.o mpeltty_boot.o mpeltty_mcode.o $(LIB)" LIBC=$(LIBC)
	@./endcheck sautil.out 1f8000 " - sautil too big to run on all systems"
	mv sautil.out boot.out

dosboot dosboot.out:	dosboot.o $(LIBSA) relsrt0.o
	touch dosboot.o
	./samake dosboot.out CRT0=relsrt0 RELOC=$(BOOT_RELOC) LIB=$(LIB) LIBC=$(LIBC)

boot.o: boot.c
	FLAGS=-DJUSTASK ./samake "CC=$(CC)" boot.o

fdboot.out: fdboot.o $(LIBSA) relsrt0.o
	./samake fdboot.out CRT0=relsrt0 RELOC=$(BOOT_RELOC) LIB=$(LIB) LIBC=$(LIBC)

hdboot.o: sautil.c $(HD_BOOT)
	cp sautil.c hdboot.c
	FLAGS="-DJUSTASK -DHDBOOT" ./samake "CC=$(CC)" hdboot.o
	$(RM) -f hdboot.c

fdboot.o: sautil.c $(SAUTIL)
	cp sautil.c fdboot.c
	FLAGS="-DJUSTASK -DFDBOOT" ./samake "CC=$(CC)" fdboot.o
	$(RM) -f fdboot.c

# the rather complicated script does the following:
# 1. extract the appropriate definitions out of samake
# 2. from that extract the appropriate definitions from rdb.h
# 3. use that information to link rdb.out and check that it isn't too big.
rdb.out: rdb.o $(LIBSA)  relsrt0.o
	touch rdb.o
	@defines=`grep "CPU=" samake | sed -e 's;CPU=.*--;-;' -e 's;["}]; ;g'` ;\
	eval `unifdef $$defines ../machine/rdb.h | \
	awk ' $$1 == "#define" { print $$2 "=" substr($$3,3) } ' `; \
	echo ./samake rdb.out RELOC=$$RDB_RELOC CRT0=relsrt0 LIB=$(LIB) LIBC=$(LIBC); \
	./samake rdb.out RELOC=$$RDB_RELOC CRT0=relsrt0 LIB=$(LIB) LIBC=$(LIBC); \
	echo "Checking value of _end against RDB_END ($${RDB_END})" ; \
	./endcheck rdb.out $${RDB_END} " - Adjust RDB_END to be bigger in ../machine/rdb.h"

rdb.o: rdb.c cmds.h ../machine/rdb.h
	rm -f rdb.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" rdb.o

cmds.h: debug.c
	grep '^[A-Z][A-Z]*,[ 	]*ARG' $? | awk -F, '{ print "#define",$$1,++cnt,"/*",$$2,$$3,"*/" }' | tr -d '",' | tr -s " 	" "  " > $@


debug.o: debug.c cmds.h

screen_conf.o: screen_conf.c screen_conf.h \
	apasixteen.h apaeight.h apaeightc.h  mono.h aed.h ega.h
	rm -f screen_conf.o
	FLAGS="${FLAGS}" ./samake "CC=$(CC)" screen_conf.o

install: $(DESTDIR)/etc ${ALL}
	install -c boothd $(DESTDIR)/usr/mdec/boothd
	install -c bootfd $(DESTDIR)/usr/mdec/bootfd
	install -c -s minidisk $(DESTDIR)/etc/minidisk
	install -c installboot.sh $(DESTDIR)/usr/mdec/installboot
	install -c -s boot.out $(DESTDIR)/boot
	install -c libsa.a $(DESTDIR)/usr/lib/libsa.a ; \
	ranlib $(DESTDIR)/usr/lib/libsa.a 
	for i in srt0.o relsrt0.o symrt0.o; \
	do\
		install -c $$i $(DESTDIR)/usr/lib/; \
	done

$(DESTDIR)/etc:
	mkdir $@

# we load boothd (and bootfd) at BOOT_RELOC, knowing that boot will
# also be loaded there. boot.c and bootxx.c are smart enough to
# handle this situation. This allows us to replace boot with /vmunix if we want.
boothd: relsrt0.o boothd.o confhd.o hd.old.o dummy.o $(LIBSA)
	touch boothd.o
	./samake boothd.out CRT0=relsrt0 RELOC=$(BOOT_RELOC) "LIB=dummy.o hd.old.o confhd.o $(LIB)" "LDFLAGS = -N -x -e start -M" LIBC=$(LIBC)
	cp boothd.out b.out;strip b.out;dd if=b.out of=boothd ibs=32 skip=1;rm b.out

bootfd: relsrt0.o bootfd.o conffd.o fd.old.o dummy.o $(LIBSA)
	touch bootfd.o
	./samake bootfd.out CRT0=relsrt0 RELOC=$(BOOT_RELOC) "LIB=dummy.o fd.old.o conffd.o $(LIB)" "LDFLAGS = -N -x -e start -M" LIBC=$(LIBC)
	cp bootfd.out b.out;strip b.out;dd if=b.out of=bootfd ibs=32 skip=1;rm b.out

boothd.c: bootxx.c
	sed -e 's/xx/hd/g' <bootxx.c >boothd.c

bootfd.c: bootxx.c
	sed -e 's/xx/fd/g' <bootxx.c >bootfd.c

confhd.c: confxx.c
	sed -e 's/XX/hd/' -e 's/xx/hd/g' <confxx.c >confhd.c

conffd.c: confxx.c
	sed -e 's/XX/fd/' -e 's/xx/fd/g' <confxx.c >conffd.c

hd.old.o: hd.c
	cp hd.c hd.old.c
	FLAGS="-DSQUEEEEZE -UDEBUG" ./samake "CC=$(CC)" hd.old.o
	rm -f hd.old.c

fd.old.o: fd.c
	cp fd.c fd.old.c
	FLAGS="-DSQUEEEEZE -UDEBUG" ./samake "CC=$(CC)" fd.old.o
	rm -f fd.old.c

relsrt0.o: srt0.s
	cp srt0.s relsrt0.s
	./samake "CPP=$(CPP) -DREL" relsrt0.o
	rm -f relsrt0.s

srt0.o: srt0.s
	./samake "CPP=$(CPP)" srt0.o

symrt0.o: srt0.s
	cp srt0.s symrt0.s
	./samake "CPP=$(CPP) -DSYMSIZE=${SYMSIZE} -DSYMSTART=0x${SYMSTART}" symrt0.o
	rm -f symrt0.s

sautil.o: $(SAUTIL)
	rm -f sautil.o
	FLAGS="-DJUSTASK ${FLAGS}" ./samake "CC=$(CC)" sautil.o

DEBUGFILES=debug.o atox.o unasm.o trace.o parse.o printf.o
debug: debugmain.o ${DEBUGFILES} ${LIB}
	cc -o debug debugmain.o ${DEBUGFILES} ${LIB} -ltermcap

minidisk: minidisk.c
	${CC} -DSTANDUTIL -o minidisk minidisk.c

debugmain.o: debugmain.c
	${CC} -c -DSTANDUTIL -I../h debugmain.c
