#!perl -w use warnings; use strict; my @data; sub srecParse { my $sr = $_[0]; # extract record type (as single digit) and data (as pairs of hex digits) $sr =~ /^S([0-9])((?:[0-9A-Fa-f][0-9A-Fa-f])+)/; (defined($1) and defined($2)) or return warn("Bad format: $_[0]"); my $rt = $1; my $hd = $2; my $bd = pack('H*', $hd); # convert the hexidecimal string to a byte stream my $cs = unpack('%8C*', $bd); # calculate byte-wise checksum, including the checksum byte # final result should be 0xFF because checksum byte is 1s complement of sum of preceding. ($cs == 0xFF) or return warn("Failed checksum: $_[0]"); my ($cnt, $adr, $adrh, $data); if ($rt eq '0') # Text data. Extract like an S1, then display text. { ($cnt, $adr, $data) = unpack('C n X3 C /a', $bd); # C:count, n:adr16, /a:data return print STDERR "# " . substr($data, 2, -1) . "\n"; } elsif (($rt eq '1') or ($rt eq '9')) # Data with 16 bit address { ($cnt, $adr, $data) = unpack('C n X3 C /a', $bd); # C:count, n:adr16, /a:data $cnt -= 2; # adjust byte count, then trim address from in front of the data. $data = substr($data, 2, -1); } elsif (($rt eq '2') or ($rt eq '8')) # Data with 24 bit address { # Because there is no code for a 24 bit integer, unpack 24 bit address as high byte and low word. ($cnt, $adrh, $adr, $data) = unpack('C C n X4 C /a', $bd); # C:count, C:adr8 (MSB), n:adr16 (LSW), /a:data $cnt -= 3; # adjust byte count $adr += $adrh * 256; # prepend high byte of address to low word $data = substr($data, 3, -1); # trim address from in front of the data. } elsif (($rt eq '3') or ($rt eq '7')) # Data with 32 bit address { ($cnt, $adr, $data) = unpack('C N X5 C /a', $bd); # C:count, N:adr32, /a:data $cnt -= 4; # adjust byte count, then trim address from in front of the data. $data = substr($data, 4, -1); } else { return warn("Unsupported record type: $_[0]"); } printf STDERR "c:%d a:%0X d:%s\n", $cnt, $adr, unpack('H*', $data); if (($rt >= 1) and ($rt <= 3)) { for my $i (0 .. ($cnt - 1)) { $data[$adr + $i] = substr($data, $i, 1); } } elsif (($rt >= 7) and ($rt <= 9)) { printf STDERR "Start address: 0x%0X\n", $adr; } } my $curFile = ''; while (<>) { print STDERR "Reading $ARGV\n" unless ($ARGV eq $curFile); $curFile = $ARGV; chomp; chomp; srecParse($_); } continue { close ARGV if eof; # Not eof()! }