###############################################################################
# Makefile for a GP32 application.
###############################################################################

# Change the line below to the name of your program
PROGRAM =	GpMain
#PROGRAM_BASE_DIR=	$$HOME/Mes_projets/firmware1

# The folder where you extracted gp32libs.tar.bz2
GPSDK_HOME =	/home/slubman/gp32gcc/sdk

# Don't change this unless you changed the compiler location when you
# installed it.
GCC_HOME =	/home/slubman/gp32gcc
#GCC_HOME =   /mnt/stock/gp32/pc/linux/gp32_gcc

# Author name for the program
AUTHOR =	"Slubman DevSoft"

# Icon for the program in the Free-Launcher
ICON=	../slubman.bmp

# Libraries to be included... this is all of the gpsdk libraries
LIBS =	-lgpos -lgpsound -lgpmem -lgpgraphic -lgpfont -lgpstdlib -lgpstdio -lfxe -lfileop -lplg

# If you have any directories with include files, add them here with -I on
# the front of each
# e.g. INCLUDES = -I/home/rfbrown/gp32/prog/include -I/usr/src/include
#INCLUDES =	-I$(PROGRAM_BASE_DIR) -I$(PROGRAM_BASE_DIR)/common -I$(PROGRAM_BASE_DIR)/libfxe -I$(PROGRAM_BASE_DIR)/libfileop -I$(PROGRAM_BASE_DIR)/libplg
INCLUDES =	-I. -I../common -I../libfxe -I../libfileop -I../libplg

# list all .c files here
#CFILES	=	GpMain.c MainWindow.c pictures.c restrict.c ../pclink/pclink.c ../options/options.c ../format/format.c ../common/graphics.c ../common/background.c ../common/pictures.c
CFILES	=	GpMain.c modules.c pictures.c\
			../common/version.c\
			../launcher/launcher.c ../launcher/gxe.c ../launcher/pictures.c\
			../explorer/explorer.c\
			../rflink/rflink.c\
			../options/options.c\
			../format/format.c\
			../common/graphics.c ../common/filelist.c ../common/intro.c ../common/langue.c ../common/pictures.c ../common/sound.c ../common/speed.c ../common/window.c ../common/type.c

# list all assembler (.s) files here
SFILES	=

##############################################################################
# You shouldn't need to change anything below here, really
##############################################################################

BASEFILES = $(GPSDK_HOME)/basefiles

ALL_INCLUDES = -I$(GPSDK_HOME)/include $(INCLUDES)
ALL_LIBS = $(LIBS) -lm -lc -lgcc

LIBDIRS = -L $(GCC_HOME)/arm-elf/lib \
			 -L $(GCC_HOME)/lib/gcc/arm-elf/3.4.2\
			 -L $(GPSDK_HOME)/lib\
			 -L ../libfxe\
			 -L ../libfileop\
			 -L ../libplg

OBJDIR = ./object

LDSCRIPT = $(BASEFILES)/lnkscript

OFILES	=	crt0.o gpstart.o ../common/gpsys.o $(SFILES:.s=.o) $(CFILES:.c=.o)

CFLAGS  =		-c -march=armv4t -marm -mthumb-interwork -msoft-float \
					-ffast-math -fshort-enums -nostdlib -fno-common -ffreestanding \
					-fno-builtin -fno-exceptions -mstructure-size-boundary=8 -O2 \
					-fomit-frame-pointer -Wall
# CFLAGS	=		-DTAG_CFLAGS_FOR_TARGET -s -march=armv4t -mtune=arm920t -finline-functions -marm -ffast-math -fshort-enums -mstructure-size-boundary=8 -O2

LDFLAGS	=	--script $(LDSCRIPT) -nostartfiles -Map $(PROGRAM).map

CC = arm-elf-gcc
AS = arm-elf-as
LD = arm-elf-ld
OBJCOPY = arm-elf-objcopy

MAPFILE	=		$(PROGRAM).map
TARGET_ELF	=	$(PROGRAM).elf
TARGET_BIN	=	$(PROGRAM).gxb
TARGET_FXE	=	$(PROGRAM).fxe

.PHONY: all clean

#depend:
#	$(CC) $(ALL_INCLUDES) $(CFLAGS) -M ../*/*.c

all:		firmware

firmware:	$(TARGET_FXE)
# PC-Link version
	../fwbuild/fwbuild -v -f ../slubFW.pclink.bin -b ../157ebios.bin -k ../main/GpMain.gxb \
	-r ../modules/fond.raw "rBackGround picture" \
	-r ../modules/sound_start.wav "rStart sound" \
	-m ../modules/pclink.gxb "mLinking module"
#Geepee 32 version
	../fwbuild/fwbuild -v -f ../fw157e.bin -b ../157ebios.old.bin -k ../main/GpMain.gxb \
	-r ../modules/fond.raw "rBackGround picture" \
	-r ../modules/sound_start.wav "rStart sound" \
	-m ../modules/pclink.gxb "mLinking module"
# #Debug version
# 	../fwbuild/fwbuild -v -f ../slubFW.pclink.debug.bin -b ../monitor.bin -k ../main/GpMain.gxb \
# 	-r ../modules/fond.raw "rBackGround picture" \
# 	-r ../modules/sound_start.wav "rStart sound" \
# 	-m ../modules/pclink.gxb "mLinking module"

#PhoDrive version
	../fwbuild/fwbuild -v -f ../slubFW.gpdrive.bin -b ../157ebios.bin -k ../main/GpMain.gxb \
	-m ../modules/phodrive.gxb "mLinking module" \
	-r ../modules/fond.raw "rBackGround picture" \
	-r ../modules/sound_start.wav "rStart sound"
#	gp32fw -v -l ../slubFW.pclink.bin -b ../157ebios.bin -m GpMain.gxb

#BLU+ version
	../fwbuild/fwbuild -v -f ../slubFW.blup.bin -b ../taiwanBIOS.bin -k ../main/GpMain.gxb \
	-m ../modules/phodrive.gxb "mLinking module" \
	-r ../modules/fond.raw "rBackGround picture" \
	-r ../modules/sound_start.wav "rStart sound"

# For Linux GeePee32 purpose
	ln -f ../slubFW.pclink.bin ../fw157e.bin

# For use with flashing program
	ln -f ../slubFW.pclink.bin ../fw.bin

# To let user check file integrity
	md5sum -b ../slubFW.*[!md5] > slubFW.md5

$(TARGET_BIN): $(TARGET_ELF)
	$(OBJCOPY) -O binary $< $@.unc
	b2fxec -A 2 -f -g $@.unc $@

$(TARGET_FXE): $(TARGET_BIN)
	b2fxe -t "Slubman Firmware ("`date +%d.%m.%G`")" -a $(AUTHOR) -b $(ICON) -r "Slubman DevSoft" $< $@
	@ln -f $(TARGET_FXE) ../slubFW.fxe

$(TARGET_ELF): $(OFILES) Makefile
	@touch ../common/version.c && make -C ../common version.o
	$(LD) $(OFILES) $(LDFLAGS) $(LIBDIRS) $(ALL_LIBS) -o $@

distrib: all
distrib: $(TARGET_FXE)
	@( ([ -e makezip.sh ] || (echo "fichier makezip.sh introuvable" ; exit 1)) && makezip.sh && echo "cration zip russie") || echo "erreur de cration du zip"

crt0.o:	$(BASEFILES)/crt0.s
	$(CC) $(ALL_INCLUDES) $(CFLAGS) -c $< -o $@

gpstart.o:	../common/gpstart.c
	$(CC) $(ALL_INCLUDES) $(CFLAGS) -c $< -o $@

GpMain.o: GpMain.c modules.c
	$(CC) $(ALL_INCLUDES) $(CFLAGS) -c $< -o $@

../%.o:	../%.c ../common/langue.h
	@$(MAKE) -C $(@D)

%.o:	%.c ../common/langue.h
	$(CC) $(ALL_INCLUDES) $(CFLAGS) -c $< -o $@

clean:
	rm -f $(OFILES) $(MAPFILE) $(TARGET_ELF) $(TARGET_BIN)

clean-all:
	rm -f $(OFILES) $(MAPFILE) $(TARGET_ELF) $(TARGET_BIN) $(TARGET_FXE)

test1 : firmware
	ln -f ../slubFW.pclink.bin fw157e.bin &&\
	$(GPSDK_HOME)/bin/geepee32 /IIC=../common/iic.bin /SMC=../common/test.smc /RUN

test2 : firmware
	ln -f ../slubFW.gpdrive.bin fw157e.bin &&\
	$(GPSDK_HOME)/bin/geepee32 /IIC=../common/iic.bin /SMC=../common/test.smc /RUN

testno : firmware
	$(GPSDK_HOME)/bin/geepee32  /IIC=../common/iic.bin /RUN

testu : firmware
	$(GPSDK_HOME)/bin/geepee32 /IIC=../common/iic.bin /SMC=../common/testu.smc /RUN

run: $(TARGET_BIN)
	gplink run $(TARGET_BIN)

send: $(TARGET_FXE)
	-gplink rm /gpmm/slubFW.fxe
	gplink put ../slubFW.fxe /gpmm -x

