aboutsummaryrefslogtreecommitdiffstats
path: root/tests/tools/vcdcd.pl
blob: 58a92b44dfb8afb77d800a6a5fd268acf7d2d4b2 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/perl -w
#
# Note: You might need to install the Verilog::VCD package using CPAN..

use strict;
use Data::Dumper;
use Verilog::VCD qw(parse_vcd list_sigs);

$| = 1;

my $opt_width = 0;
my $opt_delay = 0;

while (1)
{
	if ($ARGV[0] eq '-w') {
		$opt_width = +$ARGV[1];
		shift @ARGV;
		shift @ARGV;
		next;
	}
	if ($ARGV[0] eq '-d') {
		$opt_delay = +$ARGV[1];
		shift @ARGV;
		shift @ARGV;
		next;
	}
	last;
}

if ($#ARGV != 1) {
	print STDERR "\n";
	print STDERR "VCDCD - Value Change Dump Change Dumper\n";
	print STDERR "\n";
	print STDERR "Usage: $0 [-w N] [-d N] gold.vcd gate.vcd\n";
	print STDERR "\n";
	print STDERR "  -w N\n";
	print STDERR "    reserve N characters for bitmap in text output (default: auto)\n";
	print STDERR "\n";
	print STDERR "  -d N\n";
	print STDERR "    allow for N timesteps delay between gate and gold (default: 0)\n";
	print STDERR "\n";
	print STDERR "Compare a known-good (gold) vcd file with a second (gate) vcd file.\n";
	print STDERR "This is not very efficient -- so use with care on large vcd files.\n";
	print STDERR "\n";
	exit 1;
}

my $fn_gold = $ARGV[0];
my $fn_gate = $ARGV[1];

print "Finding common signals..\n";
my @gold_signals = list_sigs($fn_gold);
my @gate_signals = list_sigs($fn_gate);

my %gold_signals_hash;
my %gate_signals_hash;

for (@gold_signals) {
	my $fullname = $_;
	s/(\[([0-9]+|[0-9]+:[0-9]+)\])$//;
	$gold_signals_hash{$_}->{$fullname} = 1 unless /(^|\.)_[0-9]+_/;
}

for (@gate_signals) {
	my $fullname = $_;
	s/(\[([0-9]+|[0-9]+:[0-9]+)\])$//;
	$gate_signals_hash{$_}->{$fullname} = 1 unless /(^|\.)_[0-9]+_/;
}

my @signals;
for my $net (sort keys %gold_signals_hash) {
	next unless exists $gate_signals_hash{$net};
	# next unless $net eq "tst_bench_top.i2c_top.byte_controller.bit_controller.cnt";
	my %orig_net_names;
	print "common signal: $net";
	for my $fullname (keys $gold_signals_hash{$net}) {
		$orig_net_names{$fullname} = 1;
	}
	for my $fullname (keys $gate_signals_hash{$net}) {
		$orig_net_names{$fullname} = 1;
	}
	for my $net (sort keys %orig_net_names) {
		push @signals, $net;
		print " $1" if /(\[([0-9]+|[0-9]+:[0-9]+)\])$/;
	}
	print "\n";
}

print "Loading gold vcd data..\n";
my $vcd_gold = parse_vcd($fn_gold, {siglist => \@signals});

print "Loading gate vcd data..\n";
my $vcd_gate = parse_vcd($fn_gate, {siglist => \@signals});

# print Dumper($vcd_gold);
# print Dumper($vcd_gate);

my %times;
my $signal_maxlen = 8;
my $data_gold = { };
my $data_gate = { };

sub checklen($$)
{
	my ($net, $val) = @_;
	my $thislen = length $val;
	$thislen += $1 if $net =~ /\[([0-9]+)\]$/;
	$thislen += $1 if $net =~ /\[([0-9]+):[0-9]+\]$/;
	$signal_maxlen = $thislen if $signal_maxlen < $thislen;
}

print "Processing gold vcd data..\n";
for my $key (keys %$vcd_gold) {
	for my $net (@{$vcd_gold->{$key}->{'nets'}}) {
		my $netname = $net->{'hier'} . "." . $net->{'name'};
		for my $tv (@{$vcd_gold->{$key}->{'tv'}}) {
			my $time = int($tv->[0]);
			my $value = $tv->[1];
			checklen($netname, $value);
			$data_gold->{$time}->{$netname} = $value;
			$times{$time} = 1;
		}
	}
}

print "Processing gate vcd data..\n";
for my $key (keys %$vcd_gate) {
	for my $net (@{$vcd_gate->{$key}->{'nets'}}) {
		my $netname = $net->{'hier'} . "." . $net->{'name'};
		for my $tv (@{$vcd_gate->{$key}->{'tv'}}) {
			my $time = int($tv->[0]);
			my $value = $tv->[1];
			checklen($netname, $value);
			$data_gate->{$time}->{$netname} = $value;
			$times{$time} = 1;
		}
	}
}

$signal_maxlen = $opt_width if $opt_width > 0;

my $diffcount = 0;
my %state_gold;
my %state_gate;
my %signal_sync;
my %touched_nets;

sub set_state_bit($$$$)
{
	my ($state, $net, $bit, $value) = @_;
	my @data;
	@data = split //, $state->{$net} if exists $state->{$net};
	unshift @data, "-" while $#data < $bit;
	$data[$#data - $bit] = $value;
	$state->{$net} = join "", @data;
	$signal_sync{$net} = 1 unless exists $signal_sync{$net};
	$touched_nets{$net} = 1;
}

sub set_state($$$)
{
	my ($state, $net, $value) = @_;

	if ($net =~ /(.*)\[([0-9]+)\]$/) {
		set_state_bit($state, $1, $2, $value);
		return;
	}

	if ($net =~ /(.*)\[([0-9]+):([0-9]+)\]$/) {
		my ($n, $u, $d) = ($1, $2, $3);
		my @bits = split //, $value;
		my $extbit = $bits[0] eq "1" ? "0" : $bits[0];
		unshift @bits, $extbit while $#bits < $u - $d;
		set_state_bit($state, $n, $u--, shift @bits) while $u >= $d;
		return;
	}

	$state->{$net} = $value;
	$signal_sync{$net} = 1 unless exists $signal_sync{$net};
	$touched_nets{$net} = 1;
}

sub cmp_signal($$)
{
	my ($a, $b) = @_;
	return 1 if $a eq $b;

	my @a = split //, $a;
	my @b = split //, $b;

	my $trail_a = $#a < 0 ? '-' : $a[0] eq '1' ? '0' : $a[0];
	my $trail_b = $#b < 0 ? '-' : $b[0] eq '1' ? '0' : $b[0];

	unshift @a, $trail_a while $#a < $#b;
	unshift @b, $trail_b while $#b < $#a;

	for (my $i = 0; $i <= $#a; $i++) {
		next if $a[$i] eq "-" || $b[$i] eq "-";
		return 0 if $a[$i] ne "x" && $a[$i] ne $b[$i];
	}

	return 1;
}

# Message objects: .text, .time, .signal, .sync
my @messages;

print "Comparing vcd data..\n";
for my $time (sort { $a <=> $b } keys %times)
{
	%touched_nets = ();
	for my $net (keys %{$data_gold->{$time}}) {
		set_state(\%state_gold, $net, $data_gold->{$time}->{$net});
	}
	for my $net (keys %{$data_gate->{$time}}) {
		set_state(\%state_gate, $net, $data_gate->{$time}->{$net});
	}
	for my $net (sort keys %touched_nets) {
		my ($stgo, $stga) = ('-', '-');
		$stgo = $state_gold{$net} if exists $state_gold{$net};
		$stga = $state_gate{$net} if exists $state_gate{$net};
		if (cmp_signal($stgo, $stga)) {
			next if $signal_sync{$net};
			my $message = { };
			$message->{text} = sprintf "%-10s %-20d %-*s %-*s %s\n", "<sync>", $time, $signal_maxlen, $stgo, $signal_maxlen, $stga, $net;
			$message->{time} = $time;
			$message->{signal} = $net;
			$message->{sync} = 1;
			push @messages, $message;
			$signal_sync{$net} = 1;
		} else {
			my $message = { };
			$message->{text} = sprintf "%-10d %-20d %-*s %-*s %s\n", $diffcount, $time, $signal_maxlen, $stgo, $signal_maxlen, $stga, $net;
			$message->{time} = $time;
			$message->{signal} = $net;
			$message->{sync} = 0;
			push @messages, $message;
			$signal_sync{$net} = 0;
			$diffcount++;
		}
	}
}

print "Found $diffcount differences.\n";

if ($opt_delay > 0) {
	my %per_net_history;
	my $removed_diff_count = 0;
	for (my $i = 0; $i <= $#messages; $i++) {
		my $message = $messages[$i];
		$message->{deleted} = 0;
		$per_net_history{$message->{signal}} = [ ] unless exists $per_net_history{$message->{signal}};
		if ($message->{sync}) {
			my $deleted_all = 1;
			for my $j (@{$per_net_history{$message->{signal}}}) {
				my $m = $messages[$j];
				if ($m->{time} + $opt_delay >= $message->{time}) {
					$m->{deleted} = 1;
					$removed_diff_count++;
				} else {
					$deleted_all = 0;
				}
			}
			$message->{deleted} = 1 if $deleted_all;
			$per_net_history{$message->{signal}} = [ ];
		} else {
			push @{$per_net_history{$message->{signal}}}, $i;
		}
	}
	my @new_messages;
	for my $message (@messages) {
		push @new_messages, $message unless $message->{deleted};
	}
	@messages = @new_messages;
	printf "Removed %d differences using delay filtering.\n", $removed_diff_count;
	$diffcount = $diffcount - $removed_diff_count;
}

if ($#messages >= 0) {
	printf "\n%-10s %-20s %-*s %-*s %s\n", "count", "time", $signal_maxlen, "gold", $signal_maxlen, "gate", "net" if $diffcount++ == 0;
	for (my $i = 0; $i <= $#messages; $i++) {
		printf "%s", $messages[$i]->{text};
	}
}

exit ($diffcount > 0 ? 1 : 0);