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
.get('outen') is True: # special case, generate 3 pins
129 for psuffix
in ['out', 'outen', 'in']:
130 # changing the name (like sda) to (twi_sda_out)
131 _p
['name'] = "%s_%s" % (self
.pname(p
['name']), psuffix
)
132 _p
['action'] = psuffix
!= 'in'
133 self
.pins
.append(Pin(**_p
))
134 # will look like {'name': 'twi_sda_out', 'action': True}
135 # {'name': 'twi_sda_outen', 'action': True}
136 #{'name': 'twi_sda_in', 'action': False}
137 # NOTice - outen key is removed
139 _p
['name'] = self
.pname(p
['name'])
140 self
.pins
.append(Pin(**_p
))
142 # sample interface object:
144 uartinterface_decl = Interface('uart',
146 {'name': 'tx', 'action': True},
150 getifacetype is called multiple times in actual_pinmux.py
151 x = ifaces.getifacetype(temp), where temp is uart_rx, spi_mosi
152 Purpose is to identify is function : input/output/inout
155 def getifacetype(self
, name
):
156 for p
in self
.pinspecs
:
157 fname
= "%s_%s" % (self
.ifacename
, p
['name'])
158 #print "search", self.ifacename, name, fname
167 def pname(self
, name
):
168 """ generates the interface spec e.g. flexbus_ale
169 if there is only one flexbus interface, or
170 sd{0}_cmd if there are several. string format
171 function turns this into sd0_cmd, sd1_cmd as
172 appropriate. single mode stops the numerical extension.
175 return '%s_%s' % (self
.ifacename
, name
)
176 return '%s{0}_%s' % (self
.ifacename
, name
)
178 def busfmt(self
, *args
):
179 """ this function creates a bus "ganging" system based
180 on input from the {interfacename}.txt file.
181 only inout pins that are under the control of the
182 interface may be "ganged" together.
185 return '' # when self.ganged is None
188 for (k
, pnames
) in self
.ganged
.items():
189 name
= self
.pname('%senable' % k
).format(*args
)
190 decl
= 'Bit#(1) %s = 0;' % name
193 for p
in self
.pinspecs
:
194 if p
['name'] not in pnames
:
196 pname
= self
.pname(p
['name']).format(*args
)
197 if p
.get('outen') is True:
198 outname
= self
.ifacefmtoutfn(pname
)
199 ganged
.append("%s_outen" % outname
) # match wirefmt
201 gangedfmt
= '{%s} = duplicate(%s);'
202 res
.append(gangedfmt
% (',\n '.join(ganged
), name
))
203 return '\n'.join(res
) + '\n\n'
205 def wirefmt(self
, *args
):
206 res
= '\n'.join(map(self
.wirefmtpin
, self
.pins
)).format(*args
)
210 def ifacefmt(self
, *args
):
211 res
= '\n'.join(map(self
.ifacefmtdecpin
, self
.pins
)).format(*args
)
212 return '\n' + res
# pins is a list
214 def ifacefmtdecfn(self
, name
):
215 return name
# like: uart
217 def ifacefmtdecfn2(self
, name
):
218 return name
# like: uart
220 def ifacefmtdecfn3(self
, name
):
222 return "%s_outen" % name
# like uart_outen
224 def ifacefmtoutfn(self
, name
):
225 return "wr%s" % name
# like wruart
227 def ifacefmtinfn(self
, name
):
230 def wirefmtpin(self
, pin
):
231 return pin
.wirefmt(self
.ifacefmtoutfn
, self
.ifacefmtinfn
,
234 def ifacefmtdecpin(self
, pin
):
235 return pin
.ifacefmt(self
.ifacefmtdecfn
)
237 def ifacefmtpin(self
, pin
):
238 decfn
= self
.ifacefmtdecfn2
239 outfn
= self
.ifacefmtoutfn
240 #print pin, pin.outenmode
242 decfn
= self
.ifacefmtdecfn3
243 outfn
= self
.ifacefmtoutenfn
244 return pin
.ifacedef(outfn
, self
.ifacefmtinfn
,
247 def ifacedef(self
, *args
):
248 res
= '\n'.join(map(self
.ifacefmtpin
, self
.pins
))
249 res
= res
.format(*args
)
250 return '\n' + res
+ '\n'
253 class MuxInterface(Interface
):
255 def wirefmt(self
, *args
):
256 return muxwire
.format(*args
)
259 class IOInterface(Interface
):
261 def ifacefmtoutenfn(self
, name
):
262 return "cell{0}_mux_outen"
264 def ifacefmtoutfn(self
, name
):
265 """ for now strip off io{0}_ part """
266 return "cell{0}_mux_out"
268 def ifacefmtinfn(self
, name
):
269 return "cell{0}_mux_in"
271 def wirefmt(self
, *args
):
272 return generic_io
.format(*args
)
275 class Interfaces(InterfacesBase
):
276 """ contains a list of interface definitions
279 def __init__(self
, pth
=None):
280 InterfacesBase
.__init
__(self
, Interface
, pth
)
282 def ifacedef(self
, f
, *args
):
283 for (name
, count
) in self
.ifacecount
:
284 for i
in range(count
):
285 f
.write(self
.data
[name
].ifacedef(i
))
287 def busfmt(self
, f
, *args
):
288 f
.write("import BUtils::*;\n\n")
289 for (name
, count
) in self
.ifacecount
:
290 for i
in range(count
):
291 bf
= self
.data
[name
].busfmt(i
)
294 def ifacefmt(self
, f
, *args
):
296 // interface declaration between %s-{0} and pinmux'''
297 for (name
, count
) in self
.ifacecount
:
298 for i
in range(count
):
299 c
= comment
% name
.upper()
301 f
.write(self
.data
[name
].ifacefmt(i
))
303 def wirefmt(self
, f
, *args
):
304 comment
= '\n // following wires capture signals ' \
305 'to IO CELL if %s-{0} is\n' \
307 for (name
, count
) in self
.ifacecount
:
308 for i
in range(count
):
311 f
.write(self
.data
[name
].wirefmt(i
))
314 # ========= Interface declarations ================ #
316 mux_interface
= MuxInterface('cell',
317 [{'name': 'mux', 'ready': False, 'enabled': False,
318 'bitspec': '{1}', 'action': True}])
320 io_interface
= IOInterface(
322 [{'name': 'cell_out', 'enabled': True, },
323 {'name': 'cell_outen', 'enabled': True, 'outenmode': True, },
324 {'name': 'cell_in', 'action': True, 'io': True}, ])
326 # == Peripheral Interface definitions == #
327 # these are the interface of the peripherals to the pin mux
328 # Outputs from the peripherals will be inputs to the pinmux
329 # module. Hence the change in direction for most pins
331 # ======================================= #
334 if __name__
== '__main__':
336 uartinterface_decl
= Interface('uart',
338 {'name': 'tx', 'action': True},
341 twiinterface_decl
= Interface('twi',
342 [{'name': 'sda', 'outen': True},
343 {'name': 'scl', 'outen': True},
346 def _pinmunge(p
, sep
, repl
, dedupe
=True):
347 """ munges the text so it's easier to compare.
348 splits by separator, strips out blanks, re-joins.
353 p
= filter(lambda x
: x
, p
) # filter out blanks
357 """ munges the text so it's easier to compare.
359 # first join lines by semicolons, strip out returns
361 p
= map(lambda x
: x
.replace('\n', ''), p
)
363 # now split first by brackets, then spaces (deduping on spaces)
364 p
= _pinmunge(p
, "(", " ( ", False)
365 p
= _pinmunge(p
, ")", " ) ", False)
366 p
= _pinmunge(p
, " ", " ")
372 for p1
, p2
in zip(l1
, l2
):
378 ifaces
= Interfaces()
380 ifaceuart
= ifaces
['uart']
381 print (ifaceuart
.ifacedef(0))
382 print (uartinterface_decl
.ifacedef(0))
383 assert ifaceuart
.ifacedef(0) == uartinterface_decl
.ifacedef(0)
385 ifacetwi
= ifaces
['twi']
386 print (ifacetwi
.ifacedef(0))
387 print (twiinterface_decl
.ifacedef(0))
388 assert ifacetwi
.ifacedef(0) == twiinterface_decl
.ifacedef(0)