#! /usr/bin/perl

# MP3 Player, Convert a STA013 configuration file to
#    intel hex, for downloading via PAULMON2.
# Copyright (c) 2000, PJRC.COM, LLC

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.



$addr = 0xe000;


$num = 1;
$cksum = 0;
while (<>) {
	next unless /^([0-9]+) ([0-9]+)/;
	$a[$num] = $1;
	$v[$num] = $2;
	$cksum += $1;
	$cksum += $2;
	$num++;
}

$cksum &= 0xFFFF;

# store the end memory location and a checksum
# the firmware will check these to make sure there
# is a valid sta013 config file available before sending
# anything to the sta013 chip.

$a[0] = ($addr + $num * 2) & 255;
$v[0] = ($addr + $num * 2) >> 8;
$a[$num] = $cksum & 255;
$v[$num] = $cksum >> 8;
$num++;

for ($i = 0; $i < $num; $i += 16) {
	$n = $num - $i;
	$n = 16 if $n > 16;
	$sum = ($n * 2) + ($addr & 255) + ($addr >> 8);
	printf ":%02X%04X00", $n * 2, $addr;
	for ($j = 0; $j < $n; $j++) {
		printf "%02X%02X", $a[$i + $j], $v[$i + $j];
		$sum += $a[$i + $j] + $v[$i + $j];
	}
	printf "%02X\n", ($sum * -1) & 255; 
	$addr += 32;
}
print ":00000001FF\n";





