-
Notifications
You must be signed in to change notification settings - Fork 0
/
verilog.py
170 lines (140 loc) · 5.8 KB
/
verilog.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
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
#!/usr/bin/python3
#
# This file allows for parsing a flattened Verilog module
# as it is generated by Yosys after synthesis
# and for evaluation of simple assertions
#
import re
from netlists import Netlist
#
# A list of submodules accepted as primitives
#
primitives = [
"SB_LUT4",
"SB_DFFES",
"SB_DFFER",
"SB_PLL40_CORE"
]
#
# Some regular expressions to help us parsing
#
pattern_module = "module[\t ]+[a-zA-Z0-9\_]+[\t ]*\(([^\)]+)\);"
regex_module = re.compile(pattern_module)
pattern_signal = "[a-zA-Z0-9\_\.\[\:\]]+"
pattern_signal_or_literal = "[a-zA-Z0-9\_\.\[\:\]\']+"
regex_literal_decimal = re.compile("[0-9]+")
regex_literal_hexadecimal = re.compile("[0-9]+\'h[0-9a-fA-FxX]+")
regex_literal_binary = re.compile("[0-9]+\'b[0-1xX]+")
regex_assign = re.compile("[\t ]*assign[\t ]+[\\\\]*(" + pattern_signal + ")[\t ]*=[\t ]*(" + pattern_signal_or_literal + ")[\t ]*;[\t ]*\n")
#
# A class to hold static assertion methods
#
class assertion:
def result(success=False, message="<No message not specified>"):
return {"success": success, "message": message}
def isLiteral(netlist=None, expression=""):
if regex_literal_decimal.fullmatch(expression) \
or regex_literal_hexadecimal.fullmatch(expression) \
or regex_literal_binary.fullmatch(expression):
return assertion.result(success=True)
return assertion.result(success=False)
def netExists(netlist, net):
if netlist.hasPort(net):
return assertion.result(success=True, message="{:s} is a module port.".format(net))
assign = netlist.findAssign(net)
if assign is None:
return assertion.result(success=False, message="Net {:s} is not part of the design.".format(net))
return assertion.result(success=True, message="Net {:s} is part of the design.".format(net))
def netIsConstant(netlist, net):
#
# If it is constant, then there must be
# a line in the form: assign netname = literal;
#
assign = netlist.findAssign(net)
if assign is None:
return assertion.result(success=True, message="Net {:s} is not driven at all (and therefore constant).".format(net))
if assertion.isLiteral(expression=assign["rhs"])["success"]:
return assertion.result(success=True, message="Net {:s} is driven by a constant.".format(net))
return assertion.result(success=False, message="Net {:s} is driven by something but not by a constant.".format(net))
def netIsNotConstant(netlist, net):
assign = netlist.findAssign(net)
if assign is None:
return assertion.result(success=False, message="Net {:s} is not driven at all.".format(net))
if assertion.isLiteral(expression=assign["rhs"])["success"]:
return assertion.result(success=False, message="Net {:s} is driven by a constant: {:s}".format(net, assign["rhs"]))
return assertion.result(success=True, message="Net {:s} is driven by something but not by a constant.".format(net))
#
# A class to hold all relevant information about a flattened Verilog file
#
# class File(Netlist):
class File():
def __init__(self, filename):
f = open(filename, "r")
self.content = f.read()
f.close()
self.lines = self.content.split("\n")
self.parsePorts()
self.parseAssigns()
# Parse all ports of the first module in the given file
def parsePorts(self):
self.ports = []
modules = re.findall(regex_module, self.content)
if len(modules) < 1:
return;
self.ports = modules[0].replace(" ", "").split(",")
# Find a module port
def hasPort(self, portname):
# Remove the indices
i = portname.find("[")
if i > -1:
portname = portname[:i].strip()
return (portname in self.ports)
# Parse all assign statements into an array
def parseAssigns(self):
results = re.findall(regex_assign, self.content)
# print(results)
self.assigns = []
for result in results:
lhs = result[0]
rhs = result[1]
# print("{:s} - {:s}".format(lhs, rhs))
# print("assign {:s} = {:s};".format(lhs, rhs))
assign = {"lhs": lhs, "rhs": rhs}
self.assigns += [assign]
# Find an assign statement for the given netlabel
def findAssign(self, netlabel):
netlabel = netlabel.lower()
for assign in self.assigns:
if assign["lhs"].lower() == netlabel:
return assign
return None
#
# A class to hold a list of assertions a Verilog netlist must fulfill
#
class Assertions():
def __init__(self):
self.assertions = []
def append(self, assertion, arg0=""):
self.assertions += [[assertion, arg0]]
def apply(self, netlist):
succeeded = 0
failed = 0
fatal = 0
for assertion in self.assertions:
result = self.applyAssertion(netlist, assertion)
if result["success"]:
succeeded += 1
else:
failed += 1
if ("fatal" in result.keys()) and result["fatal"]:
fatal += 1
return {"succeeded": succeeded, "failed": failed, "fatal": fatal}
def applyAssertion(self, netlist, assertion):
result = assertion[0](netlist, assertion[1])
if result["success"]:
# print("[SUCCESS] {:s}(\"{:s}\"): {:s}".format(str(assertion[0]), str(assertion[1]), result["message"]))
print("[SUCCESS] {:s}".format(result["message"]))
else:
# print("[FAILED] {:s}(\"{:s}\"): {:s}".format(str(assertion[0]), str(assertion[1]), result["message"]))
print("[FAILED] {:s}".format(result["message"]))
return result