1 |
4 |
dargor |
#!/usr/bin/env python
|
2 |
|
|
# -*- coding: utf-8 -*-
|
3 |
|
|
|
4 |
|
|
#
|
5 |
|
|
# NoC RTL support in MyHDL
|
6 |
|
|
# This module adds support for mixed model and RTL descriptions in MyHDL
|
7 |
|
|
#
|
8 |
|
|
# Author: Oscar Diaz
|
9 |
|
|
# Version: 0.2
|
10 |
|
|
# Date: 01-06-2012
|
11 |
|
|
|
12 |
|
|
#
|
13 |
|
|
# This code is free software; you can redistribute it and/or
|
14 |
|
|
# modify it under the terms of the GNU Lesser General Public
|
15 |
|
|
# License as published by the Free Software Foundation; either
|
16 |
|
|
# version 2.1 of the License, or (at your option) any later version.
|
17 |
|
|
#
|
18 |
|
|
# This code is distributed in the hope that it will be useful,
|
19 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
21 |
|
|
# Lesser General Public License for more details.
|
22 |
|
|
#
|
23 |
|
|
# You should have received a copy of the GNU Lesser General Public
|
24 |
|
|
# License along with this library; if not, write to the
|
25 |
|
|
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
26 |
|
|
# Boston, MA 02111-1307 USA
|
27 |
|
|
#
|
28 |
|
|
|
29 |
|
|
#
|
30 |
|
|
# Changelog:
|
31 |
|
|
#
|
32 |
|
|
# 03-03-2011 : (OD) initial release
|
33 |
|
|
# 01-06-2012 : (OD) big refactorization
|
34 |
|
|
#
|
35 |
|
|
|
36 |
|
|
"""
|
37 |
|
|
===============================
|
38 |
|
|
NoCmodel RTL MyHDL support
|
39 |
|
|
===============================
|
40 |
|
|
|
41 |
|
|
This module extends 'noc_tbm_base' class for RTL descriptions in MyHDL
|
42 |
|
|
|
43 |
|
|
* Class 'noc_rtl_myhdl_base'
|
44 |
|
|
"""
|
45 |
|
|
|
46 |
|
|
import myhdl
|
47 |
|
|
|
48 |
|
|
from nocmodel import *
|
49 |
|
|
from noc_tbm_base import *
|
50 |
|
|
|
51 |
|
|
class noc_rtl_myhdl_base(noc_tbm_base):
|
52 |
|
|
"""
|
53 |
|
|
Extended class for a MyHDL description of a NoC router.
|
54 |
|
|
|
55 |
|
|
This class adds support for behavioral - RTL mixed modeling and
|
56 |
|
|
full RTL modeling. It is meant to add in NoC objects through
|
57 |
|
|
multiple inheritance mechanism
|
58 |
|
|
|
59 |
|
|
Features:
|
60 |
|
|
* Keep a list of (Intercon type) interface objects, each object is
|
61 |
|
|
indexed by a port index and each object stores a list of signals
|
62 |
|
|
dict: "interface_objects"
|
63 |
|
|
* Keep a list of external interface objects. Each object is a set of
|
64 |
|
|
signals (use "signalset" class), and are accessed by any python
|
65 |
|
|
object (a string is recommended).
|
66 |
|
|
dict: "external_objects"
|
67 |
|
|
* Keep a list of internal MyHDL signals, and provides correct references
|
68 |
|
|
dict: "internal_signals"
|
69 |
|
|
"""
|
70 |
|
|
# Interface objects methods
|
71 |
|
|
def add_interface(self, port_idx, object_ref=None):
|
72 |
|
|
"""
|
73 |
|
|
Add a new interface
|
74 |
|
|
Arguments:
|
75 |
|
|
"""
|
76 |
|
|
if not hasattr(self, "interface_objects"):
|
77 |
|
|
self._new_interface_objects()
|
78 |
|
|
|
79 |
|
|
self.interface_objects[port_idx] = object_ref
|
80 |
|
|
return object_ref
|
81 |
|
|
|
82 |
|
|
def get_interface_signal(self, port_idx, signalname):
|
83 |
|
|
"""
|
84 |
|
|
Try to get a signal reference from the interface object indexed by
|
85 |
|
|
"port_idx". Raise an Exception if not found.
|
86 |
|
|
Arguments:
|
87 |
|
|
* port_idx:
|
88 |
|
|
* signalname:
|
89 |
|
|
Returns:
|
90 |
|
|
the MyHDL signal reference
|
91 |
|
|
"""
|
92 |
|
|
if not hasattr(self, "interface_objects"):
|
93 |
|
|
self._new_interface_objects()
|
94 |
|
|
|
95 |
|
|
if port_idx not in self.interface_objects:
|
96 |
|
|
raise ValueError("%s : Interface '%s' not found." % (repr(self), repr(port_idx)))
|
97 |
|
|
|
98 |
|
|
# check if object is list-dict type or a class with signal attributes
|
99 |
|
|
if isinstance(self.interface_objects[port_idx], (tuple, list, dict)):
|
100 |
|
|
# indexing lookup
|
101 |
|
|
if signalname not in self.interface_objects[port_idx]:
|
102 |
|
|
raise AttributeError("%s : Signal '%s' from Interface %s not found." % (repr(self), signalname, repr(port_idx)))
|
103 |
|
|
|
104 |
|
|
return self.interface_objects[port_idx][signalname]
|
105 |
|
|
else:
|
106 |
|
|
# attribute lookup
|
107 |
|
|
try:
|
108 |
|
|
return getattr(self.interface_objects[port_idx], signalname)
|
109 |
|
|
except:
|
110 |
|
|
raise AttributeError("%s : Signal '%s' from Interface %s not found." % (repr(self), signalname, repr(port_idx)))
|
111 |
|
|
|
112 |
|
|
def get_interface_all_signals(self, port_idx):
|
113 |
|
|
"""
|
114 |
|
|
Return a dict with all signals references from interface
|
115 |
|
|
Arguments:
|
116 |
|
|
* port_idx:
|
117 |
|
|
Returns:
|
118 |
|
|
dict with references to all signals in interface
|
119 |
|
|
"""
|
120 |
|
|
if not hasattr(self, "interface_objects"):
|
121 |
|
|
self._new_interface_objects()
|
122 |
|
|
return {}
|
123 |
|
|
|
124 |
|
|
if port_idx not in self.interface_objects:
|
125 |
|
|
raise ValueError("%s : Interface '%s' not found." % (repr(self), repr(port_idx)))
|
126 |
|
|
|
127 |
|
|
if isinstance(self.interface_objects[port_idx], dict):
|
128 |
|
|
return self.interface_objects[port_idx]
|
129 |
|
|
elif isinstance(self.interface_objects[port_idx], (tuple, list)):
|
130 |
|
|
return dict([(k, v) for k, v in enumerate(self.interface_objects[port_idx])])
|
131 |
|
|
else:
|
132 |
|
|
# search through all attributes
|
133 |
|
|
retval = {}
|
134 |
|
|
for attrname in dir(self.interface_objects[port_idx]):
|
135 |
|
|
attr = getattr(self.interface_objects[port_idx], attrname)
|
136 |
|
|
if isinstance(attr, myhdl.SignalType):
|
137 |
|
|
retval[attrname] = attr
|
138 |
|
|
return retval
|
139 |
|
|
|
140 |
|
|
# External interface objects methods
|
141 |
|
|
def add_external_interface(self, index, object_ref=None):
|
142 |
|
|
if not hasattr(self, "external_objects"):
|
143 |
|
|
self._new_external_objects()
|
144 |
|
|
|
145 |
|
|
if index is None:
|
146 |
|
|
raise ValueError("Index cannot be None.")
|
147 |
|
|
|
148 |
|
|
# Use an intercon object to model the external
|
149 |
|
|
# interface objects.
|
150 |
|
|
if object_ref is not None:
|
151 |
|
|
if not isinstance(object_ref, signalset):
|
152 |
|
|
raise ValueError("Object_ref must be of signalset type (not %s, type %s)" % (repr(object_ref), type(object_ref)))
|
153 |
|
|
else:
|
154 |
|
|
object_ref = signalset(index)
|
155 |
|
|
|
156 |
|
|
self.external_objects[index] = object_ref
|
157 |
|
|
return object_ref
|
158 |
|
|
|
159 |
|
|
def get_external_signal(self, index, signalname):
|
160 |
|
|
"""
|
161 |
|
|
Try to get a signal reference from the external interface object
|
162 |
|
|
indexed by "index". Raise an Exception if not found.
|
163 |
|
|
Arguments:
|
164 |
|
|
* index:
|
165 |
|
|
* signalname:
|
166 |
|
|
Returns:
|
167 |
|
|
the MyHDL signal reference
|
168 |
|
|
"""
|
169 |
|
|
if not hasattr(self, "external_objects"):
|
170 |
|
|
self._new_external_objects()
|
171 |
|
|
|
172 |
|
|
if index is None:
|
173 |
|
|
# special case: search through all external interfaces
|
174 |
|
|
siglist = []
|
175 |
|
|
for sigset in self.external_objects.itervalues():
|
176 |
|
|
try:
|
177 |
|
|
siglist.append(sigset.get_signal_ref(signalname))
|
178 |
|
|
except:
|
179 |
|
|
pass
|
180 |
|
|
if len(siglist) == 0:
|
181 |
|
|
raise ValueError("%s : signal '%s' not found in any of the external objects." % (repr(self), repr(signalname)))
|
182 |
|
|
elif len(siglist) > 1:
|
183 |
|
|
raise ValueError("%s : signal '%s' found on multiple external objects." % (repr(self), repr(signalname)))
|
184 |
|
|
else:
|
185 |
|
|
return siglist[0]
|
186 |
|
|
|
187 |
|
|
if index not in self.external_objects:
|
188 |
|
|
raise ValueError("%s : External interface '%s' not found." % (repr(self), repr(index)))
|
189 |
|
|
|
190 |
|
|
return self.external_objects[index].get_signal_ref(signalname)
|
191 |
|
|
|
192 |
|
|
def get_external_all_signals(self, index):
|
193 |
|
|
"""
|
194 |
|
|
Return a dict with all signals references from interface
|
195 |
|
|
Arguments:
|
196 |
|
|
* index:
|
197 |
|
|
Returns:
|
198 |
|
|
dict with references to all signals in interface
|
199 |
|
|
"""
|
200 |
|
|
if not hasattr(self, "external_objects"):
|
201 |
|
|
self._new_external_objects()
|
202 |
|
|
|
203 |
|
|
if index not in self.external_objects:
|
204 |
|
|
raise ValueError("%s : External interface '%s' not found." % (repr(self), repr(index)))
|
205 |
|
|
|
206 |
|
|
retval = {}
|
207 |
|
|
for signalname in self.external_objects[index].get_signal_allnames():
|
208 |
|
|
retval[signalname] = self.external_objects[index].get_signal_ref(signalname)
|
209 |
|
|
return retval
|
210 |
|
|
|
211 |
|
|
# Internal signals methods
|
212 |
|
|
def getset_internal_signal(self, signalname, newsignal_ref=None):
|
213 |
|
|
"""
|
214 |
|
|
Try to get a reference to a internal signal with name "signalname".
|
215 |
|
|
If not found, optionally add a new signal with its reference
|
216 |
|
|
in "newsignal_ref".
|
217 |
|
|
Arguments:
|
218 |
|
|
* signalname:
|
219 |
|
|
* newsignal_ref:
|
220 |
|
|
Returns:
|
221 |
|
|
the MyHDL signal reference, or the contents of "newsignal_ref"
|
222 |
|
|
"""
|
223 |
|
|
if not hasattr(self, "internal_signals"):
|
224 |
|
|
self._new_internal_signals()
|
225 |
|
|
|
226 |
|
|
if signalname not in self.internal_signals:
|
227 |
|
|
if newsignal_ref is not None:
|
228 |
|
|
if isinstance(newsignal_ref, myhdl.SignalType):
|
229 |
|
|
self.internal_signals[signalname] = newsignal_ref
|
230 |
|
|
else:
|
231 |
|
|
# build a new signal based on newsignal_ref
|
232 |
|
|
self.internal_signals[signalname] = myhdl.Signal(newsignal_ref)
|
233 |
|
|
else:
|
234 |
|
|
raise ValueError("%s : Signal '%s' not found." % (repr(self), signalname))
|
235 |
|
|
return self.internal_signals[signalname]
|
236 |
|
|
|
237 |
|
|
def internal_signal_iter(self):
|
238 |
|
|
"""
|
239 |
|
|
Return an iterator object over all the internal signals.
|
240 |
|
|
This iterator works with tuples (signame, sigref).
|
241 |
|
|
"""
|
242 |
|
|
return self.internal_signals.iteritems()
|
243 |
|
|
|
244 |
|
|
# Methods to override
|
245 |
|
|
def build_generators_codegen(self, withbaseclass=True):
|
246 |
|
|
"""
|
247 |
|
|
Separate generator builder, for hw generators only
|
248 |
|
|
NOTES:
|
249 |
|
|
* Generators in this method must be convertible
|
250 |
|
|
* Use overridden functions
|
251 |
|
|
"""
|
252 |
|
|
raise NotImplementedError
|
253 |
|
|
|
254 |
|
|
def _new_internal_signals(self):
|
255 |
|
|
self.internal_signals = {}
|
256 |
|
|
def _new_external_objects(self):
|
257 |
|
|
self.external_objects = {}
|
258 |
|
|
def _new_interface_objects(self):
|
259 |
|
|
self.interface_objects = {}
|