#!/bin/sh

TARGET=$(basename $1 .c)
TARGET_CPU=r3000
ROM_WORDADDR_WIDTH=12
RAM_WORDADDR_WIDTH=11

echo Building $TARGET...

mipsel-elf-as.exe -march=$TARGET_CPU startup.s -o startup.o
mipsel-elf-as.exe -march=$TARGET_CPU init.s -o init.o
mipsel-elf-as.exe -march=$TARGET_CPU kernel.s -o kernel.o

#mipsel-elf-gcc -O2 -march=$TARGET_CPU $TARGET.c -o $TARGET.s -L $LIBDIR

mipsel-elf-gcc -msoft-float -O2 -c utils.c -o utils.o
mipsel-elf-gcc -msoft-float -O2 -c $TARGET.c -o $TARGET.o
mipsel-elf-ld.exe -G 0 -M -T link.ld startup.o init.o kernel.o utils.o $TARGET.o -o $TARGET.elf -L /usr/local/mipsel-elf/lib -L /usr/local/lib/gcc/mipsel-elf/3.4.6 -lc -lgcc >$TARGET.map
mipsel-elf-objdump.exe -d $TARGET.elf > $TARGET.dis

mipsel-elf-objcopy.exe -j .stext -j .ktext -j .text $TARGET.elf -O binary $TARGET.ROM.bin
mipsel-elf-objcopy.exe -j .data $TARGET.elf -O binary $TARGET.RAM.bin

./romgen.exe $TARGET.ROM.bin $ROM_WORDADDR_WIDTH
./ramgen.exe $TARGET.RAM.bin $RAM_WORDADDR_WIDTH

#xtclsh.exe $TARGET.RAM.tcl
#xtclsh.exe $TARGET.ROM.tcl

echo done.