This repository has been archived by the owner on Jul 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
vhdl.py
326 lines (263 loc) · 7.3 KB
/
vhdl.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
VHDL Classes
============
.. moduleauthor:: Jordi Masip <[email protected]>
"""
class VHDL(object):
def __init__(self):
self._libs = []
self._entities = {}
self._archs = {}
def setEntity(self, ent):
if isinstance(ent, Entity):
self._entities[ent.getName()] = ent
def getEntities(self):
return self._entities.values()
def getEntityByName(self, ent_name):
if ent_name in self._entities.keys():
return self._entities[ent_name]
return False
def setArchitecture(self, arch):
if isinstance(arch, Architecture):
self._archs[arch.getName()] = arch
def getArchitectures(self):
return self._archs.values()
def getArchitectureByName(self, arch_name):
if arch_name in self._arch.keys():
return self._arch[arch_name]
return False
def addLibrary(self, lib):
if isinstance(lib, Library):
if lib not in self._libs:
self._libs += [lib]
return True
return False
def removeLibrary(self, lib):
try:
self._libs.remove(lib)
return True
except Exception:
return False
def getLibs(self):
"""
Returns a list of Libraries
"""
return self._libs
def __str__(self):
return "\n".join([str(l) for l in self._libs])
class Library(object):
def __init__(self, value):
self._lib = value
self._packages = []
def addPackage(self, package_name):
if self._lib + "." + package_name not in self._packages:
self._packages += [self._lib + "." + package_name]
else:
print "error: el paquet '{0}' ja es troba a la library".format(package_name)
def getPackages(self):
return self._packages
def getName(self):
return self._lib
def __eq__(self, other):
if isinstance(other, Library):
return self._lib == other.getName()
return False
def __str__(self):
return "<Library %s>" % self._lib
class Entity(object):
def __init__(self, name):
self._name = name
self._port = {}
def getName(self):
return self._name
def setPortList(self, p):
if isinstance(p, PortList):
self._port = p.getPorts()
return True
return False
def getPorts(self):
return self._port
def __str__(self):
return "<Entity %s>" % self._name
def __eq__(self, other):
return self._name == other.getName() if isinstance(other, Entity) else False
class Signal(object):
_obj_name = "signal"
_name = ""
_type = ""
_value = ""
def __init__(self, name, type):
self.setName(name)
self.setType(type)
def getName(self):
return self._name
def setName(self, n):
if isinstance(n, str):
self._name = n
else:
print "error: el nom '%s' ha de ser una cadena" % self._obj_name
def setValue(self, val):
self._value = val
def getValue(self):
return self._value
def getType(self):
return self._type
def setType(self, t):
if isinstance(t, str):
self._type = t
else:
print "error: el tipus '%s' ha de ser una cadena" % self._obj_name
def __str__(self):
if self._value == "":
return "<%s %s : %s>" % (self._obj_name.capitalize(), self._name, self._type)
else:
return "<%s %s : %s := %s>" % (self._obj_name.capitalize(), self._name, self._type, self._value)
def __eq__(self, other):
if isinstance(other, Signal):
return self._name == other.getName() and self._type == other.getType()
return False
class SignalList(object):
def __init__(self, signal_str):
self._signals = self._getSignalFromString(signal_str.strip())
def getSignals(self):
return self._signals
def _getSignalFromString(self, s):
signals = {}
try:
for signal in s.split(";"):
signal = signal.strip()
if signal == "":
break
isSignalWithAssignation = ":=" in signal
if isSignalWithAssignation:
left, assignation = signal.split(":=")
else:
left = signal
if ":" not in left:
print "atenció: s'ha ignorat la línia '%s'" % left.strip()
continue
port_prefix, t = left.strip().split(":")
port_prefix = port_prefix.strip()
for i in range(len(port_prefix)):
if port_prefix[i] == " ":
variable_type = port_prefix[:i].strip()
port_prefix = port_prefix[i+1:].strip()
break
else:
print "error: el senyal '%s' és invàlid" % signal
return signals
if variable_type == "type":
continue
t = t.strip()
if "," in port_prefix:
for n in port_prefix.split(","):
n = n.strip()
signal = Signal(n, t)
if isSignalWithAssignation:
signal.setValue(assignation)
signals[n] = signal
else:
signal = Signal(port_prefix, t)
if isSignalWithAssignation:
signal.setValue(assignation)
signals[port_prefix] = signal
except Exception as e:
print "error: no es pot llegir el 'signal': %s" % e
return signals
class Port(Signal):
_obj_name = "port"
_port_type = "in"
def __init__(self, name, port_type, t):
Signal.__init__(self, name, t)
self.setPortType(port_type)
def setPortType(self, t):
if t in ["in", "out", "inout", "buffer", "linkage"]:
self._port_type = t
else:
print "error: '%s' és un port_type invàlid pel %s '%s'" % (str(t), self._obj_name, self._name)
def getPortType(self):
return self._port_type
def __str__(self):
return "<%s %s : %s %s>" % (self._obj_name.capitalize(), self._name, self._port_type, self._type)
def __eq__(self, other):
if isinstance(other, Port):
return self._name == other.getName() and self._type == other.getType() and self._port_type == other.getPortType()
return False
class PortList(object):
def __init__(self, port_str):
self._ports = self._getPortFromString(port_str.strip())
if self._ports == None:
self._ports = {}
def getPorts(self):
return self._ports
def _getPortFromString(self, s):
ports = {}
counting = False
skip_times, bracket_count = 0, 0
between_port = ""
for i in range(len(s)):
if skip_times > 0:
skip_times -= 1
continue
elif s[i:i+4] == "port":
counting = True
skip_times = 3
continue
elif s[i] == "(":
bracket_count += 1
elif s[i] == ")":
bracket_count -= 1
if bracket_count == 0:
break
if counting:
between_port += s[i]
try:
port = between_port.strip()[1:].replace("\n", "")
for p in port.split(";"):
port_name, t = p.split(":")
port_name = port_name.strip()
t = t.strip()
for i in range(len(t)):
if t[i] == " ":
port_type = t[:i].strip()
variable_type = t[i+1:].strip()
break
if "," in port_name:
for n in port_name.split(","):
n = n.strip()
ports[n] = Port(n, port_type, variable_type)
else:
ports[port_name] = Port(port_name, port_type, variable_type)
except Exception as e:
print "error: sembla que el port està malformat"
return ports
class Architecture(object):
_begin = ""
_name = ""
_archOf = None
def __init__(self, name, ent):
if isinstance(name, str):
self._name = name
else:
print "error: aquesta arquitectura té un nom invàlid"
if isinstance(ent, Entity):
self._archOf = ent
else:
print "error: l'arquitectura '%s' té una entitat invàlida" % self._name
def getName(self):
return self._name
def getEntity(self):
return self._archOf
def setSignalList(self, sl):
if isinstance(sl, SignalList):
self._signals = sl.getSignals()
return True
return False
def getSignalList(self):
return self._signals
def getEntity(self):
return self._archOf
def __str__(self):
return "<Architecture %s of %s>" % (self._name, self._archOf.getName())