VIC/PIC/Gpsim.pm

filter: Statementstmt 85.9% 287 / 334 Branchbran 54.7% 115 / 210 Conditioncond 41.1% 21 / 51 MC/DCmcdc 12.1% 4 / 33 Subroutinesub 100% 26 / 26 Podpod 0.0% 0 / 14 totaltotal 67.8% 453 / 668 SCAR 63.3
CC
133
Cov
71%
sim_assert
108.0
_get_operator
33.0
attach_led7seg
22.6
CRAP 560.3
« VIC/Command.pm ↑ Summary VIC/PIC/Functions/GPIO.pm »
1 package VIC::PIC::Gpsim;
2 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use bigint;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 34 ▶use Pegex::Base; # use this instead of Mo
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 34 our $VERSION = '0.29';
9 34 $VERSION = eval $VERSION;
10  
11 34 has type => 'gpsim';
12  
13 34 has include => 'coff.inc';
14  
15 34 has pic => undef; # refer to the PIC object
16  
17 34 has node_count => 0;
18  
19 34 has scope_channels => 0;
20  
21 34 has stimulus_count => 0;
22  
23 34 has should_autorun => 0;
24  
25 34 has disable => 0;
26  
27 sub supports_modifier {
28 12 ▶ my $self = shift;
Subroutine supports_modifier: called CC=2 SCAR=6.9
Pod undocumented
29 12 my $mod = shift;
30 12 ▶ return 1 if $mod =~ /^(?:every|wave)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $mod =~ /^(?:every|wave)$/i 11 1 12
31 1 0;
32 }
33  
34 sub init_code {
35 19 ▶ my $self = shift;
Subroutine init_code: called CC=5 SCAR=17.3
Pod undocumented
36 19 ▶ croak "This chip is not supported" unless $self->pic->doesroles(qw(Chip CodeGen GPIO));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pic->doesroles("Chip", "CodeGen", "GPIO") 0 19 19
37 19 my $pic = '';
38 19 ▶ $pic = $self->pic->type if $self->pic;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pic 19 0 19
39 19 ▶ my $freq = $self->pic->f_osc if $self->pic;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pic 19 0 19
40 19 ▶ if ($freq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($freq) { } 19 0 19
41 19 $freq = qq{\t.sim "$pic.frequency = $freq"};
42 } else {
43 0 $freq = '';
44 }
45 19 return << "...";
46 ;;;; generated common code for the Simulator
47 \t.sim "module library libgpsim_modules"
48 \t.sim "$pic.xpos = 200"
49 \t.sim "$pic.ypos = 200"
50 $freq
51 ...
52 }
53  
54 sub _gen_led {
55 30 ▶ my $self = shift;
Subroutine _gen_led: called CC=6 SCAR=18.1
56 30 my ($id, $x, $y, $name, $port, $color) = @_;
57 30 ▶ if (defined $color and ref $color eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $color and ref $color eq "HASH") 4 26 30
Condition66%2/3
defined $color and ref $color eq "HASH"
!ll&&!rl&&r
2604
MC/DC50%1/2
defined $color and ref $color eq "HASH"
defined $color ref $color eq "HASH"
Truth table66%2/3
A and B
A= defined $colorB= ref $color eq "HASH"
ABresult
0X0
100
111
58 4 $color = $color->{string};
59 }
60 30 ▶ $color = 'red' unless defined $color;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color 26 4 30
61 30 ▶ $color = 'red' unless $color =~ /red|orange|green|yellow|blue/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $color =~ /red|orange|green|yellow|blue/i 0 30 30
62 30 $color = lc $color;
63 30 ▶ $color = substr ($color, 1) if $color =~ /^@/;
Branch: 1 branch
BranchTrueFalseTotal
if $color =~ /^@/ 0 30 30
64 30 return << "...";
65 \t.sim "module load led L$id"
66 \t.sim "L$id.xpos = $x"
67 \t.sim "L$id.ypos = $y"
68 \t.sim "L$id.color = $color"
69 \t.sim "node $name"
70 \t.sim "attach $name $port L$id.in"
71 ...
72 }
73  
74  
75 sub _get_gpio_info {
76 101 ▶ my ($self, $port) = @_;
Subroutine _get_gpio_info: called CC=3 SCAR=15.4
77 101 my $gpio_pin = $self->pic->get_input_pin($port);
78 101 ▶ if ($gpio_pin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpio_pin) { } 101 0 101
79 # this is a pin
80 101 return @{$self->pic->input_pins->{$gpio_pin}};
81 } else {
82 0 $gpio_pin = $self->pic->get_output_pin($port);
83 0 ▶ if ($gpio_pin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpio_pin) 0 0 0
84 # this is a pin
85 0 return @{$self->pic->output_pins->{$gpio_pin}};
86 }
87 }
88 0 return;
89 }
90  
91 sub _get_simreg {
92 32 ▶ my ($self, $port) = @_;
Subroutine _get_simreg: called CC=5 SCAR=17.8
93 32 my $simreg = lc $port;
94 32 ▶ if ($self->pic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pic) 32 0 32
95 32 ▶ if (exists $self->pic->registers->{$port}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->pic->registers->{$port}) { } 1 31 32
elsif (exists $self->pic->pins->{$port}) { } 31 0 31
96 # this is a port
97 1 $simreg = lc $port;
98 } elsif (exists $self->pic->pins->{$port}) {
99 31 my ($io1) = $self->_get_gpio_info($port);
100 31 ▶ if (defined $io1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $io1) { } 31 0 31
101 31 $simreg = lc $io1;
102 } else {
103 0 my $pic = $self->pic->type;
104 0 carp "Cannot find '$port' in PIC $pic. Using '$simreg'";
105 }
106 } else {
107 0 my $pic = $self->pic->type;
108 0 carp "Cannot find '$port' in PIC $pic. Using '$simreg'";
109 }
110 }
111 32 return $simreg;
112 }
113  
114 sub _get_simport {
115 94 ▶ my ($self, $port, $pin) = @_;
Subroutine _get_simport: called CC=7 SCAR=21.8
116 94 my $simport = lc $port;
117 94 ▶ if ($self->pic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pic) 94 0 94
118 94 ▶ if (exists $self->pic->registers->{$port}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->pic->registers->{$port}) { } 25 69 94
elsif (exists $self->pic->pins->{$port}) { } 69 0 69
119 # this is a port
120 25 $simport = lc $port;
121 25 ▶ $simport .= $pin if defined $pin;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pin 24 1 25
122 } elsif (exists $self->pic->pins->{$port}) {
123 69 my ($io1, $io2, $io3) = $self->_get_gpio_info($port);
124 69 ▶ if (defined $io1 and defined $io3) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $io1 and defined $io3) { } 69 0 69
Condition33%1/3
defined $io1 and defined $io3
!ll&&!rl&&r
0069
MC/DC0%0/2
defined $io1 and defined $io3
defined $io1 defined $io3
Truth table33%1/3
A and B
A= defined $io1B= defined $io3
ABresult
0X0
100
111
125 69 $simport = lc "$io1$io3";
126 } else {
127 0 my $pic = $self->pic->type;
128 0 carp "Cannot find '$port' in PIC $pic. Using '$simport'";
129 }
130 } else {
131 0 my $pic = $self->pic->type;
132 0 carp "Cannot find '$port' in PIC $pic. Using '$simport'";
133 }
134 }
135 94 return $simport;
136 }
137  
138 sub _get_portpin {
139 27 ▶ my ($self, $port) = @_;
Subroutine _get_portpin: called CC=6 SCAR=19.3
140 27 my $simport = lc $port;
141 27 my $simpin;
142 27 ▶ if ($self->pic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pic) 27 0 27
143 27 ▶ if (exists $self->pic->registers->{$port}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->pic->registers->{$port}) { } 0 27 27
elsif (exists $self->pic->pins->{$port}) { } 1 26 27
144 # this is a port
145 0 $simport = lc $port;
146 } elsif (exists $self->pic->pins->{$port}) {
147 1 my ($io1, $io2, $io3) = $self->_get_gpio_info($port);
148 1 ▶ if (defined $io1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $io1) { } 1 0 1
149 1 $simport = lc $io1;
150 1 $simpin = $io3;
151 } else {
152 0 my $pic = $self->pic->type;
153 0 carp "Cannot find '$port' in PIC $pic. Using '$simport'";
154 }
155 } else {
156 26 return;
157 }
158 }
159 1 ▶ return wantarray ? ($simport, $simpin) : $simport;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
160 }
161  
162 sub attach_led {
163 17 ▶ my ($self, $port, $count, $color) = @_;
Subroutine attach_led: called CC=8 SCAR=20.8
Pod undocumented
164 17 ▶ $count = 1 unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 14 3 17
165 17 ▶ $count = 1 if int($count) < 1;
Branch: 1 branch
BranchTrueFalseTotal
if int $count < bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) 0 17 17
166 17 my $code = '';
167 17 ▶ if ($count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == {sign => '+', value => ['1']}) { } 14 3 17
168 14 my $c = $self->node_count;
169 14 my $node = lc $port . 'led';
170 14 $self->node_count($c + 1);
171 14 ▶ my $x = ($c >= 4) ? 400 : 100;
Branch: 1 branch
BranchTrueFalseTotal
$c >= {sign => '+', value => ['4']} ? : 0 14 14
172 14 my $y = 50 + 50 * $c;
173 # use the default pin 0 here
174 14 my $simport = $self->_get_simport($port, 0);
175 14 $code = $self->_gen_led($c, $x, $y, $node, $simport, $color);
176 } else {
177 3 $count--;
178 3 ▶ if ($self->pic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pic) 3 0 3
179 3 for (0 .. $count) {
180 16 my $c = $self->node_count + $_;
181 16 ▶ my $x = ($_ >= 4) ? 400 : 100;
Branch: 1 branch
BranchTrueFalseTotal
$_ >= {sign => '+', value => ['4']} ? : 4 12 16
182 16 my $y = 50 + 50 * $c;
183 16 my $node = lc $port . $c . 'led';
184 16 my $simport = $self->_get_simport($port, $_);
185 16 $code .= $self->_gen_led($c, $x, $y, $node, $simport, $color);
186 }
187 3 $self->node_count($self->node_count + $count + 1);
188 }
189 }
190 17 return $code;
191 }
192  
193 sub attach_led7seg {
194 1 ▶ my ($self, @pins) = @_;
Subroutine attach_led7seg: called CC=14 SCAR=31.2
Pod undocumented
195 1 my $code = '';
196 1 my @simpins = ();
197 1 my $color = 'red';
198 1 foreach my $p (@pins) {
199 2 ▶ if (defined $p and ref $p eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p and ref $p eq "HASH") 0 2 2
Condition33%1/3
defined $p and ref $p eq "HASH"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $p and ref $p eq "HASH"
defined $p ref $p eq "HASH"
Truth table33%1/3
A and B
A= defined $pB= ref $p eq "HASH"
ABresult
0X0
100
111
200 0 $p = $p->{string};
201 0 ▶ next unless defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 0 0 0
202 }
203 2 ▶ if (exists $self->pic->pins->{$p}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $self->pic->pins->{$p}) { } 1 1 2
elsif (exists $self->pic->registers->{$p}) { } 1 0 1
elsif ($p =~ /red|orange|green|yellow|blue/i) { } 0 0 0
204 1 push @simpins, $p;
205 } elsif (exists $self->pic->registers->{$p}) {
206 # find all the output pins for the port
207 1 foreach (sort(keys %{$self->pic->output_pins})) {
208 17 ▶ next unless defined $self->pic->output_pins->{$_}->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->pic->output_pins->{$_}[bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )] 0 17 17
209 17 ▶ push @simpins, $_ if $self->pic->output_pins->{$_}->[0] eq $p;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pic->output_pins->{$_}[bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )] eq $p 8 9 17
210 }
211 } elsif ($p =~ /red|orange|green|yellow|blue/i) {
212 0 $color = $p;
213 0 ▶ $color = substr($p, 1) if $p =~ /^@/;
Branch: 1 branch
BranchTrueFalseTotal
if $p =~ /^@/ 0 0 0
214 0 next;
215 } else {
216 0 carp "Ignoring port $p as it doesn't exist\n";
217 }
218 }
219 1 ▶ return unless scalar @simpins;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @simpins 0 1 1
220 1 my $id = $self->node_count;
221 1 $self->node_count($id + 1);
222 1 my $x = 500;
223 1 my $y = 50 + 50 * $id;
224 1 $code .= << "...";
225 \t.sim "module load led_7segments L$id"
226 \t.sim "L$id.xpos = $x"
227 \t.sim "L$id.ypos = $y"
228 ...
229 1 my @nodes = qw(cc seg0 seg1 seg2 seg3 seg4 seg5 seg6);
230 1 foreach my $n (@nodes) {
231 8 my $p = shift @simpins;
232 8 my $sp = $self->_get_simport($p);
233 8 $code .= << "...";
234 \t.sim "node $n"
235 \t.sim "attach $n $sp L$id.$n"
236 ...
237 }
238 1 return $code;
239 }
240  
241 sub stop_after {
242 16 ▶ my ($self, $usecs) = @_;
Subroutine stop_after: called CC=1 SCAR=0.0
Pod undocumented
243 # convert $secs to cycles
244 16 my $cycles = $usecs * 10;
245 16 my $code = << "...";
246 \t.sim "break c $cycles"
247 ...
248 16 return $code;
249 }
250  
251 sub logfile {
252 7 ▶ my ($self, $file) = @_;
Subroutine logfile: called CC=6 SCAR=20.3
Pod undocumented
253 7 ▶ $file = "vicsim.log" unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 7 7
254 7 ▶ if (ref $file eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq "HASH") 7 0 7
255 7 ▶ $file = $file->{string} || 'vicsim.log';
Condition50%1/2
$file->{'string'} || 'vicsim.log'
l!l
70
MC/DC0%0/1
$file->{'string'} || 'vicsim.log'
$file->{'string'}
Truth table50%1/2
A
A= $file->{'string'}
Aresult
11
00
256 }
257 7 ▶ $file = substr($file, 1) if $file =~ /^@/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^@/ 0 7 7
258 7 ▶ return "\t.sim \"log lxt $file\"\n" if $file =~ /\.lxt/i;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.lxt/i 7 0 7
259 0 return "\t.sim \"log on $file\"\n";
260 }
261  
262 sub log {
263 20 ▶ my $self = shift;
Subroutine log: called CC=8 SCAR=21.5
Pod undocumented
264 20 my $code = '';
265 20 foreach my $port (@_) {
266 29 ▶ if ($port =~ /US?ART/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port =~ /US?ART/) { } 3 26 29
267 3 ▶ next unless $self->pic->doesrole('USART');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pic->doesrole("USART") 0 3 3
268 3 my $ipin = $self->pic->usart_pins->{async_in};
269 3 my $opin = $self->pic->usart_pins->{async_out};
270 3 ▶ if (defined $ipin and defined $opin) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ipin and defined $opin) 3 0 3
Condition33%1/3
defined $ipin and defined $opin
!ll&&!rl&&r
003
MC/DC0%0/2
defined $ipin and defined $opin
defined $ipin defined $opin
Truth table33%1/3
A and B
A= defined $ipinB= defined $opin
ABresult
0X0
100
111
271 3 my $ireg = $self->_get_simreg($ipin);
272 3 my $oreg = $self->_get_simreg($opin);
273 3 $code .= $self->log($ipin);
274 3 ▶ $code .= $self->log($opin) if $ireg ne $oreg;
Branch: 1 branch
BranchTrueFalseTotal
if $ireg ne $oreg 0 3 3
275 }
276 } else {
277 26 my $reg = $self->_get_simreg($port);
278 26 ▶ next unless $reg;
Branch: 1 branch
BranchTrueFalseTotal
unless $reg 0 26 26
279 26 $code .= << "...";
280 \t.sim "log r $reg"
281 \t.sim "log w $reg"
282 ...
283 }
284 }
285 20 return $code;
286 }
287  
288 sub _set_scope {
289 33 ▶ my ($self, $port) = @_;
Subroutine _set_scope: called CC=8 SCAR=21.2
290 33 my $simport = $self->_get_simport($port);
291 33 my $chnl = $self->scope_channels;
292 33 ▶ carp "Maximum of 8 channels can be used in the scope\n" if $chnl > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $chnl > bless( {"sign" => "+","value" => bless( [7], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) 0 33 33
293 33 ▶ return '' if $chnl > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $chnl > bless( {"sign" => "+","value" => bless( [7], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) 0 33 33
294 33 ▶ if (lc($simport) eq lc($port)) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $simport eq lc $port) { } 1 32 33
295 1 my @code = ();
296 1 for (0 .. 7) {
297 8 $simport = $self->_get_simport($port, $_);
298 8 ▶ if ($self->scope_channels < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scope_channels < bless( {"sign" => "+","value" => bless( [8], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 8 0 8
299 8 $chnl = $self->scope_channels;
300 8 push @code, "\t.sim \"scope.ch$chnl = \\\"$simport\\\"\"";
301 8 $self->scope_channels($chnl + 1);
302 }
303 8 ▶ carp "Maximum of 8 channels can be used in the scope\n" if $chnl > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $chnl > bless( {"sign" => "+","value" => bless( [7], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) 0 8 8
304 8 ▶ last if $chnl > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $chnl > bless( {"sign" => "+","value" => bless( [7], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) 0 8 8
305 }
306 1 return join("\n", @code);
307 } else {
308 32 $self->scope_channels($chnl + 1);
309 32 return << "...";
310 \t.sim "scope.ch$chnl = \\"$simport\\""
311 ...
312 }
313 }
314  
315 sub scope {
316 17 ▶ my $self = shift;
Subroutine scope: called CC=6 SCAR=18.2
Pod undocumented
317 17 my $code = '';
318 17 foreach my $port (@_) {
319 30 ▶ if ($port =~ /US?ART/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port =~ /US?ART/) { } 3 27 30
320 3 ▶ next unless $self->pic->doesrole('USART');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pic->doesrole("USART") 0 3 3
321 3 my $ipin = $self->pic->usart_pins->{async_in};
322 3 my $opin = $self->pic->usart_pins->{async_out};
323 3 ▶ $code .= $self->_set_scope($ipin) if defined $opin;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opin 3 0 3
324 3 ▶ $code .= $self->_set_scope($opin) if defined $opin;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opin 3 0 3
325 } else {
326 27 $code .= $self->_set_scope($port);
327 }
328 }
329 17 return $code;
330 }
331  
332 ### have to change the operator back to the form acceptable by gpsim
333 sub _get_operator {
334 27 ▶ my $self = shift;
Subroutine _get_operator: called CC=7 SCAR=35.0
335 27 my $op = shift;
336 27 ▶ return '==' if $op eq 'EQ';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "EQ" 27 0 27
337 0 ▶ return '!=' if $op eq 'NE';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "NE" 0 0 0
338 0 ▶ return '>' if $op eq 'GT';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "GT" 0 0 0
339 0 ▶ return '>=' if $op eq 'GE';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "GE" 0 0 0
340 0 ▶ return '<' if $op eq 'LT';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "LT" 0 0 0
341 0 ▶ return '<=' if $op eq 'LE';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "LE" 0 0 0
342 0 return undef;
343 }
344  
345 sub sim_assert {
346 29 ▶ my ($self, $condition, $msg) = @_;
Subroutine sim_assert: called CC=28 SCAR=46.8
Pod undocumented
347 29 my $assert_msg;
348 29 ▶ if ($condition =~ /@@/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition =~ /\@@/) { } 27 2 29
349 27 my @args = split /@@/, $condition;
350 27 my $literal = qr/^\d+$/;
351 27 ▶ if (scalar @args == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args == bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 27 0 27
352 27 my $lhs = shift @args;
353 27 my $op = shift @args;
354 27 my $rhs = shift @args;
355 27 my $op2 = $self->_get_operator($op);
356 27 ▶ if ($lhs !~ $literal) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lhs =~ /$literal/) { } 27 0 27
357 27 my ($port, $pin) = $self->_get_portpin($lhs);
358 27 ▶ if (defined $pin) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $pin) { } 1 26 27
elsif (defined $port) { } 0 26 26
359 1 my $pval = sprintf "0x%02X", (1 << $pin);
360 1 $lhs = lc "($port & $pval)";
361 } elsif (defined $port) {
362 0 $lhs = lc $port;
363 } else {
364 # may be a variable
365 26 $lhs = uc $lhs;
366 }
367 } else {
368 0 $lhs = sprintf "0x%02X", $lhs;
369 }
370 27 ▶ if ($rhs !~ $literal) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rhs =~ /$literal/) { } 0 27 27
371 0 my ($port, $pin) = $self->_get_portpin($lhs);
372 0 ▶ if (defined $pin) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $pin) { } 0 0 0
elsif (defined $port) { } 0 0 0
373 0 my $pval = sprintf "0x%02X", (1 << $pin);
374 0 $rhs = lc "($port & $pval)";
375 } elsif (defined $port) {
376 0 $rhs = lc $port;
377 } else {
378 # may be a variable
379 0 $rhs = uc $rhs;
380 }
381 } else {
382 27 $rhs = sprintf "0x%02X", $rhs;
383 }
384 27 $condition = "$lhs $op2 $rhs";
385 }
386 #TODO: handle more complex expressions
387 27 ▶ if (defined $msg and ref $msg eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msg and ref $msg eq "HASH") 26 1 27
Condition66%2/3
defined $msg and ref $msg eq "HASH"
!ll&&!rl&&r
1026
MC/DC50%1/2
defined $msg and ref $msg eq "HASH"
defined $msg ref $msg eq "HASH"
Truth table66%2/3
A and B
A= defined $msgB= ref $msg eq "HASH"
ABresult
0X0
100
111
388 26 $msg = $msg->{string};
389 }
390 27 ▶ $msg = "$condition is false" unless $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg 1 26 27
391 27 ▶ $msg = substr($msg, 1) if $msg =~ /^@/;
Branch: 1 branch
BranchTrueFalseTotal
if $msg =~ /^@/ 0 27 27
392 27 ▶ $condition = substr($condition, 1) if $condition =~ /^@/;
Branch: 1 branch
BranchTrueFalseTotal
if $condition =~ /^@/ 0 27 27
393 27 $assert_msg = qq{$condition, \\\"$msg\\\"};
394 } else {
395 2 ▶ if (defined $msg and ref $msg eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msg and ref $msg eq "HASH") 0 2 2
Condition33%1/3
defined $msg and ref $msg eq "HASH"
!ll&&!rl&&r
200
MC/DC0%0/2
defined $msg and ref $msg eq "HASH"
defined $msg ref $msg eq "HASH"
Truth table33%1/3
A and B
A= defined $msgB= ref $msg eq "HASH"
ABresult
0X0
100
111
396 0 $msg = $msg->{string};
397 }
398 2 ▶ if (defined $condition and ref $condition eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $condition and ref $condition eq "HASH") 2 0 2
Condition33%1/3
defined $condition and ref $condition eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $condition and ref $condition eq "HASH"
defined $condition ref $condition eq "HASH"
Truth table33%1/3
A and B
A= defined $conditionB= ref $condition eq "HASH"
ABresult
0X0
100
111
399 2 $condition = $condition->{string};
400 }
401 2 ▶ if (defined $condition and defined $msg) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $condition and defined $msg) { } 0 2 2
elsif (defined $condition and not defined $msg) { } 2 0 2
elsif (defined $msg and not defined $condition) { } 0 0 0
Condition33%1/3
defined $condition and defined $msg
!ll&&!rl&&r
020
MC/DC0%0/2
defined $condition and defined $msg
defined $condition defined $msg
Truth table33%1/3
A and B
A= defined $conditionB= defined $msg
ABresult
0X0
100
111
402 0 ▶ $msg = substr($msg, 1) if $msg =~ /^@/;
Branch: 1 branch
BranchTrueFalseTotal
if $msg =~ /^@/ 0 0 0
403 0 ▶ $condition = substr($condition, 1) if $condition =~ /^@/;
Branch: 1 branch
BranchTrueFalseTotal
if $condition =~ /^@/ 0 0 0
404 0 $assert_msg = qq{$condition, \\\"$msg\\\"};
405 ▶ } elsif (defined $condition and not defined $msg) {
Condition33%1/3
defined $condition and not defined $msg
!ll&&!rl&&r
002
MC/DC0%0/2
defined $condition and not defined $msg
defined $condition not defined $msg
Truth table33%1/3
A and not(B)
A= defined $conditionB= not defined $msg
ABresult
0X0
100
111
406 2 ▶ $condition = substr($condition, 1) if $condition =~ /^@/;
Branch: 1 branch
BranchTrueFalseTotal
if $condition =~ /^@/ 0 2 2
407 2 $assert_msg = qq{\\\"$condition\\\"};
408 ▶ } elsif (defined $msg and not defined $condition) {
Condition0%0/3
defined $msg and not defined $condition
!ll&&!rl&&r
000
MC/DC0%0/2
defined $msg and not defined $condition
defined $msg not defined $condition
Truth table0%0/3
A and not(B)
A= defined $msgB= not defined $condition
ABresult
0X0
100
111
409 0 ▶ $msg = substr($msg, 1) if $msg =~ /^@/;
Branch: 1 branch
BranchTrueFalseTotal
if $msg =~ /^@/ 0 0 0
410 0 $assert_msg = qq{\\\"$msg\\\"};
411 } else {
412 0 $assert_msg = qq{\\\"user requested an assert\\\"};
413 }
414 }
415  
416 return << "..."
417 \t;; break if the condition evaluates to false
418 \t.assert "$assert_msg"
419 \tnop ;; needed for the assert
420 ...
421 29 }
422  
423 sub stimulate {
424 9 ▶ my $self = shift;
Subroutine stimulate: called CC=14 SCAR=26.9
Pod undocumented
425 9 my $pin = shift;
426 9 my %hh = ();
427 9 foreach my $href (@_) {
428 11 %hh = (%hh, %$href);
429 }
430 9 my $period = '';
431 9 ▶ $period = $hh{EVERY} if (defined $hh{EVERY} and length $hh{EVERY});
Branch: 1 branch
BranchTrueFalseTotal
if defined $hh{"EVERY"} and length $hh{"EVERY"} 2 7 9
Condition66%2/3
defined $hh{"EVERY"} and length $hh{"EVERY"}
!ll&&!rl&&r
702
MC/DC50%1/2
defined $hh{"EVERY"} and length $hh{"EVERY"}
defined $hh{"EVERY"} length $hh{"EVERY"}
Truth table66%2/3
A and B
A= defined $hh{"EVERY"}B= length $hh{"EVERY"}
ABresult
0X0
100
111
432 9 ▶ $period = qq{\t.sim "period $period"} if (defined $period and length $period);
Branch: 1 branch
BranchTrueFalseTotal
if defined $period and length $period 2 7 9
Condition66%2/3
defined $period and length $period
!ll&&!rl&&r
072
MC/DC50%1/2
defined $period and length $period
defined $period length $period
Truth table66%2/3
A and B
A= defined $periodB= length $period
ABresult
0X0
100
111
433 9 my $wave = '';
434 9 my $wave_type = 'digital';
435 9 ▶ if (exists $hh{WAVE} and ref $hh{WAVE} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hh{"WAVE"} and ref $hh{"WAVE"} eq "ARRAY") 9 0 9
Condition33%1/3
exists $hh{"WAVE"} and ref $hh{"WAVE"} eq "ARRAY"
!ll&&!rl&&r
009
MC/DC0%0/2
exists $hh{"WAVE"} and ref $hh{"WAVE"} eq "ARRAY"
exists $hh{"WAVE"} ref $hh{"WAVE"} eq "ARRAY"
Truth table33%1/3
A and B
A= exists $hh{"WAVE"}B= ref $hh{"WAVE"} eq "ARRAY"
ABresult
0X0
100
111
436 9 my $arr = $hh{WAVE};
437 9 ▶ $wave = "\t.sim \"{ " . join(',', @$arr) . " }\"" if scalar @$arr;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$arr 9 0 9
438 9 my $ad = 0;
439 9 foreach (@$arr) {
440 66 ▶ $ad |= 1 unless /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\d+$/ 5 61 66
441 }
442 9 ▶ $wave_type = 'analog' if $ad;
Branch: 1 branch
BranchTrueFalseTotal
if $ad 1 8 9
443 }
444 9 ▶ my $start = $hh{START} || 0;
Condition50%1/2
$hh{'START'} || {sign => '+', value => ['0']}
l!l
09
MC/DC0%0/1
$hh{'START'} || {sign => '+', value => ['0']}
$hh{'START'}
Truth table50%1/2
A
A= $hh{'START'}
Aresult
11
00
445 9 $start = qq{\t.sim "start_cycle $start"};
446 9 ▶ my $init = $hh{INITIAL} || 0;
Condition50%1/2
$hh{'INITIAL'} || {sign => '+', value => ['0']}
l!l
09
MC/DC0%0/1
$hh{'INITIAL'} || {sign => '+', value => ['0']}
$hh{'INITIAL'}
Truth table50%1/2
A
A= $hh{'INITIAL'}
Aresult
11
00
447 9 $init = qq{\t.sim "initial_state $init"};
448 9 my $num = $self->stimulus_count;
449 9 $self->stimulus_count($num + 1);
450 9 my $node = "stim$num$pin";
451 9 my $simpin = $self->_get_simport($pin);
452 return << "..."
453 \t.sim \"echo creating stimulus number $num\"
454 \t.sim \"stimulus asynchronous_stimulus\"
455 $init
456 $start
457 \t.sim \"$wave_type\"
458 $period
459 $wave
460 \t.sim \"name stim$num\"
461 \t.sim \"end\"
462 \t.sim \"echo done creating stimulus number $num\"
463 \t.sim \"node $node\"
464 \t.sim \"attach $node stim$num $simpin\"
465 ...
466 9 }
467  
468 sub get_autorun_code {
469 12 ▶ return qq{\t.sim "run"\n};
Subroutine get_autorun_code: called CC=1 SCAR=0.0
Pod undocumented
470 }
471  
472 sub autorun {
473 12 ▶ my $self = shift;
Subroutine autorun: called CC=1 SCAR=0.0
Pod undocumented
474 12 $self->should_autorun(1);
475 12 return "\t;;;; will autorun on start\n";
476 }
477  
478 sub stopwatch {
479 1 ▶ my ($self, $rollover) = @_;
Subroutine stopwatch: called CC=3 SCAR=11.3
Pod undocumented
480 1 my $code = qq{\t.sim "stopwatch.enable = true"\n};
481 1 ▶ $code .= qq{\t.sim "stopwatch.rollover = $rollover"\n} if defined $rollover;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rollover 1 0 1
482 1 ▶ $code .= qq{\t.sim "break stopwatch"\n} if defined $rollover;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rollover 1 0 1
483 1 return $code;
484 }
485  
486 sub attach {
487 3 ▶ my $self = shift;
Subroutine attach: called CC=14 SCAR=28.1
Pod undocumented
488 3 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 3 3
489 3 my $pin = shift;
490 3 my $code = '';
491 3 ▶ if ($pin =~ /US?ART/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pin =~ /US?ART/) 3 0 3
492 # TX - connect to UART
493 # RX - connect to UART but also send it data
494 3 ▶ unless ($self->pic->doesrole('USART')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->pic->doesrole("USART")) 0 3 3
495 0 carp "PIC ", $self->pic->type, " does not do USART";
496 0 return;
497 }
498 3 ▶ my $baudrate = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 0 3
499 3 ▶ my $loopback = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 1 3
500 3 ▶ my $key = ($pin =~ /^UART/) ? 'uart' : 'usart';
Branch: 1 branch
BranchTrueFalseTotal
$pin =~ /^UART/ ? : 3 0 3
501 $baudrate = $self->pic->code_config->{$key}->{baud} unless defined
502 3 ▶ $baudrate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $baudrate 0 3 3
503 3 ▶ $baudrate = 9600 unless defined $baudrate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $baudrate 0 3 3
504 3 my $id = $self->node_count;
505 3 $self->node_count($id + 1);
506 3 my $ipin = $self->pic->usart_pins->{async_in};
507 3 my $rxport = $self->_get_simport($ipin);
508 3 my $opin = $self->pic->usart_pins->{async_out};
509 3 my $txport = $self->_get_simport($opin);
510 3 ▶ return unless (exists $self->pic->pins->{$ipin} and exists $self->pic->pins->{$opin});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->pic->pins->{$ipin} and exists $self->pic->pins->{$opin} 0 3 3
Condition33%1/3
exists $self->pic->pins->{$ipin} and exists $self->pic->pins->{$opin}
!ll&&!rl&&r
003
MC/DC0%0/2
exists $self->pic->pins->{$ipin} and exists $self->pic->pins->{$opin}
exists $self->pic->pins->{$ipin} exists $self->pic->pins->{$opin}
Truth table33%1/3
A and B
A= exists $self->pic->pins->{$ipin}B= exists $self->pic->pins->{$opin}
ABresult
0X0
100
111
511 3 $code .= qq{\t.sim "module load usart U$id"\n};
512 3 $code .= qq{\t.sim "node TX_U$id"\n};
513 3 $code .= qq{\t.sim "node RX_U$id"\n};
514 3 $code .= qq{\t.sim "attach TX_U$id $txport U$id.RXPIN"\n};
515 3 $code .= qq{\t.sim "attach RX_U$id $rxport U$id.TXPIN"\n};
516 3 $code .= qq{\t.sim "U$id.txbaud = $baudrate"\n};
517 3 $code .= qq{\t.sim "U$id.rxbaud = $baudrate"\n};
518 3 my $x = 500;
519 3 my $y = 50 + 50 * $id;
520 3 $code .= qq{\t.sim "U$id.xpos = $x"\n};
521 3 $code .= qq{\t.sim "U$id.ypos = $y"\n};
522 3 ▶ if (defined $loopback) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loopback) 2 1 3
523 2 ▶ if (ref $loopback eq 'HASH' and $loopback->{string} =~ /loopback/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $loopback eq "HASH" and $loopback->{"string"} =~ /loopback/i) 2 0 2
Condition33%1/3
ref $loopback eq "HASH" and $loopback->{"string"} =~ /loopback/i
!ll&&!rl&&r
002
MC/DC0%0/2
ref $loopback eq "HASH" and $loopback->{"string"} =~ /loopback/i
ref $loopback eq "HASH" $loopback->{"string"} =~ /loopback/i
Truth table33%1/3
A and B
A= ref $loopback eq "HASH"B= $loopback->{"string"} =~ /loopback/i
ABresult
0X0
100
111
524 2 $code .= qq{\t.sim "U$id.loop = true"\n};
525 }
526 }
527 }
528 3 return $code;
529 }
530  
531 34 1;
532  
533 =encoding utf8
534  
535 =head1 NAME
536  
537 VIC::Receiver
538  
539 =head1 SYNOPSIS
540  
541 The Pegex::Receiver class for handling the grammar.
542  
543 =head1 DESCRIPTION
544  
545 INTERNAL CLASS.
546  
547 =head1 AUTHOR
548  
549 Vikas N Kumar <vikas@cpan.org>
550  
551 =head1 COPYRIGHT
552  
553 Copyright (c) 2014. Vikas N Kumar
554  
555 This program is free software; you can redistribute it and/or modify it
556 under the same terms as Perl itself.
557  
558 See http://www.perl.com/perl/misc/Artistic.html
559  
560 =cut
« VIC/Command.pm ↑ Summary VIC/PIC/Functions/GPIO.pm »