# Makefile for MemTest-86
#
# Author:		Chris Brady
# Created:		January 1, 1996
#
# Enhancements - V1.2:	May 28, 1996  by Loic Prylli
#

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

#
# Comment out the following to disable testing with cache off
#
CACHE=-DCACHE

#
# Comment out the following to disable testing with long refresh timing
#
REFRESH=-DREFRESH

#
# gcc compiler options, these settings should suffice
#
CCFLAGS=-Wall -m486 -O2

# 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
# mtest.h.  The second REL_TXT_ADR must be set to (TESTADR+MAIN_SZ+RELOBASE)
# in mtest.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 mtest.h. REL_DAT_ADR should be set to
# REL_TXT_ADR + (DAT_ADR - TXT_ADR)
#
TXT_ADR=0x1000
DAT_ADR=0x2600
REL_TXT_ADR=0x102800
REL_DAT_ADR=0x103e00

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 >image

mtest.o: mtest.c mtest.h
	cc -c $(CCFLAGS) $(REFRESH) $(CACHE) mtest.c

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

relo: mtest.o
	ld -m elf_i386 -o $@ -e do_test -Ttext $(REL_TXT_ADR) -Tdata \
		$(REL_DAT_ADR) -Map mapfile.relo head.o mtest.o
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.s: head.S mtest.h
	cc -E -traditional $< -o $@

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

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

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

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

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

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

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

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

install: all
	dd <image >$(FDISK) bs=8192

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

