#############################################################################
# 						ALMOS-MK  Makefile for TSAR                         #
#############################################################################

-include params.mk

# Compilation flags.
CFLAGS = -Wall -ffreestanding -mno-gpopt -mips32 -g -O2 \
	 	 -fno-delete-null-pointer-checks
#	 	 -fno-delete-null-pointer-checks -std=c99

# Export all variables to applications sub-Makefile.
export 
CC = mipsel-unknown-elf-gcc
AS = mipsel-unknown-elf-as
LD = mipsel-unknown-elf-ld
DU = mipsel-unknown-elf-objdump
AR = mipsel-unknown-elf-ar

# Default values for hardware parameters.
# These parameters should be defined in the 'params.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		?= 1
FBF_WIDTH	?= 256
IOC_TYPE	?= IOC_BDV
MWR_TYPE	?= MWR_CPY


# 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			        \
		dirs				    \
		list				    \
		extract				    \
		fsck				    \
		clean				    \
		build-disk			    \
		build/boot/boot.elf	    \
		build/kernel/kernel.elf	

# Objects to be linked for boot.elf generation
BOOT_OBJS 	= build/boot/boot.o            \
			  build/boot/boot_fat32.o      \
			  build/boot/boot_utils.o      \
			  build/boot/boot_entry.o      \
              build/boot/boot_tty_driver.o \
              build/boot/boot_hba_driver.o \
		   	  build/boot/boot_bdv_driver.o \
		   	  build/boot/boot_mmc_driver.o	

# List of directories to be searched for included files
# when compiling for boot.elf generation
BOOT_INCLUDE = -Itools/bootloader_tsar  \
		       -Itools/arch_info        \
               -Ihal/tsar_mips32        \
               -I.

# Objects to be linked for the soclib drivers library 
DRIVERS_OBJS = build/kernel/drivers/soclib_tty.o  \
               build/kernel/drivers/soclib_bdv.o  \
               build/kernel/drivers/soclib_hba.o  \
               build/kernel/drivers/soclib_mmc.o  \
               build/kernel/drivers/soclib_xcu.o  \
               build/kernel/drivers/soclib_pic.o  \
               build/kernel/drivers/soclib_nic.o  \
               build/kernel/drivers/soclib_iob.o

# Objects to be linked for kernel.elf generation 
KERN_OBJS	= build/kernel/kern/kernel_init.o     \
              build/kernel/kern/printk.o          \
              build/kernel/kern/thread.o          \
              build/kernel/kern/process.o         \
              build/kernel/kern/device.o          \
              build/kernel/kern/cluster.o         \
              build/kernel/kern/scheduler.o       \
              build/kernel/kern/core.o            \
              build/kernel/kern/dqdt.o            \
              build/kernel/kern/remote_sem.o      \
              build/kernel/kern/rpc.o

HAL_OBJS    = build/kernel/hal/hal_special.o      \
              build/kernel/hal/hal_context.o      \
              build/kernel/hal/hal_atomic.o       \
              build/kernel/hal/hal_remote.o       \
              build/kernel/hal/hal_uspace.o       \
              build/kernel/hal/hal_irqmask.o      \
              build/kernel/hal/hal_gpt.o       

DEV_OBJS    = build/kernel/devices/dev_txt.o      \
              build/kernel/devices/dev_ioc.o      \
              build/kernel/devices/dev_icu.o      \
              build/kernel/devices/dev_mmc.o      \
              build/kernel/devices/dev_nic.o      \
              build/kernel/devices/dev_pic.o      \
              build/kernel/devices/dev_iox.o 

MM_OBJS     = build/kernel/mm/ppm.o               \
              build/kernel/mm/vmm.o               \
              build/kernel/mm/vseg.o              \
              build/kernel/mm/page.o              \
              build/kernel/mm/kcm.o               \
              build/kernel/mm/khm.o               \
              build/kernel/mm/mapper.o            \
              build/kernel/mm/kmem.o

LIBK_OBJS   = build/kernel/libk/grdxt.o           \
              build/kernel/libk/bits.o            \
              build/kernel/libk/elf.o             \
              build/kernel/libk/string.o          \
              build/kernel/libk/ctype.o           \
              build/kernel/libk/rwlock.o          \
              build/kernel/libk/spinlock.o        \
              build/kernel/libk/remote_barrier.o  \
              build/kernel/libk/remote_spinlock.o \
              build/kernel/libk/remote_rwlock.o   \
              build/kernel/libk/remote_fifo.o     \
              build/kernel/libk/memcpy.o          \
              build/kernel/libk/xhtab.o

SYS_OBJS    = build/kernel/syscalls/sys_exec.o          \
              build/kernel/syscalls/sys_thread_create.o

VFS_OBJS    = build/kernel/vfs/vfs.o              \
              build/kernel/vfs/fatfs.o            \
              build/kernel/vfs/ramfs.o

# List of directories to be searched for included files
# when compiling for kernel.elf generation 
KERNEL_INCLUDE = -Ikernel                \
                 -Ikernel/kern           \
                 -Ikernel/devices        \
                 -Ikernel/drivers/soclib \
                 -Ikernel/syscalls       \
                 -Ikernel/libk           \
                 -Ikernel/mm             \
                 -Ikernel/vfs            \
                 -Ikernel/fs/fatfs       \
                 -Ikernel/fs/sysfs       \
                 -Ikernel/fs/ramfs       \
                 -Ikernel/fs/devfs       \
                 -Itools/arch_info       \
                 -Ihal/tsar_mips32       \
                 -I.

# Macros to be processed by the C preprocessor.
MACROS		 = -DARCHINFO_PATHNAME="\"arch_info.bin\""		\
			   -DKERNEL_PATHNAME="\"/bin/kernel/kernel.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-VM is not MS-DOS compliant. 
# Setting this variable prevents these checks.
MTOOLS_SKIP_CHECK := 1

# Rule to compile boot.elf, kernel.elf, and update virtual disk.
compile: dirs			            \
		 hard_config.h              \
		 build/boot/boot.elf	    \
         build/kernel/kernel.elf    \
         list

# Rule to create the build directories.
dirs:
	@mkdir -p build/boot
	@mkdir -p build/kernel
	@mkdir -p build/kernel/kern
	@mkdir -p build/kernel/hal
	@mkdir -p build/kernel/devices
	@mkdir -p build/kernel/mm
	@mkdir -p build/kernel/libk
	@mkdir -p build/kernel/drivers
	@mkdir -p build/kernel/vfs 
	@mkdir -p hdd

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

# Rule to make a file system check for the virtual disk.
fsck:
	fsck.msdos $(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 *.o *.elf *.bin *.txt core
	rm -f hard_config.h arch_info.xml arch_info.bin
	rm -rf build

####################################################	
# Rule for building a new virtual disk from scratch.
# It creates the bin / home directories.
# This requires the generic Linux/MacOS 'create_dmg' script, that should be
# placed in the same directory as this Makefile.
build-disk:
	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) ::/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: $(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)	\
								--mwr_type=$(MWR_TYPE)	\
								--hard=.				\
								--bin=.					\
								--xml=.					
	mcopy -o -i $(DISK_IMAGE) arch_info.bin ::/   || true

################################################
# Rules to compile boot drivers used by boot.elf
build/boot/boot_tty_driver.o:	tools/bootloader_tsar/boot_tty_driver.c \
								tools/bootloader_tsar/boot_tty_driver.h \
								tools/bootloader_tsar/boot_utils.h      \
								tools/bootloader_tsar/boot_config.h     \
								hal/tsar_mips32/hal_types.h             \
								hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

build/boot/boot_bdv_driver.o:	tools/bootloader_tsar/boot_bdv_driver.c \
								tools/bootloader_tsar/boot_bdv_driver.h \
								tools/bootloader_tsar/boot_utils.h      \
								tools/bootloader_tsar/boot_config.h     \
								hal/tsar_mips32/hal_types.h             \
								hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

build/boot/boot_hba_driver.o:	tools/bootloader_tsar/boot_hba_driver.c \
								tools/bootloader_tsar/boot_hba_driver.h \
								tools/bootloader_tsar/boot_utils.h      \
								tools/bootloader_tsar/boot_config.h     \
								hal/tsar_mips32/hal_types.h             \
								hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

build/boot/boot_mmc_driver.o:	tools/bootloader_tsar/boot_mmc_driver.c \
								tools/bootloader_tsar/boot_mmc_driver.h \
								tools/bootloader_tsar/boot_utils.h      \
								tools/bootloader_tsar/boot_config.h     \
								hal/tsar_mips32/hal_types.h             \
								hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

##############################
# Rule to compile boot_fat32.o
build/boot/boot_fat32.o:	 	tools/bootloader_tsar/boot_fat32.c      \
								tools/bootloader_tsar/boot_fat32.h      \
								tools/bootloader_tsar/boot_utils.h      \
								tools/bootloader_tsar/boot_config.h     \
								hal/tsar_mips32/hal_types.h             \
								hard_config.h			
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

##############################
# Rule to compile boot_utils.o 
build/boot/boot_utils.o:		tools/bootloader_tsar/boot_utils.c       \
								tools/bootloader_tsar/boot_utils.h       \
								tools/bootloader_tsar/boot_tty_driver.h  \
								hal/tsar_mips32/hal_types.h              \
								hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

########################
# Rule to compile boot.o
build/boot/boot.o:				tools/bootloader_tsar/boot.c             \
								tools/bootloader_tsar/boot_utils.h       \
								tools/bootloader_tsar/boot_fat32.h       \
								tools/bootloader_tsar/boot_tty_driver.h  \
								tools/bootloader_tsar/boot_hba_driver.h  \
								tools/bootloader_tsar/boot_bdv_driver.h  \
								tools/bootloader_tsar/boot_mmc_driver.h  \
								tools/bootloader_tsar/boot_config.h      \
								hal/tsar_mips32/hal_types.h              \
								hard_config.h				
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) $(MACROS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

##############################
# Rule to compile boot_entry.o
build/boot/boot_entry.o:		tools/bootloader_tsar/boot_entry.S   \
								tools/bootloader_tsar/boot_config.h  \
								hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

#####################################################################
# Rule to generate boot.elf and place it in sector #2 of virtual disk
build/boot/boot.elf:				$(BOOT_OBJS) \
									tools/bootloader_tsar/boot.ld					
	$(LD) -o $@ -T tools/bootloader_tsar/boot.ld $(BOOT_OBJS) 
	$(DU) -D $@ > $@.txt
	dd if=build/boot/boot.elf of=$(DISK_IMAGE) seek=2 conv=notrunc


##############################
# rules to compile the drivers 
build/kernel/drivers/%.o:       kernel/drivers/soclib/%.c   \
                                kernel/drivers/soclib/%.h   \
                                almos_config.h              \
                                hal/tsar_mips32/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS)  -c -o $@ $<
	$(DU) -D $@ > $@.txt

#######################################
# Rules to generate kernel/kern objects
build/kernel/kern/%.o:		    kernel/kern/%.c             \
                                kernel/kern/%.h             \
                                almos_config.h              \
                                hal/tsar_mips32/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

######################################
# Rules to generate kernel/hal objects
build/kernel/hal/%.o:           hal/tsar_mips32/%.c         \
                                hal/tsar_mips32/%.h         \
                                almos_config.h              \
                                hal/tsar_mips32/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

######################################
# Rules to generate kernel/dev objects
build/kernel/devices/%.o:       kernel/devices/%.c          \
                                kernel/devices/%.h          \
                                almos_config.h              \
                                hal/tsar_mips32/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

#####################################
# Rules to generate kernel/mm objects
build/kernel/mm/%.o:            kernel/mm/%.c               \
                                kernel/mm/%.h               \
                                almos_config.h              \
                                hal/tsar_mips32/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

#######################################
# Rules to generate kernel/libk objects
build/kernel/libk/%.o:          kernel/libk/%.c             \
                                kernel/libk/%.h             \
                                almos_config.h              \
                                hal/tsar_mips32/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

#######################################
# Rules to generate kernel/vfs objects
build/kernel/vfs/%.o:           kernel/vfs/%.c              \
                                kernel/vfs/vfs.h            \
                                almos_config.h              \
                                hal/tsar_mips32/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

###########################################################
# Rule to generate kernel.elf and place it on virtual disk
build/kernel/kernel.elf:	    $(KERN_OBJS)                \
                                $(HAL_OBJS)                 \
                                $(DEV_OBJS)                 \
                                $(MM_OBJS)                  \
                                $(LIBK_OBJS)                \
                                $(DRIVERS_OBJS)             \
                                $(VFS_OBJS)                 \
								kernel/kernel.ld            
	$(LD) -o $@ -T kernel/kernel.ld                         \
          $(KERN_OBJS) $(HAL_OBJS) $(DEV_OBJS) $(MM_OBJS)   \
          $(LIBK_OBJS) $(DRIVERS_OBJS) $(VFS_OBJS)
	$(DU) -D $@ > $@.txt
	mcopy -o -i $(DISK_IMAGE) build/kernel/kernel.elf ::/bin/kernel

