-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcd_parse.py
55 lines (49 loc) · 1.81 KB
/
vcd_parse.py
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
import collections
Signal = collections.namedtuple('Signal', 'name bits type sym')
def parse(src):
dump_section = object()
def value_line(l):
if l[0] == 'b' or l[0] == 'B':
bv, sym = l.split()
return sym, int(bv[1:], 2)
else:
assert len(l) == 2, l
return l[1], int(l[0], 2)
def values_dict(sym_vals, sigs):
return {sig.name: sym_vals[sig.sym] for sig in sigs}
section = None
contents = []
signals = []
timestamp = None
current_values = {}
for line in src:
if section in ('dumpvars', dump_section):
if line == '$end\n':
section = dump_section
else:
if line.startswith('#'):
if timestamp is not None:
yield timestamp, values_dict(current_values, signals)
timestamp = int(line[1:])
else:
sym, val = value_line(line.strip())
current_values[sym] = val
else:
for t in line.strip().split():
if t == '$end':
if section == 'var':
type, bits, sym, name = contents
signals.append(Signal(name=name, bits=bits, type=type, sym=sym))
section = dump_section if section == 'dumpvars' else None
contents = []
elif section in ('date', 'version', 'comment'):
pass
elif t[0] == '$' and len(t) > 1: # signals may have symbol '$'
section = t[1:]
else:
contents.append(t)
yield timestamp, values_dict(current_values, signals)
if __name__ == '__main__':
import sys
for t, v in parse(sys.stdin):
print(t, v)