#
# define      description
# ------      ------------------------------------------------------
# DECODER     select MP3 decoder -- ITTIAM, MAD, or VSOFTS (default)
# TEST        compile in test program, testProc.c
# DEBUG       enable debugging statements
# DEBUG_ERROR enable only error messages
# PERF        enable performance measurement and reporting
# USB         console i/o goes to USB, else JTAG
# UNDERFLOW   0=ignore underflows (default), 1=treat underflow as an
#             error

DEBUG_ERROR = 1
USB = 1

include $(ROOT)/usr/include/make/SCdefs

MP3_ITTIAM_LIB = ../../lib/ittiam/mp3
MP3_MAD_LIB = ../../lib/mad
MP3_VSOFTS_LIB = $(ROOT)/usr/lib/sc

TARGETS = mp3.elf libmp3.a

# enable internal test program
ifdef TEST
LCDEFS += -DTEST_PROC
endif

# enable debugging support
ifdef DEBUG
LCDEFS += -DMP3_DEBUG -DMP3U_DEBUG -DMAD_DEBUG -DITT_DEBUG -DVSS_DEBUG -DMP3DT_DEBUG
endif

ifdef DEBUG_ERROR
LCDEFS += -DMP3_DEBUG_ERROR -DMP3U_DEBUG_ERROR -DMAD_DEBUG_ERROR -DITT_DEBUG_ERROR -DVSS_DEBUG_ERROR -DMP3DT_DEBUG_ERROR
endif

# enable performance measurements
ifdef PERF
LCDEFS += -DPERF
CFILES += perf.c
endif

# determine whether underflow will be treated as an error;
# for debugging purposes, underflow can be treated as an
# error by setting UNDERFLOW != 0; note that underflow 
# should NOT be treated as an error for production builds
ifdef UNDERFLOW
ifeq ($(UNDERFLOW),0)
LCDEFS += -DMP3_IGNORE_UNDERFLOW
endif
else
LCDEFS += -DMP3_IGNORE_UNDERFLOW
endif

# define ITTIAM for the Ittiam MP3 decoder, otherwise the MAD MP3
# decoder will be used
ifeq ($(DECODER),ITTIAM)
LIB_CFILES += mp3dec_ittiam.c
LCDEFS += -DMP3_DECODER=MP3_DECODER_ITTIAM
LCINCS += -I$(MP3_ITTIAM_LIB)/test/include
LLDLIBS += $(MP3_ITTIAM_LIB)/lib/IttiamAudioMP3DecoderLib.a
else 
ifeq ($(DECODER),MAD)
LIB_CFILES += mp3dec_mad.c
LCDEFS += -DMP3_DECODER=MP3_DECODER_MAD
LLDLIBS += -lmad
else
#default is VSOFTS
LIB_CFILES += mp3dec_vsofts.c
LCDEFS += -DMP3_DECODER=MP3_DECODER_VSOFTS
LCINCS += -I$(MP3_VSOFTS_LIB)
LLDLIBS += $(MP3_VSOFTS_LIB)/vsmp3lib.a
endif
endif
LLDLIBS += $(ROOT)/import/usr/lib/sc/libvoice.a
LLDLIBS += $(ROOT)/import/usr/lib/sc/libvnp.a 

ASFILES = 
LIB_CFILES += mp3.c mp3utils.c mp3dec_thread.c mp3_lib.c
CFILES += mp3_main.c $(LIB_CFILES)
HEADERS = mp3.h mediactl.h
LOCHEADERS = mp3dec.h mp3dec_thread.h mp3dec_vsofts.h mp3utils.h
LIB_OBJECTS = $(LIB_CFILES:.c=.o)

default: $(TARGETS)

LCOPTS += -marm

LLDLIBS += -lisfs -lios -lioslibc
ifdef USB
LLDLIBS += -lvio_usb
else
LLDLIBS += -lvio
endif

#LLDOPTS += -Ttext $(MK_MEDIA_START) -T $(SDRAM_LINK) -Wl,--no-warn-mismatch
LLDOPTS += -Ttext $(MK_APP_START) -T $(SDRAM_LINK) -Wl,--no-warn-mismatch

headers: $(HEADERS)
	$(INSTALL) $(HEADERS) $(ROOT)/usr/include/sc

exports: $(TARGETS)
	$(INSTALL) mp3.elf $(ROOT)/usr/bin/sc
	$(INSTALL) libmp3.a $(ROOT)/usr/lib/sc

libmp3.a: %: %($(LIB_OBJECTS))
	ar cru $@ $?

mp3.elf: $(CRT0) mp3_main.o libmp3.a
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

%app.mrg: %.elf $(ROOT)/usr/bin/sc/sk.elf 
	$(MERGE) -k $(ROOT)/usr/bin/sc/sk.elf -f $(ROOT)/usr/bin/sc/fs.elf -s $(MK_SHARED_START) -z 0x20000 -a $(ROOT)/usr/bin/sc/aud.elf -n $< -o $@

run%: %app.mrg
	dd if=/dev/urandom of=in bs=16k count=1
	export NAND_FILE=/var/tmp/fl.dummy; $(ROOT)/usr/bin/x86/sce -t 0x1ff -f -o $< --audioIn in --audioOut out

include $(COMMONRULES)
