#######################################
# Include the main configuration file #
#######################################

include Makefile.inc

#####################
# Compilation Flags #
#####################

TEXT_BASE_STAGE1 = 0x80A02000
TEXT_BASE_STAGE2 = 0x81FD0000

ASFLAGS = -mips32 -G0

LDFLAGS_STAGE1 = -Bstatic -Wl,-Tlink.x -Wl,-Ttext=$(TEXT_BASE_STAGE1)

LDFLAGS_STAGE2 = -Bstatic -Wl,-Tlink.x -Wl,-Ttext=$(TEXT_BASE_STAGE2)

CFLAGS = -O2 -march=mips32 -mno-mips16 -mno-abicalls -mno-long-calls -Wno-parentheses -fomit-frame-pointer \
			-fshort-wchar -fno-pic -fno-builtin -fno-exceptions -nostartfiles -nodefaultlibs -ffunction-sections \
			-msoft-float -G0

##############
# Make rules #
##############

.DEFAULT_GOAL = all

stage1.bin : stage2.bin
	$(AS) $(ASFLAGS) stage1.s -o stage1.o
	$(LD) $(LDFLAGS_STAGE1) $(CFLAGS) stage1.o -o stage1_linked.o
	$(OBJCOPY) -O binary stage1_linked.o stage1.bin

stage2.bin : stage2.c
	$(AS) $(ASFLAGS) stage2.s -o start.o
	$(CC) $(CFLAGS) -c stage2.c -o stage2.o
	$(LD) $(LDFLAGS_STAGE2) $(CFLAGS) start.o stage2.o -o stage2_linked.o
	$(OBJCOPY) -O binary stage2_linked.o stage2.bin

.PHONY : all clean

all : stage1.bin

clean:
	rm -f stage1.o
	rm -f stage1_linked.o
	rm -f stage1.bin
	rm -f stage2.o
	rm -f stage2_linked.o
	rm -f stage2.bin
