This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.py
61 lines (45 loc) · 1.46 KB
/
util.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
# Disable pylint's "your name is too short" warning.
# pylint: disable=C0103
"""
This module provides various global utilities.
"""
import sys
from nmigen.back import rtlil
from nmigen.hdl import Fragment
if sys.version_info < (3, 8):
print("Python 3.8 or above is required")
sys.exit(1)
def all_true(*args):
cond = 1
for arg in args:
cond &= arg.bool()
return cond
def main(cls, filename="toplevel.il"):
"""Runs a file in simulate or generate mode.
Add this to your file:
from util import main
if __name__ == "__main__":
main(YourClass)
Then, you can run the file in simulate or generate mode:
python <file.py> sim will run YourClass.sim and output to whatever vcd
file you wrote to.
python <file.py> gen will run YourClass.formal and output in RTLIL format
to toplevel.il. You can then formally verify using
sby -f <file.sby>.
"""
if len(sys.argv) < 2 or (sys.argv[1] != "sim" and sys.argv[1] != "gen" and sys.argv[1] != "rtl"):
print(f"Usage: python {sys.argv[0]} sim|gen|rtl")
sys.exit(1)
if sys.argv[1] == "sim":
cls.sim()
return
design = None
ports = []
if sys.argv[1] == "rtl":
design, ports = cls.toRTL()
else:
design, ports = cls.formal()
fragment = Fragment.get(design, None)
output = rtlil.convert(fragment, ports=ports)
with open(filename, "w") as f:
f.write(output)