##
## The unofficial LEGO Mindstorms RCX SDK
## Makefile - allows you to keep the upper hand
## (c) 1998 by Markus L. Noga <noga@inrialpes.fr>    
##

# tool prefix 
INSTPREFIX=/usr/local/bin/h8300-hitachi-hms-

# our target
TARGET=tm-test

# kernel sources & objects
KSOURCES=kmain.c mm.c tm.c tm-asm.S conio.o
KOBJECTS=kmain.o mm.o tm.o tm-asm.o conio.o

# user sources
USOURCES=$(TARGET).c

# which assembler startup file to use for C files
CBOOTSTRAP=cboot2

# cflags
CFLAGS=-O2 -fno-builtin

# linker command file.
# necessary because h8300 standard linkage stubbornly places
# read-only data segment in front of text segment. 
# took a while to figure that out.
LDFLAGS=-T h8300.rcx

# do we need zero padding of the srec file?
#NEED_ZERO_PADDING=--pad-to 0xC000


##
## older options
##

# how to disassemble
ODFLAGS = --disassemble-all --no-show-raw-insn -m h8300

# which firmware version to act upon
FIRMVERS=kernel1 #firm0309


##
## no user servicable parts below
##

AS=$(INSTPREFIX)as
LD=$(INSTPREFIX)ld
OBJCOPY=$(INSTPREFIX)objcopy
OBJDUMP=$(INSTPREFIX)objdump
GCC=$(INSTPREFIX)gcc

all: $(TARGET).srec

depend:
	makedepend *.c

tag:	$(KSOURCES) $(USOURCES)
	ctags $(KSOURCES) $(USOURCES)
	
clean:
	rm -f *.o *.coff *.srec *~ *.bak *.tgz *.s
	
%.tgz:
	sh -c "mkdir $*"
	cp Makefile h8300.rcx $(KSOURCES) $(USOURCES) *.h $(CBOOTSTRAP)*.S $*/
	tar -czf $*.tgz $*/*
	rm -rf $*/
	
	
# target linking
%.srec: $(KOBJECTS) %.o $(CBOOTSTRAP)-head.o $(CBOOTSTRAP)-tail.o
	$(LD) $(LDFLAGS) $(CBOOTSTRAP)-head.o $(KOBJECTS) $*.o $(CBOOTSTRAP)-tail.o -o $*.srec
	chmod a-x $*.srec

# in case you want symbols
%.coff: $(KOBJECTS) %.o $(CBOOTSTRAP)-head.o $(CBOOTSTRAP)-tail.o
	$(LD) $(LDFLAGS) $(CBOOTSTRAP)-head.o $(KOBJECTS) $* $(CBOOTSTRAP)-tail.o -o $*.coff --oformat coff-h8300
	chmod a-x $*.coff
	
###
### generic rules
###

# how to assemble
%.o: %.S
	$(AS) $*.S -o $*.o

# how to compile C source
%.o: %.c
	$(GCC) $(CFLAGS) -c $*.c -o $*.o

# how to make a srec file of the appropriate length
%.srec: %.coff	
	$(OBJCOPY) -I coff-h8300 -O srec $(NEED_ZERO_PADDING) $*.coff $*.srec
	chmod a-x $*.srec

# how to disassemble new firmware files
%.dis: %.srec
	$(OBJDUMP) $(ODFLAGS) $*.srec > $*.dis

# how to disassemble original firmware file
%.dis: %.lgo
	$(OBJDUMP) $(ODFLAGS) $*.lgo > $*.dis

# how to merge labels etc into disassembly
%.s: %.fix $(FIRMVERS).dis
	./fixfirm.pl $*.fix $(FIRMVERS).dis > $*.s

# how to reassemble new firmware file
%.srec: %.s
	$(AS) $*.s -o $*.o
	$(LD) -Ttext=0x8000 $*.o -o $*.out
	$(OBJCOPY) -O srec $*.out $*.srec
	rm -f $*.o $*.out
	chmod -x $*.srec

# DO NOT DELETE - makedepend depends on this!

btest.o: rcx-lcd.h conio.h direct-button.h
conio-test.o: conio.h rcx-lcd.h
conio-test2.o: conio.h rcx-lcd.h direct-lcd.h bitops.h
conio.o: mem.h conio.h rcx-lcd.h direct-lcd.h bitops.h
dlcd-test.o: direct-lcd.h bitops.h rcx-lcd.h
fontdesign.o: /usr/include/stdio.h /usr/include/libio.h
fontdesign.o: /usr/include/features.h /usr/include/sys/cdefs.h
fontdesign.o: /usr/include/gnu/stubs.h /usr/include/_G_config.h
fontdesign.o: /usr/include/gnu/types.h /usr/include/stddef.h
fontdesign.o: /usr/lib/gcc-lib/i386-redhat-linux/2.7.2.3/include/stdarg.h
fontdesign.o: /usr/include/stdio_lim.h
hello-world.o: rcx-lcd.h
kmain-0.o: stdlib.h mem.h mm-internal.h rcx-lcd.h rcx-sound.h rcx-rom.h
kmain.o: stdlib.h mem.h mm-internal.h rcx-lcd.h direct-button.h rcx-sound.h
kmain.o: rcx-rom.h conio.h
lcd-test.o: rcx-rom.h rcx-lcd.h
main.o: mem.h stdlib.h rcx-lcd.h
main2.o: mem.h process.h stdlib.h rcx-lcd.h
mm.o: mm-internal.h mem.h stdlib.h rcx-lcd.h
process.o: process.h mem.h stdlib.h rcx-lcd.h
