-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetlists.py
More file actions
269 lines (228 loc) · 8.13 KB
/
netlists.py
File metadata and controls
269 lines (228 loc) · 8.13 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/python
#
# A pin is one physical connection of a component.
# It has a name (string) and a reference to it's component (object reference).
#
class Pin:
def __init__(self, component=None, name=""):
self.component = component
self.name = name
def getName(self):
return self.name
def getComponent(self):
return self.component
def __str__(self):
return self.getComponent().getDesignator() + "," + self.getName()
#
# Electrical component, as found on a PCB
# has a designator (string) and a list of pins,
# maybe also a description (string) and a footprint (string)
#
class Component:
def __init__(self, designator="", description="", footprint=""):
self.designator = designator.strip()
if self.designator == "*":
print("Warning: Created component with wildcard designator.")
self.description = description.strip()
self.footprint = footprint.strip()
self.pins = []
def getPinByNumber(self, number):
if len(self.pins) < number:
return None
return self.pins[number-1]
def getPinByName(self, name):
for pin in self.pins:
if str(pin.getName()) == str(name):
return pin
return None
def createPinFromName(self, name):
# Does a pin with that name already exist?
p = self.getPinByName(name)
if p is None:
p = Pin(component=self, name=name)
self.pins += [p]
return p
def getDesignator(self):
return self.designator
def getDescription(self):
return self.description
def getPins(self):
return self.pins
#
# A net is a list of connected pins.
# It has a name i.e. label and a list of pins (object references).
#
class Net:
def __init__(self, label=""):
self.label = label
self.pins = []
def getLabel(self):
return self.label
def isPower(self):
# TODO: Match regexp to detect power net label
return self.label in ["GND", "DGND", "1V2", "1.2V", "3V3", "3.3V", "5V", "12V", "HV+", "HV-", "DC+", "DC-"]
def addPin(self, pin):
self.pins += [pin]
def getPins(self):
return self.pins
#
# Return the first pin on this net, which belongs
# to a component with the given designator
#
def getPin(self, componentDesignator=None):
if componentDesignator is None:
return None
componentDesignator = componentDesignator.upper().strip()
for pin in self.pins:
if pin.getComponent().getDesignator().upper() == componentDesignator:
return pin
return None
def __str__(self):
return self.getLabel()
#
# Convert string for compatibility with regular expressions
#
def cleanupEncoding(s):
return str(s.encode("ascii", "replace")).replace("\\n", "\n")
#
# A list of components and nets
# (both stored as a object references).
#
class Netlist:
def __init__(self):
self.components = []
self.nets = []
#
# Only reads the text from a file.
# A format-specific method still needs
# to take care of parsing afterwards.
#
def readFromFile(self, filename):
f = open(filename, "r", encoding="iso8859_15")
# f = open(filename, "r")
self.text = cleanupEncoding(f.read())
# print(self.text)
f.close()
#
# Return all components in this netlist
#
def getComponents(self):
return self.components
#
# Return all nets in this netlist
#
def getNets(self):
return self.nets
#
# Returns the net with the given label if present, else None
#
def getNet(self, netlabel):
for net in self.getNets():
if net.getLabel() == netlabel:
return net
return None
#
# Returns true, if the given net is present in this netlist
#
def hasNet(self, netlabel):
return not (self.getNet(netlabel) is None)
#
# Return the component with the given designator (not case-sensitive)
#
def getComponentByDesignator(self, designator):
designator = designator.upper().strip()
for component in self.components:
if component.getDesignator().upper() == designator:
return component
print("Error: Component not found: " + designator)
return None
#
# Return the component with the given description (not case-sensitive)
#
def getComponentByDescription(self, description):
description = description.upper().strip()
for component in self.components:
if component.getDescription().upper() == description:
return component
print("Error: Component not found: " + description)
return None
#
# Return the first component containing the given keyword
# in either description or designator (not case-sensitive)
#
def getComponentByKeyword(self, keyword):
keyword = keyword.upper().strip()
component = None
for component in self.getComponents():
if (component.getDesignator().upper().find(keyword) > -1) \
or (component.getDescription().upper().find(keyword) > -1):
return component
print("Error: Component not found.")
return None
#
# A convenience wrapper for the above methods
#
def getComponent(self, designator=None, description=None, keyword=None):
if not (designator is None):
return self.getComponentByDesignator(designator)
if not (description is None):
return self.getComponentByDescription(description)
if not (keyword is None):
return self.getComponentByKeyword(keyword)
return None
#
# Returns the net (object reference) of the given pin (object reference)
#
def getNetOnPin(self, pin, debug=False):
if pin is None:
if debug:
print("Error: Illegal argument None given to getNetOnPin().")
return None
for net in self.getNets():
for p in net.getPins():
if p == pin:
if debug:
print("Found net {:s} for component {:s}, pin {:s}.".format(net.getLabel(), p.getComponent().getDesignator(), p.getName()))
return net
if debug:
print("Error: Unable to detect the net connected to component {:s}, pin {:s}.".format(pin.getComponent().getDesignator(), pin.getName()))
return None
#
# Extract all connections between two components (except power pins)
# Requires the two components' designators and
# returns a list of Net objects
#
def elaborateComponentConnections(self, designator1, designator2, debug=False):
#
# Iterate over all netlist nets and extract the ones with pins matching the given designators
#
connectedNets = []
for net in self.getNets():
pin1 = net.getPin(componentDesignator=designator1)
if pin1 is None:
# Component 1 is not connected to this net
continue
pin2 = net.getPin(componentDesignator=designator2)
if pin2 is None:
# Component 2 is not connected to this net
continue
if net.isPower():
# Disregard non-signal net
print("Info: Net {:s} is a power net. Skipping.".format(net.getLabel()))
continue
if len(net.getPins()) < 2:
# Disregard unconnected nets
print("Info: Net {:s} is not connected anywhere. Skipping.".format(net.getLabel()))
continue
if debug:
print("Component {:s} pin {:s} is connected to component {:s} pin {:s} on net {:s}." \
.format(
designator1,
pin1.getName(),
designator2,
pin2.getName(),
net.getLabel()
)
)
connectedNets += [(net, pin1, pin2)]
return connectedNets