This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
vcd-prune
executable file
·169 lines (140 loc) · 4.45 KB
/
vcd-prune
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/perl -w
# See LICENSE for license details.
use Getopt::Long;
use IO::File;
use Pod::Usage;
use strict;
use warnings;
use vars qw ($debug $PREFIX);
$debug = 0;
my $opt_modules = "";
my $opt_output_filename;
my @opt_files;
Getopt::Long::config ("no_auto_abbrev","no_pass_through");
if (! GetOptions
("debug" => \$debug,
"h|help" => \&usage,
"m|module=s" => sub{
shift;
my $module = shift;
if ($opt_modules eq "") { $opt_modules = "($module)"; }
else {$opt_modules = "$opt_modules|($module)"}
},
"o|output=s" => \$opt_output_filename,
"<>" => \¶meter,
)) {
\&usage and die "[ERROR] Bad usage\n"
}
print STDERR "[ERROR] Specify at least one root module to dump.\n" and usage()
if !$opt_modules;
if ($debug) { print "[DEBUG] Using module regex match: /$opt_modules/\n"; }
my $file_out;
if ($opt_output_filename) {
open $file_out, ">", "$opt_output_filename" or
die "[ERROR] Unable to open $file_out\n";
}
if (!@opt_files) {
prune(\*STDIN);
} else {
foreach (@opt_files) {
open my $fh, "<", $_ or die "[ERROR] Unable to open $_\n";
prune($fh);
close $fh;
}
}
if ($opt_output_filename) {
close $file_out;
}
sub prune {
my $fh = shift;
# Build up a hash of all signals to include and signals to skip
my %includes;
my %skips;
my @scope;
my $header = "";
my $found_module = 0;
while (<$fh>) {
if ($_ =~ /^\$enddefinitions \$end$/) {
if ($file_out) {print $file_out $_} else {print $_};
last;
}
if ($_ =~ /\$version|\$date|^\s*\$end$|\$timescale/) {
if ($file_out) {print $file_out $_} else {print $_};
next;
}
if ($_ =~ /^\s*\$scope module (.+) \$end$/) {
my $module = $1;
if ($_ =~ /$opt_modules/ || $found_module) {
$found_module++;
if ($file_out) {print $file_out $_} else {print $_};
}
push @scope, $module;
if ($debug) { print "[DEBUG] $found_module $header$module\n"; }
$header = "$header ";
next;
}
if ($_ =~ /^\s*\$upscope \$end$/) {
if ($found_module) {
$found_module--;
if ($file_out) {print $file_out $_} else {print $_};
}
$header = substr($header, 0, -2);
pop @scope;
next;
}
if ($found_module) {
if ($_ !~ /^\s*\$var\s+wire\s+\d+\s+(.+?)\s+(.+)\$end$/) {
die;
}
if ($file_out) {print $file_out $_} else {print $_};
$includes{$1}++;
}
}
# Prune the VCD file
while (<$fh>) {
if ($_ =~ /^#/ || /^$/) {
if ($file_out) {print $file_out $_} else {print $_};
next;
}
my $signal;
if ($_ =~ /^[01](.+)$/ || /^.+ (.+)$/) {
$signal = $1;
}
if ($includes{$signal}) {
if ($file_out) {print $file_out $_} else {print $_};
}
}
}
sub usage {
pod2usage(-verbose=>2, -exitval=>2, -output=>\*STDOUT, -noperldoc=>1);
}
sub parameter {
my $param = shift;
if ($param =~ /^--?/) {
die "[ERROR] Unknown parameter: $param\n";
} else {
push @opt_files, "$param";
}
}
__END__
=pod
=head1 NAME
vcd-prune - Remove/keep the signals of specific modules in a VCD file
=head1 SYNOPSIS
vcd-prune -m MODULE [-m MODULE...] [OPTION]... [VCD...]
=head1 DESCRIPTION
This reads an explicitly specified VCD file or STDIN and extracts
only those signals encapsulated by the specified MODULE (or multiple
modules). The output vcd file will be written to STDOUT by default
or to a file if so specified.
=head1 OPTIONS
Mandatory arguments to long options are mandatory to short options, too.
--debug Enable debugging information as the hierarchy is
traversed.
-h, --help Print this help text
-m, --module=MODULE Modules to include in the VCD file. All submodules
will be included. This option can be specified
multiple times for multiple modules. By default
all top modules are instrumented (why would you do
this?). Note, this is technically a regex.
-o, --output=FILE Output file. Default is STDOUT.