# $Log: Makefile,v $
# Revision 1.5  2004/10/02 16:20:46  richq
# Version 1 release and cleanup.
#
# Revision 1.4  2004/08/18 11:10:21  richq
# Disc modules added
#
# Revision 1.3  2004/08/08 20:01:51  richq
# First working version!!
# Still needs nicer menus, a proper scaling fix and some
# other options (colour 0 on/off to fix strykers run, for
# example.)
#
# Revision 1.2  2004/05/02 15:32:10  richq
# Decent working version of the emulator. Handles just mode 2,
# but includes multi game selection and key configuration menus.
#
# Revision 1.1.1.1  2004/04/24 14:59:45  richq
# PocketBeeb - BBC emulator for the GBA
#
# Revision 1.2  2004/04/04 15:50:54  richq
# Final C version. It works but is verrrry slow (< 1fps!)
#
# Revision 1.1.1.1  2004/03/31 20:23:55  richq
# BBC Emu
#
#

.SUFFIX : .s .c .S

IMG = ./img
SND = ./snd
export DEVKITARM := 1
include ../gbalib/master.mk
OUTPUTDIR = o
ifdef DEVKITARM
LDFLAGS = -Wl,-Map,bbc.map -mthumb-interwork -specs=gba.specs
else
LDFLAGS = -Wl,-Map,bbc.map -mthumb-interwork -nostartfiles -Tlnkscript 
endif
LIBS = -L../gbalib -lquirkygba -Ldata -ldata -Limages -limg


DEBUG=
ifdef DEVKITARM
  DEBUG += -DDevkitARM
endif

ifdef elite
DEBUG += -DELITE_HACK 
export use1770 := 1
endif
ifdef use1770
export DISCFILE_C := bbcdisc1770.c
export DISCFILE_ASM := disc1770.S 
DEBUG += -DUSE_1770_DISC
else
export DISCFILE_C := bbcdisc.c
export DISCFILE_ASM := disc8271.S
endif

# choose a size here that's big enough to definitely pad the .gba file
# otherwise stuff just won't work...
padsize = 150000
CFLAGS += -DPADDED_SIZE=$(padsize)   -O0 -g

INCLUDES = -I../gbalib -I includes
#-I../krawall

ASFLAGS	= -mthumb-interwork -g

PROGNAME = bbc
PROJECT = $(PROGNAME).gba
ifdef usezip
ZIP = zip
ZIPFLAGS =  -rq
ZIPSUFFIX = zip
endif

PROGFILES= \
	main.c gfx.c inter.arm.c bbcmemory.c savestate.c initsound.c menudata.c \
	sysvia.c uservia.c via.c  fastvideo.c inputcodes.c menu.c gamemenu.c \
	dis6502.c input.c virtualkb.c settings.c pogo.c pcxgba.c \
	$(DISCFILE_C) 

ASMFILES= 6502.S video.S minisysvia.S mem.S mini_input.S sound.S teletext.S asmkeypad.S \
	  $(DISCFILE_ASM)

# disc1770.S  
# disc8271.S
OFILES1 =  $(PROGFILES:%.c=%.o) $(ASMFILES:%.S=%.o)
ifndef DEVKITARM
OFILES1 := crt0.o farProc.o  $(OFILES1)
#crt0.o
else 
OFILES1 := farProc.o $(OFILES1)
endif

OFILES := $(OFILES1:%.o=$(OUTPUTDIR)/%.o)

#DEPENDS = $(PROGFILES:%.c=%.d)

all: 
	make -C data all
	make -C images all
#	make -C ../gbalib
	make $(PROJECT)

clean:
	rm -f  $(OFILES)
	make -C images clean
	make -C data clean
##	rm -f  $(DEPENDS)


RELEASEDIR := pocketbeeb-


releasedir :
# create a file that contains, eg. pocketbeeb-0.68	
	@echo -n \
	$(RELEASEDIR)`cat VERSION` > .fname
# remove the dist directory, remake it, then copy all the stuff in there 
	@-rm -rf `cat .fname`
	@-mkdir `cat .fname`
	@cp ./emu.html `cat .fname`/readme.html

# make the distribution tar files 
dist: releasedir
	@echo "Building the distribution.."
	@cp bbc.gba  `cat .fname`
	@cp splash.pcx  `cat .fname`
	@-cp pbinject/pbinject.exe  `cat .fname`
	@$(ZIP) $(ZIPFLAGS) `cat .fname`.$(ZIPSUFFIX) `cat .fname`
	@echo "... done for" `cat .fname` "!" 
	@-rm -rf `cat .fname` .fname

distsrc: releasedir
	@echo "Building the source distribution.."
	@mkdir  `cat .fname`/includes
	@mkdir  `cat .fname`/pbinject
	@mkdir  `cat .fname`/pbinject/out
	@mkdir  `cat .fname`/images
	@mkdir  `cat .fname`/o
	@touch  `cat .fname`/o/obj
	@touch  `cat .fname`/pbinject/out/obj
	@mkdir  `cat .fname`/data
	@find . -type f -maxdepth 1 \( -name "*.[cSh]" -o -name "*.cpp" -o -name "Makefile"  \) \
  | xargs cp --target-directory=`cat .fname` 
	@find includes -type f -maxdepth 1 \( -name "*.[cSh]" -o -name "*.cpp" -o -name "Makefile" \) \
  | xargs cp --target-directory=`cat .fname`/includes 
	@find pbinject -type f -maxdepth 1 \( -name "*.[ch]" -o -name "*.cpp" -o -name "Makefile" -o -name "*.xpm" \) \
  | xargs cp --target-directory=`cat .fname`/pbinject
	@find data -type f -maxdepth 1 \( -name "*.rom" -o -name "Makefile" \) \
  | xargs cp --target-directory=`cat .fname`/data
	@find images -type f -maxdepth 1 \( -name "*.pcx" -o -name "Makefile" -o -name "mkimg*" \) \
  | xargs cp --target-directory=`cat .fname`/images
	@$(ZIP) $(ZIPFLAGS) `cat .fname`.src.$(ZIPSUFFIX) `cat .fname`
	@echo "... done for" `cat .fname` "!" 
	@-rm -rf `cat .fname` .fname

padder : padder.cpp
	g++ padder.cpp -o padder


# final step, create bin file from elf file
$(PROJECT) : $(PROGNAME).elf
	@$(OBJCOPY) -v -O binary $(PROGNAME).elf $(PROGNAME).tofix.gba
	@$(GBAFIX) $(GBAFIX_FLAGS) -n "Pocketbeeb" -o $@ $(PROGNAME).tofix.gba
#	@$(JAVA) Padder $@ $(padsize) $@
	@./padder $@ $(padsize) $@
	@rm $(PROGNAME).tofix.gba

# link all the object files into an elf file 
$(PROGNAME).elf : $(OFILES)
	@echo link $(PROGNAME)
	$(GCC) $(LDFLAGS) -o $(PROGNAME).elf $(OFILES) $(LIBS)


# make object files from the source code
$(OUTPUTDIR)/%.arm.o : %.arm.c
	@echo compile arm $< $(DEBUG)
#	@$(GCC) $(INCLUDES) $(CFLAGS) $(DEBUG) -mthumb-interwork $< -o $@
	@$(GCC) $(INCLUDES) $(CFLAGS) -marm -mthumb-interwork $(DEBUG)  $< -o $@
	
# make object files from the source code
$(OUTPUTDIR)/%.o : %.c
	@echo compile thumb $< $(DEBUG)
#	@$(GCC) $(INCLUDES) $(CFLAGS) -mthumb -mlong-calls  $(DEBUG) -marm -mthumb-interwork $< -o $@
	@$(GCC) $(INCLUDES) $(CFLAGS) -mthumb $(DEBUG)  $< -o $@

# make object files from binary data
$(OUTPUTDIR)/%.all.rodata.o : %.bin
	@echo objcopy rodata bin $<
	@$(OBJCOPY) $(OBJCOPYFLAGS) $< $@

%.d:%.c
	@echo "build $@ from $<"
#	@$(GCC) $(INCLUDES) -M $< > $@
	@echo -n $(OUTPUTDIR)/ > $@
	@$(GCC) $(INCLUDES) -M $< >> $@

# make crt0 object file
#$(OUTPUTDIR)/crt0.o: crt0.s
#	@$(AS) $(INCLUDES) $(ASFLAGS) -o $@ $<

# make .s object file
$(OUTPUTDIR)/%.o : %.S
	@echo assemble $(ASFLAGS) $(INCLUDES) $(DEBUG) -o $@ $<
	@$(GCC) $(ASFLAGS) -c $(INCLUDES) $(DEBUG) -o $@ $<

$(OUTPUTDIR)/%.o : %.s
	@echo assemble $(ASFLAGS) $(INCLUDES) $(DEBUG) -o $@ $<
	@$(GCC) $(ASFLAGS) -c $(INCLUDES) $(DEBUG) -o $@ $<


$(OUTPUTDIR)/6502.o: 6502.S includes/6502macros.h

#-include $(DEPENDS)

