win: prom.bin prom24.bin boot1053b.img \
	player1053bbut.bin 
#	copy boot1053b.img c:\panze\vskit\vs1000bcwork\ee.img


all: coff2boot prom.bin boot1053b.img \
	player1053bbut.bin player1053bsci.bin player1053bsci-boot.bin \
	recorder1053b.bin

boot1053b.img: player1053bbut.bin
	./coff2boot -x 0x50 player1053bbut.bin boot1053b.img
	echo _%COMPUTERNAME%_%DATE%_%TIME% >>boot1053b.img

rom1053b.o: rom1053b.txt
	mkabs -o $@ -f $<

VCC = vcc
INCDIR = -Ilibc16 -Ih1053 -I. #-Iflac
LIBDIR = -Llibc16 -L. #-Lflac -lflac_sa
#normal SW3 functions: toggle random play when in pause, loudness when play
# -D SW3_RANDOM_PLAY
# -D NO_RANDOM   -39 words
#COMMONFLAGS = -D ENABLE_WATCHDOG -D ENABLE_LAYER12 -D NO_RANDOM -D NO_MIDI -D NO_AAC
#COMMONFLAGS = -D ENABLE_WATCHDOG -D ENABLE_LAYER12 -D NO_RANDOM -D NO_MIDI
#COMMONFLAGS = -D ENABLE_WATCHDOG -D NO_RANDOM
#COMMONFLAGS = -D RECORD_FS=24000
COMMONFLAGS = -D RECORD_FS=8000
# -D ENABLE_WATCHDOG
# -D ENABLE_LAYER12
# -D LOUDNESS_ON
VCCFLAGS = -P130 -O -fsmall-code $(COMMONFLAGS)
ASMDEFS  = -D ASM -D STANDALONE $(COMMONFLAGS) \
	-D VOL_VAL=0x2020 -D BASS_VAL=0x33d9 -D MODEORMASK="(1<<14)"
#16 earspeaker low
#128 earspeaker high
#144 earspeaker extreme


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

.s.o:
	vsa $(ASMDEFS) -o $@ $<

####
# This is the SPI EEPROM programmer. prom.bin reads boot.img
####
EOBJS = proms.o prom.o rom1053b.o
prom.bin: $(EOBJS)
	vslink -m mem_desc.lnk -k $(EOBJS) -o $@ -L./ -Llibc16/ -lc
	vsomd -o prom.omd prom.bin

E24OBJS = proms.o prom24.o rom1053b.o
prom24.bin: $(E24OBJS)
	vslink -m mem_desc.lnk -k $(E24OBJS) -o $@ -L./ -Llibc16/ -lc
	vsomd -o prom24.omd prom24.bin




####
# Here are the actual player versions.
####

main1053but.o: standalone.c standalone.h fat.h Makefile
	$(VCC) $(VCCFLAGS) $(INCDIR) -D VS1053 -D ENABLE_UI -o $@ standalone.c

main1053sci.o: standalone.c standalone.h fat.h Makefile
	$(VCC) $(VCCFLAGS) $(INCDIR) -D VS1053 -D SCI_UI -D UART_UI  -o $@ standalone.c

####
# Startup codes
#
# Change CLOCKF_VAL to match your system.
#
# VOL_VAL sets the default volume after bootup and watchdog reset.
# If VOL_VAL is not defined, the default will be -6.0dB (0x0c0c).
#
# BASS_VAL sets the SCI_BASS register value.
# If BASS_VAL is not defined, 0x0000 is used.
#
####

#3.5x 12.288MHz
r1053.o: c.s Makefile
	vsa -D RECORDER -D VS1053 -D CLOCKF_VAL=0x8000 -D PLL3_5 $(ASMDEFS) -o $@ c.s

#3.5x 12.288MHz
r1053-sci.o: c.s Makefile
	vsa -D RECORDER -D SCI_UI -D UART_UI -D VS1053 -D CLOCKF_VAL=0x8000 -D PLL3_5 $(ASMDEFS) -o $@ c.s


#3.5x 12.288MHz
c1053.o: c.s Makefile
	vsa -D VS1053 -D CLOCKF_VAL=0x8000 -D PLL3_5 $(ASMDEFS) -o $@ c.s
#	vsa -D VS1053 -D CLOCKF_VAL=0xe000 -D PLL3_5 $(ASMDEFS) -o $@ c.s

#3.5x 12.288MHz
c1053-sci.o: c.s Makefile
	vsa -D VS1053 -D CMD -D SCI_UI -D UART_UI -D CLOCKF_VAL=0x8000 -D PLL3_5 $(ASMDEFS) -o $@ c.s

#3.5x 12.288MHz
c1053-boot.o: c.s Makefile
	vsa -D VS1053 -D SCI_UI -D UART_UI -D CLOCKF_VAL=0x8000 -D PLL3_5 $(ASMDEFS) -o $@ c.s

# Library objects
LIBOBJS = scis.o ref.o scisrec.o buts.o buts+ref.o butsrec.o asm-mmc.o asm-readdisk.o asm-fraglist.o asm-initfsys.o asm-initfsysr.o asm-openfile.o asm-writesec.o asm-writesecswap.o asm-nextsect.o asm-chk1053.o asm-swap.o asm-saveword.o asm-clocksdi.o asm-myrand.o asm-shuffle.o \
c-iterate.o c-fatfindentry.o c-fatmakefile.o c-mmcwrite.o asm-swapword.o c-fatalloc2.o asm-imafix.o \
uartasm.o patchframe.o #flac_abs.o

flac_abs.o: Makefile
	mkabs -o flac_abs.o _flacData=0x2000 _flacSave=0x1000

c-mmcwrite.o: c-mmcwrite.c standalone.h

asm-initfsysr.o: asm-initfsys.s fat.h
	vsa -D RECORDER $(ASMDEFS) -o $@ $<
asm-chk1053.o: asm-chk.s Makefile
	vsa -D VS1053 $(ASMDEFS) -o $@ asm-chk.s

asm-mmc.o: asm-mmc.s Makefile
asm-fraglist.o: asm-fraglist.s fat.h
asm-initfsys.o: asm-initfsys.s fat.h
asm-openfile.o: asm-openfile.s fat.h

libstandalone.a: $(LIBOBJS)
	vsar ruv libstandalone.a $(LIBOBJS)


recorder1053.o: standalone.c standalone.h fat.h Makefile
	$(VCC) $(VCCFLAGS) -D RECORDER -D ENABLE_UI -D VS1053 $(INCDIR) -o $@ standalone.c

recorder1053b-sci.o: standalone.c standalone.h fat.h Makefile
	$(VCC) $(VCCFLAGS) -D RECORDER -D SCI_UI -D UART_UI -D VS1053 $(INCDIR) -o $@ standalone.c

#asm-mmc2.o: asm-mmc.s
#	vsa -o asm-mmc2.o asm-mmc.s

#c2.o: c2.s Makefile
#	vsa -o $@ c2.s

#c3.o: c2.s Makefile
#	vsa $(ASMDEFS) -o $@ c2.s

REC1053 = r1053.o recorder1053.o
recorder1053b.bin: $(REC1053) rom1053b.o libstandalone.a Makefile
	vslink -m mem_desc.vs1053 -k -o $@ $(REC1053) rom1053b.o $(LIBDIR) -lstandalone -lc
	vssym -Q $@

recorder1053b.img: recorder1053b.bin
	./coff2boot -x 0x50 recorder1053b.bin recorder1053b.img

REC1053BSCI = r1053-sci.o recorder1053b-sci.o
recorder1053bsci.bin: $(REC1053BSCI) rom1053b.o libstandalone.a Makefile
	vslink -m mem_desc.vs1053 -k -o $@ $(REC1053BSCI) rom1053b.o $(LIBDIR) -lstandalone -lc
	vssym -Q $@




player1053bbut.bin: c1053.o main1053but.o rom1053b.o libstandalone.a
	vslink -m mem_desc.vs1053 -k -o $@ c1053.o main1053but.o rom1053b.o $(LIBDIR) -lc -lstandalone
	vssym -Q $@

player1053bsci.bin: c1053-sci.o main1053sci.o rom1053b.o libstandalone.a
	vslink -m mem_desc.vs1053 -k -o $@ c1053-sci.o main1053sci.o rom1053b.o $(LIBDIR) -lc -lstandalone

player1053bsci-boot.bin: c1053-boot.o main1053sci.o rom1053b.o libstandalone.a
	vslink -m mem_desc.vs1053 -k -o $@ c1053-boot.o main1053sci.o rom1053b.o $(LIBDIR) -lc -lstandalone


coff2boot: coff2boot.c
	gcc -Wall -O -o coff2boot coff2boot.c

coff2boot.exe: coff2boot.c
	cl coff2boot.c

dist:	player1053bbut.bin player1053bsci.bin player1053bsci-boot.bin \
	recorder1053b.bin recorder1053bsci.bin
#	(cd docs; gmake pdf)
	rm -f code/*
# Generate boot images for 25LC640
	./coff2boot -x 0x50 player1053bbut.bin code/player1053bbut.bin
	./coff2boot -x 0x50 player1053bsci-boot.bin code/player1053bsci.bin
	./coff2boot -x 0x50 recorder1053b.bin code/recorder1053b.bin
	./coff2boot -x 0x50 recorder1053bsci.bin code/recorder1053bsci.bin
# Insert an extra byte for word-programming software..
	echo >>code/player1053bbut.bin
	echo >>code/player1053bsci.bin
	echo >>code/recorder1053b.bin
	echo >>code/recorder1053bsci.bin
# Generate command files
	perl coff2cmd.pl player1053bbut.bin >code/player1053bbut.cmd
#
	perl coff2cmd.pl player1053bsci.bin >code/player1053bsci.cmd
#
	perl coff2cmd.pl recorder1053bsci.bin >code/recorder1053bsci.cmd
	perl cmdtotab.pl code/recorder1053bsci.cmd >code/recorder1053bsci.c
# Generate C loading tables
	perl cmdtotab.pl code/player1053bsci.cmd >code/player1053bsci.c
#
	perl cmdtotab.pl code/player1053bbut.cmd >code/player1053bbut.c
	rm -f standalone.zip
# Add the code subdirectory so it does not need to created by Makefile
	zip code/sources.zip README.txt Makefile *desc* *.[chs] h1053/* libc16/* *.pl rom1053b.txt promthis.bat prom.bin *.cmd code/*.c
	zip -r standalone.zip code/*
# docs/standalone.pdf

