.SUFFIXES: .img .bin .prg .spi .run .abs

all: spiall.spi progall.bin VS1000_B.RUN SDUPDATE.PRG

#Must be in decimal
#Because it's a SPI EEPROM, the offset does not need to be aligned.
PLAYERSTART = 6656
MMCSTART = 13312

VS1000LIB = lib
INCDIRS = -I . -I $(VS1000LIB)
LIBDIRS = -L . -L $(VS1000LIB)

LIBS  = -lc -ldev1000 -laudiomod
VCC   = vcc
FEATURES = 
ASMFLAGS = -D ASM $(FEATURES)

# For FreeBSD
ifeq ($(OSTYPE),FreeBSD)
COFF2BOOT = ./fsb-bin/coff2BoOt
COFF2SPIBOOT = ./fsb-bin/coff2spiboot
COMBINEIMG = ./fsb-bin/combineimg
COMBREF = ./fsb-bin/combineimg
VCCFLAGS = -P130 -g -O -fsmall-code $(FEATURES) \
	-DPLAYERSTART=$(PLAYERSTART) -DMMCSTART=$(MMCSTART)
RM = rm -f
CP = cp
else
ifeq ($(OSTYPE),linux)
# For Linux
COFF2BOOT = ./coff2BoOt
COFF2SPIBOOT = ./coff2spiboot
COMBINEIMG = ./combineimg
COMBREF = ./combineimg
VCCFLAGS = -P130 -g -O -fsmall-code $(FEATURES) \
	-DPLAYERSTART=$(PLAYERSTART) -DMMCSTART=$(MMCSTART)
RM = rm -f
CP = cp
else
# For Windows
COFF2BOOT = coff2nandboot.exe
COFF2SPIBOOT = coff2spiboot.exe
COMBINEIMG = combineimg.exe
COMBREF =
VCCFLAGS = -P130 -O -fsmall-code $(FEATURES) \
	-DPLAYERSTART=$(PLAYERSTART) -DMMCSTART=$(MMCSTART)
RM = del
CP = copy
endif
endif


.c.o:
	$(VCC) $(VCCFLAGS) $(INCDIRS) -o $@ $<

.s.o:
	vsa $(ASMFLAGS) $(INCDIRS) -o $@ $<

#Run program
.bin.run: Makefile
	vs3emu -chip vs1000 -s 115200 -l $< -c run.cmd

#rom1000.o: rom1000.txt
#	mkabs -o $@ -f $<

coff2spiboot: coff2spiboot.c
	gcc -Wall -o $@ $<

coff2BoOt: coff2BoOt.c
	gcc -Wall -o coff2BoOt coff2BoOt.c

#.bin.img: Makefile coff2BoOt
#	./coff2BoOt -c 2 -w 50 -x 0x50 $< $@

.bin.spi: Makefile
	$(COFF2SPIBOOT) -x 0x50 $< $@

.bin.prg: Makefile
	$(COFF2BOOT) -n -x 0x50 $< $@

.abs.o: Makefile
	mkabs -f $< -o $@

.o.bin:
	vslink -k -m mem_desc.vs1000 -o $@ $(VS1000LIB)/c-nand.o $< $(VS1000LIB)/rom1000.o $(LIBDIRS) $(LIBS)

usbmass.bin: usbmass.o jafterspi.o libaudiomod.a
	vslink -k -m mem_desc.vs1000 -o $@ $(VS1000LIB)/c-restart.o usbmass.o jafterspi.o $(VS1000LIB)/rom1000.o $(LIBDIRS) $(LIBS)

# With no NAND-Flash header
VS1000_B.RUN: flasher24.bin
	$(COFF2BOOT) -n -x 0x50 flasher24.bin VS1000_B.RUN

LIBOBJS = common.o gpioctrl.o putstrp.o
libaudiomod.a: $(LIBOBJS)
	vsar ruv libaudiomod.a $(LIBOBJS)

usbmass.o: usbmass.c Makefile system.h
spiplayer.o: spiplayer.c Makefile system.h gpioctrl.h
	$(VCC) $(VCCFLAGS) $(INCDIRS) -o $@ $<
sdplayer.o: sdplayer.c Makefile system.h gpioctrl.h
	$(VCC) $(VCCFLAGS) $(INCDIRS) -o $@ $<

spiplayer.bin: spiplayer.o libaudiomod.a
	vslink -k -m mem_desc.vs1000 -o $@ $(VS1000LIB)/c-restart.o spiplayer.o $(VS1000LIB)/rom1000.o $(LIBDIRS) $(LIBS)

sdplayer.bin: sdplayer.o libaudiomod.a
	vslink -k -m mem_desc.vs1000 -o $@ $(VS1000LIB)/c-restart.o sdplayer.o $(VS1000LIB)/rom1000.o $(LIBDIRS) $(LIBS)

spiall.spi: usbmass.spi spiplayer.spi sdplayer.spi
	$(COMBINEIMG) -o spiall.spi -m 32768 +0 usbmass.spi +$(PLAYERSTART) spiplayer.spi +$(MMCSTART) sdplayer.spi
	echo >> spiall.spi

SDUPDATE.PRG: sdupdate.bin
	$(COFF2BOOT) -x 0x50 sdupdate.bin SDUPDATE.PRG

clean:
	$(RM) -f *.o *.a *.bin *.img *~

# Program firmware
program: spiall.spi prommer24.bin
	vs3emu -chip vs1000 -s 115200 -p 1 -l prommer24.bin -c run.cmd

# Program content from content.raw (FAT image)
content: content.bin
	vs3emu -chip vs1000 -s 115200 -p 1 -l content.bin -c run.cmd

# Dump content to dump.raw (FAT image)
dump: dump.bin
	vs3emu -chip vs1000 -s 115200 -p 1 -l dump.bin -c run.cmd

dist: clean
	zip audiomodule_gpio.zip Makefile *_desc* *.[chs] *.cmd *.bat *RUN *.spi *.exe *.abs lib/* docs/*.pdf README.TXT

progall.run: spiall.spi
