# For FreeBSD
ifeq ($(OSTYPE),FreeBSD)
COFF2BOOT = ./coff2boot.freebsd
RM = rm -f
else
ifeq ($(OSTYPE),linux)
COFF2BOOT = ./coff2boot
RM = rm -f
else
# For Windows
COFF2BOOT = coff2boot.exe
RM = del
endif
endif


all: $(COFF2BOOT) prom.bin boot1063a.img \
	player1063abut.bin player1063asci.bin player1063asci-boot.bin \
	recorder1063a.bin
# recorder1063asci.bin


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

VCC = vcc
INCDIR = -Ilibc16 -Ih1063 -I. -I../libc16
LIBDIR = -Llibc16 -L.

COMMONFLAGS = -D ENABLE_LAYER12
# -D LOUDNESS_ON
VCCFLAGS = -P130 -O -g -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 rom1063a.o
prom.bin: $(EOBJS)
	vslink -m mem_desc.lnk -k $(EOBJS) -o $@ -L./ -Llibc16/ -lc
	vsomd -o prom.omd prom.bin

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

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

main1063sci.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
r1063.o: c2.s standalone.h Makefile
	vsa -D RECORDER -D VS1053 -D CLOCKF_VAL=0x8000 -D PLL3_5 $(ASMDEFS) -o $@ c2.s

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


#3.5x 12.288MHz
c1063.o: c2.s standalone.h Makefile
	vsa -D VS1053 -D CLOCKF_VAL=0x8000 -D PLL3_5 $(ASMDEFS) -o $@ c2.s
#	vsa -D VS1053 -D CLOCKF_VAL=0x84e2 -D PLL3_5 $(ASMDEFS) -o $@ c2.s
#14MHx	vsa -D VS1053 -D CLOCKF_VAL=0x85dc -D PLL3_5 $(ASMDEFS) -o $@ c2.s
#	vsa -D VS1053 -D CLOCKF_VAL=0xe000 -D PLL3_5 $(ASMDEFS) -o $@ c2.s

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

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

# Library objects
LIBOBJS = buts.o stddebug.o \
	asm-mmc.o asm-readdisk.o asm-fraglist.o asm-initfsys.o \
	asm-openfile.o asm-writesec.o asm-nextsect.o \
	asm-chk.o asm-swap.o asm-saveword.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 \
	uartasm.o \
	vs1063a-patch.o

c-mmcwrite.o: c-mmcwrite.c standalone.h
asm-chk.o: asm-chk.s Makefile standalone.h
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)


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

recorder1063a-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

REC1063 = c.o r1063.o recorder1063.o
recorder1063a.bin: $(REC1063) rom1063a.o libstandalone.a Makefile
	vslink -m mem_desc.vs1063 -k -o $@ $(REC1063) rom1063a.o $(LIBDIR) -lstandalone -lc
	vssym -Q $@

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

#REC1063ASCI = r1063-sci.o recorder1063a-sci.o
#recorder1063asci.bin: $(REC1063ASCI) rom1063a.o libstandalone.a Makefile
#	vslink -m mem_desc.vs1063 -k -o $@ $(REC1063ASCI) rom1063a.o $(LIBDIR) -lstandalone -lc
#	vssym -Q $@




player1063abut.bin: c.o c1063.o main1063but.o rom1063a.o libstandalone.a
	vslink -m mem_desc.vs1063 -k -o $@ c.o c1063.o main1063but.o rom1063a.o $(LIBDIR) -lstandalone -lc
	vssym -Q $@

player1063asci.bin: c.o c1063-sci.o main1063sci.o rom1063a.o libstandalone.a
	vslink -m mem_desc.vs1063 -k -o $@ c.o c1063-sci.o main1063sci.o rom1063a.o $(LIBDIR) -lc -lstandalone

player1063asci-boot.bin: c.o c1063-boot.o main1063sci.o rom1063a.o libstandalone.a
	vslink -m mem_desc.vs1063 -k -o $@ c.o c1063-boot.o main1063sci.o rom1063a.o $(LIBDIR) -lc -lstandalone


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

coff2boot.exe: coff2boot.c
	cl coff2boot.c

boot1063a.img: player1063abut.bin
	$(COFF2BOOT) -x 0x50 player1063abut.bin boot1063a.img
	echo >>boot1063a.img

rec1063a.img: recorder1063a.bin
	$(COFF2BOOT) -x 0x50 recorder1063a.bin rec1063a.img
	echo >>rec1063a.img

bins: player1063abut.bin player1063asci.bin player1063asci-boot.bin \
	recorder1063a.bin
# recorder1063asci.bin
	$(RM) code\*.*
	$(COFF2BOOT) -x 0x50 player1063abut.bin code/player1063abut.bin
	$(COFF2BOOT) -x 0x50 player1063asci-boot.bin code/player1063asci.bin
	$(COFF2BOOT) -x 0x50 recorder1063a.bin code/recorder1063a.bin
#	$(COFF2BOOT) -x 0x50 recorder1063asci.bin code/recorder1063asci.bin


# Create full package
# Generate boot images for 25LC640
# Generate command files
# Generate C loading tables
# Add the code subdirectory so it does not need to created by Makefile
dist:	player1063abut.bin player1063asci.bin player1063asci-boot.bin \
	recorder1063a.bin
# recorder1063asci.bin
	(cd docs; gmake pdf)
	$(RM) code/*
	$(COFF2BOOT) -x 0x50 player1063abut.bin code/player1063abut.bin
	$(COFF2BOOT) -x 0x50 player1063asci-boot.bin code/player1063asci.bin
	$(COFF2BOOT) -x 0x50 recorder1063a.bin code/recorder1063a.bin
#	$(COFF2BOOT) -x 0x50 recorder1063asci.bin code/recorder1063asci.bin
	echo >>code/player1063abut.bin
	echo >>code/player1063asci.bin
	echo >>code/recorder1063a.bin
#	echo >>code/recorder1063asci.bin
	perl coff2cmd.pl player1063abut.bin >code/player1063abut.cmd
	perl coff2cmd.pl player1063asci.bin >code/player1063asci.cmd
#	perl coff2cmd.pl recorder1063asci.bin >code/recorder1063asci.cmd
#	perl cmdtotab.pl code/recorder1063asci.cmd >code/recorder1063asci.c
	perl cmdtotab.pl code/player1063asci.cmd >code/player1063asci.c
	perl cmdtotab.pl code/player1063abut.cmd >code/player1063abut.c
	rm -f standalone.zip
	zip code/sources.zip CHANGES.txt Makefile *desc* *.[chs] h1063/* libc16/* *.pl rom1063a.txt promthis.bat prom.bin *.cmd coff2boot.exe code/*.c
	zip -r standalone.zip code/* docs/standalone.pdf
