######################################
# target path
######################################
TARGET = MS51FB

#######################################
# Build path
#######################################
BUILD_DIR = build

######################################
# source
######################################
SRCDIR 		= src
LIB_SRC 	= lib
USER_SRC 	= test
INC_SRC	 	= include

# C sources
C_SOURCES := $(wildcard $(SRCDIR)/*.c)
C_SOURCES += $(wildcard $(USER_SRC)/*.c)
C_SOURCES += $(wildcard $(LIB_SRC)/*.c)
ASM_SOURCES = $(wildcard $(SRCDIR)/*.asm)

C_SRC_FILE = $(notdir $(C_SOURCES))
C_OBJ_FILE = $(C_SRC_FILE:%.c=%.rel)

ASM_SRC_FILE = $(notdir $(ASM_SOURCES))
ASM_OBJ_FILE = $(ASM_SRC_FILE:%.asm=%.rel)

######################################
# building variables
######################################
# debug build?
DEBUG = 0
# optimization
OPT = 

#######################################
# cross compile
#######################################
PREFIX = 

CC = $(PREFIX)sdcc 
AS = $(PREFIX)sdas8051

MCU_MODEL = -mmcs51

RM = rm -rf 
MAKE = make 

# ------------------------------------------------------
# Usually SDCC's small memory model is the best choice.  If
# you run out of internal RAM, you will need to declare
# variables as "xdata", or switch to larger model

# Memory Model (small, medium, large, huge)
MODEL  = --model-large
# ------------------------------------------------------
# Memory Layout
# PRG Size = 4K Bytes
#CODE_SIZE = --code-loc 0x0000 --code-size 18432
CODE_SIZE = --code-size 65536
# INT-MEM Size = 256 Bytes
#IRAM_SIZE = --idata-loc 0x0000  --iram-size 256
IRAM_SIZE = --iram-size 256
# EXT-MEM Size = 4K Bytes
#XRAM_SIZE = --xram-loc 0x0000 --xram-size 768
XRAM_SIZE = --xram-size 8192

# ------------------------------------------------------

#######################################
# FLAGS
#######################################
# macros for gcc
# AS defines
AS_DEFS = 

# C defines
C_DEFS = 


# AS includes
AS_INCLUDES = 

# C includes
C_INCLUDES = -I$(INC_SRC) \
-I$(SRCDIR) \
-I$(USER_SRC) \
-I$(LIB_SRC) 

# libraries
LIBS = 
LIBDIR = 

# compile gcc flags
ASFLAGS = -l -s

CFLAGS = $(MCU_MODEL) $(C_DEFS) $(C_INCLUDES) $(MODEL) --out-fmt-ihx --no-xinit-opt #--peep-file tools/peep.def

ifeq ($(DEBUG), 1)
CFLAGS += --debug
else
CFLAGS += $(OPT)
endif


#######################################
# LDFLAGS
#######################################
LDFLAGS = $(LIBDIR) $(LIBS) $(MCU_MODEL) $(MODEL) $(CODE_SIZE) $(IRAM_SIZE) $(XRAM_SIZE) --out-fmt-ihx

ifeq ($(DEBUG), 1)
LDFLAGS += --debug
else
LDFLAGS += $(OPT)
endif
#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(C_OBJ_FILE))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(ASM_OBJ_FILE))


# default action: build all
.PHONY: all
all: $(BUILD_DIR)/$(TARGET).hex


$(BUILD_DIR)/$(TARGET).hex:$(BUILD_DIR)/$(TARGET).ihx 
	packihx $^ > $@
	
$(BUILD_DIR)/$(TARGET).ihx:$(OBJECTS)
	@$(CC) $(LDFLAGS) $^ -o $@

$(BUILD_DIR)/%.rel:$(SRCDIR)/%.c | $(BUILD_DIR)
	@$(CC) $(CFLAGS) -c $^ -o $@

$(BUILD_DIR)/%.rel:$(USER_SRC)/%.c | $(BUILD_DIR)
	@$(CC) $(CFLAGS) -c $^ -o $@

$(BUILD_DIR)/%.rel:$(LIB_SRC)/%.c | $(BUILD_DIR) 
	@$(CC) $(CFLAGS) -c $^ -o $@

$(BUILD_DIR)/%.rel:$(SRCDIR)/%.asm | $(BUILD_DIR)
	@$(AS) $(ASFLAGS) -o $@ $^ 

$(BUILD_DIR):
	mkdir -p $@


#######################################
# clean up
#######################################
.PHONY: clean distclean
clean:
	$(RM) $(BUILD_DIR)/*

distclean:
	$(RM) $(BUILD_DIR)


# *** EOF ***

