ENDIAN_FLAGS=-EB
CFLAGS=$(ENDIAN_FLAGS) -msoft-float -O2 -march=r3000 -I. -Wl,-M
LIBS=-lc -lm


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 testbench test_irq dhry queens stanford paranoia rmd160_test Bessel whet phrasen dttl richards_benchmark test_exception life r3 gunzip basic_math jman_patches jman_patchmeshes jman_polys

all: $(PROG)

libsys: startup.S kernel.S libsys.c xcpt.c irq.c
	$(CC) $(CFLAGS) -G 0 -c startup.S -o startup.o
	$(CC) $(CFLAGS) -G 0 -c kernel.S -o kernel.o
	$(CC) $(CFLAGS) -G 0 -DSTDERR_FUNCTION=_ser_putchar -DSTDOUT_FUNCTION=_ser_putchar -D_putchar=_ser_putchar -c libsys.c -o libsys.o
	$(CC) $(CFLAGS) -G 0 -c xcpt.c -o xcpt.o
	$(CC) $(CFLAGS) -G 0 -c irq.c -o irq.o
	$(CC) $(CFLAGS) -G 0 -c dbg.c -o dbg.o
	$(CC) $(CFLAGS) -G 0 -c mipsdis.c -o mipsdis.o
	$(AR) -r libsys.a libsys.o xcpt.o irq.o dbg.o mipsdis.o
	
hello: hello.c
	$(CC) $(CFLAGS) -o $@.elf -Os hello.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)

testbench: testbench.c 
	$(CC) $(CFLAGS) -O3 -o $@.elf -DNOSIGNAL -DBATCHMODE -DNOMAIN -Os -g testbench.c paranoia.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)

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

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

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

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

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

rmd160_test: rmd160_test.c rmd160.c rmd160.h
	$(CC) $(CFLAGS) -O2 -o $@.elf -DNOSIGNAL -DBATCHMODE rmd160.c rmd160_test.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)

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

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

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

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

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

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

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

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

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

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

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:	$(JMAN_SRC) jman/main_patches.c
	$(CC) $(CFLAGS) -I../ -o $@.elf -o $@.elf jman/main_patches.c $(JMAN_SRC) $(LIBS) >$@.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin $(ENDIAN_FLAGS)

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

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

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


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