4 from UserDict
import UserDict
6 from collections
import UserDict
8 from bsv
.wire_def
import generic_io
# special case
9 from bsv
.wire_def
import muxwire
# special case
10 from ifacebase
import InterfacesBase
14 """ pin interface declaration.
15 * name is the name of the pin
16 * ready, enabled and io all create a (* .... *) prefix
17 * action changes it to an "in" if true
20 def __init__(self
, name
,
29 self
.enabled
= enabled
32 self
.bitspec
= bitspec
if bitspec
else 'Bit#(1)'
33 self
.outenmode
= outenmode
35 # bsv will look like this (method declaration):
37 (*always_ready,always_enabled*) method Bit#(1) io0_cell_outen;
38 (*always_ready,always_enabled,result="io"*) method
39 Action io0_inputval (Bit#(1) in);
42 def ifacefmt(self
, fmtfn
):
46 status
.append('always_ready')
48 status
.append('always_enabled')
50 status
.append('result="io"')
53 res
+= ','.join(status
)
58 name
= fmtfn(self
.name
)
62 res
+= ' (%s in)' % self
.bitspec
64 res
+= " %s " % self
.bitspec
69 # sample bsv method definition :
71 method Action cell0_mux(Bit#(2) in);
76 def ifacedef(self
, fmtoutfn
, fmtinfn
, fmtdecfn
):
79 fmtname
= fmtinfn(self
.name
)
81 res
+= fmtdecfn(self
.name
)
82 res
+= '(%s in);\n' % self
.bitspec
83 res
+= ' %s<=in;\n' % fmtname
86 fmtname
= fmtoutfn(self
.name
)
87 res
+= "%s=%s;" % (self
.name
, fmtname
)
89 # sample bsv wire (wire definiton):
91 Wire#(Bit#(2)) wrcell0_mux<-mkDWire(0);
94 def wirefmt(self
, fmtoutfn
, fmtinfn
, fmtdecfn
):
95 res
= ' Wire#(%s) ' % self
.bitspec
97 res
+= '%s' % fmtinfn(self
.name
)
99 res
+= '%s' % fmtoutfn(self
.name
)
100 res
+= "<-mkDWire(0);"
104 class Interface(object):
105 """ create an interface from a list of pinspecs.
106 each pinspec is a dictionary, see Pin class arguments
107 single indicates that there is only one of these, and
108 so the name must *not* be extended numerically (see pname)
110 # sample interface object:
112 twiinterface_decl = Interface('twi',
113 [{'name': 'sda', 'outen': True},
114 {'name': 'scl', 'outen': True},
118 def __init__(self
, ifacename
, pinspecs
, ganged
=None, single
=False):
119 self
.ifacename
= ifacename
120 self
.ganged
= ganged
or {}
121 self
.pins
= [] # a list of instances of class Pin
122 self
.pinspecs
= pinspecs
# a list of dictionary
127 if _p
.has_key('type'):
129 if p
.get('outen') is True: # special case, generate 3 pins
131 for psuffix
in ['out', 'outen', 'in']:
132 # changing the name (like sda) to (twi_sda_out)
133 _p
['name'] = "%s_%s" % (self
.pname(p
['name']), psuffix
)
134 _p
['action'] = psuffix
!= 'in'
135 self
.pins
.append(Pin(**_p
))
136 # will look like {'name': 'twi_sda_out', 'action': True}
137 # {'name': 'twi_sda_outen', 'action': True}
138 #{'name': 'twi_sda_in', 'action': False}
139 # NOTice - outen key is removed
141 _p
['name'] = self
.pname(p
['name'])
142 self
.pins
.append(Pin(**_p
))
144 # sample interface object:
146 uartinterface_decl = Interface('uart',
148 {'name': 'tx', 'action': True},
152 getifacetype is called multiple times in actual_pinmux.py
153 x = ifaces.getifacetype(temp), where temp is uart_rx, spi_mosi
154 Purpose is to identify is function : input/output/inout
157 def getifacetype(self
, name
):
158 for p
in self
.pinspecs
:
159 fname
= "%s_%s" % (self
.ifacename
, p
['name'])
160 #print "search", self.ifacename, name, fname
169 def pname(self
, name
):
170 """ generates the interface spec e.g. flexbus_ale
171 if there is only one flexbus interface, or
172 sd{0}_cmd if there are several. string format
173 function turns this into sd0_cmd, sd1_cmd as
174 appropriate. single mode stops the numerical extension.
177 return '%s_%s' % (self
.ifacename
, name
)
178 return '%s{0}_%s' % (self
.ifacename
, name
)
180 def busfmt(self
, *args
):
181 """ this function creates a bus "ganging" system based
182 on input from the {interfacename}.txt file.
183 only inout pins that are under the control of the
184 interface may be "ganged" together.
187 return '' # when self.ganged is None
190 for (k
, pnames
) in self
.ganged
.items():
191 name
= self
.pname('%senable' % k
).format(*args
)
192 decl
= 'Bit#(1) %s = 0;' % name
195 for p
in self
.pinspecs
:
196 if p
['name'] not in pnames
:
198 pname
= self
.pname(p
['name']).format(*args
)
199 if p
.get('outen') is True:
200 outname
= self
.ifacefmtoutfn(pname
)
201 ganged
.append("%s_outen" % outname
) # match wirefmt
203 gangedfmt
= '{%s} = duplicate(%s);'
204 res
.append(gangedfmt
% (',\n '.join(ganged
), name
))
205 return '\n'.join(res
) + '\n\n'
207 def wirefmt(self
, *args
):
208 res
= '\n'.join(map(self
.wirefmtpin
, self
.pins
)).format(*args
)
212 def ifacefmt(self
, *args
):
213 res
= '\n'.join(map(self
.ifacefmtdecpin
, self
.pins
)).format(*args
)
214 return '\n' + res
# pins is a list
216 def ifacefmtdecfn(self
, name
):
217 return name
# like: uart
219 def ifacefmtdecfn2(self
, name
):
220 return name
# like: uart
222 def ifacefmtdecfn3(self
, name
):
224 return "%s_outen" % name
# like uart_outen
226 def ifacefmtoutfn(self
, name
):
227 return "wr%s" % name
# like wruart
229 def ifacefmtinfn(self
, name
):
232 def wirefmtpin(self
, pin
):
233 return pin
.wirefmt(self
.ifacefmtoutfn
, self
.ifacefmtinfn
,
236 def ifacefmtdecpin(self
, pin
):
237 return pin
.ifacefmt(self
.ifacefmtdecfn
)
239 def ifacefmtpin(self
, pin
):
240 decfn
= self
.ifacefmtdecfn2
241 outfn
= self
.ifacefmtoutfn
242 #print pin, pin.outenmode
244 decfn
= self
.ifacefmtdecfn3
245 outfn
= self
.ifacefmtoutenfn
246 return pin
.ifacedef(outfn
, self
.ifacefmtinfn
,
249 def ifacedef(self
, *args
):
250 res
= '\n'.join(map(self
.ifacefmtpin
, self
.pins
))
251 res
= res
.format(*args
)
252 return '\n' + res
+ '\n'
255 class MuxInterface(Interface
):
257 def wirefmt(self
, *args
):
258 return muxwire
.format(*args
)
261 class IOInterface(Interface
):
263 def ifacefmtoutenfn(self
, name
):
264 return "cell{0}_mux_outen"
266 def ifacefmtoutfn(self
, name
):
267 """ for now strip off io{0}_ part """
268 return "cell{0}_mux_out"
270 def ifacefmtinfn(self
, name
):
271 return "cell{0}_mux_in"
273 def wirefmt(self
, *args
):
274 return generic_io
.format(*args
)
277 class Interfaces(InterfacesBase
):
278 """ contains a list of interface definitions
281 def __init__(self
, pth
=None):
282 InterfacesBase
.__init
__(self
, Interface
, pth
)
284 def ifacedef(self
, f
, *args
):
285 for (name
, count
) in self
.ifacecount
:
286 for i
in range(count
):
287 f
.write(self
.data
[name
].ifacedef(i
))
289 def busfmt(self
, f
, *args
):
290 f
.write("import BUtils::*;\n\n")
291 for (name
, count
) in self
.ifacecount
:
292 for i
in range(count
):
293 bf
= self
.data
[name
].busfmt(i
)
296 def ifacefmt(self
, f
, *args
):
298 // interface declaration between %s-{0} and pinmux'''
299 for (name
, count
) in self
.ifacecount
:
300 for i
in range(count
):
301 c
= comment
% name
.upper()
303 f
.write(self
.data
[name
].ifacefmt(i
))
305 def wirefmt(self
, f
, *args
):
306 comment
= '\n // following wires capture signals ' \
307 'to IO CELL if %s-{0} is\n' \
309 for (name
, count
) in self
.ifacecount
:
310 for i
in range(count
):
313 f
.write(self
.data
[name
].wirefmt(i
))
316 # ========= Interface declarations ================ #
318 mux_interface
= MuxInterface('cell',
319 [{'name': 'mux', 'ready': False, 'enabled': False,
320 'bitspec': '{1}', 'action': True}])
322 io_interface
= IOInterface(
324 [{'name': 'cell_out', 'enabled': True, },
325 {'name': 'cell_outen', 'enabled': True, 'outenmode': True, },
326 {'name': 'cell_in', 'action': True, 'io': True}, ])
328 # == Peripheral Interface definitions == #
329 # these are the interface of the peripherals to the pin mux
330 # Outputs from the peripherals will be inputs to the pinmux
331 # module. Hence the change in direction for most pins
333 # ======================================= #
336 if __name__
== '__main__':
338 uartinterface_decl
= Interface('uart',
340 {'name': 'tx', 'action': True},
343 twiinterface_decl
= Interface('twi',
344 [{'name': 'sda', 'outen': True},
345 {'name': 'scl', 'outen': True},
348 def _pinmunge(p
, sep
, repl
, dedupe
=True):
349 """ munges the text so it's easier to compare.
350 splits by separator, strips out blanks, re-joins.
355 p
= filter(lambda x
: x
, p
) # filter out blanks
359 """ munges the text so it's easier to compare.
361 # first join lines by semicolons, strip out returns
363 p
= map(lambda x
: x
.replace('\n', ''), p
)
365 # now split first by brackets, then spaces (deduping on spaces)
366 p
= _pinmunge(p
, "(", " ( ", False)
367 p
= _pinmunge(p
, ")", " ) ", False)
368 p
= _pinmunge(p
, " ", " ")
374 for p1
, p2
in zip(l1
, l2
):
380 ifaces
= Interfaces()
382 ifaceuart
= ifaces
['uart']
383 print (ifaceuart
.ifacedef(0))
384 print (uartinterface_decl
.ifacedef(0))
385 assert ifaceuart
.ifacedef(0) == uartinterface_decl
.ifacedef(0)
387 ifacetwi
= ifaces
['twi']
388 print (ifacetwi
.ifacedef(0))
389 print (twiinterface_decl
.ifacedef(0))
390 assert ifacetwi
.ifacedef(0) == twiinterface_decl
.ifacedef(0)