##
## 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=tm2-test

# kernel sources & objects
KSOURCES=kmain.c mm.c tm.c direct-ir.c direct-motor.c conio.c
KOBJECTS=kmain.o mm.o tm.o direct-ir.o direct-motor.o conio.o

# user sources
USOURCES=$(TARGET).c

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

# cflags
CFLAGS=-O2 -fno-builtin -DNO_DIRECT_IR #-DNO_DIRECT_MOTOR#-DNO_TASK_MANAGEMENT -DNO_MEMORY_MANAGEMENT

# 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
dir-test.o: direct-ir.h mem.h direct-button.h rcx-lcd.h conio.h
direct-ir.o: direct-ir.h mem.h h8.h rcx-irq.h conio.h rcx-lcd.h
direct-motor.o: direct-motor.h h8.h rcx-irq.h
direct-sound.o: bitops.h direct-button.h rcx-lcd.h conio.h h8.h rcx-irq.h
direct-sound.o: sample.h
dlcd-test.o: direct-lcd.h bitops.h rcx-lcd.h
dmot-test.o: direct-motor.h rcx-lcd.h
ds-old.o: bitops.h direct-button.h rcx-lcd.h conio.h h8.h rcx-irq.h sample.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 direct-ir.h direct-motor.h
lcd-test.o: rcx-rom.h rcx-lcd.h
main.o: mem.h stdlib.h rcx-lcd.h
mm-test.o: rcx-lcd.h conio.h direct-button.h rcx-sound.h mm-internal.h mem.h
mm-test.o: stdlib.h
mm.o: mm-internal.h mem.h stdlib.h tm-internal.h tm.h
send-raw.o: /usr/include/sys/types.h /usr/include/features.h
send-raw.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
send-raw.o: /usr/include/gnu/types.h /usr/include/time.h
send-raw.o: /usr/include/stddef.h /usr/include/endian.h
send-raw.o: /usr/include/bytesex.h /usr/include/sys/select.h
send-raw.o: /usr/include/selectbits.h /usr/include/sys/stat.h
send-raw.o: /usr/include/statbuf.h /usr/include/fcntl.h
send-raw.o: /usr/include/fcntlbits.h stdlib.h mem.h /usr/include/unistd.h
send-raw.o: /usr/include/posix_opt.h /usr/include/confname.h
send-raw.o: /usr/include/termios.h /usr/include/termbits.h
send-raw.o: /usr/include/sys/ttydefaults.h /usr/include/stdio.h
send-raw.o: /usr/include/libio.h /usr/include/_G_config.h
send-raw.o: /usr/lib/gcc-lib/i386-redhat-linux/2.7.2.3/include/stdarg.h
send-raw.o: /usr/include/stdio_lim.h /usr/include/string.h
send-raw.o: /usr/include/sys/time.h /usr/include/timebits.h
send-raw.o: /usr/include/ctype.h
sensortest.o: rcx-lcd.h conio.h direct-button.h bitops.h
tm-test.o: tm.h mem.h rcx-lcd.h conio.h direct-button.h
tm.o: tm-internal.h tm.h mem.h bitops.h stdlib.h rcx-lcd.h
