# Makefile for memtestARM
#
# Copr. 2006-2007 Damian Yerrick
# 
# This work is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


# The wintermute-approved way to ensure devkitARM is in the PATH
ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>devkitPro)
endif
ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM)
endif
export PATH	:=	$(DEVKITARM)/bin:$(PATH)


objdir := obj/ds
CC := gcc
CFLAGS := -Wall -O -std=gnu99
LD := gcc
LDFLAGS := -s
LDLIBS := -lalleg

PROJNAME := memtestARM
PROJTITLE := "memtestARM;SLOT-2 RAM tester;by Damian Yerrick"
GAMEICON := src/memtest_icon.bmp
DEPOBJS := \
  $(objdir)/main.o $(objdir)/fontdraw.o $(objdir)/memtest.o $(objdir)/ram.o
A7OBJS := $(objdir)/dsarm7.o $(objdir)/lookup_tables7.o

srcdir := src
EXE := $(objdir)/$(PROJNAME).elf
EXE7 := $(objdir)/$(PROJNAME)7.elf
ASSETSFILE := $(objdir)/assets.s
ASSETS := $(objdir)/vwfont.bin #$(objdir)/Chester.spr $(objdir)/Resetti.spr
ARMGCC=arm-eabi-gcc
ARMOBJ=arm-eabi-objcopy
CFLAGS7=-std=gnu99 -Wall -O2 -mcpu=arm7tdmi -mtune=arm7tdmi -fomit-frame-pointer -ffast-math -mthumb-interwork
CFLAGS9=-std=gnu99 -Wall -O2 -march=armv5te -mtune=arm946e-s -fomit-frame-pointer -ffast-math -mthumb-interwork
NDSLIB_INCLUDE=$(DEVKITPRO)/libnds/include
NDSLIB_LIB=$(DEVKITPRO)/libnds/lib
#LDLIBS9 = -lfat -lnds9
LDLIBS9 = -lnds9
OBJS := $(DEPOBJS) $(ASSETSFILE)
EMU := /e/ds/nocash/no\$$gba.exe

.PHONY: run clean cf

run: $(PROJNAME).nds
	$(EMU) $<

$(PROJNAME).nds: $(objdir)/$(PROJNAME).bin $(objdir)/$(PROJNAME)7.bin $(GAMEICON)
	ndstool -c $@ \
	-9 $(objdir)/$(PROJNAME).bin \
	-7 $(objdir)/$(PROJNAME)7.bin \
	-b $(GAMEICON) $(PROJTITLE)

%.nds.gba: %.nds
	dsbuild $< -o $@

%.bin: %.elf
	$(ARMOBJ) -O binary $< $@

$(EXE): $(OBJS)
	$(ARMGCC) -s -mthumb-interwork -mno-fpu -specs=ds_arm9.specs $^ -L$(NDSLIB_LIB) $(LDLIBS9) -o $@

$(EXE7): $(A7OBJS)
	$(ARMGCC) -s -mthumb-interwork -mno-fpu -specs=ds_arm7.specs $^ -L$(NDSLIB_LIB) -lnds7 -o $@

$(objdir)/%7.o: $(srcdir)/%7.c
	$(ARMGCC) $(CFLAGS7) -I$(NDSLIB_INCLUDE) -MMD -DARM7 -c $< -o $@
	@cp $(objdir)/$*7.d $(objdir)/$*7.P; \
	sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
	-e '/^$$/ d' -e 's/$$/ :/' < $(objdir)/$*7.d >> $(objdir)/$*7.P; \
	rm -f $(objdir)/$*7.d

$(objdir)/%.o: $(srcdir)/%.c
	$(ARMGCC) $(CFLAGS9) -I$(NDSLIB_INCLUDE) -MMD -DARM9 -c $< -o $@
	@cp $(objdir)/$*.d $(objdir)/$*.P; \
	sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
	-e '/^$$/ d' -e 's/$$/ :/' < $(objdir)/$*.d >> $(objdir)/$*.P; \
	rm -f $(objdir)/$*.d

%.o: %.s
	$(ARMGCC) $(CFLAGS9) -I$(NDSLIB_INCLUDE) -MMD -DARM9 -c $< -o $@
	@cp /$*.d /$*.P; \
	sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
	-e '/^$$/ d' -e 's/$$/ :/' < /$*.d >> $(objdir)/$*.P; \
	rm -f /$*.d

%7.o: %7.s
	$(ARMGCC) $(CFLAGS7) -I$(NDSLIB_INCLUDE) -MMD -DARM7 -c $< -o $@
	@cp /$*7.d /$*7.P; \
	sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
	-e '/^$$/ d' -e 's/$$/ :/' < /$*7.d >> $(objdir)/$*7.P; \
	rm -f /$*7.d

$(ASSETSFILE): $(ASSETS)
	bin2s $^ > $@

$(objdir)/lookup_tables7.s: tools/mktables.exe
	$<

$(objdir)/vwfont.bin: tools/fontconv.exe $(srcdir)/font.bmp
	$^ $@

$(objdir)/%.spr: tools/bmp2bg.exe $(srcdir)/%.bmp
	$^ $@

tools/fontconv.exe: tools/fontconv.o
	$(LD) $(LDFLAGS) $^ $(LDLIBS) -o $@

tools/bmp2bg.exe: tools/bmp2bg.o
	$(LD) $(LDFLAGS) $^ $(LDLIBS) -o $@

tools/mktables.exe: tools/mktables.o
	$(LD) $(LDFLAGS) $^ $(LDLIBS) -o $@

# the master copy of vwf is in the RAC (Restore Animal Crossing) sourcetree

$(srcdir)/fontdraw.c: /e/games/ac/double/rac/src/fontdraw.c
	cp $< $@

$(srcdir)/fontdraw.h: /e/games/ac/double/rac/src/fontdraw.h
	cp $< $@

# a few phony maintenance targets

cf: $(PROJNAME).nds
	cp $< /h/
	ejector -q H:

clean:
	-rm $(objdir)/*.s
	-rm $(objdir)/*.o
	-rm $(objdir)/*.d
	-rm $(objdir)/*.P
	-rm $(objdir)/*.elf
	-rm $(objdir)/*.bin
	-rm $(objdir)/*.spr

-include $(DEPOBJS:%.o=%.P)
