Info/Module.pm

filter: Statementstmt 27.2% 18 / 66 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 23.8% 5 / 21 Podpod 100% 2 / 2 totaltotal 22.7% 25 / 110 CC 10 SCAR 28.2
CC
10
Cov
21%
_value_group
56.0
_registers_offset
20.0
_new
2.0
CRAP 16.8
↑ Summary Info.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2014-2019 -- leonerd@leonerd.org.uk
5  
6 package Device::AVR::Info::Module;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.03';
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Struct::Dumb 'readonly_struct';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 C<Device::AVR::Info::Module> - represent a single kind of peripheral module type from an F<AVR> chip
21  
22 =head1 SYNOPSIS
23  
24 Instances in this class are returned from L<Device::AVR::Info>:
25  
26 use Device::AVR::Info;
27  
28 my $avr = Device::AVR::Info->new_from_file( "devices/ATtiny84.xml" );
29  
30 my $fuses = $avr->peripheral( 'FUSE' );
31 my $module = $fuses->module;
32  
33 printf "The FUSE module has %d registers\n",
34 scalar $module->registers( 'FUSE' );
35  
36 =cut
37  
38 sub _new
39 0 ▶{
Subroutine _new: not called CC=1 SCAR=6.9
40 0 my $class = shift;
41 0 my ( $module ) = @_;
42  
43 0 return bless {
44 _module => $module,
45 }, $class;
46 }
47  
48 =head1 ACCESSORS
49  
50 =cut
51  
52 =head2 name
53  
54 $name = $module->name
55  
56 Returns the name of the module
57  
58 =cut
59  
60 0 ▶sub name { shift->{_module}{name} }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
61  
62 =head2 registers
63  
64 @registers = $module->registers( $groupname )
65  
66 Returns a list of register instances, representing the registers in the named
67 group.
68  
69 Each is a structure of the following fields.
70  
71 $register->name
72 $register->offset
73 $register->size
74 $register->initval
75 $register->caption
76 $register->mask
77 @fields = $register->bitfields
78  
79 The C<bitfields> field returns a list of structures of the following fields:
80  
81 $field->name
82 $field->caption
83 $field->mask
84  
85 =cut
86  
87 {
88 package
89 Device::AVR::Info::Module::_Register;
90  
91 0 ▶ sub name { shift->[0] }
Subroutine name: not called CC=1 SCAR=6.9
92 0 ▶ sub offset { shift->[1] }
Subroutine offset: not called CC=1 SCAR=6.9
93 0 ▶ sub size { shift->[2] }
Subroutine size: not called CC=1 SCAR=6.9
94 0 ▶ sub initval { shift->[3] }
Subroutine initval: not called CC=1 SCAR=6.9
95 0 ▶ sub caption { shift->[4] }
Subroutine caption: not called CC=1 SCAR=6.9
96 0 ▶ sub mask { shift->[5] }
Subroutine mask: not called CC=1 SCAR=6.9
97 0 ▶ sub bitfields { @{ shift->[6] } }
Subroutine bitfields: not called CC=1 SCAR=6.9
98  
99 package
100 Device::AVR::Info::Module::_Bitfield;
101  
102 0 ▶ sub name { shift->[0] }
Subroutine name: not called CC=1 SCAR=6.9
103 0 ▶ sub caption { shift->[1] }
Subroutine caption: not called CC=1 SCAR=6.9
104 0 ▶ sub mask { shift->[2] }
Subroutine mask: not called CC=1 SCAR=6.9
105 0 ▶ sub values { @{ shift->[3] } }
Subroutine values: not called CC=1 SCAR=6.9
106 }
107  
108 sub registers
109 1 ▶{
Subroutine registers: not called CC=1 SCAR=3.5
Pod documented
110 0 my $self = shift;
111 0 my ( $name ) = @_;
112 0 $self->_registers_offset( $name, 0 );
113 }
114  
115 sub _registers_offset
116 0 ▶{
Subroutine _registers_offset: not called CC=4 SCAR=30.0
117 0 my $self = shift;
118 0 my ( $name, $offset ) = @_;
119  
120 0 ▶ my $registers = $self->{_module}{"register-group"}( name => eq => $name )
Branch: 1 branch
BranchTrueFalseTotal
my $registers = $self->{"_module"}{"register-group"}->("name", "eq", $name) or croak("No register group named '${name}'") 0 0 0
121 or croak "No register group named '$name'";
122  
123 map {
124 my @fields = exists $_->{bitfield} ?
125 map {
126 0 my $mask = hex "$_->{mask}";
127 0 ▶ my $values = exists $_->{values} ? $self->_value_group( $_->{values}, $mask ) : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $_->{'values'} ? : 0 0 0
128 0 bless [ "$_->{name}", "$_->{caption}", $mask, $values ], "Device::AVR::Info::Module::_Bitfield";
129 0 ▶ } @{ $_->{bitfield} } : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $_->{'bitfield'} ? : 0 0 0
130  
131 0 bless [ "$_->{name}", $offset + hex "$_->{offset}", "$_->{size}", hex "$_->{initval}",
132 "$_->{caption}", hex "$_->{mask}", \@fields ], "Device::AVR::Info::Module::_Register";
133 0 } @{ $registers->{register} };
134 }
135  
136 1 readonly_struct Value => [qw( name caption value )];
137  
138 sub _value_group
139 0 ▶{
Subroutine _value_group: not called CC=7 SCAR=40.3
140 0 my $self = shift;
141 0 my ( $name, $mask ) = @_;
142  
143 0 ▶ my $values = $self->{_module}{"value-group"}( name => eq => $name )
Branch: 1 branch
BranchTrueFalseTotal
my $values = $self->{"_module"}{"value-group"}->("name", "eq", $name) or croak("No value group named '${name}'") 0 0 0
144 or croak "No value group named '$name'";
145  
146 [ map {
147 0 my $value_in = hex "$_->{value}";
148  
149 # The bits in $value are "compressed", and have to be expanded out to
150 # only the bit positions set in $mask.
151 0 my $value_out = 0;
152 0 my $in_bit = 0;
153 0 my $out_bit = 0;
154 0 ▶ while( $value_in ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_in) 0 0 0
155 0 ▶ $out_bit++ until $out_bit > 16 or $mask & 1<<$out_bit;
Branch: 1 branch
BranchTrueFalseTotal
($out_bit > 16 or $mask & 1 << $out_bit) or do { ++$out_bit } 0 0 0
Condition0%0/3
$out_bit > 16 or $mask & 1 << $out_bit
l!l&&r!l&&!r
000
MC/DC0%0/2
$out_bit > 16 or $mask & 1 << $out_bit
$out_bit > 16 $mask & 1 << $out_bit
Truth table0%0/3
A or B
A= $out_bit > 16B= $mask & 1 << $out_bit
ABresult
1X1
011
000
156 0 ▶ die "Ran out of mask bits before value bits" if $in_bit > 16;
Branch: 1 branch
BranchTrueFalseTotal
if $in_bit > 16 0 0 0
157  
158 0 ▶ $value_out |= 1<<$out_bit if $value_in & 1<<$in_bit;
Branch: 1 branch
BranchTrueFalseTotal
if $value_in & 1 << $in_bit 0 0 0
159 0 $value_in &= ~( 1<<$in_bit );
160  
161 0 $in_bit++;
162 0 $out_bit++;
163 }
164  
165 0 Value( "$_->{name}", "$_->{caption}", $value_out )
166 0 } @{ $values->{value} } ];
167 }
168  
169 =head1 AUTHOR
170  
171 Paul Evans <leonerd@leonerd.org.uk>
172  
173 =cut
174  
175 1 0x55AA;
↑ Summary Info.pm »