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
11 from bsv
.peripheral_gen
import PeripheralIface
12 from bsv
.peripheral_gen
import PeripheralInterfaces
16 """ pin interface declaration.
17 * name is the name of the pin
18 * ready, enabled and io all create a (* .... *) prefix
19 * action changes it to an "in" if true
22 def __init__(self
, name
,
33 self
.enabled
= enabled
36 self
.bitspec
= bitspec
if bitspec
else 'Bit#(1)'
37 self
.outenmode
= outenmode
39 # bsv will look like this (method declaration):
41 (*always_ready,always_enabled*) method Bit#(1) io0_cell_outen;
42 (*always_ready,always_enabled,result="io"*) method
43 Action io0_inputval (Bit#(1) in);
46 def ifacepfmt(self
, fmtfn
):
50 name
= fmtfn(self
.name_
)
55 res
+= "#(%s) %s;" % (self
.bitspec
, name
)
58 def ifacefmt(self
, fmtfn
):
62 status
.append('always_ready')
64 status
.append('always_enabled')
66 status
.append('result="io"')
69 res
+= ','.join(status
)
74 name
= fmtfn(self
.name
)
78 res
+= ' (%s in)' % self
.bitspec
80 res
+= " %s " % self
.bitspec
85 # sample bsv method definition :
87 method Action cell0_mux(Bit#(2) in);
92 def ifacedef(self
, fmtoutfn
, fmtinfn
, fmtdecfn
):
95 fmtname
= fmtinfn(self
.name
)
97 res
+= fmtdecfn(self
.name
)
98 res
+= '(%s in);\n' % self
.bitspec
99 res
+= ' %s<=in;\n' % fmtname
102 fmtname
= fmtoutfn(self
.name
)
103 res
+= "%s=%s;" % (self
.name
, fmtname
)
105 # sample bsv wire (wire definiton):
107 Wire#(Bit#(2)) wrcell0_mux<-mkDWire(0);
110 def wirefmt(self
, fmtoutfn
, fmtinfn
, fmtdecfn
):
111 res
= ' Wire#(%s) ' % self
.bitspec
113 res
+= '%s' % fmtinfn(self
.name
)
115 res
+= '%s' % fmtoutfn(self
.name
)
116 res
+= "<-mkDWire(0);"
120 class Interface(PeripheralIface
):
121 """ create an interface from a list of pinspecs.
122 each pinspec is a dictionary, see Pin class arguments
123 single indicates that there is only one of these, and
124 so the name must *not* be extended numerically (see pname)
126 # sample interface object:
128 twiinterface_decl = Interface('twi',
129 [{'name': 'sda', 'outen': True},
130 {'name': 'scl', 'outen': True},
134 def __init__(self
, ifacename
, pinspecs
, ganged
=None, single
=False):
135 PeripheralIface
.__init
__(self
, ifacename
)
136 self
.ifacename
= ifacename
137 self
.ganged
= ganged
or {}
138 self
.pins
= [] # a list of instances of class Pin
139 self
.pinspecs
= pinspecs
# a list of dictionary
147 if p
.get('outen') is True: # special case, generate 3 pins
149 for psuffix
in ['out', 'outen', 'in']:
150 # changing the name (like sda) to (twi_sda_out)
151 _p
['name_'] = "%s_%s" % (p
['name'], psuffix
)
152 _p
['name'] = "%s_%s" % (self
.pname(p
['name']), psuffix
)
153 _p
['action'] = psuffix
!= 'in'
154 self
.pins
.append(Pin(**_p
))
155 # will look like {'name': 'twi_sda_out', 'action': True}
156 # {'name': 'twi_sda_outen', 'action': True}
157 #{'name': 'twi_sda_in', 'action': False}
158 # NOTice - outen key is removed
160 _p
['name_'] = p
['name']
161 _p
['name'] = self
.pname(p
['name'])
162 self
.pins
.append(Pin(**_p
))
164 # sample interface object:
166 uartinterface_decl = Interface('uart',
168 {'name': 'tx', 'action': True},
172 getifacetype is called multiple times in actual_pinmux.py
173 x = ifaces.getifacetype(temp), where temp is uart_rx, spi_mosi
174 Purpose is to identify is function : input/output/inout
177 def getifacetype(self
, name
):
178 for p
in self
.pinspecs
:
179 fname
= "%s_%s" % (self
.ifacename
, p
['name'])
180 # print "search", self.ifacename, name, fname
190 """ generates the interface spec e.g. flexbus_ale
191 if there is only one flexbus interface, or
192 sd{0}_cmd if there are several. string format
193 function turns this into sd0_cmd, sd1_cmd as
194 appropriate. single mode stops the numerical extension.
197 return self
.ifacename
198 return '%s{0}' % self
.ifacename
200 def pname(self
, name
):
201 """ generates the interface spec e.g. flexbus_ale
202 if there is only one flexbus interface, or
203 sd{0}_cmd if there are several. string format
204 function turns this into sd0_cmd, sd1_cmd as
205 appropriate. single mode stops the numerical extension.
207 return "%s_%s" % (self
.iname(), name
)
209 def busfmt(self
, *args
):
210 """ this function creates a bus "ganging" system based
211 on input from the {interfacename}.txt file.
212 only inout pins that are under the control of the
213 interface may be "ganged" together.
216 return '' # when self.ganged is None
219 for (k
, pnames
) in self
.ganged
.items():
220 name
= self
.pname('%senable' % k
).format(*args
)
221 decl
= 'Bit#(1) %s = 0;' % name
224 for p
in self
.pinspecs
:
225 if p
['name'] not in pnames
:
227 pname
= self
.pname(p
['name']).format(*args
)
228 if p
.get('outen') is True:
229 outname
= self
.ifacefmtoutfn(pname
)
230 ganged
.append("%s_outen" % outname
) # match wirefmt
232 gangedfmt
= '{%s} = duplicate(%s);'
233 res
.append(gangedfmt
% (',\n '.join(ganged
), name
))
234 return '\n'.join(res
) + '\n\n'
236 def wirefmt(self
, *args
):
237 res
= '\n'.join(map(self
.wirefmtpin
, self
.pins
)).format(*args
)
241 def ifacepfmt(self
, *args
):
242 res
= '\n'.join(map(self
.ifacepfmtdecpin
, self
.pins
)).format(*args
)
243 return '\n' + res
# pins is a list
245 def ifacefmt(self
, *args
):
246 res
= '\n'.join(map(self
.ifacefmtdecpin
, self
.pins
)).format(*args
)
247 return '\n' + res
# pins is a list
249 def ifacepfmtdecfn(self
, name
):
252 def ifacefmtdecfn(self
, name
):
253 return name
# like: uart
255 def ifacefmtdecfn2(self
, name
):
256 return name
# like: uart
258 def ifacefmtdecfn3(self
, name
):
260 return "%s_outen" % name
# like uart_outen
262 def ifacefmtoutfn(self
, name
):
263 return "wr%s" % name
# like wruart
265 def ifacefmtinfn(self
, name
):
268 def wirefmtpin(self
, pin
):
269 return pin
.wirefmt(self
.ifacefmtoutfn
, self
.ifacefmtinfn
,
272 def ifacepfmtdecpin(self
, pin
):
273 return pin
.ifacepfmt(self
.ifacepfmtdecfn
)
275 def ifacefmtdecpin(self
, pin
):
276 return pin
.ifacefmt(self
.ifacefmtdecfn
)
278 def ifacefmtpin(self
, pin
):
279 decfn
= self
.ifacefmtdecfn2
280 outfn
= self
.ifacefmtoutfn
281 # print pin, pin.outenmode
283 decfn
= self
.ifacefmtdecfn3
284 outfn
= self
.ifacefmtoutenfn
285 return pin
.ifacedef(outfn
, self
.ifacefmtinfn
,
288 def ifacedef(self
, *args
):
289 res
= '\n'.join(map(self
.ifacefmtpin
, self
.pins
))
290 res
= res
.format(*args
)
291 return '\n' + res
+ '\n'
294 class MuxInterface(Interface
):
296 def wirefmt(self
, *args
):
297 return muxwire
.format(*args
)
300 class IOInterface(Interface
):
302 def ifacefmtoutenfn(self
, name
):
303 return "cell{0}_mux_outen"
305 def ifacefmtoutfn(self
, name
):
306 """ for now strip off io{0}_ part """
307 return "cell{0}_mux_out"
309 def ifacefmtinfn(self
, name
):
310 return "cell{0}_mux_in"
312 def wirefmt(self
, *args
):
313 return generic_io
.format(*args
)
316 class Interfaces(InterfacesBase
, PeripheralInterfaces
):
317 """ contains a list of interface definitions
320 def __init__(self
, pth
=None):
321 InterfacesBase
.__init
__(self
, Interface
, pth
)
322 PeripheralInterfaces
.__init
__(self
)
324 def ifacedef(self
, f
, *args
):
325 for (name
, count
) in self
.ifacecount
:
326 for i
in range(count
):
327 f
.write(self
.data
[name
].ifacedef(i
))
329 def busfmt(self
, f
, *args
):
330 f
.write("import BUtils::*;\n\n")
331 for (name
, count
) in self
.ifacecount
:
332 for i
in range(count
):
333 bf
= self
.data
[name
].busfmt(i
)
336 def ifacepfmt(self
, f
, *args
):
338 // interface declaration between {0} and pinmux
339 (*always_ready,always_enabled*)
340 interface PeripheralSide{0};'''
341 for (name
, count
) in self
.ifacecount
:
342 f
.write(comment
.format(name
.upper()))
343 f
.write(self
.data
[name
].ifacepfmt(0))
344 f
.write("\n endinterface\n")
346 def ifacefmt(self
, f
, *args
):
348 // interface declaration between %s-{0} and pinmux'''
349 for (name
, count
) in self
.ifacecount
:
350 for i
in range(count
):
351 c
= comment
% name
.upper()
353 f
.write(self
.data
[name
].ifacefmt(i
))
355 def ifacefmt2(self
, f
, *args
):
357 interface PeripheralSide{0} {1};'''
358 for (name
, count
) in self
.ifacecount
:
359 for i
in range(count
):
360 iname
= self
.data
[name
].iname().format(i
)
361 f
.write(comment
.format(name
.upper(), iname
))
363 def wirefmt(self
, f
, *args
):
364 comment
= '\n // following wires capture signals ' \
365 'to IO CELL if %s-{0} is\n' \
367 for (name
, count
) in self
.ifacecount
:
368 for i
in range(count
):
371 f
.write(self
.data
[name
].wirefmt(i
))
374 # ========= Interface declarations ================ #
376 mux_interface
= MuxInterface('cell',
377 [{'name': 'mux', 'ready': False, 'enabled': False,
378 'bitspec': '{1}', 'action': True}])
380 io_interface
= IOInterface(
382 [{'name': 'cell_out', 'enabled': True, },
383 {'name': 'cell_outen', 'enabled': True, 'outenmode': True, },
384 {'name': 'cell_in', 'action': True, 'io': True}, ])
386 # == Peripheral Interface definitions == #
387 # these are the interface of the peripherals to the pin mux
388 # Outputs from the peripherals will be inputs to the pinmux
389 # module. Hence the change in direction for most pins
391 # ======================================= #
394 if __name__
== '__main__':
396 uartinterface_decl
= Interface('uart',
398 {'name': 'tx', 'action': True},
401 twiinterface_decl
= Interface('twi',
402 [{'name': 'sda', 'outen': True},
403 {'name': 'scl', 'outen': True},
406 def _pinmunge(p
, sep
, repl
, dedupe
=True):
407 """ munges the text so it's easier to compare.
408 splits by separator, strips out blanks, re-joins.
413 p
= filter(lambda x
: x
, p
) # filter out blanks
417 """ munges the text so it's easier to compare.
419 # first join lines by semicolons, strip out returns
421 p
= map(lambda x
: x
.replace('\n', ''), p
)
423 # now split first by brackets, then spaces (deduping on spaces)
424 p
= _pinmunge(p
, "(", " ( ", False)
425 p
= _pinmunge(p
, ")", " ) ", False)
426 p
= _pinmunge(p
, " ", " ")
432 for p1
, p2
in zip(l1
, l2
):
438 ifaces
= Interfaces()
440 ifaceuart
= ifaces
['uart']
441 print (ifaceuart
.ifacedef(0))
442 print (uartinterface_decl
.ifacedef(0))
443 assert ifaceuart
.ifacedef(0) == uartinterface_decl
.ifacedef(0)
445 ifacetwi
= ifaces
['twi']
446 print (ifacetwi
.ifacedef(0))
447 print (twiinterface_decl
.ifacedef(0))
448 assert ifacetwi
.ifacedef(0) == twiinterface_decl
.ifacedef(0)