#
#	P2MESAC
#	main Makefile
#

EE_BIN = P2MESAC.ELF
EE_OBJS = obj/p2m_dp_loader.o obj/p2mesac.o obj/atad_p2m_init.o obj/ps2dev9.o obj/poweroff.o
EE_CFLAGS = -nostartfiles
EE_OBJS_DIR = obj/
EE_ASM_DIR = asm/
EE_ASFLAGS +=
EE_SRC_DIR = ee_loader/


#Set thw following line to 1 to build the modules poweroff and dev9 as well.
#BUILD_PWROFF_DEV9=0
BUILD_PWROFF_DEV9=1

#The following sets the loading address of the PPC-IOP patch data:
PPC_PATCH_ADDR_VAL=0x00A82000


#A flag with name PPC_PATCH_ADDR_VAL is passed with a value stored in PPC_PATCH_ADDR_VAL.
P2M_CFG_FLAGS = PPC_PATCH_ADDR_VAL=$(PPC_PATCH_ADDR_VAL)

EE_INCS = -I. -I$(PS2SDK)/ee/include -I$(PS2SDK)/common/include -Iinclude
EE_CFLAGS = -nostartfiles -DPPC_PATCH_ADDR=$(PPC_PATCH_ADDR_VAL)

EE_LDFLAGS = -L$(PS2SDK)/ee/lib -Xlinker -Map -Xlinker dp_loader.map
EE_LIBS = -lm -lc -ldebug -lkernel

BIN2S = $(PS2SDK)/bin/bin2s

all: 
	@mkdir -p obj
	@mkdir -p asm
	$(MAKE) $(EE_BIN)

clean:
	rm -f -r $(EE_BIN) $(EE_OBJS) $(EE_ASM_DIR) dp_loader.map
	$(MAKE) $(P2M_CFG_FLAGS) -C ppc-iop_patch clean
	$(MAKE) -C mips-iop_init clean
ifeq ($(BUILD_PWROFF_DEV9),1)
	$(MAKE) -C poweroff clean
	$(MAKE) -C dev9 clean
endif

p2mesac.s:
	$(MAKE) $(P2M_CFG_FLAGS) -C ppc-iop_patch
	$(BIN2S) ppc-iop_patch/p2mesac.bin asm/p2mesac.s p2mesac_bin

atad_p2m_init.s:
	$(MAKE) -C mips-iop_init
	$(BIN2S) mips-iop_init/atad_p2m_init.irx asm/atad_p2m_init.s atad_p2m_init_irx

poweroff.s:
ifeq ($(BUILD_PWROFF_DEV9),1)
	$(MAKE) -C poweroff
endif
	$(BIN2S) poweroff/irx/poweroff.irx asm/poweroff.s poweroff_irx

ps2dev9.s:
ifeq ($(BUILD_PWROFF_DEV9),1)
	$(MAKE) -C dev9
endif
	$(BIN2S) dev9/irx/ps2dev9.irx asm/ps2dev9.s ps2dev9_irx

$(EE_OBJS_DIR)%.o : $(EE_SRC_DIR)%.c
	$(EE_CC) $(EE_CFLAGS) $(EE_INCS) -c $< -o $@

$(EE_OBJS_DIR)%.o : %.s
	$(EE_AS) $(EE_ASFLAGS) $(EE_ASM_DIR)$< -o $@

include $(PS2SDK)/samples/Makefile.pref
include $(PS2SDK)/samples/Makefile.eeglobal
