OpenCores
URL https://opencores.org/ocsvn/xgate/xgate/trunk

Subversion Repositories xgate

[/] [xgate/] [trunk/] [sw/] [tools/] [misc/] [srec.pl] - Blame information for rev 31

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 rehayes
#!/usr/bin/perl -w
2
use strict;
3
use IO::File;
4
 
5
use Getopt::Long;
6
my $outfile="out";
7
my $lenlen=2;
8
my $dowordswap= 0;
9
my $maxgap= 1;
10
GetOptions(
11
    "o=s" => \$outfile,
12
    "l=i" => \$lenlen,
13
    "m=s" => sub { $maxgap= eval($_[1]) },
14
    "w" => \$dowordswap)
15
    or die "Usage: srec [-o=OUTPREFIX] [-l={4|2}] [-w {swap}] [-m=MAXGAP]\n";
16
my %data;
17
 
18
# s-records have the following format:
19
#   "S" + 1 byte  type
20
#   2 hex digits :  length  ( of rest of line - incl address and checksum )
21
#   4,6,8 hex digits address ( for type 1, 2, 3 )
22
#   hex data [ 0..64 bytes ]
23
#   checksum [ 1 byte ]   such that unpack('%8C*', pack('H*', $line))==0xff
24
#            ( or checksum=0xff-sum(otherbytes)
25
#
26
#
27
#   type S0 : version  : char mname[10], byte ver, byte rev, char description[18]
28
#           or usually : 00 00 + 'HDR'
29
#   type S1 : 2 byte address + data
30
#   type S2 : 3 byte address + data
31
#   type S3 : 4 byte address + data
32
#   type S5 : 2 byte count of S1, S2, S3 records transmitted
33
#   type S7 : 4 byte entrypoint address
34
#   type S8 : 3 byte entrypoint address
35
#   type S9 : 2 byte entrypoint address
36
 
37
while (<>) {
38
    my $type= hex(substr($_, 1,1));
39
    my $length= hex(substr($_,2,$lenlen));
40
 
41
    my $adrlen= $type eq "1" ? 4 : $type eq "2" ? 6 :  $type eq "3" ? 8 : 0;
42
    next if (!$adrlen);
43
    my $address= hex(substr($_,2+$lenlen,$adrlen));
44
    my $data= pack("H*", substr($_,2+$lenlen+$adrlen, 2*$length-$adrlen-2));
45
    if ($dowordswap) {
46
        $data= pack('v*', unpack('n*',$data));
47
    }
48
 
49
    $data{$address}= $data;
50
}
51
 
52
#     |---------|...|
53
#                  |-----|
54
#
55
my @addrs= sort { $a <=> $b } keys %data;
56
my $fh;
57
for (0..$#addrs) {
58
    my $startcur= $addrs[$_];
59
 
60
    if ($_>0) {
61
        my $startlast= $addrs[$_-1];
62
        my $endlast= length($data{$startlast})+ $startlast;
63
 
64
        if ($endlast +$maxgap <= $startcur) {
65
            printf("  %08lx-%08lx .. gap .. %08lx\n", $startlast, $endlast, $startcur);
66
 
67
            $fh->close();
68
            undef $fh;
69
        }
70
        elsif ($endlast < $startcur ) {
71
            $fh->seek($startcur-$endlast, SEEK_CUR);
72
        }
73
        elsif ($endlast > $startcur) {
74
            printf("WARNING: overlap found: %08lx-%08lx ~ %08lx\n", $startlast, $endlast, $startcur);
75
            $fh->close();
76
            undef $fh;
77
        }
78
    }
79
 
80
    if (!$fh) {
81
        $fh= IO::File->new(sprintf("%s-%08lx.bin", $outfile, $startcur), "w");
82
        binmode($fh);
83
    }
84
    $fh->print($data{$startcur});
85
}
86
$fh->close();
87
#print map { $data{$_} } sort keys %data;
88
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.