#!/bin/sh

#set -x

CROSS_COMPILE=/home/s0mbre/aWork/chroot/superh-linux-gnu/gcc-3.4.5-glibc-2.3.6/sh4-unknown-linux-gnu/bin/sh4-unknown-linux-gnu-
GCC=${CROSS_COMPILE}gcc
LD=${CROSS_COMPILE}ld
OBJCOPY=${CROSS_COMPILE}objcopy

CFLAGS="-O2 -pipe -fPIC -c"

#source="start end"
source="start"

text=0x8c200000
of=0

copy=$1

for s in $source; do
	echo "Compiling $s -> text: $text, offset: $of"

	$GCC $CFLAGS $s.S || exit -1
	$LD -EL -e start $s.o -o $s.exe -Ttext $text || exit -1
	$OBJCOPY -S $s.exe -O binary $s.bin || exit -1

	if [ x$copy = x ]; then
		continue
	fi

	dd if=$s.bin of=/dev/sdb obs=1 seek=$of > /dev/null
	of=`calc -p "$of + 512"`
	text=`calc -p "base(16); $text + $of" | tail -n1`
done

sync
