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

-include params-soft.mk

ifeq ($(ARCH_NAME),)
$(error Please define 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
IOC_TYPE    ?= IOC_BDV
TXT_TYPE    ?= TXT_TTY
FBF_TYPE    ?= FBF_SCL
SYS_CLK     ?= 50000

# 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               \
        user/windows/build/windows.elf       \
        user/convol/build/convol.elf         \
        user/transpose/build/transpose.elf   \
        user/kleenex/build/kleenex.elf       \
        user/tcp_server/build/tcp_server.elf \
        user/tcp_client/build/tcp_client.elf \
        user/chat/build/chat.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 the virtual disk
# when the corresponding sources files have been modified or destroyed.
# The "home" directory on the virtual disk is not modified
compile: dirs                                 \
         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               \
         user/windows/build/windows.elf       \
         user/convol/build/convol.elf         \
         user/transpose/build/transpose.elf   \
         user/kleenex/build/kleenex.elf       \
         user/tcp_server/build/tcp_server.elf \
         user/tcp_client/build/tcp_client.elf \
         user/chat/build/chat.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 user/windows clean
	$(MAKE) -C user/convol clean
	$(MAKE) -C user/kleenex clean
	$(MAKE) -C user/transpose clean
	$(MAKE) -C user/tcp_server clean
	$(MAKE) -C user/tcp_client clean
	$(MAKE) -C user/chat 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=131072
	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
	mmd             -o -i $(DISK_IMAGE) ::/home/convol                    || true
	mmd             -o -i $(DISK_IMAGE) ::/home/transpose                 || true
	mmd             -o -i $(DISK_IMAGE) ::/home/fft                       || true
	mmd             -o -i $(DISK_IMAGE) ::/misc                           || true 
	mcopy           -o -i $(DISK_IMAGE) images/lena_256.raw ::/misc       || true             
	mcopy           -o -i $(DISK_IMAGE) images/images_128.raw ::/misc     || true             
	mcopy           -o -i $(DISK_IMAGE) images/philips_1024.raw ::/misc   || true             
	mcopy           -o -i $(DISK_IMAGE) images/couple_512.raw ::/misc     || true             
	mcopy           -o -i $(DISK_IMAGE) images/wood_32.raw ::/misc        || true             
	mcopy           -o -i $(DISK_IMAGE) images/door_32.raw ::/misc        || true             
	mcopy           -o -i $(DISK_IMAGE) images/rock_32.raw ::/misc        || true             
	mcopy           -o -i $(DISK_IMAGE) images/handle_32.raw ::/misc      || 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: $(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)	    \
					--ioc_type=$(IOC_TYPE)	    \
                    --txt_type=$(TXT_TYPE)      \
                    --fbf_type=$(FBF_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
user/windows/build/windows.elf: build_libs
	$(MAKE) -C user/windows
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
user/convol/build/convol.elf: build_libs
	$(MAKE) -C user/convol
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
user/kleenex/build/kleenex.elf: build_libs
	$(MAKE) -C user/kleenex
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
user/transpose/build/transpose.elf: build_libs
	$(MAKE) -C user/transpose
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
user/tcp_server/build/tcp_server.elf: build_libs
	$(MAKE) -C user/tcp_server
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
user/tcp_client/build/tcp_client.elf: build_libs
	$(MAKE) -C user/tcp_client
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
user/chat/build/chat.elf: build_libs
	$(MAKE) -C user/chat
	mcopy -o -i $(DISK_IMAGE) $@ ::/bin/user
