-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspice2verilog.py
executable file
·87 lines (69 loc) · 2.28 KB
/
spice2verilog.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
#! /usr/bin/env python3
from more_itertools import peekable
class Netlist():
def __init__(self):
self.cells = set()
self.wires = set()
self.instances = []
self.args = None
def to_escaped_id(self, name):
if name in self.args:
return name
return "\\" + name + " "
def parse_instantiation(self, line):
name = line[-1]
ident = line[0]
args = line[1:-1]
assert line[1] in ["VGND", "VPWR"]
assert line[2] in ["VGND", "VPWR"]
assert name in self.cells
for signal in args:
self.wires.add(signal)
if not ("decap" in name or "diode" in name):
self.instances.append((name, ident, args))
def parse_subckt(self):
try:
self.s.peek()
except StopIteration:
return True
subckt = next(self.s).split()
if len(subckt) == 0 or subckt[0].startswith("*"):
return
assert subckt[0].lower() == ".subckt"
cell_name = subckt[1]
if cell_name != "challenge":
assert subckt[2] in ["VGND", "VPWR"]
assert subckt[3] in ["VGND", "VPWR"]
self.cells.add(cell_name)
while not next(self.s).lower().startswith(".ends"): pass
return
self.args = subckt[2:]
while (line := next(self.s).split())[0].lower() != ".ends":
self.parse_instantiation(line)
def parse_spice(self, fn):
with open(fn) as f:
self.s = peekable(iter(f.readlines()))
while not self.parse_subckt():
pass
def to_verilog(self):
v = "module challenge(" + ", ".join(self.args) + ");\n"
v += "".join(
("output " if "led" in a else "input ") +
"wire " + a + ";\n"
for a in self.args
)
v += "\n"
v += "".join(
"wire " + self.to_escaped_id(w) + ";\n"
for w in self.wires
if not w in self.args
)
v += "".join(
name + " " + ident + "(" + ", ".join(map(self.to_escaped_id, args)) + ");\n"
for (name, ident, args) in self.instances
)
v += "endmodule\n"
return v
n = Netlist()
n.parse_spice("ext/challenge.spice")
print(n.to_verilog())