ifneq ($(wildcard make.config),)
include make.config
endif

.PRECIOUS: %.o %.main.elf		# don't erase intermediate files!

########################################################################
# Global environment variables and settings
#

MAJOR_VERSION = 1
MINOR_VERSION = 4
MINISCULE_VERSION = 6

MAIN_MAP = main.lds
FSB_MAP  = fsb.lds
INIT_MAP = init.lds
APPMAP= applet.lds
DIAGS = $(LOCALROOT)/sw/diags
PALDIR = $(LOCALROOT)/sw/palcode
FWTOOLS = $(LOCALROOT)/sw/fwtools
LIBDIR = $(DIAGS)/libs

# added superpage bits here to eliminate page faults in DBM (1-1 mapping)

DBMENTRY = 0xFFFF800000010000
APPENTRY = 0xFFFF800000800000			# link apps to run at 8M
PALENTRY = 8000

ASFLAGS = -m21264 

ifndef CC
CC = gcc
endif

GCCLIBNAME = $(shell $(CC) -print-libgcc-file-name)


IFLAGS = -I$(DIAGS) -I$(DIAGS)/include -I$(PALDIR)/include
CFLAGS = -Os -mcpu=21264 -Wall -fno-builtin

#CPP = /usr/cygnus/alphalinux-000201/H-alpha-linux-gnu/bin/cpp
#CPP = cpp
CPP = /lib/cpp
LDSFLAGS = -P -U LANGUAGE_C -U __LANGUAGE_C__ -U __LANGUAGE_C
CPPFLAGS = $(IFLAGS)

LDFLAGS = -relax -N -Bstatic 
INITLDFLAGS = -relax -N -Bstatic 



OBJSTRIP	= $(FWTOOLS)/bin/objstrip
SYSGEN 		= $(FWTOOLS)/bin/sysgen
MAKEROM		= $(FWTOOLS)/bin/makerom
MKDATA		= $(FWTOOLS)/bin/mkdata
HAL		= $(FWTOOLS)/hal/hal


########################################################################
# Core components of the various modules
# 

HDR_s = dbmentry
DBM_s = asmstuff
DBM_c = main uilib file printf console memory xmodem handler tabledriver \
	hwrpb ftype time ctype fileio interrupts fatdrv strtoul \
	scanf rand string atof linux bios abort flashfw smp file_elf asset info
ifdef USE_DLD
DBM_c += dld
endif
CPU_c = ev6
DEV_c = cmos_rtc sromport uart floppy beep vga vga_console vgafont \
	kbd kbd_xlate lpt rom nvram nvramenv
DIAGS_s = mem_testcores vga_widgets # csrlib smallmem
DIAGS_c = floppy mem mem_tests cmos irq vga isadevs go smp i2c pci nvram \
	  smp_ipi nvlog asset
ifdef USE_TEST
DIAGS_c += test 
endif	# USE_TEST

ZIP_s = 
ZIP_c = misc unzip inflate

INIT_s= init_hdr
INIT_c= init init_io init_unzip unzip inflate

#
# Platform dependent components
#

ifdef CONFIG_NAUTILUS
SYS = nautilus
PL_c = nautilus nautilus_rom
I2C_c = i2c_module lm75 adm9240 pcf8574 pcf-eprom
PALCODE = $(PALDIR)/bin/nautilus_dbm.pal
# 0 for debug monitor, 9 for diags
DIAGS_ROMID	= 9
# part number of this binary 
DIAGS_PART	= 61-0039-3B
DIAGS_ROM	= 0x80000
DIAGS_BASE	= 0x8000
# 6 = fail-safe booter, 0 = dbm for old SROM code
FSB_ROMID	= 0
FSB_PART	= 61-0041-3B
FSB_ROM		= 0xc0000
FSB_BASE	= 0x8000
endif


ifdef CONFIG_SWORDFISH
SYS = swordfish
PL_c = swordfish swordfish_rom
I2C_c = i2c_module lm75 adm9240 pcf8574 pcf-eprom
PALCODE = $(PALDIR)/bin/swordfish_dbm.pal
# 0 for debug monitor, 9 for diags
DIAGS_ROMID	= 9
DIAGS_PART	= 61-0047-3B
DIAGS_ROM	= 0x60000
DIAGS_BASE	= 0x8000
# 6 = fail-safe booter
FSB_ROMID	= 6
FSB_PART	= 61-0046-3B
FSB_ROM		= 0x0000
FSB_BASE	= 0x8000
endif


ifdef CONFIG_SHARK
SYS = swordfish
PL_c = shark swordfish_rom
I2C_c = i2c_module adm9240 lm75 pcf8574 pcf-eprom
PALCODE = $(PALDIR)/bin/swordfish_dbm.pal
# 0 for debug monitor, 9 for diags
DIAGS_ROMID	= 9
DIAGS_PART	= 61-0056-3B
DIAGS_ROM	= 0x80000
DIAGS_BASE	= 0x8000
# 6 = fail-safe booter
FSB_ROMID	= 6
FSB_PART	= "SW-BETA"
FSB_ROM		= 0x0000
FSB_BASE	= 0x8000
endif


ifdef CONFIG_TINOSA
SYS = tinosa
PL_c = tinosa tinosa_rom
I2C_c = i2c_module lm75 adm9240 pcf8574 pcf-eprom
PALCODE = $(PALDIR)/bin/tinosa_dbm.pal
# 0 for debug monitor, 9 for diags
DIAGS_ROMID	= 9
DIAGS_PART	= 61-0053-3B
DIAGS_ROM	= 0x80000
DIAGS_BASE	= 0x8000
# 6 = fail-safe booter
FSB_ROMID	= 6
FSB_PART	= 61-0054-3B
FSB_ROM		= 0xc0000
FSB_BASE	= 0x8000
endif


# Build options for the northbridge

ifdef CONFIG_IRONGATE
NB_s =
NB_c = irongate pci
endif

ifdef CONFIG_TSUNAMI
NB_s = 
NB_c = tsunami pci
endif

ifdef CONFIG_CASPIAN
NB_s = 
NB_c = caspian pci
endif


# Build options for the southbridge

ifdef CONFIG_ALI		# ALI M1543 southbridge
SB_s = 
SB_c = ali acersio pic
endif


ifdef CONFIG_CYPRESS		# Cypress CY693 southbridge
SB_s = 
SB_c = cypress 
endif


ifdef CONFIG_SMCC669		# SMCC669 Super-IO chip
SB_c += smcc669
endif


ifdef CONFIG_PCF8584		# Philips PCF8584 I2C bus controller
SB_c += pcf8584
endif


#
# fixups for including the DEC BIOS emulator
#
ifdef USE_BIOSEMULATOR 

BIOSEM_LDFLAGS = -defsym printk=printf_dbm -defsym printf=printf_dbm \
		-defsym sprintf=sprintf_dbm

#BIOSEM_O = bios cpp diag iface oper_cpp target control cpu hw instr quad x86tbl
#BIOSEM_OBJS = $(BIOSEM_O:%=$(LIBDIR)/%.o)
#BIOSEM_LIBS += $(BIOSEM_OBJS) \
#	/usr/lib/gcc-lib/alpha-redhat-linux/egcs-2.91.66/libgcc.a

BIOSEM_LIBS += $(LIBDIR)/biosem.a \
	$(GCCLIBNAME)

endif	# USE_BIOSEMULATOR



#
# fixups for building an image with IDE support
#
ifdef USE_IDE

#DEV_c += ide

endif	# USE_IDE





########################################################################
# the unified list of all required object modules
#

HDR_S = $(HDR_s:%=dbm/%.S)
HDR_O = $(HDR_S:%.S=%.o)

DBM_S = $(DBM_s:%=dbm/%.S)
DBM_C = $(DBM_c:%=dbm/%.c)
DBM_O = $(DBM_S:%.S=%.o) $(DBM_C:%.c=%.o)

CPU_C = $(CPU_c:%=cpu/%.c)
CPU_O = $(CPU_S:%.S=%.o) $(CPU_C:%.c=%.o)

DEV_C = $(DEV_c:%=devices/%.c)
DEV_O = $(DEV_C:%.c=%.o)

DIAGS_S = $(DIAGS_s:%=diagnostics/%.S)
DIAGS_C = $(DIAGS_c:%=diagnostics/%.c)
DIAGS_O = $(DIAGS_S:%.S=%.o) $(DIAGS_C:%.c=%.o)

ZIP_C = $(ZIP_c:%=zip/%.c)
ZIP_O = $(ZIP_s:%=zip/%.o) $(ZIP_c:%=zip/%.o)

PL_C = $(PL_c:%=platform/%.c)
PL_O = $(PL_C:%.c=%.o)

I2C_C = $(I2C_c:%=i2c/%.c)
I2C_O = $(I2C_C:%.c=%.o)

NB_S = $(NB_s:%=northbridge/%.S)
NB_C = $(NB_c:%=northbridge/%.c)
NB_O = $(NB_S:%.S=%.o) $(NB_C:%.c=%.o)

SB_S = $(SB_s:%=southbridge/%.S)
SB_C = $(SB_c:%=southbridge/%.c)
SB_O = $(SB_S:%.S=%.o) $(SB_C:%.c=%.o)

INIT_S=$(INIT_s:%=init/%.S)
INIT_C=$(INIT_c:%=init/%.c)
INIT_O=$(INIT_S:%.S=%.o) $(INIT_C:%.c=%.o)

MAIN_O= $(HDR_O) $(NB_O) $(SB_O) $(PL_O) $(I2C_O) $(DEV_O) $(DBM_O) $(CPU_O) \
	$(DIAGS_O) $(ZIP_O)
ALL_O = $(MAIN_O) $(INIT_O)

# Add this on to the end of any libs included.

LIBS += $(LIBDIR)/mylibc.a

########################################################################
# Failsafe booter uses a choice selection of files...

FSB_C = fsb/main.c fsb/stubs.c \
	devices/rom.c dbm/time.c dbm/fileio.c dbm/fatdrv.c devices/floppy.c \
	devices/beep.c dbm/malloc.c dbm/string.c dbm/ctype.c \
	devices/cmos_rtc.c dbm/tabledriver.c dbm/printf.o
	
FSB_S = fsb/init_hdr.S dbm/asmstuff.S # fsb/fsbentry.S

# Build options for the northbridge

ifdef CONFIG_IRONGATE
FSB_C += northbridge/irongate.c northbridge/pci.c
endif

ifdef CONFIG_TSUNAMI
FSB_C += northbridge/tsunami.c northbridge/pci.c
endif

ifdef CONFIG_CASPIAN
FSB_C += northbridge/caspian.c northbridge/pci.c
endif


# Build options for the southbridge

ifdef CONFIG_ALI
FSB_C += southbridge/ali.c southbridge/acersio.c
endif

ifdef CONFIG_CYPRESS
FSB_C += southbridge/cypress.c southbridge/smcc669.c
endif


# Build options for the platform

ifdef CONFIG_NAUTILUS
FSB_C += platform/nautilus_rom.c
endif

ifdef CONFIG_SWORDFISH
FSB_C += platform/swordfish_rom.c
endif

ifdef CONFIG_TINOSA
FSB_C += platform/tinosa_rom.c
endif

FSB_O=$(FSB_S:%.S=%.o) $(FSB_C:%.c=%.o)

########################################################################
# Main targets and build rules
#

DIAGS_PART_STRING = "${MAJOR_VERSION}.${MINOR_VERSION}-${MINISCULE_VERSION} \
	${DIAGS_PART}"

FSB_PART_STRING = "${MAJOR_VERSION}.${MINOR_VERSION}-${MINISCULE_VERSION} \
	${FSB_PART}"

.PHONY: all diags fsb objs clean distclean make.config config

#all: config diags.rom fsb.rom config

all:
	@echo "You must 'make config' then 'make diags.rom'"

config: clean
	./Configure

objs: 
	@echo $(MAIN_O)

diags: diags.main.bin.gz.o diags.rom

fsb: fsb.main.bin.gz.o fsb.rom

# sources we depend on (besides sw/diags/...)

CVSREVISION	= -r api-release-1.3.3	# distribution
CVSREVISION	= -A			# main line

depsources:
	cvs co $(CVSREVISION) sw/palcode
	cvs co $(CVSREVISION) sw/fwtools
	cvs co $(CVSREVISION) sw/linux22/include/asm-alpha
#
# build rules
#

%.lds: %.ld include/memory.h
	$(CPP) $(LDSFLAGS) $(CPPFLAGS) $< > $@

$(OBJSTRIP):
	@echo Making  $(OBJSTRIP)...
	cd $(FWTOOLS);make

$(HAL):
	@echo Making  $(HAL)...
	cd $(FWTOOLS);make

$(PALCODE): $(HAL)
	@echo Making PALcode for $(SYS)...
	make -C $(PALDIR) $(SYS)

%.o: %.S
	$(CPP) $(CPPFLAGS) $< -o /tmp/$(*F).s
	$(AS) $(ASFLAGS) -o $@ /tmp/$(*F).s
	@rm -f /tmp/$(*F).s

$(LIBDIR)/%.a: $(LIBDIR)/%.uue
	uudecode -o $@ $<

%.init.o: $(INIT_O)
	ld -r -o $@ $(INIT_O)

%.main.elf: $(MAIN_O) $(LIBS) make.config $(BIOSEM_LIBS) $(MAIN_MAP)
	ld $(LDFLAGS) $(BIOSEM_LDFLAGS) -o $@ -T $(MAIN_MAP) $(MAIN_O) $(BIOSEM_LIBS) $(LIBS) 

%.bin: %.elf $(OBJSTRIP)
	cp $< $*.strip
	strip $*.strip
	$(OBJSTRIP) -v $*.strip $@

%.bin.gz: %.bin
	gzip -f $<

%.main.bin.gz: %.main.bin
	gzip -f $<

%.main.bin.gz.c: %.main.bin.gz
	$(MKDATA) $< $@

%.main.bin.gz.o: %.main.bin.gz.c
	$(CC) -c -o $@ $*.main.bin.gz.c

%.init.elf: %.main.bin.gz.o $(INIT_O) make.config $(INIT_MAP)
	ld $(INITLDFLAGS) -o $@ -T $(INIT_MAP) $(INIT_O) $*.main.bin.gz.o

%.init.bin: %.init.elf
	cp $< $*.strip
	strip $*.strip
	$(OBJSTRIP) -v $*.strip $@

%.img: %.init.bin $(PALCODE)
	$(SYSGEN) -v -e$(PALENTRY) -s $(PALCODE) -e$(DBMENTRY) -s $< -o $@ 2>&1 | tee $@.lst

diags.rom: diags.img
	$(MAKEROM) -v -d 2 -i $(DIAGS_ROMID) -p $(DIAGS_PART_STRING) -r $(DIAGS_ROM) -l $(DIAGS_BASE) $< -o $@ | tee $@.lst

fsb.rom: $(FSB_O) $(PALCODE) make.config $(BIOSEM_LIBS) $(LIBS) $(FSB_MAP)
	ld $(LDFLAGS) $(BIOSEM_LDFLAGS) -o fsb.elf -T $(FSB_MAP) $(FSB_O) $(BIOSEM_LIBS) $(LIBS)
	cp fsb.elf fsb.strip
	strip fsb.strip
	$(OBJSTRIP) -v fsb.strip fsb.bin
	$(SYSGEN) -v -e$(PALENTRY) -s $(PALCODE) -e$(DBMENTRY) -s fsb.bin -o fsb.img >fsb.img.lst
	$(MAKEROM) -v -i $(FSB_ROMID) -p $(FSB_PART_STRING) -r $(FSB_ROM) -l $(FSB_BASE) fsb.img -o $@ >$@.lst

#
# Build rules for downloadable applets
#

%.app: %.o $(SYS).main.elf
	ld -N -Bstatic -o $*.elf -T $(APPMAP) $< $(SYS).main.elf
	cp $*.elf $*.strip
	strip $*.strip
	$(OBJSTRIP) -v $*.strip $@

APP_c = blob #shark # asmtest # romslurp # pcf8584 adm-dbg test_app test_pcc romscan romdump 
APPLETS = $(APP_c:%=applets/%.app)

applets: $(APPLETS)



devices/ide.o: devices/ide.c devices/triton.c
	cd devices; make -f ide.mk


#
# Misc targets
#

clean:
	rm -f $(DIAGS_O) $(CPU_O) $(NB_O) $(SB_O) $(PL_O) $(I2C_O) $(DEV_O) \
		$(DBM_O) $(INIT_O) $(HDR_O) $(ZIP_O)
	rm -f *.rom *.img *.elf *.bin.gz.o *.bin.gz.c *.strip *.bin *.map
	rm -f applets/*.app applets/*.elf applets/*.strip applets/*.o
	rm -f diags.img.lst diags.rom.lst
	rm -f $(APPLETS)

distclean: clean
	rm -f make.config make.config.old include/diag_conf.h
	rm -f libs/mylibc.a libs/biosem.a
	make -C $(FWTOOLS) clean
	make -C $(PALDIR) clean

