CONF=pccpu
CONFLIST=pc pccpu pcfs pccpuf pcboot pcbootload pcbootpxe
CRAPLIST=pccd pcflop
VENTILIST=pccpuf pcf pcfs
EXTRACOPIES=fsstand consoles

objtype=386
</$objtype/mkfile
p=9

# KZERO & APBOOTSTRAP must match def'ns in mem.h
KZERO=0xE0000000
KTZEROP=0x100020

DEVS=`{rc ../port/mkdevlist $CONF}

PORT=\
	alarm.$O\
	alloc.$O\
	allocb.$O\
	auth.$O\
	awaitbit.$O\
	byteserial.$O\
	cache.$O\
	chan.$O\
	ctype.$O\
	dev.$O\
	devtab.$O\
	edf.$O\
	fault.$O\
	latin1.$O\
	logpow.$O\
	page.$O\
	parse.$O\
	pgrp.$O\
	portclock.$O\
	print.$O\
	proc.$O\
	qio.$O\
	qlock.$O\
	rdb.$O\
	rebootcmd.$O\
	segment.$O\
	sum.$O\
	swap.$O\
	sysfile.$O\
	sysproc.$O\
	taslock.$O\
	tod.$O\
	watermarks.$O\
	xalloc.$O\

OBJ=\
	l.$O\
	plan9l.$O\
	apu.$O\
	cga.$O\
	fp.$O\
	i8253.$O\
	i8259.$O\
	kbd.$O\
	main.$O\
	memmap.$O\
	memory.$O\
	mmu.$O\
	nommudebug.$O\
	mtrr.$O\
	multiboot.$O\
	random.$O\
	syscall.$O\
	syscallfmt.$O\
	trap.$O\
	$CONF.root.$O\
	$CONF.rootc.$O\
	$DEVS\
	$PORT\

ETHER=`{echo devether.c ether*.c | sed 's/\.c/.'$O'/g'}
VGA=`{echo devvga.c screen.c vga*.c | sed 's/\.c/.'$O'/g'}
SDEV=`{echo devsd.c sd*.c | sed 's/\.c/.'$O'/g'}

#	echo -n elf kernel...
#	$LD -o $target.elf -PH5 -R4096 -T$KTZERO -F$KTZEROP $OBJ $CONF.$O
$p$CONF: $CONF.c $OBJ $LIB /$objtype/expand-pxe /$objtype/expand-pbs
	$CC $CFLAGS '-DKERNDATE='`{date -n} $CONF.c
	# this KTZERO sits after the a.out header, unlike mem.h's
	KTZERO=`{echo $KZERO+$KTZEROP'=X' | dbcalc}
	$LD -o $target -T$KTZERO $OBJ $CONF.$O
	size $target
	echo -n self-decompressing kernel...
	{ cat /$objtype/expand-pxe; strzip <$target } >$target^lz
	echo

# don't strip the gzipped kernels -- too frustrating when that's all you have!
$p%.gz:D:	$p%
	gzip -9 <$p$stem >$p$stem.gz
$p%.lz:D:	$p%
	lzip -9 <$p$stem >$p$stem.lz


# pcflop and pccd need all the space they can get
9pcflop.gz:D: 9pcflop
	strip -o /fd/1 9pcflop | gzip -9 >9pcflop.gz

9pccd.gz:D: 9pccd
	strip -o /fd/1 9pccd | gzip -9 >9pccd.gz

/$objtype/expand-pxe /$objtype/expand-pbs: ../pcexp/expand.c ../pcexp/expand.l.s \
	../pcexp/mem.h ../pcexp/a20.c ../pcexp/bios.s ../pcexp/cga.c ../pcexp/print.c
	@ { cd ../pcexp && mk install}

# we don't need gzipped kernels otherwise, so don't make them
install:V:	$p$CONF	# $p$CONF^lz
	cp -x $p$CONF /$objtype/ &
	# 608 = 640 - 32
	if (test `{ls -s $p$CONF^lz | awk '{print $1}'} -lt 608)
		cp -x $p$CONF^lz /$objtype/ &
	for(i in $EXTRACOPIES)
		{ 9fs $i && cp -x $p$CONF $p$CONF^lz /n/$i/$objtype && echo -n $i... & }
	wait
	echo

<../boot/bootmkfile
<../port/portmkfile
<|../port/mkbootrules $CONF

l.$O:				lreal.s
$ETHER: 			etherif.h ../port/netif.h
ether8003.$O ether8390.$O:	ether8390.h
$VGA mouse.$O:			screen.h /sys/include/memdraw.h

#devfloppy.$O: 			floppy.h
archmp.$O mp.$O:		apbootstrap.h
pci.$O pcipc.$O:		io.h
apic.$O archmp.$O mp.$O:	mp.h
$SDEV:				../port/sd.h
sd53c8xx.$O:			sd53c8xx-fw.h
devaoe.$O sdaoe.$O:		../port/aoe.h
main.$O:			init.h reboot.h
memory.$O memmap.$O multiboot.$O: memmap.h multiboot.h
wavelan.$O:			wavelan.c ../pc/wavelan.c ../pc/wavelan.h
etherwavelan.$O:		etherwavelan.c ../pc/wavelan.h
devusb.$O usb*.$O:		../port/usb.h ../port/portusbehci.h ../pc/usbehci.h
trap.$O:			/sys/include/tos.h
uartaxp.$O:			uartaxp-fw.h
etherm10g.$O:			etherm10g2k-fw.h etherm10g4k-fw.h

init.h:D:		../port/initcode.c init9.c
	$CC ../port/initcode.c
	$CC init9.c
	$LD -l -R1 -s -o init.out init9.$O initcode.$O /386/lib/libc.a
	{echo 'uchar initcode[]={'
	 xd -1x <init.out |
		sed -e 's/^[0-9a-f]+ //' -e 's/ ([0-9a-f][0-9a-f])/0x\1,/g'
	 echo '};'} > init.h

# -T argument must match REBOOTADDR in mem.h
reboot.h:D:	rebootcode.s
	$AS rebootcode.s
	$LD -l -s -T0x11000 -R4 -o reboot.out rebootcode.$O
	{echo 'uchar rebootcode[]={'
	 xd -1x reboot.out |
		sed -e '1,2d' -e 's/^[0-9a-f]+ //' -e 's/ ([0-9a-f][0-9a-f])/0x\1,/g'
	 echo '};'} > reboot.h

apbootstrap.h:	apbootstrap.s mem.h
	$AS $prereq
	APBOOTSTRAP=`{echo $KZERO+0x3000'=X' | dbcalc}
	$LD -o apbootstrap.out -T$APBOOTSTRAP -R4 -l -s apbootstrap.$O
	{echo 'uchar apbootstrap[]={'
	 xd -1x apbootstrap.out |
		sed -e '1,2d' -e 's/^[0-9a-f]+ //' -e 's/ ([0-9a-f][0-9a-f])/0x\1,/g'
	 echo '};'} > $target

# convert firmware to C for inclusion
sd53c8xx-fw.h:	sd53c8xx.n
	aux/na $prereq > $target

uartaxp-fw.h:	a100p.cp
	{echo 'static uchar uartaxpcp[] = {'
	 xd -1x $prereq |
		sed -e 's/^[0-9a-f]+ //' -e '/^$/d' -e 's/ ([0-9a-f][0-9a-f])/0x\1,/g'
	 echo '};'
	} > $target

acid:: init.h reboot.h
	8c -a -w -I. main.c >acid

%.checkether:VQ:
	for (i in ether*.c){
		x=`{echo $i | sed 's/\.c//'}
		if(! ~ $x ether8390 && ! grep -s '^	'^$x^'([ 	]|$)' $stem)
			echo $x not included in $stem
	}
	exit 0

%.checkvga:VQ:
	for (i in vga*.c){
		x=`{echo $i | sed 's/\.c//'}
		if(! ~ $x vga vgax vgasavage && ! grep -s '^	'^$x^'([ 	]|$)' $stem)
			echo $x not included in $stem
	}
	exit 0

checkdist:VQ:
	for(i in pcdisk pcflop)
	for(j in checkvga checkether)
		mk $i.$j

%.clean:V:
	rm -f $stem.c [9bz]$stem [9bz]$stem.gz [9bz]$stem^lz boot$stem.* reboot.h apbootstrap.h init.h [9bz]$stem.elf
