# Makefile for MemTest-86
#
# Author:		Chris Brady
# Created:		January 1, 1996

#
# Path for the floppy disk device
#
FDISK=/dev/fd0

#
# gcc compiler options, these settings should suffice
#
CCFLAGS=-Wall -m486 -O2 -fomit-frame-pointer
TSTFLAGS=-Wall -m486 -O3 -funroll-loops -fomit-frame-pointer

# WARNING - Read the following carefully if you modify the code in any way!
#
# TXT_ADR sets the origin for the text segment.  Since the  code is
# relocated there are two code segments. The first must match TESTADR in
# defs.h.  The second REL_TXT_ADR must be set to (TESTADR+MAIN_SZ+RELOBASE)
# in defs.h.
# 
# DAT_ADR sets the origin for the data segment. This is is set by hand and
# will need to be adjusted if the code grows much.  The current settings
# leave only a small amount of room for growth. Look at the loader map to
# check this address.  The total size of the text and data segments must
# be less than MAINSZ in defs.h. REL_DAT_ADR should be set to
# REL_TXT_ADR + (DAT_ADR - TXT_ADR)
#
TXT_ADR=0x1000
DAT_ADR=0x7000
REL_TXT_ADR=0x108800
REL_DAT_ADR=0x10e800

OBJS=head.o main.o test.o init.o lib.o

OBJDUMP=objdump -k -q
OBJCOPY=objcopy -O binary -R .note -R .comment -R .stab -R .stabstr

all: setup bootsect head.out relo.out build
	./build bootsect setup head.out relo.out >memtest.bin

test.o: test.c test.h defs.h
	cc -c $(TSTFLAGS) test.c

main.o: main.c test.h defs.h
	cc -c $(CCFLAGS) main.c

init.o: init.c test.h defs.h io.h
	cc -c $(CCFLAGS) init.c

lib.o: lib.c test.h defs.h io.h serial.h
	cc -c $(CCFLAGS) lib.c

relo: $(OBJS)
	ld -m elf_i386 -o $@ -e do_test -Ttext $(REL_TXT_ADR) -Tdata \
		$(REL_DAT_ADR) -Map mapfile.relo $(OBJS)

relo.out: relo
	if hash encaps 2> /dev/null; then \
	    $(OBJDUMP) -o $(REL_TXT_ADR) relo >relo.out; \
	else \
	    $(OBJCOPY) relo relo.out; \
	fi

head.o: head.s
	as -o $@ $<

head: $(OBJS)
	ld -m elf_i386 -o $@ -e do_test -Ttext $(TXT_ADR) -Tdata $(DAT_ADR) \
		-Map mapfile $(OBJS)
head.out: head
	if hash encaps 2> /dev/null; then \
	    $(OBJDUMP) -o $(TXT_ADR) head >head.out; \
	else \
	    $(OBJCOPY) head head.out; \
	fi

head.s: head.S test.h
	cc -E -traditional $< -o $@

setup: setup.o
	ld86 -0 -s -o $@ $<

setup.o: setup.s
	as86 -a -0 -o $@ $<

setup.s: setup.S test.h
	cc -E -traditional $< -o $@

bootsect: bootsect.o
	ld86 -0 -s -o $@ $<

bootsect.o: bootsect.s
	as86 -a -0 -o $@ $<

bootsect.s: bootsect.S test.h
	cc -E -traditional $< -o $@

build:	build.c
	cc -DELF -o build build.c

clean:
	rm -f *.o *.s build memtest.bin bootsect setup head head.out mapfile relo \
	      relo.out mapfile.relo

install: all
	dd <memtest.bin >$(FDISK) bs=8192

install-bin:
	dd <precomp.bin >$(FDISK) bs=8192

