CFLAGS=-Os -I. -I../ -msoft-float -march=r3000
LFLAGS=-M -T bootloader.ld
LIB_DIRS=-L $(MIPS_TOOLS_PREFIX)/mipsel-elf/lib -L $(MIPS_TOOLS_PREFIX)/lib/gcc/mipsel-elf/$(MIPS_GCC_VER)
LIBS=-lc -lgcc

CC=mipsel-elf-gcc
LD=mipsel-elf-ld
OBJDUMP=mipsel-elf-objdump
OBJCOPY=mipsel-elf-objcopy

all: bootloader_flash

libsys: startup_boot.S init_boot.S kernel_boot.S libsys_boot.c
	$(CC) $(CFLAGS) -c startup_boot.S -o startup.o
	$(CC) $(CFLAGS) -c init_boot.S -o init.o
	$(CC) $(CFLAGS) -c kernel_boot.S -o kernel.o
	$(CC) $(CFLAGS) -c libsys_boot.c -o libsys.o

bootloader: libsys bootloader.c 
	$(CC) $(CFLAGS) -g -c bootloader.c
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o libsys.o bootloader.o -o bootloader.elf $(LIBS) >bootloader.map
	$(OBJDUMP) -d bootloader.elf > bootloader.dis
	$(OBJCOPY) bootloader.elf -O binary bootloader.ROM.bin
	$(OBJCOPY) -O srec bootloader.elf bootloader.srec
	romgen bootloader.ROM.bin 10

bootloader_flash: libsys bootloader_with_flash.c 
	$(CC) $(CFLAGS) -g -c bootloader_with_flash.c
	$(CC) $(CFLAGS) -g -c ../cfiflash.c
	$(LD) $(LFLAGS) $(LIB_DIRS) startup.o init.o kernel.o libsys.o cfiflash.o bootloader_with_flash.o -o bootloader.elf $(LIBS) >bootloader.map
	$(OBJDUMP) -d bootloader.elf > bootloader.dis
	$(OBJCOPY) bootloader.elf -O binary bootloader.ROM.bin
	$(OBJCOPY) -O srec bootloader.elf bootloader.srec
	romgen bootloader.ROM.bin 11

clean:
	rm -rf *.o *.bin *.map *.dis *.srec *.elf *.vhd* *.tcl bootloader > /dev/null
