MCU = atmega128

AS = avr-as
ASFLAGS = -mmcu=$(MCU)
CC = avr-gcc
CFLAGS = -I. -O3 -g3 -mmcu=$(MCU)

OBJS = threefish_common.o threefish_enc.o threefish_dec.o skein.o rand.o

HEX_FLASH_FLAGS = -R .eeprom -R .fuse -R .lock -R .signature

all: libfhreefish.a version mem.dump

test: test.o libfhreefish.a
	$(CC) $(CFLAGS) -o $@ $^
	# check for correct alignment of param struct
	@avr-nm $@ | grep "^008...00 b tparam$$"
	# display size
	@avr-size $@

mem.dump: test.bin
	simulavr -d atmega128 --gdbserver $< > /dev/null 2> /dev/null & PID=$$!; sleep 1; avr-gdb test -ex 'target remote localhost:1212' -ex 'b _exit' -ex 'c' -ex 'x/1024xb 0x800100' < /dev/null > mem.dump; kill $$PID

%.hex: %
	avr-objcopy -O ihex $(HEX_FLASH_FLAGS)  $< $@

%.bin: %
	avr-objcopy -O binary $(HEX_FLASH_FLAGS)  $< $@

libfhreefish.a: $(OBJS)
	$(AR) cru $@ $?

clean:
	-$(RM) -r version test *.o *.a *.bin *.hex *.dump default *~

version: fkein.h
	grep SKEIN_VERSION fkein.h | sed -e 's/.* //;s/.$$//' > $@

dist: version
	mkdir fhreefish-`cat version`
	cp *.c *.h *.s *.aps *.aws Makefile README.txt COPYING.txt fhreefish-`cat version`
	zip -9rp fhreefish-`cat version`.zip fhreefish-`cat version`
