TOOLS_DIR=../../tools

CFLAGS=-mno-gpopt -msoft-float -O2 -march=r3000 -I.
LFLAGS=-M -T link.ld
LIB_DIRS=-L /usr/local/mipsel-elf/lib -L /usr/local/lib/gcc/mipsel-elf/4.3.2
LIBS=-lc -lm -lgcc

CC=mipsel-elf-gcc
LD=mipsel-elf-ld
OBJDUMP=mipsel-elf-objdump
OBJCOPY=mipsel-elf-objcopy
FLASHGEN=$(TOOLS_DIR)/flashgen_little

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

all: $(PROG)

libsys: startup.S init.S kernel.S libsys.c xcpt.c irq.o
	$(CC) $(CFLAGS) -c startup.S -o startup.o
	$(CC) $(CFLAGS) -c init.S -o init.o
	$(CC) $(CFLAGS) -c kernel.S -o kernel.o
	$(CC) $(CFLAGS) -c libsys.c -o libsys.o
	$(CC) $(CFLAGS) -c xcpt.c -o xcpt.o
	$(CC) $(CFLAGS) -c irq.c -o irq.o

hello: libsys hello.c 
	$(CC) $(CFLAGS) -Os -g -c hello.c
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o xcpt.o irq.o libsys.o hello.o -o hello.elf $(LIBS) >hello.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin

testbench: libsys testbench.c 
	$(CC) $(CFLAGS) -Os -g -c testbench.c paranoia.c -DNOSIGNAL -DBATCHMODE -DNOMAIN
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o xcpt.o irq.o libsys.o paranoia.o testbench.o -o testbench.elf $(LIBS) >testbench.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin

test_irq: libsys test_irq.c 
	$(CC) $(CFLAGS) -Os -g -c test_irq.c
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o xcpt.o irq.o libsys.o test_irq.o -o test_irq.elf $(LIBS) >test_irq.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin

dhry: libsys dhry_1.c dhry_2.c dhry.h
	$(CC) $(CFLAGS) -g -DHZ=1000 -c dhry_1.c dhry_2.c
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o xcpt.o irq.o libsys.o dhry_1.o dhry_2.o -o dhry.elf $(LIBS) >dhry.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin

queens: libsys queens.c 
	$(CC) $(CFLAGS) -DUNIX_Old -DHZ=1000 -g -c queens.c
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o xcpt.o irq.o libsys.o queens.o -o queens.elf $(LIBS) >queens.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin

stanford: libsys stanford.c
	$(CC) $(CFLAGS) -g -DHZ=1000 -c stanford.c
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o xcpt.o irq.o libsys.o stanford.o -o stanford.elf $(LIBS) >stanford.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin

paranoia: libsys paranoia.c
	$(CC) $(CFLAGS) -DNOSIGNAL -DBATCHMODE -g -c paranoia.c
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o xcpt.o irq.o libsys.o paranoia.o -o paranoia.elf $(LIBS) >paranoia.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin

rmd160_test: libsys rmd160_test.c rmd160.c rmd160.h
	$(CC) $(CFLAGS) -DNOSIGNAL -DBATCHMODE -g -c rmd160.c rmd160_test.c
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o xcpt.o irq.o libsys.o rmd160.o rmd160_test.o -o rmd160_test.elf $(LIBS) >rmd160.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin

Bessel: libsys Bessel.c 
	$(CC) $(CFLAGS) -g -c Bessel.c
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o xcpt.o irq.o libsys.o Bessel.o -o Bessel.elf $(LIBS) >Bessel.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin

whet: libsys whet.c 
	$(CC) $(CFLAGS) -g -c whet.c
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o xcpt.o irq.o libsys.o whet.o -o whet.elf $(LIBS) >whet.map
	$(OBJDUMP) -d $@.elf > $@.dis
	$(OBJCOPY) $@.elf -O binary $@.bin
	$(OBJCOPY) -O srec $@.elf $@.srec
	$(FLASHGEN) $@.bin

phrasen: libsys phrasen.c 
	$(CC) $(CFLAGS) -g -c phrasen.c
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o xcpt.o irq.o libsys.o phrasen.o -o phrasen.elf $(LIBS) >phrasen.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
