#!/bin/bash

TTY=/dev/ttyUSB0
DIR=${1:-.}


boot()
{
	make -C ../../../m1/jtag-boot boot
}


xterm -e "flterm --log $DIR/log --port $TTY --kernel boot.4e53273.bin" &

n=0
while true; do
        n=`expr $n + 1`
        echo ===== $n =====

        labsw ch1=0 ch2=0
        sleep 2
        labsw ch1=1 ch2=1
        sleep 1

	bs=`./bootsts`

	if [ "$bs" = "0x0001" ]; then
		boot
		sleep 10
		echo echo === $n === `date` >$TTY
		sleep 5
		continue
	fi

	echo bootsts = $bs >$DIR/$n-stat

	./getstby $DIR/$n-corrupt.bin

	boot

	# 1.0-4.3 seconds
	sleep 2

	echo -e '\033serialboot' >$TTY

	# >= 7.9 seconds
	sleep 9

	echo -n a >$TTY

	# takes about 3 seconds
	sleep 4

	./putstby standby.fpg

	diff -u <(hexdump -C standby.fpg) \
	    <(hexdump -C $DIR/$n-corrupt.bin) >$DIR/$n-diff
done