############################################################################
# arch/z80/src/Makefile.sdccl
#
#   Copyright (C) 2008, 2011-2012, 2014 Gregory Nutt. All rights reserved.
#   Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
#    used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################

# Tools
# CFLAGS, CPPFLAGS, ASFLAGS, LDFLAGS are set in $(TOPDIR)/Make.defs

CFLAGS += -I$(ARCH_SRCDIR)/chip -I$(ARCH_SRCDIR)/common -I$(TOPDIR)/sched
CPPFLAGS += -D__ASSEMBLY__

# Files and directories
# There should be one head source (.asm file)

HEAD_OBJ = $(HEAD_ASRC:$(ASMEXT)=$(OBJEXT))
STARTUP_OBJS ?= $(HEAD_OBJ)

# Assembly sources and objects

ASRCS = $(CHIP_ASRCS) $(CMN_ASRCS)
AOBJS = $(ASRCS:$(ASMEXT)=$(OBJEXT))

# C sources and objects

CSRCS = $(CHIP_CSRCS) $(CMN_CSRCS)
COBJS = $(CSRCS:.c=$(OBJEXT))

# All sources and objcts

SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)

# Sources that can have dependencies (no .asm files)

DEPSRCS = $(CSRCS)

# Directories

ARCH_SRCDIR = $(TOPDIR)/arch/$(CONFIG_ARCH)/src
BOARDDIR = $(TOPDIR)/arch/$(CONFIG_ARCH)/src/board

VPATH = chip:common:board

# Libraries

LIBGCC = ${shell "$(CC)" $(ARCHCPUFLAGS) -print-libgcc-file-name}

# Targets

all: $(HEAD_OBJ) libarch$(LIBEXT)

.PHONY: board/libboard$(LIBEXT) nuttx.lnk

$(AOBJS) $(HEAD_OBJ): %$(OBJEXT): %$(ASMEXT)
	$(call ASSEMBLE, $<, $@)

$(COBJS): %$(OBJEXT): %.c
	$(call COMPILE, $<, $@)

# This is a kludge to work around some conflicting symbols in the SDCC libraries

$(TOPDIR)/lib/$(SDCCLIB): $(SDCC_LIBDIR)/$(SDCCLIB)
	$(Q) cp $(SDCC_LIBDIR)/$(SDCCLIB) $(TOPDIR)/lib/$(SDCCLIB)
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _calloc.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _malloc.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _realloc.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _free.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) printf_large.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) sprintf.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) vprintf.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) strcpy.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) strlen.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _strcat.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _strchr.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _strcmp.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _strcspn.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _strncat.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _strncmp.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _strncpy.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _strpbrk.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _strrchr.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _strspn.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _strstr.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _strtok.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _memchr.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _memcmp.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _memcpy.rel
	$(Q) sdar d $(TOPDIR)/lib/$(SDCCLIB) _memset.rel

# Create a header file that contains addressing information needed by the
# assemlby language start-up code.  Locate the IDLE thread stack at the
# end of RAM.  The heap then extends from s__HEAP to the bottom of the
# IDLE thread stack

asm_mem.h:
	@echo "	CONFIG_STACK_END == ($(CONFIG_RAM_SIZE) - 1)" >> asm_mem.h
	@echo "	CONFIG_STACK_BASE == (CONFIG_STACK_END - $(CONFIG_IDLETHREAD_STACKSIZE))" >> asm_mem.h

# Combine all objects in this directory into a library

libarch$(LIBEXT): asm_mem.h $(OBJS)
	$(call ARCHIVE, $@, $(OBJS))

# This builds the libboard library in the board/ subdirectory

board/libboard$(LIBEXT):
	$(Q) $(MAKE) -C board TOPDIR="$(TOPDIR)" libboard$(LIBEXT) EXTRADEFINES=$(EXTRADEFINES)

# This target builds the final executable

nuttx.lnk:
	@echo "LD: nuttx.lnk"
	@echo "--" >nuttx.lnk						# Non-interactive
	@echo "-k $(BOARDDIR)" >>nuttx.lnk			# Path to board library
	@echo "-k $(TOPDIR)/lib" >>nuttx.lnk		# Path to top-level lib directory
	@echo "-l libboard$(LIBEXT)" >>nuttx.lnk	# Name of board library
	$(Q) for LIB in $(LINKLIBS); do \
		echo "-l $(TOPDIR)/lib/$$LIB" >> nuttx.lnk ;\
	done
	@echo "-l $(SDCCLIB)" >>nuttx.lnk			# Name of SDCC z80 library
ifneq ($(CONFIG_LINKER_HOME_AREA),)
	@echo "-b _HOME=$(CONFIG_LINKER_HOME_AREA)" >>nuttx.lnk	# Start of _HOME area
endif
ifneq ($(CONFIG_LINKER_CODE_AREA),)
	@echo "-b _CODE=$(CONFIG_LINKER_CODE_AREA)" >>nuttx.lnk	# Start of _CODE area
else
	@echo "-b _CODE=0x0200" >>nuttx.lnk			# Start of _CODE area
endif
ifneq ($(CONFIG_LINKER_DATA_AREA),)
	@echo "-b _DATA=$(CONFIG_LINKER_DATA_AREA)" >>nuttx.lnk
else
	@echo "-b _DATA=0x8000" >>nuttx.lnk			# Start of _DATA area
endif
	@echo "-i" >>nuttx.lnk						# Intel hex format
	@echo "-x" >>nuttx.lnk						# Hexadecimal
	@echo "-m" >>nuttx.lnk						# Generate a map file
	@echo "-j" >>nuttx.lnk						# Generate a symbol file
	@echo "nuttx.ihx" >>nuttx.lnk				# Path to head object
	@echo "$(HEAD_OBJ)" >>nuttx.lnk				# Path to head object
	@echo "-e" >>nuttx.lnk						# End of script

nuttx$(EXEEXT): asm_mem.h $(TOPDIR)/lib/$(SDCCLIB) $(HEAD_OBJ) board/libboard$(LIBEXT) nuttx.lnk
	@echo "LD: nuttx.ihx"
	$(Q) $(LD) -f nuttx.lnk
	$(Q) cp -f nuttx.map $(TOPDIR)/.
ifeq ($(EXEEXT),.cmd)
	sed s/:00000001FF/:00520001AD/ nuttx.ihx | \
	hex2cmd > $(TOPDIR)/nuttx.cmd
else
	$(Q) packihx nuttx.ihx > $(TOPDIR)/nuttx$(EXEEXT)
endif

# This is part of the top-level export target

export_startup: board/libboard$(LIBEXT) $(STARTUP_OBJS)
	$(Q) if [ -d "$(EXPORT_DIR)/startup" ]; then \
		cp -f $(STARTUP_OBJS) "$(EXPORT_DIR)/startup"; \
	 else \
		echo "$(EXPORT_DIR)/startup does not exist"; \
		exit 1; \
	 fi

# Build dependencies

.depend: Makefile asm_mem.h chip/Make.defs $(DEPSRCS)
	$(Q) if [ -e board/Makefile ]; then \
		$(MAKE) -C board TOPDIR="$(TOPDIR)" depend ; \
	fi
	$(Q) $(MKDEP) --dep-path chip --dep-path common "$(CC)" -- $(CFLAGS) -- $(DEPSRCS) >Make.dep
	$(Q) touch $@

depend: .depend

clean:
	$(Q) if [ -e board/Makefile ]; then \
		$(MAKE) -C board TOPDIR="$(TOPDIR)" clean ; \
	fi
	$(call DELFILE, asm_mem.h)
	$(call DELFILE, nuttx.*)
	$(call DELFILE, libarch$(LIBEXT))
	$(call CLEAN)


distclean: clean
	$(Q) if [ -e board/Makefile ]; then \
		$(MAKE) -C board TOPDIR="$(TOPDIR)" distclean ; \
	fi
	$(call DELFILE, Make.dep)
	$(call DELFILE, .depend)

-include Make.dep

