Writer.pm

filter: Statementstmt 100% 66 / 66 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 100% 6 / 6 totaltotal 97.9% 96 / 98 CC 6 SCAR 5.4
CC
6
Cov
97%
_redirectSTDOUT
4.1
writeHeaders
2.0
addValue
2.0
CRAP 1.7
« Writer/Signal.pm ↑ Summary Writer/Symbol.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Verilog::VCD::Writer;
4 2 $Verilog::VCD::Writer::VERSION = '0.002';
5 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Verilog::VCD::Writer::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: VCD waveform File creation module.
9
10  
11  
12 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 2 has timescale =>(is =>'ro',default=>'1ps');
18 2 has vcdfile =>(is =>'ro');
19 2 ▶has date =>(is=>'ro',isa=>'DateTime',default=>sub{DateTime->now()});
Subroutine __ANON__: called CC=1 SCAR=0.0
20 has _modules=>(is=>'ro',isa=>'ArrayRef[Verilog::VCD::Writer::Module]',
21 4 ▶ default=>sub{[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
22 2 traits=>['Array'],
23 handles=>{modules_push=>'push',
24 modules_all=>'elements'}
25 );
26 has _comments=>(is=>'ro',isa=>'ArrayRef',
27 4 ▶ default=>sub{[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
28 2 traits=>['Array'],
29 handles=>{comments_push=>'push',
30 comments_all=>'elements'}
31 );
32 2 has _fh=>(is=>'ro',lazy=>1,builder=>"_redirectSTDOUT");
33  
34 sub _redirectSTDOUT{
35 2 ▶ my $self=shift;
Subroutine _redirectSTDOUT: called CC=4 SCAR=14.0
36 2 my $fh;
37 2 ▶ if(defined $self->vcdfile){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->vcdfile) { } 1 1 2
38 1 ▶ open($fh, ">", $self->vcdfile) or die "unable to write to $self->vcdfile";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $self->vcdfile or die "unable to write to $self->vcdfile" 0 1 1
39 }else{
40 1 ▶ open($fh, ">-") or die "unable to write to STDOUT";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">-" or die "unable to write to STDOUT" 0 1 1
41 }
42 2 return $fh;
43 }
44  
45  
46 sub writeHeaders{
47 2 ▶my $self=shift;
Subroutine writeHeaders: called CC=2 SCAR=6.9
Pod documented
48 2 my $fh=$self->_fh;
49 2 say $fh '$date';
50 2 say $fh $self->date;
51 2 say $fh '$end
52 $version
53 Perl VCD Writer Version '.$Verilog::VCD::Writer::VERSION.'
54 $end
55 $comment';
56 2 say $fh join("::\n",$self->comments_all);
57 2 say $fh '$end
58 $timescale '.$self->timescale.' $end';
59 2 $_->printScope($fh) foreach ($self->modules_all);
60 2 say $fh '$enddefinitions $end
61 $dumpvars
62 ';
63 }
64  
65  
66 sub addModule{
67 4 ▶ my ($self,$modulename)=@_;
Subroutine addModule: called CC=1 SCAR=0.0
Pod documented
68 4 my $m=Verilog::VCD::Writer::Module->new(name=>$modulename,type=>"module");
69 4 $self->modules_push($m);
70 4 return $m;
71 }
72  
73  
74  
75 sub setTime {
76 4 ▶ my ($self,$time)=@_;
Subroutine setTime: called CC=1 SCAR=0.0
Pod documented
77 4 my $fh=$self->_fh;
78 4 say $fh '#'.$time;
79
80 }
81 sub _dec2bin {
82 6 ▶ my $str = unpack("B32", pack("N", shift));
Subroutine _dec2bin: called CC=1 SCAR=0.0
83 6 $str =~ s/^0+(?=\d)//; # otherwise you'll get leading zeros
84 6 return $str;
85 }
86  
87  
88 sub addValue {
89 12 ▶ my ($self,$sig,$value)=@_;
Subroutine addValue: called CC=2 SCAR=6.9
Pod documented
90 12 my $fh=$self->_fh;
91 #say STDERR "Adding Values $sig $value";
92 12 ▶ if ($sig->width == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($sig->width == 1) { } 6 6 12
93 6 say $fh $value.$sig->symbol;
94 }else {
95 6 say $fh "b"._dec2bin($value)." ". $sig->symbol;
96 }
97 }
98  
99  
100 sub addComment{
101 2 ▶ my ($self,$comment)=@_;
Subroutine addComment: called CC=1 SCAR=0.0
Pod documented
102 2 $self->comments_push(" ".$comment);
103 }
104  
105  
106 sub flush{
107 2 ▶ my ($self)=shift;
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
108 2 my$fh=$self->_fh;
109 2 $fh->autoflush(1);
110  
111 }
112  
113  
114  
115 2 1;
116  
117 __END__
118  
119 =pod
120  
121 =encoding UTF-8
122  
123 =head1 NAME
124  
125 Verilog::VCD::Writer - VCD waveform File creation module.
126  
127 =head1 VERSION
128  
129 version 0.002
130  
131 =head1 SYNOPSIS
132  
133 use Verilog::VCD::Writer;
134  
135 my $writer=Verilog::VCD::Writer->new(timescale=>'1 ns',vcdfile=>"test.vcd");
136 $writer->addComment("Author:Vijayvithal");
137  
138 my $top=$writer->addModule("top"); # Create toplevel module
139 my $TX=$writer->addSignal("TX",7,0); #Add Signals to top
140 my $RX=$writer->addSignal("RX",7,0);
141  
142 my $dut=$top->addModule("DUT"); Create SubModule
143 $dut->dupSignal($TX,"TX",7,0); #Duplicate signals from Top in submodule
144 $dut->dupSignal($RX,"RX",7,0);
145
146 $writer->writeHeaders(); # Output the VCD Header.
147 $writer->setTime(0); # Time 0
148 $writer->addValue($TX,0); # Record Transition
149 $writer->addValue($RX,0);
150 $writer->setTime(5); # Time 1ns
151 $writer->addValue($TX,1);
152 $writer->addValue($RX,0);
153  
154 =head1 METHODS
155  
156 =head2 addComment(comment)
157  
158 Adds a comment to the VCD file header. This method should be called before writeHeaders();
159  
160 =head2 flush()
161  
162 Flushes the output buffer.
163  
164 =head1 DESCRIPTION
165 This module originated out of my need to view the <Time,Voltage> CSV dump from the scope using GTKWave.
166  
167 This module provides an interface for creating a VCD (Value change Dump) file.
168  
169 =head2 new (timescale=>'1ps',vcdfile=>'test.vcd',date=>DateTime->now());
170  
171 The constructor takes the following options
172  
173 =over 4
174  
175 =item *
176  
177 timescale: default is '1ps'
178  
179 =item *
180  
181 vcdfile: default is STDOUT, if a filename is given the VCD output will be written to it.
182  
183 =item *
184  
185 Date: a DateTime object, default is current date.
186  
187 =back
188  
189 =head2 writeHeaders()
190  
191 This method should be called after all the modules and signals are declared.
192 This method outputs the header of the VCD file
193  
194 =head2 addModule(ModuleName)
195  
196 This method takes the module name as an input string and returns the corresponding Verilog::VCD::Writer::Module object.
197  
198 =head2 setTime(time)
199  
200 This module takes the time information as an integer value and writes it out to the VCD file.
201  
202 =head2 addValue(Signal,Value)
203  
204 This method takes two parameters, an Object of the type Verilog::VCD::Writer::Signal and the decimal value of the signal at the current time.
205 This module prints the <Signal,Value> information as a formatted line to the VCD file
206  
207 =head1 AUTHOR
208  
209 Vijayvithal Jahagirdar<jvs@cpan.org>
210  
211 =head1 COPYRIGHT AND LICENSE
212  
213 This software is copyright (c) 2017 by Vijayvithal.
214  
215 This is free software; you can redistribute it and/or modify it under
216 the same terms as the Perl 5 programming language system itself.
217  
218 =cut
« Writer/Signal.pm ↑ Summary Writer/Symbol.pm »