-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
107 lines (87 loc) · 2.69 KB
/
main.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
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
import json
import os
import argparse
from collections import namedtuple
import re
Node = namedtuple('Node', 'node_name sig_name idx wave value edge')
Edge = namedtuple('Edge', 'type_ n1 n2 delay')
mapping = {"1" : "$rise" , "0": "$fell" , ".": "$stable"}
edge_mapping = {"-~>": "curve", "->": "hor", "-|>" : "vert"}
def find_node(nodes, name):
for n in nodes:
if name == n.node_name:
return n
return None
def get_last_value(wave):
v = None
for i in reversed(wave):
if i != ".":
v = i
break
return v
def main():
# args
parser = argparse.ArgumentParser()
parser.add_argument('wave_file', help='Path to Wave')
args = parser.parse_args()
# load wavedrom
fh = open(args.wave_file)
wd = json.load(fh)
# Parse signals
nodes = []
for sig in wd["signal"]:
sig_name = sig["name"]
wave = sig["wave"]
node = sig["node"]
for i, n in enumerate(node):
# Parse labels
if n != ".":
edge = mapping[wave[i]]
assert (edge in mapping.values())
"""
in case of 1 or 0, that is the correct value
in case of ".", i need to get the last non-"." value
"""
value = None
if wave[i] != ".":
value = wave[i]
else:
value = get_last_value(wave[0:i])
assert(value is not None)
nn = Node(n, sig_name, i , wave[i], value, edge)
nodes.append(nn)
# Parse edges for assertions
edges = []
for e in wd["edge"]:
ed = None
# a-~>b DELAY
# a |-> ##[DELAY] b
result = re.match("([a-zA-Z])-~>([a-zA-Z]) (.*)", e)
if result:
tokens = result.groups()
ed = Edge("timed", *tokens)
# a-|>b
# a |-> b
result = re.match("([a-zA-Z])-\|>([a-zA-Z])", e)
if result:
tokens = result.groups()
ed = Edge("zero-delay", *tokens, None)
edges.append(ed)
"""
TODO
$rose(b) |-> ##[3:5] $past(a)
"""
"""
Generation
"""
for ed in edges:
if ed.type_ == "timed":
x1 = find_node(nodes, ed.n1)
x2 = find_node(nodes, ed.n2)
print(f"{x1.edge}({x1.sig_name}) |-> ##[{ed.delay}] {x2.edge}({x2.sig_name}); ")
if ed.type_ == "zero-delay":
x1 = find_node(nodes, ed.n1)
x2 = find_node(nodes, ed.n2)
print(f"{x1.edge}({x1.sig_name}) |-> ({x2.edge}({x2.sig_name}); && {x2.sig_name} == {x2.value} ")
if __name__ == "__main__":
main()