VIC.pm

filter: Statementstmt 43.5% 74 / 170 Branchbran 12.2% 13 / 106 Conditioncond 11.1% 3 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 73.0% 19 / 26 Podpod 0.0% 0 / 13 totaltotal 30.5% 109 / 357 CC 66 SCAR 49.5
CC
66
Cov
30%
assemble
462.0
_load_simulator
132.0
simulate
72.0
CRAP 141.5
« VIC/Command.pm ↑ Summary VIC/PIC/Base.pm »
1 package VIC;
2 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 33 ▶use Env qw(@PATH);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use File::Which qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Capture::Tiny ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use VIC::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶use VIC::Grammar;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 33 ▶use VIC::Receiver;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 33 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 33 our @EXPORT = qw(
15 compile
16 assemble
17 simulate
18 supported_chips
19 supported_simulators
20 gpasm
21 gplink
22 gputils
23 bindir
24 is_chip_supported
25 is_simulator_supported
26 list_chip_features
27 );
28  
29 33 our $Debug = 0;
30 33 our $Verbose = 0;
31 33 our $Intermediate = 0;
32 33 our $GPASM;
33 33 our $GPLINK;
34 33 our $GPUTILSDIR;
35  
36 33 our $VERSION = '0.32';
37 33 $VERSION = eval $VERSION;
38  
39 33 ▶sub compile {
Subroutine compile: called CC=3 SCAR=11.2
Pod undocumented
40 33 my ($input, $pic) = @_;
41  
42 33 ▶ die "No code given to compile\n" unless $input;
Branch: 1 branch
BranchTrueFalseTotal
unless $input 0 33 33
43 33 my $parser = VIC::Parser->new(
44 grammar => VIC::Grammar->new,
45 receiver => VIC::Receiver->new(
46 pic_override => $pic,
47 intermediate_inline => $Intermediate,
48 ),
49 debug => $Debug,
50 throw_on_error => 1,
51 );
52  
53 33 my $output = $parser->parse($input);
54 31 my $chip = $parser->receiver->current_chip();
55 31 my $sim = $parser->receiver->current_simulator();
56 31 ▶ return wantarray ? ($output, $chip, $sim) : $output;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 31 31
57 }
58  
59 1 ▶sub supported_chips { return VIC::Receiver::supported_chips(); }
Subroutine supported_chips: called CC=1 SCAR=0.0
Pod undocumented
60  
61 1 ▶sub supported_simulators { return VIC::Receiver::supported_simulators(); }
Subroutine supported_simulators: called CC=1 SCAR=0.0
Pod undocumented
62  
63 19 ▶sub is_chip_supported { return VIC::Receiver::is_chip_supported(@_) };
Subroutine is_chip_supported: called CC=1 SCAR=0.0
Pod undocumented
64  
65 0 ▶sub is_simulator_supported { return VIC::Receiver::is_simulator_supported(@_) };
Subroutine is_simulator_supported: not called CC=1 SCAR=6.9
Pod undocumented
66  
67 0 ▶sub list_chip_features { return VIC::Receiver::list_chip_features(@_) };
Subroutine list_chip_features: not called CC=1 SCAR=6.9
Pod undocumented
68  
69 0 ▶sub print_pinout { return VIC::Receiver::print_pinout(@_) };
Subroutine print_pinout: not called CC=1 SCAR=6.9
Pod undocumented
70  
71 3 ▶sub _load_gputils {
Subroutine _load_gputils: called CC=8 SCAR=32.0
72 3 my ($gpasm, $gplink, $bindir);
73 3 ▶ my ($stdo, $stde) = capture {
Subroutine __ANON__: called CC=14 SCAR=41.4
74 3 my $alien;
75 3 ▶ eval q{
Branch: 1 branch
BranchTrueFalseTotal
eval "\n require Alien::gputils;\n \$alien = Alien::gputils->new();\n " or warn "Cannot find Alien::gputils. Ignoring\n" 0 3 3
76 require Alien::gputils;
77 $alien = Alien::gputils->new();
78 } or warn "Cannot find Alien::gputils. Ignoring\n";
79 3 ▶ if ($alien) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alien) 0 3 3
80 0 ▶ print "Looking for gpasm and gplink using Alien::gputils\n" if $Verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $VIC::Verbose 0 0 0
81 0 ▶ $gpasm = $alien->gpasm() if $alien->can('gpasm');
Branch: 1 branch
BranchTrueFalseTotal
if $alien->can("gpasm") 0 0 0
82 0 ▶ $gplink = $alien->gplink() if $alien->can('gplink');
Branch: 1 branch
BranchTrueFalseTotal
if $alien->can("gplink") 0 0 0
83 0 ▶ $bindir = $alien->bin_dir() if $alien->can('bin_dir');
Branch: 1 branch
BranchTrueFalseTotal
if $alien->can("bin_dir") 0 0 0
84 }
85 3 ▶ unless (defined $gpasm and defined $gplink) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gpasm and defined $gplink) 3 0 3
Condition33%1/3
defined $gpasm and defined $gplink
!ll&&!rl&&r
300
MC/DC0%0/2
defined $gpasm and defined $gplink
defined $gpasm defined $gplink
Truth table33%1/3
A and B
A= defined $gpasmB= defined $gplink
ABresult
0X0
100
111
86 3 ▶ print "Looking for gpasm and gplink in \$ENV{PATH}\n" if $Verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $VIC::Verbose 0 3 3
87 3 $gpasm = which('gpasm');
88 3 $gplink = which('gplink');
89 }
90 3 ▶ unless (defined $bindir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bindir) 3 0 3
91 3 ▶ if ($gpasm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpasm) 0 3 3
92 0 my @dirs = File::Spec->splitpath($gpasm);
93 0 ▶ pop @dirs if @dirs;
Branch: 1 branch
BranchTrueFalseTotal
if @dirs 0 0 0
94 0 ▶ $bindir = File::Spec->catdir(@dirs) if @dirs;
Branch: 1 branch
BranchTrueFalseTotal
if @dirs 0 0 0
95 }
96 }
97 3 };
98 3 ▶ if ($Verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($VIC::Verbose) 0 3 3
99 0 ▶ print $stdo if $stdo;
Branch: 1 branch
BranchTrueFalseTotal
if $stdo 0 0 0
100 0 ▶ print STDERR $stde if $stde;
Branch: 1 branch
BranchTrueFalseTotal
if $stde 0 0 0
101 0 ▶ print "Using gpasm: $gpasm\n" if $gpasm;
Branch: 1 branch
BranchTrueFalseTotal
if $gpasm 0 0 0
102 0 ▶ print "Using gplink: $gplink\n" if $gplink;
Branch: 1 branch
BranchTrueFalseTotal
if $gplink 0 0 0
103 0 ▶ print "gputils installed in: $bindir\n" if $bindir;
Branch: 1 branch
BranchTrueFalseTotal
if $bindir 0 0 0
104 }
105 3 $GPASM = $gpasm;
106 3 $GPLINK = $gplink;
107 3 $GPUTILSDIR = $bindir;
108 3 ▶ return wantarray ? ($gpasm, $gplink, $bindir) : [$gpasm, $gplink, $bindir];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
109 }
110  
111 0 ▶sub _load_simulator {
Subroutine _load_simulator: not called CC=11 SCAR=48.8
112 0 my $simtype = shift;
113 0 my $simexe;
114 0 ▶ die "Simulator type $simtype not supported yet\n" unless $simtype eq 'gpsim';
Branch: 1 branch
BranchTrueFalseTotal
unless $simtype eq "gpsim" 0 0 0
115 0 ▶ if ($^O =~ /mswin32/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /mswin32/i) { } 0 0 0
116 0 foreach (qw{PROGRAMFILES ProgramFiles PROGRAMFILES(X86)
117 ProgramFiles(X86) ProgamFileW6432 PROGRAMFILESW6432}) {
118 0 ▶ next unless exists $ENV{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ENV{$_} 0 0 0
119 0 ▶ my $dir = ($ENV{$_} =~ /\s+/) ? Win32::GetShortPathName($ENV{$_}) : $ENV{$_};
Branch: 1 branch
BranchTrueFalseTotal
$ENV{$_} =~ /\s+/ ? : 0 0 0
120 0 ▶ push @PATH, File::Spec->catdir($dir, $simtype, 'bin') if $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir 0 0 0
121 }
122 0 $simexe = which("$simtype.exe");
123 0 ▶ $simexe = which($simtype) unless $simexe;
Branch: 1 branch
BranchTrueFalseTotal
unless $simexe 0 0 0
124 } else {
125 0 $simexe = which($simtype);
126 }
127 0 ▶ print "$simtype found at $simexe\n" if ($Verbose and $simexe);
Branch: 1 branch
BranchTrueFalseTotal
if $VIC::Verbose and $simexe 0 0 0
Condition0%0/3
$VIC::Verbose and $simexe
!ll&&!rl&&r
000
MC/DC0%0/2
$VIC::Verbose and $simexe
$VIC::Verbose $simexe
Truth table0%0/3
A and B
A= $VIC::VerboseB= $simexe
ABresult
0X0
100
111
128 0 ▶ warn "$simtype not found\n" unless $simexe;
Branch: 1 branch
BranchTrueFalseTotal
unless $simexe 0 0 0
129 0 return $simexe;
130 }
131  
132 1 ▶sub gputils {
Subroutine gputils: called CC=4 SCAR=17.9
Pod undocumented
133 1 ▶ return ($GPASM, $GPLINK, $GPUTILSDIR) if (defined $GPASM and defined $GPLINK
Branch: 1 branch
BranchTrueFalseTotal
if defined $VIC::GPASM and defined $VIC::GPLINK and defined $VIC::GPUTILSDIR 0 1 1
Condition33%2/6
defined $VIC::GPASM and defined $VIC::GPLINK and defined $VIC::GPUTILSDIR
!ll&&!rl&&r
100
defined $VIC::GPASM and defined $VIC::GPLINK
!ll&&!rl&&r
100
MC/DC0%0/3
defined $VIC::GPASM and defined $VIC::GPLINK and defined $VIC::GPUTILSDIR
defined $VIC::GPASM defined $VIC::GPLINK defined $VIC::GPUTILSDIR
Truth table25%1/4
A and B and C
A= defined $VIC::GPASMB= defined $VIC::GPLINKC= defined $VIC::GPUTILSDIR
ABCresult
0XX0
10X0
1100
1111
134 and defined $GPUTILSDIR);
135 1 return &_load_gputils();
136 }
137  
138 1 ▶sub gpasm {
Subroutine gpasm: called CC=2 SCAR=7.1
Pod undocumented
139 1 ▶ return $GPASM if defined $GPASM;
Branch: 1 branch
BranchTrueFalseTotal
if defined $VIC::GPASM 0 1 1
140 1 my @out = &_load_gputils();
141 1 return $out[0];
142 }
143  
144 1 ▶sub gplink {
Subroutine gplink: called CC=2 SCAR=7.1
Pod undocumented
145 1 ▶ return $GPLINK if defined $GPLINK;
Branch: 1 branch
BranchTrueFalseTotal
if defined $VIC::GPLINK 0 1 1
146 1 my @out = &_load_gputils();
147 1 return $out[1];
148 }
149  
150 0 ▶sub bindir {
Subroutine bindir: not called CC=2 SCAR=17.9
Pod undocumented
151 0 ▶ return $GPUTILSDIR if defined $GPUTILSDIR;
Branch: 1 branch
BranchTrueFalseTotal
if defined $VIC::GPUTILSDIR 0 0 0
152 0 my @out = &_load_gputils();
153 0 return $out[2];
154 }
155  
156 0 ▶sub assemble($$) {
Subroutine assemble: not called CC=21 SCAR=61.4
Pod undocumented
157 0 my ($chip, $output) = @_;
158 0 ▶ return unless defined $chip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chip 0 0 0
159 0 ▶ return unless defined $output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 0 0
160 0 my $hexfile = $output;
161 0 my $objfile = $output;
162 0 my $codfile = $output;
163 0 my $stcfile = $output;
164 0 ▶ if ($output =~ /\.asm$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output =~ /\.asm$/) { } 0 0 0
165 0 $hexfile =~ s/\.asm$/\.hex/g;
166 0 $objfile =~ s/\.asm$/\.o/g;
167 0 $codfile =~ s/\.asm$/\.cod/g;
168 0 $stcfile =~ s/\.asm$/\.stc/g;
169 } else {
170 0 $hexfile = $output . '.hex';
171 0 $objfile = $output . '.o';
172 0 $codfile = $output . '.hex';
173 0 $stcfile = $output . '.o';
174 }
175 0 my ($gpasm, $gplink, $bindir) = VIC::gputils();
176 0 ▶ unless (defined $gpasm and defined $gplink and -e $gpasm and -e $gplink) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gpasm and defined $gplink and -e $gpasm and -e $gplink) 0 0 0
Condition0%0/9
defined $gpasm and defined $gplink and -e $gpasm and -e $gplink
!ll&&!rl&&r
000
defined $gpasm and defined $gplink and -e $gpasm
!ll&&!rl&&r
000
defined $gpasm and defined $gplink
!ll&&!rl&&r
000
MC/DC0%0/4
defined $gpasm and defined $gplink and -e $gpasm and -e $gplink
defined $gpasm defined $gplink -e $gpasm -e $gplink
Truth table0%0/5
A and B and C and D
A= defined $gpasmB= defined $gplinkC= -e $gpasmD= -e $gplink
ABCDresult
0XXX0
10XX0
110X0
11100
11111
177 0 die "Cannot find gpasm/gplink to compile $output into a hex file $hexfile.";
178 }
179 0 my ($inc1, $inc2) = ('', '');
180 0 ▶ if (defined $bindir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bindir) 0 0 0
181 0 my @dirs = File::Spec->splitdir($bindir);
182 0 ▶ my $l = pop @dirs if @dirs;
Branch: 1 branch
BranchTrueFalseTotal
if @dirs 0 0 0
183 0 ▶ if (defined $l and $l ne 'bin') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $l and $l ne "bin") 0 0 0
Condition0%0/3
defined $l and $l ne "bin"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $l and $l ne "bin"
defined $l $l ne "bin"
Truth table0%0/3
A and B
A= defined $lB= $l ne "bin"
ABresult
0X0
100
111
184 0 push @dirs, $l; # return the last directory
185 }
186 0 my @includes = ();
187 0 my @linkers = ();
188 0 push @includes, File::Spec->catdir(@dirs, 'header');
189 0 push @linkers, File::Spec->catdir(@dirs, 'lkr');
190 0 push @includes, File::Spec->catdir(@dirs, 'share', 'gputils', 'header');
191 0 push @linkers, File::Spec->catdir(@dirs, 'share', 'gputils', 'lkr');
192 0 foreach (@includes) {
193 0 ▶ $inc1 .= " -I $_ " if -d $_;
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 0 0 0
194 }
195 0 foreach (@linkers) {
196 0 ▶ $inc2 .= " -I $_ " if -d $_;
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 0 0 0
197 }
198 }
199 0 $codfile = File::Spec->rel2abs($codfile);
200 0 $stcfile = File::Spec->rel2abs($stcfile);
201 0 $hexfile = File::Spec->rel2abs($hexfile);
202 0 $objfile = File::Spec->rel2abs($objfile);
203 0 my $gpasm_cmd = "$gpasm $inc1 -p $chip -M -c $output";
204 0 my $gplink_cmd = "$gplink $inc2 -q -m -o $hexfile $objfile ";
205 0 ▶ print "$gpasm_cmd\n" if $Verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $VIC::Verbose 0 0 0
206 0 ▶ system($gpasm_cmd) == 0 or die "Unable to run '$gpasm_cmd': $?";
Branch: 1 branch
BranchTrueFalseTotal
system($gpasm_cmd) == 0 or die "Unable to run '${gpasm_cmd}': $?" 0 0 0
207 0 ▶ print "$gplink_cmd\n" if $Verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $VIC::Verbose 0 0 0
208 0 ▶ system($gplink_cmd) == 0 or die "Unable to run '$gplink_cmd': $?";
Branch: 1 branch
BranchTrueFalseTotal
system($gplink_cmd) == 0 or die "Unable to run '${gplink_cmd}': $?" 0 0 0
209 0 my $fh;
210 0 ▶ open $fh, ">$stcfile" or die "Unable to write $stcfile: $?";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">$stcfile" or die "Unable to write ${stcfile}: $?" 0 0 0
211 0 print $fh "load s '$codfile'\n";
212 0 close $fh;
213 0 return { hex => $hexfile, obj => $objfile, cod => $codfile, stc => $stcfile };
214 }
215  
216 0 ▶sub simulate {
Subroutine simulate: not called CC=8 SCAR=42.8
Pod undocumented
217 0 my ($sim, $hh) = @_;
218 0 my $stc;
219 0 ▶ if (ref $hh eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hh eq 'HASH') { } 0 0 0
elsif (ref $hh eq 'ARRAY') { } 0 0 0
220 0 $stc = $hh->{stc};
221 } elsif (ref $hh eq 'ARRAY') {
222 0 ($stc) = grep {/\.stc$/} @$hh;
223 } else {
224 0 $stc = $hh;
225 }
226 0 ▶ die "Cannot find $stc to run the simulator $sim on\n" unless (defined $stc and -e $stc);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stc and -e $stc 0 0 0
Condition0%0/3
defined $stc and -e $stc
!ll&&!rl&&r
000
MC/DC0%0/2
defined $stc and -e $stc
defined $stc -e $stc
Truth table0%0/3
A and B
A= defined $stcB= -e $stc
ABresult
0X0
100
111
227 0 my $simexe = &_load_simulator($sim);
228 0 ▶ die "$sim is not present in your system PATH for simulation\n" unless $simexe;
Branch: 1 branch
BranchTrueFalseTotal
unless $simexe 0 0 0
229 0 my $sim_cmd = "$simexe $stc";
230 0 ▶ print "$sim_cmd\n" if $Verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $VIC::Verbose 0 0 0
231 0 ▶ system($sim_cmd) == 0 or die "Unable to run '$sim_cmd': $?";
Branch: 1 branch
BranchTrueFalseTotal
system($sim_cmd) == 0 or die "Unable to run '${sim_cmd}': $?" 0 0 0
232 0 1;
233 }
234  
235 33 1;
236  
237 =encoding utf8
238  
239 =head1 NAME
240  
241 VIC - A Viciously Simple Syntax for PIC Microcontrollers
242  
243 =head1 SYNOPSIS
244  
245 $ vic program.vic -o program.asm
246  
247 $ vic -h
248  
249 =head1 DESCRIPTION
250  
251 Refer documentation at L<http://selectiveintellect.github.io/vic/>.
252  
253 =head1 AUTHOR
254  
255 Vikas N Kumar <vikas@cpan.org>
256  
257 =head1 COPYRIGHT
258  
259 Copyright (c) 2014-2016. Vikas N Kumar
260  
261 This program is free software; you can redistribute it and/or modify it
262 under the same terms as Perl itself.
263  
264 See http://www.perl.com/perl/misc/Artistic.html
265  
266 =cut
« VIC/Command.pm ↑ Summary VIC/PIC/Base.pm »