PROG	= gp81
SMC		= /mnt/flash


LIBS		= -lgpmem -lgpos -lgpstdio -lgpstdlib -lgpgraphic -lgpfont -lgpsound -lm
BINOBJS		= $(addsuffix .o, $(BINS))

INCLUDE		= -I ./
# AFLAGS		= -Wall -mthumb-interwork -msoft-float
AFLAGS		= -Wall
# CFLAGS		= -Wall -mcpu=arm9  -mthumb-interwork -msoft-float -O2 -g $(INCLUDE)
CFLAGS		= -Wall -mcpu=arm9 -O2 -g $(INCLUDE)
LFLAGS		= -nostartfiles -Wall -Wl,-Map,$(PROG).map -T lnkscript crt0.o  
CPPFLAGS	= -Wall 

CROSS		= arm-elf-
CC			= $(CROSS)gcc
CXX			= $(CROSS)g++
AS			= $(CROSS)gcc -c
CPP			= $(CROSS)gcc -E -P
LD			= $(CROSS)gcc
OBJCOPY		= $(CROSS)objcopy


#------------------------------#

# Place all your filenames to be compiled below in there .o format.
OBJS				=	$(GP_OBJS) \
						gpmain.o \
						z80.o \
						gpstart.o \
						user_init.o \
						kbdrv.o \
						kbdio.o \
						gp_irq.o \
						FileSel.o

.PHONY : all clean

all: $(PROG).fxe

$(PROG).fxe: $(PROG).bin
	b2fxec -f -t "gp81" -a "Woogal" $< $@


$(PROG).bin: $(PROG).elf
	$(OBJCOPY) -O binary $< $@

$(PROG).elf: crt0.o $(OBJS)
	$(LD) $(LFLAGS) -o $(PROG).elf $(OBJS) $(LIBS)

%.o: %.bin
	$(OBJCOPY) -I binary -O elf32-little $< $@ 

%.o: %.c
	$(CC) -c $(CFLAGS) -o $@ $<

%.o: %.cpp
	$(CXX) -c $(CFLAGS) -o $@ $<

%.o: %.S
	$(CPP) "-DIN_ASM" $(CPPFLAGS) $< > temp.s
	$(CC) -c $(CFLAGS) -o $@ temp.s 
	-rm temp.s

%.o: %.s
	$(CC) -c $(CFLAGS) -o $@ $<

install: all
	mount $(SMC)
	cp $(PROG).fxe $(SMC)/gpmm/
	-umount $(SMC)

install_link: all
	gplink put $(PROG).fxe gpmm/
	gplink end

clean:
	rm -rf $(OBJS) crt0.o $(PROG).fxe $(PROG).elf $(PROG).bin *.map *~

run: all
	GpRun $(PROG).bin

