##
## legOS - the independent LEGO Mindstorms OS
## Makefile - allows you to keep the upper hand
## (c) 1998 by Markus L. Noga <noga@inrialpes.fr>    
##

# our target
TARGET=demo/first-c++

# kernel sources & objects
KSOURCES=kmain.c mm.c systime.c tm.c semaphore.c conio.c lcd.c \
         direct-ir.c direct-motor.c direct-sensor.c direct-sound.c
         
KOBJECTS=$(KSOURCES:.c=.o)
      
# user sources
USOURCES=$(TARGET).cpp

# 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 -relax -Llib

# link all libraries. they consist of individual files per function,
# so nothing unnecessary will be linked.
LIBS= -lc -lmint -lc++

# do we need to pad the srec file with zeroes?
#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=firm0309


##
## no user servicable parts below
##

all: $(TARGET).srec $(TARGET).coff $(TARGET).o $(TARGET).dis2

include Makefile.common

# doc/html subdirectory
html:
	doxygen Doxyfile

# libraries subdirectory
libs:
	(cd lib; make)

depend:
	makedepend *.c -Yinclude

tag:
	ctags *.c include/*.h include/*/*.h $(USOURCES)

clean:
	rm -rf *.o *.map *.coff *.srec *.dis* *~ *.bak *.tgz *.s tags demo/*.o demo/*.map demo/*.coff demo/*.srec demo/*.dis*

realclean: clean
	(cd lib; make clean)
	rm -f doc/html/*.html
		
# how to build coff kernel / application image
%.coff: %.o $(KOBJECTS) libs
	$(LD) $(LDFLAGS) $(KOBJECTS) $*.o $(LIBS) -o $*.coff --oformat coff-h8300
	chmod a-x $*.coff

###
### generic rules
###

# 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 merge map files with symbols
%.dis2: %.map %.dis
	util/merge-map $*.map $*.dis > $*.dis2
        
# how to make map files
%.map: %.coff
	$(NM) $*.coff | sort -u > $*.map

# how to disassemble new firmware files
%.dis: %.coff
	$(OBJDUMP) $(ODFLAGS) $*.coff > $*.dis
	
# 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!

conio.o: include/conio.h include/config.h include/rom/lcd.h include/mem.h
conio.o: include/direct-lcd.h include/sys/bitops.h
direct-ir.o: include/direct-ir.h include/config.h include/mem.h
direct-ir.o: include/sys/h8.h include/sys/irq.h include/direct-lcd.h
direct-ir.o: include/sys/bitops.h include/conio.h include/rom/lcd.h
direct-motor.o: include/direct-motor.h include/config.h include/sys/h8.h
direct-motor.o: include/sys/irq.h
direct-sensor.o: include/direct-sensor.h include/config.h include/sys/h8.h
direct-sensor.o: include/sys/bitops.h include/sys/irq.h
direct-sensor.o: include/rom/registers.h include/unistd.h include/tm.h
direct-sensor.o: include/mem.h include/time.h include/conio.h
direct-sensor.o: include/rom/lcd.h
direct-sound.o: include/direct-sound.h include/config.h include/sys/bitops.h
direct-sound.o: include/sys/h8.h include/sys/irq.h
kmain.o: include/sys/mm.h include/config.h include/mem.h include/stdlib.h
kmain.o: include/sys/time.h include/time.h include/sys/tm.h include/tm.h
kmain.o: include/sys/irq.h include/sys/direct-sensor.h include/sys/h8.h
kmain.o: include/direct-sensor.h include/sys/bitops.h include/sys/direct-ir.h
kmain.o: include/direct-ir.h include/rom/system.h include/rom/sound.h
kmain.o: include/direct-button.h include/direct-motor.h include/conio.h
kmain.o: include/rom/lcd.h
lcd.o: include/rom/lcd.h
mm.o: include/sys/mm.h include/config.h include/mem.h include/stdlib.h
mm.o: include/sys/tm.h include/tm.h include/semaphore.h include/string.h
semaphore.o: include/semaphore.h include/config.h include/unistd.h
semaphore.o: include/tm.h include/mem.h include/time.h
systime.o: include/config.h include/sys/time.h include/time.h
systime.o: include/sys/h8.h include/sys/irq.h include/direct-motor.h
tm.o: include/sys/tm.h include/config.h include/tm.h include/mem.h
tm.o: include/sys/mm.h include/stdlib.h include/sys/time.h include/time.h
tm.o: include/sys/irq.h include/sys/bitops.h include/unistd.h include/tm.h
tm.o: include/time.h include/rom/lcd.h include/conio.h
