CFLAGS=-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_el

PROG = hello testbench test_irq dhry queens stanford paranoia rmd160_test Bessel whet phrasen dttl richards_benchmark test_exception

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 -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
	$(AR) -r libsys.a libsys.o xcpt.o irq.o
	
hello: hello.c
	$(CC) $(CFLAGS) -o $@.elf -Os -g hello.c $(LIBS) >$@.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin

testbench: testbench.c 
	$(CC) $(CFLAGS) -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

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

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

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

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

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

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

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

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

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

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

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

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

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