CC			= arm-none-eabi-gcc
LD			= arm-none-eabi-gcc
OBJCOPY		= arm-none-eabi-objcopy
LKR			= arm_lib.lkr
LTO			= -flto
COMMON		= -Os -g -ggdb3 -I. -ffixed-r9 -DUSE_TRAPS=0 -fpic -Wno-multichar -Wall -march=armv7e-m -mcpu=cortex-m4 -mno-unaligned-access -fno-builtin -fno-builtin-memcpy -fno-builtin-memset
CCFLAGS		= $(COMMON) $(LTO) -ffunction-sections -fdata-sections
LDFLAGS		= $(COMMON) $(LTO) -Wl,--gc-sections -Wl,-T $(LKR) -fpic
CRTGENARCH	= v7m

RSRC		= amdc0000
PRC			= nand_driver.prc
SRCS		+= main.c slot.c libpalmos.c librepalm.c crt.S pace.S ftl.S nandMsio.c
SLIBS		=


#add SDK
CCFLAGS		+=  -I../../SDK
CCFLAGS		+=  -I../../SDK/Core
CCFLAGS		+=  -I../../SDK/Core/System
CCFLAGS		+=  -I../../SDK/Core/Hardware
CCFLAGS		+=  -I../../SDK/Core/UI
CCFLAGS		+=  -I../../SDK/Dynamic
CCFLAGS		+=  -I../../SDK/libraries

#add repalm
CCFLAGS		+=  -isystem ../dal


#resources
EXTRA_RSRCS	+= amdi0000.bin
EXTRA_RSRCS	+= tver03e8.bin


#do not touch below
OBJS		= $(patsubst %.S,%.o,$(patsubst %.c,%.o,$(SRCS)))
INCS		= $(wildcard *.h)

$(PRC): $(RSRC).bin $(EXTRA_RSRCS)
	../../tools/mkrom/mkprc NandSlot-rePalm libs rpNN $^ > $@

$(RSRC).bin: $(RSRC).elf  Makefile
	$(OBJCOPY) -I elf32-littlearm -O binary $(RSRC).elf $(RSRC).bin -j.vec -j.text -j.rodata -j.data

$(RSRC).elf: $(OBJS) $(LKR) Makefile $(SLIBS)
	$(LD) -o $(RSRC).elf $(LDFLAGS) $(OBJS) $(SLIBS)

libpalmos.c: ../dal/libpalmos.c
	cp $< $@

librepalm.c: ../dal/librepalm.c
	cp $< $@

%.o : %.c $(INCS) Makefile
	$(CC) $(CCFLAGS) -c $< -o $@
	
%.o : %.S Makefile
	$(CC) $(CCFLAGS) -c $< -o $@

pace.S: ../../tools/libtools/PACE.libdescr Makefile
	echo "#define _ASSEMBLY\n#include \"common.h\"\n#define LIB_SLOT_IN_GLOBALS 0x10" > $@
	../../tools/libtools/inclgen.sh $(CRTGENARCH) $< >> $@

ftl.S: ../LibFTL/LibFTL.libdescr Makefile
	echo "#define _ASSEMBLY\n#include \"common.h\"\n#define LIB_SLOT_IN_GLOBALS 0x18" > $@
	../../tools/libtools/inclgen.sh $(CRTGENARCH) $< >> $@

crt.S: Makefile slotdriver.libdescr
	../../tools/libtools/crtgen.sh $(CRTGENARCH) slotdriver.libdescr >crt.S

clean:
	rm -f $(OBJS) $(RSRC).bin $(RSRC).elf $(PRC) libpalmos.c librepalm.c crt.S pace.S


