#############################################################################
#                         ALMOS-MKH Global Makefile                         #
#############################################################################

-include params-soft.mk
ifeq ($(ARCH_NAME),)
$(error Please define in ARCH_NAME parameter in params-soft.mk!)
endif

-include params-hard.mk

# Default values for hardware parameters.
# These parameters should be defined in the 'params-hard.mk' file.
ARCH		?= /users/alain/soc/tsar-trunk-svn-2013/platforms/tsar_generic_iob
X_SIZE		?= 2
Y_SIZE		?= 2
NB_PROCS	?= 2
NB_TTYS		?= 3
FBF_WIDTH	?= 256
IOC_TYPE	?= IOC_BDV

# Checking hardware platform definition.
ifeq ($(wildcard $(ARCH)),)
$(error Please define in ARCH parameter the path to the hardware platform!)
endif

# Rules that don't build target files
# always out-of-date, need to be regenerated everytime they are called
.PHONY: compile		     	               \
        hard_config.h                      \
		dirs				               \
		list				               \
		extract				               \
		fsck				               \
		clean				               \
		build_libs                         \
		build-disk			               \
		$(BOOTLOADER_PATH)/build/boot.elf  \
		kernel/build/kernel.elf            \
		user/init/build/init.elf           \
		user/ksh/build/ksh.elf             \
		user/pgdc/build/pgcd.elf           \
		user/idbg/build/idbg.elf           \
		user/sort/build/sort.elf           \
        user/fft/build/fft.elf

# Virtual disk path
DISK_IMAGE	:= hdd/virt_hdd.dmg

# The Mtools used to build the FAT32 disk image perfom a few sanity checks, to
# make sure that the disk is indeed an MS-DOS disk. However, the size of the
# disk image used by ALMOS-MKH is not MS-DOS compliant.
# Setting this variable prevents these checks.
MTOOLS_SKIP_CHECK := 1

# Rule to generate boot.elf, kernel.elf, all user.elf files, and update virtual disk.
compile: dirs                              \
         build_disk                        \
         hard_config.h                     \
         build_libs                        \
         $(BOOTLOADER_PATH)/build/boot.elf \
         kernel/build/kernel.elf           \
         user/init/build/init.elf          \
         user/ksh/build/ksh.elf            \
         user/pgcd/build/pgcd.elf          \
         user/idbg/build/idbg.elf          \
         user/sort/build/sort.elf          \
         user/fft/build/fft.elf           \
         list

# Rule to create the hdd directory
dirs:
	@mkdir -p hdd

# Rule to make a recursive list of the virtual disk content.
list:
	mdir -/ -b -i $(DISK_IMAGE) ::/

##############################################################
# Rule to copy the files generated by the virtual prototype
# from the virtual disk 'home' directory to the current directory.
extract:
	mcopy -o -i $(DISK_IMAGE) ::/home .

# Rules to delete all binary files from Unix File System
# without modifying the virtual disk.
clean:
	rm -f hard_config.h arch_info.xml arch_info.bin
	rm -rf build
	$(MAKE) -C kernel clean
	$(MAKE) -C $(BOOTLOADER_PATH) clean
	$(MAKE) -C $(LIBC_PATH) clean
	$(MAKE) -C $(LIBPTHREAD_PATH) clean
	$(MAKE) -C $(LIBSEMAPHORE_PATH) clean
	$(MAKE) -C $(LIBALMOSMKH_PATH) clean
	$(MAKE) -C $(LIBMATH_PATH) clean
	$(MAKE) -C user/init clean
	$(MAKE) -C user/ksh clean
	$(MAKE) -C user/sort clean
	$(MAKE) -C user/pgcd clean
	$(MAKE) -C user/idbg clean
	$(MAKE) -C user/fft clean
	$(MAKE) -C $(HAL_ARCH) clean

####################################################	
# Rule for building a new virtual disk from scratch.
# It creates the bin, bin/kernel, bin/user, and home directories.
# This requires the generic Linux/MacOS 'create_dmg' script, that should be
# placed in the same directory as this Makefile.
build_disk: dirs
	rm -f $(DISK_IMAGE)
	./create_dmg  	create $(basename $(DISK_IMAGE))
	dd 				if=$(DISK_IMAGE) of=temp.dmg count=65536
	mv 				temp.dmg $(DISK_IMAGE)
	mmd 			-o -i $(DISK_IMAGE) ::/bin         || true
	mmd 			-o -i $(DISK_IMAGE) ::/bin/kernel  || true
	mmd 			-o -i $(DISK_IMAGE) ::/bin/user    || true
	mmd 			-o -i $(DISK_IMAGE) ::/home        || true
	mdir		 -/ -b -i $(DISK_IMAGE) ::/
	
##############################################################
# Rules to generate hardware description files (hard_config.h,
# arch_info.bin and arch_info.xml), and update the virtual disk.
hard_config.h: build_disk $(ARCH)/arch_info.py
	tools/arch_info/genarch.py	--arch=$(ARCH)			\
								--x_size=$(X_SIZE)		\
								--y_size=$(Y_SIZE)		\
								--nb_cores=$(NB_PROCS)	\
								--nb_ttys=$(NB_TTYS)	\
								--fbf_size=$(FBF_WIDTH)	\
								--ioc_type=$(IOC_TYPE)	\
								--sys_clk=$(SYS_CLK)	\
								--hard=.				\
								--bin=.					\
								--xml=.					
	mcopy -o -i $(DISK_IMAGE) arch_info.bin ::/   || true
	mdir		 -/ -b -i $(DISK_IMAGE) ::/

############################################
# Rules to generate the user level libraries
build_libs: build_hal
	$(MAKE) -C $(LIBALMOSMKH_PATH) headers
	$(MAKE) -C $(LIBPTHREAD_PATH) headers
	$(MAKE) -C $(LIBSEMAPHORE_PATH) headers
	$(MAKE) -C $(LIBMATH_PATH) headers
	$(MAKE) -C $(LIBC_PATH)
	$(MAKE) -C $(LIBALMOSMKH_PATH)
	$(MAKE) -C $(LIBPTHREAD_PATH)
	$(MAKE) -C $(LIBSEMAPHORE_PATH) 
	$(MAKE) -C $(LIBMATH_PATH) 

#####################################################################
# Rule to generate boot.elf and place it in sector #2 of virtual disk
$(BOOTLOADER_PATH)/build/boot.elf:
	$(MAKE) -C $(BOOTLOADER_PATH)
	dd if=$@ of=$(DISK_IMAGE) seek=2 conv=notrunc

######################################################
# Rule to generate HAL objects (depending on HAL_ARCH)
build_hal:
	$(MAKE) -C $(HAL_ARCH)

#############################################################
# Rule to generate kernel.elf and place it on virtual disk
kernel/build/kernel.elf: build_hal
	$(MAKE) -C kernel/
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/kernel

#####################################################
# Rules to generate user.elf and copy on virtual disk
user/init/build/init.elf: build_libs
	$(MAKE) -C user/init
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
user/ksh/build/ksh.elf: build_libs
	$(MAKE) -C user/ksh
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
user/pgcd/build/pgcd.elf: build_libs
	$(MAKE) -C user/pgcd
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
user/idbg/build/idbg.elf: build_libs
	$(MAKE) -C user/idbg
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
user/sort/build/sort.elf: build_libs
	$(MAKE) -C user/sort
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
user/fft/build/fft.elf: build_libs
	$(MAKE) -C user/fft
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
