# define the TMS9900 compilation tools

GAS=tms9900-as
LD=tms9900-ld
CC=tms9900-gcc

# define paths to elf2 conversion utilities

ELF2PROGRAM=elf2ea5
ELF2CART=/tools/overrides/elf2cart
C2UBIN=/tools/c2ubin/c2ubin

# make flags

MAKEFLAGS += --silent

# compiler flags

C_FLAGS=\
  -Os -fno-function-cse -fno-peephole2 -std=c99 -Wall -DBUILD_TI99 -fno-builtin-fputs -fno-builtin-fprintf
#--save-temp -Wa,-adhln -g

INC_PATH=-I/libunix99r3/include -I/libunix99r3/include/private -I/libti99

PROGRAM_ASM_SRC := $(wildcard *_ea5.asm)
PROGRAM_ASM_OBJ := $(patsubst %.asm,%.o,$(PROGRAM_ASM_SRC))

PROGRAM_LIBS=-ltrampoline -lcommon -ltrampoline -lti99 -lgcc

LIB_PATH=-L$(BUILD_DIR) -L/libunix99r3/build -L/libti99 -L/opt/tms9900/gcc-4.4.0/lib/gcc/tms9900/4.4.0

# link flags

# define and locate the sections across the address space
# .cache    = 2000 - 245f
# .dylib    = 2462 - 24f7
# .stdfiles = 24f8 - 24fd
# .errno    = 24fe - 24ff
# .data     = 2500 - 3FFF
# .scratch  = 8320 -

PROGRAM_LD_FLAGS=\
  --section-start .text=A000 --section-start .data=2500 --section-start .cache=2000 --section-start .dylib=2460 --section-start .stdfiles=24f8 --section-start .errno=24fe --section-start .scratch_cache=8320 --section-start .scratch_speech=833c -M

# define the image paths

BUILD_DIR=build
EXECUTABLES_DIR=$(BUILD_DIR)/image

# define executables

hello=$(BUILD_DIR)/hello.elf

# define building 'all'

all: $(hello)

# build recipes for the programs

$(hello): $(PROGRAM_ASM_OBJ) $(LIB_TRAMPOLINE) $(LIB_COMMON) hello.o
	echo ${@}
	$(LD) $(PROGRAM_ASM_OBJ) $(subst .elf,.o,$(notdir ${@})) $(LIB_PATH) $(PROGRAM_LIBS) $(PROGRAM_LD_FLAGS) -o $(@) > $(subst .elf,.map,$(@))
	$(ELF2PROGRAM) $(@) $(subst .elf,.bin,$(@))
	$(C2UBIN) -exec $(subst .elf,.bin,$(@)) $(subst .elf,.c2ubin,$(@))

# define build rules for C objects

%.o: %.c
	echo $<
	$(CC) $(C_FLAGS) $(INC_PATH) -c $< -o $@

# define build rules for ASM objects

%.o: %.asm
	echo $<
	$(GAS) $< -o $@

# Recipe to clean all compiled objects

clean:
	-rm *.o
	-rm $(BUILD_DIR)/*
