| 1 | package Hex::Record; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 8 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | sub new { | ||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | $args{parts} = [] unless exists $args{parts}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 6 | return bless \%args, $class; | |||||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | sub write { | ||||||||||||||||||||||||||||||||
| 17 | 10 | ▶ | my ($self, $from, $bytes_hex_ref) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
write: called CC=7 SCAR=21.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 10 | $self->remove($from, scalar @$bytes_hex_ref); | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 10 | my $to = $from + @$bytes_hex_ref; | |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | # insert part | ||||||||||||||||||||||||||||||||
| 24 | 10 | ▶ | for (my $part_i = 0; $part_i < @{$self->{parts}}; $part_i++){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | 25 | my $part = $self->{parts}->[$part_i]; | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 25 | my $start_addr = $part->{start}; | |||||||||||||||||||||||||||||||
| 28 | 25 | my $end_addr = $part->{start} + $#{$part->{bytes}}; | |||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | # merge with this part | ||||||||||||||||||||||||||||||||
| 31 | 25 | ▶ | if ($to == $start_addr){ | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 32 | 1 | $part->{start} = $from; | |||||||||||||||||||||||||||||||
| 33 | 1 | unshift @{$part->{bytes}}, @$bytes_hex_ref; | |||||||||||||||||||||||||||||||
| 34 | 1 | return; | |||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | elsif ($from == $end_addr + 1){ | ||||||||||||||||||||||||||||||||
| 37 | 2 | push @{$part->{bytes}}, @$bytes_hex_ref; | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | return if $part_i+1 == @{$self->{parts}}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 0 | my $next_part = $self->{parts}->[$part_i+1]; | |||||||||||||||||||||||||||||||
| 42 | # merge with next part | ||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ($to == $next_part->{start}){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | 0 | push @{$part->{bytes}}, @{$next_part->{bytes}}; | |||||||||||||||||||||||||||||||
| 45 | 0 | splice @{$self->{parts}}, $part_i+1, 1; | |||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | 0 | return; | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | elsif ($from < $start_addr){ | ||||||||||||||||||||||||||||||||
| 50 | 0 | splice @{$self->{parts}}, $part_i, 0, { | |||||||||||||||||||||||||||||||
| 51 | start => $from, | ||||||||||||||||||||||||||||||||
| 52 | bytes => $bytes_hex_ref | ||||||||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||||||
| 54 | 0 | return; | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 7 | push @{$self->{parts}}, { | |||||||||||||||||||||||||||||||
| 59 | start => $from, | ||||||||||||||||||||||||||||||||
| 60 | bytes => $bytes_hex_ref | ||||||||||||||||||||||||||||||||
| 61 | }; | ||||||||||||||||||||||||||||||||
| 62 | 7 | return; | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | sub get { | ||||||||||||||||||||||||||||||||
| 66 | 7 | ▶ | my ($self, $from, $length) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 7 | my $to = $from + $length - 1; | |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 7 | my @bytes_hex; | |||||||||||||||||||||||||||||||
| 71 | 7 | ▶ | for (my $part_i = 0; $part_i < @{$self->{parts}}; $part_i++){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 25 | my $part = $self->{parts}->[$part_i]; | |||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 25 | my $start_addr = $part->{start}; | |||||||||||||||||||||||||||||||
| 75 | 25 | my $end_addr = $part->{start} + $#{$part->{bytes}}; | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | # from inside this part | ||||||||||||||||||||||||||||||||
| 78 | 25 | ▶ | if ($from >= $start_addr && $from <= $end_addr){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$from >= $start_addr and $from <= $end_addr
MC/DC100%2/2
$from >= $start_addr and $from <= $end_addr
$from >= $start_addr
$from <= $end_addr
Truth table100%3/3
A and B
A= $from >= $start_addrB= $from <= $end_addr
| |||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | # this part also includes end | ||||||||||||||||||||||||||||||||
| 81 | 5 | ▶ | if ($to <= $end_addr){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 3 | @bytes_hex = @{$part->{bytes}}[ $from - $start_addr .. $to - $start_addr ]; | |||||||||||||||||||||||||||||||
| 83 | 3 | return \@bytes_hex; | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||||||
| 86 | 2 | @bytes_hex = @{$part->{bytes}}[ $from - $start_addr .. $#{$part->{bytes}} ]; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | while (++$part_i < @{$self->{parts}}){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | 2 | my $part = $self->{parts}->[$part_i]; | |||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 2 | my $start_addr = $part->{start}; | |||||||||||||||||||||||||||||||
| 92 | 2 | my $end_addr = $part->{start} + $#{$part->{bytes}}; | |||||||||||||||||||||||||||||||
| 93 | # desired part ended before this one | ||||||||||||||||||||||||||||||||
| 94 | 2 | ▶ | if ( $to < $start_addr ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | 0 | return [@bytes_hex, (undef) x ($length - @bytes_hex)]; | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||||||||||||||
| 99 | # fill gap betwenn this part and part before with undef | ||||||||||||||||||||||||||||||||
| 100 | 2 | my $part_before = $self->{parts}->[$part_i - 1]; | |||||||||||||||||||||||||||||||
| 101 | 2 | my $part_before_end_addr = $part_before->{start} + $#{$part_before->{bytes}}; | |||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 2 | push @bytes_hex, (undef) x ($start_addr - $part_before_end_addr); | |||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 2 | ▶ | if ( $to <= $end_addr ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 106 | 1 | push @bytes_hex, @{$part->{bytes}}[ 0 .. $to - $start_addr ]; | |||||||||||||||||||||||||||||||
| 107 | 1 | return \@bytes_hex; | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 1 | push @bytes_hex, @{$part->{bytes}}; | |||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | # did not find start, but did find end | ||||||||||||||||||||||||||||||||
| 117 | ▶ | elsif ($to <= $end_addr && $to >= $start_addr){ | |||||||||||||||||||||||||||||||
|
Condition66%2/3
$to <= $end_addr and $to >= $start_addr
MC/DC0%0/2
$to <= $end_addr and $to >= $start_addr
$to <= $end_addr
$to >= $start_addr
Truth table66%2/3
A and B
A= $to <= $end_addrB= $to >= $start_addr
| |||||||||||||||||||||||||||||||||
| 118 | @bytes_hex = ( (undef) x ($start_addr - $from), | ||||||||||||||||||||||||||||||||
| 119 | 0 | @{$part->{bytes}}[ 0 .. $to - $start_addr ] ); | |||||||||||||||||||||||||||||||
| 120 | 0 | return \@bytes_hex; | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | 3 | return [@bytes_hex, (undef) x ($length - @bytes_hex)]; | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | sub remove { | ||||||||||||||||||||||||||||||||
| 127 | 21 | ▶ | my ($self, $from, $length) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=12 SCAR=25.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 21 | my $to = $from + $length; | |||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | 21 | ▶ | for (my $part_i = 0; $part_i < @{ $self->{parts} }; $part_i++){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 132 | 46 | my $part = $self->{parts}->[$part_i]; | |||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 46 | my $start_addr = $part->{start}; | |||||||||||||||||||||||||||||||
| 135 | 46 | my $end_addr = $part->{start} + @{$part->{bytes}}; | |||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 46 | ▶ | if ($from < $end_addr){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 138 | 13 | ▶ | if ($to <= $end_addr){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | 7 | ▶ | if ($from <= $start_addr){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 140 | 4 | ▶ | if ($to == $end_addr){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | 2 | splice @{$self->{parts}}, $part_i, 1; | |||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||||||
| 144 | 2 | splice @{$part->{bytes}}, 0, @{$part->{bytes}} - $end_addr + $to; | |||||||||||||||||||||||||||||||
| 145 | 2 | $part->{start} = $to; | |||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | 4 | return; | |||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||
| 149 | elsif ($to == $end_addr){ | ||||||||||||||||||||||||||||||||
| 150 | 1 | splice @{$part->{bytes}}, $from-$start_addr, $length; | |||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | else { | ||||||||||||||||||||||||||||||||
| 153 | 2 | splice @{$self->{parts}}, $part_i, 1, ( | |||||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||||||
| 155 | start => $start_addr, | ||||||||||||||||||||||||||||||||
| 156 | 2 | bytes => [@{$part->{bytes}}[ 0 .. $from - $start_addr - 1]], | |||||||||||||||||||||||||||||||
| 157 | }, | ||||||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||||||
| 159 | start => $from + $length, | ||||||||||||||||||||||||||||||||
| 160 | 2 | bytes => [@{$part->{bytes}}[ $from - $start_addr + $length .. $#{$part->{bytes}}]], | |||||||||||||||||||||||||||||||
| 161 | }, | ||||||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | 3 | return; | |||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||
| 166 | else { | ||||||||||||||||||||||||||||||||
| 167 | 6 | splice @{$self->{parts}}, $part_i, 1; | |||||||||||||||||||||||||||||||
| 168 | 6 | --$part_i; | |||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | 6 | ▶ | while (++$part_i < @{$self->{parts}}){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 171 | 14 | my $part = $self->{parts}->[$part_i]; | |||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 14 | my $start_addr = $part->{start}; | |||||||||||||||||||||||||||||||
| 174 | 14 | ▶ | return if $to < $start_addr; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 11 | my $end_addr = $part->{start} + $#{$part->{bytes}}; | |||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | 11 | ▶ | if ($to < $end_addr){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | 0 | splice @{$part->{bytes}}, 0, $to - $start_addr; | |||||||||||||||||||||||||||||||
| 180 | 0 | $part->{start} = $to; | |||||||||||||||||||||||||||||||
| 181 | 0 | return; | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | 11 | splice @{$self->{parts}}, $part_i, 1; | |||||||||||||||||||||||||||||||
| 184 | 11 | --$part_i; | |||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | 11 | ▶ | return if $to == $end_addr; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | elsif ($to <= $end_addr){ | ||||||||||||||||||||||||||||||||
| 190 | 0 | splice @{$part->{bytes}}, 0, $to - $start_addr; | |||||||||||||||||||||||||||||||
| 191 | 0 | $part->{bytes} = $to; | |||||||||||||||||||||||||||||||
| 192 | 0 | return; | |||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | sub as_intel_hex { | ||||||||||||||||||||||||||||||||
| 198 | 2 | ▶ | my ($self, $bytes_hex_a_line) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
as_intel_hex: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | 2 | my $intel_hex_string = ''; | |||||||||||||||||||||||||||||||
| 201 | 2 | ▶ | for (my $part_i = 0; $part_i < @{ $self->{parts} }; $part_i++){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 202 | 7 | my $part = $self->{parts}->[$part_i]; | |||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | 7 | my $start_addr = $part->{start}; | |||||||||||||||||||||||||||||||
| 205 | 7 | my $end_addr = $part->{start} + $#{$part->{bytes}}; | |||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | 7 | my $cur_high_addr_hex = '0000'; | |||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | 7 | ▶ | for (my $slice_i = 0; $slice_i * $bytes_hex_a_line < @{$part->{bytes}}; $slice_i++){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 210 | 12 | my $total_addr = $start_addr + $slice_i*$bytes_hex_a_line; | |||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | 12 | my ($addr_high_hex, $addr_low_hex) = unpack '(A4)*', sprintf('%08X', $total_addr); | |||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||
| 214 | 12 | ▶ | if ($cur_high_addr_hex ne $addr_high_hex){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 215 | 3 | $cur_high_addr_hex = $addr_high_hex; | |||||||||||||||||||||||||||||||
| 216 | 3 | $intel_hex_string .= _intel_hex_line_of( '0000', 4, [unpack '(A2)*', $cur_high_addr_hex]); | |||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | 12 | ▶ | if (($slice_i + 1) * $bytes_hex_a_line <= $#{$part->{bytes}}){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 220 | $intel_hex_string .= _intel_hex_line_of( | ||||||||||||||||||||||||||||||||
| 221 | $addr_low_hex, 0, | ||||||||||||||||||||||||||||||||
| 222 | 5 | [ @{$part->{bytes}}[ $slice_i * $bytes_hex_a_line .. ($slice_i + 1) * $bytes_hex_a_line - 1 ] ] | |||||||||||||||||||||||||||||||
| 223 | ); | ||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||
| 225 | else { | ||||||||||||||||||||||||||||||||
| 226 | $intel_hex_string .= _intel_hex_line_of( | ||||||||||||||||||||||||||||||||
| 227 | 7 | $addr_low_hex, 0, [ @{$part->{bytes}}[ $slice_i * $bytes_hex_a_line .. $#{$part->{bytes}} ] ] | |||||||||||||||||||||||||||||||
| 228 | ); | ||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||
| 232 | # intel hex eof | ||||||||||||||||||||||||||||||||
| 233 | 2 | return $intel_hex_string . ":00000001FF\n"; | |||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | sub _intel_hex_line_of { | ||||||||||||||||||||||||||||||||
| 237 | 15 | ▶ | my ($addr_low_hex, $type, $bytes_hex_ref) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_intel_hex_line_of: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | 15 | ▶ | my $byte_count = defined $bytes_hex_ref ? scalar @$bytes_hex_ref : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||
| 241 | 15 | my $sum = 0; | |||||||||||||||||||||||||||||||
| 242 | 15 | $sum += $_ for ( $byte_count, (map { hex $_ } unpack '(A2)*', $addr_low_hex), | |||||||||||||||||||||||||||||||
| 243 | 121 | $type, (map { hex $_ } @$bytes_hex_ref) ); | |||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | #convert to hex, take lsb | ||||||||||||||||||||||||||||||||
| 246 | 15 | $sum = substr(sprintf( '%02X', $sum ), -2); | |||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||
| 248 | 15 | my $checksum_hex = sprintf '%02X', (hex $sum ^ 255) + 1; | |||||||||||||||||||||||||||||||
| 249 | 15 | ▶ | $checksum_hex = '00' if length $checksum_hex != 2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||
| 251 | 15 | return join '', | |||||||||||||||||||||||||||||||
| 252 | ( | ||||||||||||||||||||||||||||||||
| 253 | ':', | ||||||||||||||||||||||||||||||||
| 254 | sprintf( '%02X', $byte_count ), | ||||||||||||||||||||||||||||||||
| 255 | $addr_low_hex, | ||||||||||||||||||||||||||||||||
| 256 | sprintf( '%02X', $type ), | ||||||||||||||||||||||||||||||||
| 257 | @$bytes_hex_ref, | ||||||||||||||||||||||||||||||||
| 258 | $checksum_hex, | ||||||||||||||||||||||||||||||||
| 259 | "\n" | ||||||||||||||||||||||||||||||||
| 260 | ); | ||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | sub as_srec_hex { | ||||||||||||||||||||||||||||||||
| 264 | 1 | ▶ | my ($self, $bytes_hex_a_line) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
as_srec_hex: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||
| 266 | 1 | my $srec_hex_string = ''; | |||||||||||||||||||||||||||||||
| 267 | 1 | ▶ | for (my $part_i = 0; $part_i < @{ $self->{parts} }; $part_i++){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 268 | 7 | my $part = $self->{parts}->[$part_i]; | |||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | 7 | my $start_addr = $part->{start}; | |||||||||||||||||||||||||||||||
| 271 | 7 | my $end_addr = $part->{start} + $#{$part->{bytes}}; | |||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||
| 273 | 7 | ▶ | for (my $slice_i = 0; $slice_i * $bytes_hex_a_line < @{$part->{bytes}}; $slice_i++){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 274 | 12 | my $total_addr = $start_addr + $slice_i*$bytes_hex_a_line; | |||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | 12 | ▶ | if (($slice_i + 1) * $bytes_hex_a_line <= $#{$part->{bytes}}){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 277 | $srec_hex_string .= _srec_hex_line_of( | ||||||||||||||||||||||||||||||||
| 278 | $total_addr, | ||||||||||||||||||||||||||||||||
| 279 | 5 | [@{$part->{bytes}}[$slice_i * $bytes_hex_a_line .. ($slice_i + 1) * $bytes_hex_a_line - 1]] | |||||||||||||||||||||||||||||||
| 280 | ); | ||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||
| 282 | else { | ||||||||||||||||||||||||||||||||
| 283 | $srec_hex_string .= _srec_hex_line_of( | ||||||||||||||||||||||||||||||||
| 284 | $total_addr, | ||||||||||||||||||||||||||||||||
| 285 | 7 | [@{$part->{bytes}}[$slice_i * $bytes_hex_a_line .. $#{$part->{bytes}}]] | |||||||||||||||||||||||||||||||
| 286 | ); | ||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | 1 | return $srec_hex_string; | |||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||
| 294 | sub _srec_hex_line_of { | ||||||||||||||||||||||||||||||||
| 295 | 12 | ▶ | my ($total_addr, $bytes_hex_ref) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_srec_hex_line_of: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | 12 | my $total_addr_hex = sprintf '%04X', $total_addr; | |||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||
| 299 | 12 | my $type; | |||||||||||||||||||||||||||||||
| 300 | # 16 bit addr | ||||||||||||||||||||||||||||||||
| 301 | 12 | ▶ | if (length $total_addr_hex == 4){ | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 302 | 5 | $type = 1; | |||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||
| 304 | # 24 bit addr | ||||||||||||||||||||||||||||||||
| 305 | elsif (length $total_addr_hex <= 6){ | ||||||||||||||||||||||||||||||||
| 306 | 4 | ▶ | $total_addr_hex = "0$total_addr_hex" if length $total_addr_hex == 5; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 307 | 4 | $type = 2; | |||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||
| 309 | # 32 bit addr | ||||||||||||||||||||||||||||||||
| 310 | elsif (length $total_addr_hex <= 8){ | ||||||||||||||||||||||||||||||||
| 311 | 3 | ▶ | $total_addr_hex = "0$total_addr_hex" if length $total_addr_hex == 7; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 312 | 3 | $type = 3; | |||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||
| 314 | else { | ||||||||||||||||||||||||||||||||
| 315 | 0 | die "$total_addr_hex to big for 32 bit address"; | |||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||
| 318 | # count of data bytes + address bytes | ||||||||||||||||||||||||||||||||
| 319 | 12 | ▶ | my $byte_count = defined $bytes_hex_ref ? scalar @$bytes_hex_ref : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 320 | 12 | $byte_count += length($total_addr_hex) / 2; | |||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||
| 322 | 12 | my $sum = 0; | |||||||||||||||||||||||||||||||
| 323 | 12 | $sum += $_ for ( $byte_count, | |||||||||||||||||||||||||||||||
| 324 | 34 | (map { hex $_ } unpack '(A2)*', $total_addr_hex), | |||||||||||||||||||||||||||||||
| 325 | 93 | (map { hex $_ } @$bytes_hex_ref) ); | |||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | #convert to hex, take lsb | ||||||||||||||||||||||||||||||||
| 328 | 12 | $sum = substr(sprintf( '%02X', $sum ), -2); | |||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||
| 330 | 12 | my $checksum_hex = sprintf '%02X', (hex $sum ^ 255); | |||||||||||||||||||||||||||||||
| 331 | 12 | ▶ | $checksum_hex = '00' if length $checksum_hex != 2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | 12 | return join '', | |||||||||||||||||||||||||||||||
| 334 | ( | ||||||||||||||||||||||||||||||||
| 335 | "S$type", | ||||||||||||||||||||||||||||||||
| 336 | sprintf('%02X', $byte_count), | ||||||||||||||||||||||||||||||||
| 337 | $total_addr_hex, | ||||||||||||||||||||||||||||||||
| 338 | @$bytes_hex_ref, | ||||||||||||||||||||||||||||||||
| 339 | $checksum_hex, | ||||||||||||||||||||||||||||||||
| 340 | "\n" | ||||||||||||||||||||||||||||||||
| 341 | ); | ||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | 8 | 1; | |||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | Hex::Record - manipulate intel and srec hex records | ||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||
| 350 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | use Hex::Record; | ||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||
| 354 | # create your own hex records | ||||||||||||||||||||||||||||||||
| 355 | my $hex_record = Hex::Record->new; | ||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||
| 357 | # write/overwrite 3 bytes starting at address 0x100 | ||||||||||||||||||||||||||||||||
| 358 | $hex_record->write(0x100, [qw(AA BB CC)]); | ||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||
| 360 | # or use the parser | ||||||||||||||||||||||||||||||||
| 361 | use Hex::Record::Parser qw(parse_intel_hex parse_srec_hex); | ||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||
| 363 | my $hex_record = Hex::Record->new( | ||||||||||||||||||||||||||||||||
| 364 | parts => parse_intel_hex($intel_hex_record_as_string) | ||||||||||||||||||||||||||||||||
| 365 | ); | ||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||
| 367 | # get 100 bytes (hex format) starting at address 0x100 | ||||||||||||||||||||||||||||||||
| 368 | # every single byte that is not found is returned as undef | ||||||||||||||||||||||||||||||||
| 369 | my $bytes_ref = $hex_record->get(0x100, 10); | ||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||
| 371 | # remove 100 bytes starting at address 0x100 | ||||||||||||||||||||||||||||||||
| 372 | $hex_record->remove(0x100, 10); | ||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | # dump as intel hex (will use extended linear addresses for offset) | ||||||||||||||||||||||||||||||||
| 375 | # maximum of 10 bytes in data field | ||||||||||||||||||||||||||||||||
| 376 | my $intel_hex_string = $hex_record->as_intel_hex(10); | ||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||
| 378 | # dump as srec hex (always tries to use smallest address) | ||||||||||||||||||||||||||||||||
| 379 | # maximum of 10 bytes in data field | ||||||||||||||||||||||||||||||||
| 380 | my $srec_hex_string = $hex_record->as_screc_hex(10); | ||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | Manipulate intel/srec hex files. | ||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||
| 386 | =head2 Methods of Hex | ||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||
| 388 | =over 12 | ||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||
| 390 | =item C<get( $from, $count )> | ||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||
| 392 | Returns $count hex bytes in array reference starting at address $from. | ||||||||||||||||||||||||||||||||
| 393 | If hex byte is not found, undef instead. | ||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | ['AA', '00', undef, undef, 'BC', undef] | ||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||
| 397 | =item C<remove( $from, $count )> | ||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||
| 399 | Removes $count bytes starting at address $from. | ||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||
| 401 | =item C<write( $from, $bytes_ref )> | ||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||
| 403 | (Over)writes bytes starting at address $from with bytes in $bytes_ref. | ||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||
| 405 | =item C<as_intel_hex( $bytes_hex_a_line )> | ||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||
| 407 | Returns a string containing hex bytes formated as intel hex. | ||||||||||||||||||||||||||||||||
| 408 | Extended linear addresses as offset are used if needed. | ||||||||||||||||||||||||||||||||
| 409 | Extended segment addresses are not supported. | ||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||
| 411 | =item C<as_srec_hex( $bytes_hex_a_line )> | ||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||
| 413 | Returns a string containing hex bytes formated as srec hex. | ||||||||||||||||||||||||||||||||
| 414 | Maximum of $hytes_hex_a_line in data field. | ||||||||||||||||||||||||||||||||
| 415 | Tries to use the smallest address field. | ||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||
| 417 | =back | ||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||
| 419 | =head1 LICENSE | ||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||
| 421 | This is released under the Artistic License. | ||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||
| 423 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | spebern <bernhard@specht.net> | ||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||
| 427 | =cut | ||||||||||||||||||||||||||||||||