ENDIAN='big'

ifeq ($(ENDIAN), 'big')
  ENDIAN_FLAGS=-EB
  CFLAGS=$(ENDIAN_FLAGS) -specs=specs/eb/specs -msoft-float -O2 -march=r3000 -I. -Ilibsys -Wl,-M
else
  ENDIAN_FLAGS=-EL
  CFLAGS=$(ENDIAN_FLAGS) -specs=specs/specs -msoft-float -O2 -march=r3000 -I. -Ilibsys -Wl,-M
endif

LIBS=-lm -lsys

AS=mipsel-elf-as
AR=mipsel-elf-ar
CC=mipsel-elf-gcc
LD=mipsel-elf-ld
OBJDUMP=mipsel-elf-objdump
OBJCOPY=mipsel-elf-objcopy
FLASHGEN=flashgen

PROG = hello.elf testbench.elf test_irq.elf dhry.elf queens.elf stanford.elf paranoia.elf rmd160_test.elf Bessel.elf whet.elf phrasen.elf dttl.elf richards_benchmark.elf test_exception.elf life.elf r3.elf gunzip.elf basic_math.elf jman_patches.elf jman_patchmeshes.elf jman_polys.elf test_hpi.elf test_vga.elf

all: $(PROG)

libsys.a:
	$(MAKE) -C libsys

hello.elf: hello.c libsys.a
	$(CC) $(CFLAGS) -o $@ -Os hello.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

testbench.elf: testbench.c  libsys.a
	$(CC) $(CFLAGS) -O3 -o $@ -DHZ=1000 -DNOSIGNAL -DBATCHMODE -DNOMAIN -Os -g testbench.c paranoia.c delay_mips.c bogomips.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

test_irq.elf: test_irq.c libsys.a
	$(CC) $(CFLAGS) -o $@ test_irq.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

dhry.elf: dhry_1.c dhry_2.c dhry.h libsys.a
	$(CC) $(CFLAGS) -o $@ -DHZ=1000 dhry_1.c dhry_2.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

queens.elf: queens.c libsys.a
	$(CC) $(CFLAGS) -o $@ -DUNIX_Old -DHZ=1000 queens.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

stanford.elf: stanford.c libsys.a
	$(CC) $(CFLAGS) -O3 -o $@ -DHZ=1000 stanford.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

paranoia.elf: paranoia.c libsys.a
	$(CC) $(CFLAGS) -O3 -o $@ -DNOSIGNAL -DBATCHMODE paranoia.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

rmd160_test.elf: rmd160_test.c rmd160.c rmd160.h libsys.a
	$(CC) $(CFLAGS) -O3 -o $@ rmd160_test.c rmd160.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

hashtest.elf: hashtest.c rmd160.c rmd160.h libsys.a
	$(CC) $(CFLAGS) -O3 -o $@ hashtest.c rmd160.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

Bessel.elf: Bessel.c libsys.a
	$(CC) $(CFLAGS) -o $@ Bessel.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

whet.elf: whet.c libsys.a
	$(CC) $(CFLAGS) -o $@ whet.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

phrasen.elf: phrasen.c libsys.a
	$(CC) $(CFLAGS) -o $@ phrasen.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

dttl.elf: dttl.c random.c libsys.a
	$(CC) $(CFLAGS) -o $@ dttl.c random.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

richards_benchmark.elf: richards_benchmark.c libsys.a
	$(CC) $(CFLAGS) -o $@ richards_benchmark.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

test_exception.elf: test_exception.c libsys.a
	$(CC) $(CFLAGS) -o $@ -O1 test_exception.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

life.elf: life.c libsys.a
	$(CC) $(CFLAGS) -o $@ -g life.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

r3.elf: r3.c libsys.a
	$(CC) $(CFLAGS) -O3 -o $@ -DJMIPS_VGA -O2 r3.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

gunzip.elf: gunzip.c inflate.c crc32.c libsys.a
	$(CC) $(CFLAGS) -o $@ -o $@ gunzip.c inflate.c crc32.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

basic_math.elf: basicmath_small.c   rad2deg.c  cubic.c   isqrt.c libsys.a
	$(CC) $(CFLAGS) -o $@ -o $@ basicmath_small.c rad2deg.c cubic.c isqrt.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

JMAN_SRC= \
	jman/bintree.c \
	jman/camset.c \
	jman/engine.c \
	jman/graph_state.c \
	jman/grid.c \
	jman/imageio.c \
	jman/linklist.c \
	jman/matrix.c \
	jman/object.c \
	jman/render.c \
	jman/ri.c \
	jman/ribgen.c \
	jman/stack.c \
	jman/vars.c

jman_patches.elf:	$(JMAN_SRC) jman/main_patches.c libsys.a
	$(CC) $(CFLAGS) -I../ -o $@ -o $@ jman/main_patches.c $(JMAN_SRC) $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

jman_patchmeshes.elf:	$(JMAN_SRC) jman/main_patchmeshes.c jman/patches2.c libsys.a
	$(CC) $(CFLAGS) -I../ -o $@ -o $@ jman/main_patchmeshes.c jman/patches2.c $(JMAN_SRC) $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

jman_polys.elf:	$(JMAN_SRC) jman/main_polypin.c jman/polys.c  libsys.a
	$(CC) $(CFLAGS) -I../ -o $@ -o $@ jman/main_polypin.c jman/polys.c $(JMAN_SRC) $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

serdump.elf: serdump.c  libsys.a
	$(CC) $(CFLAGS) -O3 -o $@ -O2 serdump.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

pppi.elf: pppissue/test1.c pppissue/utils.c pppissue/pppd.h libsys.a
	$(CC) $(CFLAGS) -O2 -o $@ -O2 pppissue/test1.c pppissue/utils.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

lwl.elf: lwl.c  libsys.a
	$(CC) $(CFLAGS) -O2 -o $@ lwl.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

ucb.elf: ucb.c  libsys.a
	$(CC) $(CFLAGS) -O2 -o $@ ucb.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

test_hpi.elf: hpi.c test_hpi.c cfiflash.c libsys.a
	$(CC) $(CFLAGS) -o $@ -g hpi.c test_hpi.c cfiflash.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin
	cat $@.srec | packhex > $@.pack

test_vga.elf: test_vga.c libsys.a
	$(CC) $(CFLAGS) -o $@ test_vga.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

barcode.elf: barcode.c libsys.a
	$(CC) $(CFLAGS) -o $@ barcode.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

lua.elf: lua_init.c libsys.a
	$(CC) $(CFLAGS) -I./lua-5.1.4/etc -I./lua-5.1.4/src -o $@ lua_init.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

mandelbrot.elf: mandelbrot.c libsys.a
	$(CC) $(CFLAGS) -o $@ mandelbrot.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

bogomips.elf: bogomips.c delay_mips.c libsys.a
	$(CC) $(CFLAGS) -DHZ=1000 -o $@ bogomips.c delay_mips.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

test_asm.elf: test_asm.S libsys.a
	$(CC) $(CFLAGS) -DHZ=1000 -o $@ test_asm.S $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

test_ac97.elf: test_ac97.c libsys.a
	$(CC) $(CFLAGS) -o $@ test_ac97.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

test_dcache.elf: test_dcache.c libsys.a
	$(CC) $(CFLAGS) -O0 -o $@ test_dcache.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

aes256_1.elf: aes256_1.c aes256.c aes256.h libsys.a
	$(CC) $(CFLAGS) -O3 -o $@ aes256_1.c aes256.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@ > $@.dis
	$(OBJCOPY) $@ -O binary $@.bin
	$(OBJCOPY) -O srec $@ $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)
	cat $@.srec | packhex > $@.pack

clean:
	rm -rf *.a *.o *.bin *.map *.dis *.srec *.pack *.elf $(PROG) > /dev/null
