Repository navigation
Expand file tree
/
Copy pathvcdcat.py
More file actions
executable file
·66 lines (57 loc) · 2.04 KB
/
Copy pathvcdcat.py
File metadata and controls
executable file
·66 lines (57 loc) · 2.04 KB
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
#!/usr/bin/env python3
"""Print signals of a VCD file once per rising clock edge (values before the edge).
vcdcat.py wave.vcd clk sig1 sig2 ... [--from N] [--count N]
Signal names are matched as suffixes of the full hierarchical name.
"""
import argparse
def main():
ap = argparse.ArgumentParser()
ap.add_argument("vcd")
ap.add_argument("clock")
ap.add_argument("signals", nargs="+")
ap.add_argument("--from", dest="start", type=int, default=0)
ap.add_argument("--count", type=int, default=40)
a = ap.parse_args()
ids, names, scope = {}, {}, []
with open(a.vcd) as f:
for line in f:
t = line.split()
if not t:
continue
if t[0] == "$scope":
scope.append(t[2])
elif t[0] == "$upscope":
scope.pop()
elif t[0] == "$var":
names.setdefault(t[3], ".".join(scope + [t[4]]))
elif t[0] == "$enddefinitions":
break
def find(s):
for code, n in names.items():
if n == s or n.endswith("." + s):
return code
raise SystemExit("no signal " + s)
clk = find(a.clock)
want = [find(s) for s in a.signals]
val = {}
cycle = 0
print("cycle " + " ".join(a.signals))
for line in f:
line = line.strip()
if not line or line[0] == "#":
continue
if line[0] in "bB":
v, code = line[1:].split()
else:
v, code = line[0], line[1:]
if code == clk and v == "1":
if a.start <= cycle < a.start + a.count:
print("%5d " % cycle + " ".join(
("%x" % int(val.get(w, "0"), 2)) if set(val.get(w, "0")) <= {"0", "1"} else val.get(w, "x")
for w in want))
cycle += 1
if cycle >= a.start + a.count:
break
val[code] = v
if __name__ == "__main__":
main()