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
13 """ pin interface declaration.
14 * name is the name of the pin
15 * ready, enabled and io all create a (* .... *) prefix
16 * action changes it to an "in" if true
19 def __init__(self
, name
,
27 self
.enabled
= enabled
30 self
.bitspec
= bitspec
if bitspec
else 'Bit#(1)'
32 def ifacefmt(self
, fmtfn
):
36 status
.append('always_ready')
38 status
.append('always_enabled')
40 status
.append('result="io"')
43 res
+= ','.join(status
)
48 name
= fmtfn(self
.name
)
52 res
+= ' (%s in)' % self
.bitspec
54 res
+= " %s " % self
.bitspec
59 def ifacedef(self
, fmtoutfn
, fmtinfn
, fmtdecfn
):
62 fmtname
= fmtinfn(self
.name
)
64 res
+= fmtdecfn(self
.name
)
65 res
+= '(%s in);\n' % self
.bitspec
66 res
+= ' %s<=in;\n' % fmtname
69 fmtname
= fmtoutfn(self
.name
)
70 res
+= "%s=%s;" % (self
.name
, fmtname
)
73 def wirefmt(self
, fmtoutfn
, fmtinfn
, fmtdecfn
):
74 res
= ' Wire#(%s) ' % self
.bitspec
76 res
+= '%s' % fmtinfn(self
.name
)
78 res
+= '%s' % fmtoutfn(self
.name
)
79 res
+= "<-mkDWire(0);"
83 class Interface(object):
84 """ create an interface from a list of pinspecs.
85 each pinspec is a dictionary, see Pin class arguments
86 single indicates that there is only one of these, and
87 so the name must *not* be extended numerically (see pname)
90 def __init__(self
, ifacename
, pinspecs
, ganged
=None, single
=False):
91 self
.ifacename
= ifacename
92 self
.ganged
= ganged
or {}
94 self
.pinspecs
= pinspecs
99 if p
.get('outen') is True: # special case, generate 3 pins
101 for psuffix
in ['out', 'outen', 'in']:
102 _p
['name'] = "%s_%s" % (self
.pname(p
['name']), psuffix
)
103 _p
['action'] = psuffix
!= 'in'
104 self
.pins
.append(Pin(**_p
))
106 _p
['name'] = self
.pname(p
['name'])
107 self
.pins
.append(Pin(**_p
))
109 def getifacetype(self
, name
):
110 for p
in self
.pinspecs
:
111 fname
= "%s_%s" % (self
.ifacename
, p
['name'])
112 #print "search", self.ifacename, name, fname
121 def pname(self
, name
):
122 """ generates the interface spec e.g. flexbus_ale
123 if there is only one flexbus interface, or
124 sd{0}_cmd if there are several. string format
125 function turns this into sd0_cmd, sd1_cmd as
126 appropriate. single mode stops the numerical extension.
129 return '%s_%s' % (self
.ifacename
, name
)
130 return '%s{0}_%s' % (self
.ifacename
, name
)
132 def busfmt(self
, *args
):
133 """ this function creates a bus "ganging" system based
134 on input from the {interfacename}.txt file.
135 only inout pins that are under the control of the
136 interface may be "ganged" together.
142 for (k
, pnames
) in self
.ganged
.items():
143 name
= self
.pname('%senable' % k
).format(*args
)
144 decl
= 'Bit#(1) %s = 0;' % name
147 for p
in self
.pinspecs
:
148 if p
['name'] not in pnames
:
150 pname
= self
.pname(p
['name']).format(*args
)
151 if p
.get('outen') is True:
152 outname
= self
.ifacefmtoutfn(pname
)
153 ganged
.append("%s_outen" % outname
) # match wirefmt
155 gangedfmt
= '{%s} = duplicate(%s);'
156 res
.append(gangedfmt
% (',\n '.join(ganged
), name
))
157 return '\n'.join(res
) + '\n\n'
159 def wirefmt(self
, *args
):
160 res
= '\n'.join(map(self
.wirefmtpin
, self
.pins
)).format(*args
)
162 for p
in self
.pinspecs
:
163 name
= self
.pname(p
['name'])
164 typ
= self
.getifacetype(name
.format(""))
165 name
= name
.format(*args
)
166 res
+= " # declare %s_io, set up as type '%s'\n" % (name
, typ
)
167 res
+= " GenericIOType %s_io = GenericIOType{\n" % name
170 outname
= self
.ifacefmtoutfn(name
)
171 params
.append('outputval:%s_out,' % outname
)
172 params
.append('output_en:%s_outen,' % outname
) # match busfmt
173 params
.append('input_en:~%s_outen,' % outname
)
175 outname
= self
.ifacefmtoutfn(name
)
176 params
.append('outputval:%s,' % outname
)
177 params
.append('output_en:1,')
178 params
.append('input_en:0,')
180 params
.append('outputval:0,')
181 params
.append('output_en:0,')
182 params
.append('input_en:1,')
186 def ifacefmt(self
, *args
):
187 res
= '\n'.join(map(self
.ifacefmtdecpin
, self
.pins
)).format(*args
)
190 def ifacefmtdecfn(self
, name
):
193 def ifacefmtdecfn2(self
, name
):
196 def ifacefmtoutfn(self
, name
):
199 def ifacefmtinfn(self
, name
):
202 def wirefmtpin(self
, pin
):
203 return pin
.wirefmt(self
.ifacefmtoutfn
, self
.ifacefmtinfn
,
206 def ifacefmtdecpin(self
, pin
):
207 return pin
.ifacefmt(self
.ifacefmtdecfn
)
209 def ifacefmtpin(self
, pin
):
210 return pin
.ifacedef(self
.ifacefmtoutfn
, self
.ifacefmtinfn
,
213 def ifacedef(self
, *args
):
214 res
= '\n'.join(map(self
.ifacefmtpin
, self
.pins
))
215 res
= res
.format(*args
)
216 return '\n' + res
+ '\n'
219 class MuxInterface(Interface
):
221 def wirefmt(self
, *args
):
222 return muxwire
.format(*args
)
225 class IOInterface(Interface
):
227 def ifacefmtoutfn(self
, name
):
228 """ for now strip off io{0}_ part """
229 return "cell{0}_mux_out"
231 def ifacefmtinfn(self
, name
):
232 return "cell{0}_mux_in"
234 def wirefmt(self
, *args
):
235 return generic_io
.format(*args
)
238 class Interfaces(UserDict
):
239 """ contains a list of interface definitions
242 def __init__(self
, pth
):
245 UserDict
.__init
__(self
, {})
246 ift
= 'interfaces.txt'
248 ift
= os
.path
.join(pth
, ift
)
249 with
open(ift
, 'r') as ifile
:
250 for ln
in ifile
.readlines():
255 spec
, ganged
= self
.read_spec(pth
, name
)
256 iface
= Interface(name
, spec
, ganged
, count
== 1)
257 self
.ifaceadd(name
, count
, iface
)
259 def getifacetype(self
, fname
):
260 # finds the interface type, e.g sd_d0 returns "inout"
261 for iface
in self
.values():
262 typ
= iface
.getifacetype(fname
)
267 def ifaceadd(self
, name
, count
, iface
, at
=None):
269 at
= len(self
.ifacecount
)
270 self
.ifacecount
.insert(at
, (name
, count
))
273 def read_spec(self
, pth
, name
):
276 fname
= '%s.txt' % name
278 ift
= os
.path
.join(pth
, fname
)
279 with
open(ift
, 'r') as sfile
:
280 for ln
in sfile
.readlines():
287 elif ln
[1] == 'inout':
291 if bus
not in ganged
:
293 ganged
[bus
].append(name
)
297 def ifacedef(self
, f
, *args
):
298 for (name
, count
) in self
.ifacecount
:
299 for i
in range(count
):
300 f
.write(self
.data
[name
].ifacedef(i
))
302 def busfmt(self
, f
, *args
):
303 f
.write("import BUtils::*;\n\n")
304 for (name
, count
) in self
.ifacecount
:
305 for i
in range(count
):
306 bf
= self
.data
[name
].busfmt(i
)
309 def ifacefmt(self
, f
, *args
):
311 // interface declaration between %s-{0} and pinmux'''
312 for (name
, count
) in self
.ifacecount
:
313 for i
in range(count
):
314 c
= comment
% name
.upper()
316 f
.write(self
.data
[name
].ifacefmt(i
))
318 def wirefmt(self
, f
, *args
):
319 comment
= '\n // following wires capture signals ' \
320 'to IO CELL if %s-{0} is\n' \
322 for (name
, count
) in self
.ifacecount
:
323 for i
in range(count
):
326 f
.write(self
.data
[name
].wirefmt(i
))
329 # ========= Interface declarations ================ #
331 mux_interface
= MuxInterface('cell', [{'name': 'mux', 'ready': False,
333 'bitspec': '{1}', 'action': True}])
335 io_interface
= IOInterface(
337 [{'name': 'cell', 'enabled': False, 'bitspec': 'GenericIOType'},
338 {'name': 'inputval', 'action': True, 'io': True}, ])
340 # == Peripheral Interface definitions == #
341 # these are the interface of the peripherals to the pin mux
342 # Outputs from the peripherals will be inputs to the pinmux
343 # module. Hence the change in direction for most pins
345 # ======================================= #
348 if __name__
== '__main__':
350 uartinterface_decl
= Interface('uart',
352 {'name': 'tx', 'action': True},
355 twiinterface_decl
= Interface('twi',
356 [{'name': 'sda', 'outen': True},
357 {'name': 'scl', 'outen': True},
360 def _pinmunge(p
, sep
, repl
, dedupe
=True):
361 """ munges the text so it's easier to compare.
362 splits by separator, strips out blanks, re-joins.
367 p
= filter(lambda x
: x
, p
) # filter out blanks
371 """ munges the text so it's easier to compare.
373 # first join lines by semicolons, strip out returns
375 p
= map(lambda x
: x
.replace('\n', ''), p
)
377 # now split first by brackets, then spaces (deduping on spaces)
378 p
= _pinmunge(p
, "(", " ( ", False)
379 p
= _pinmunge(p
, ")", " ) ", False)
380 p
= _pinmunge(p
, " ", " ")
386 for p1
, p2
in zip(l1
, l2
):
392 ifaces
= Interfaces()
394 ifaceuart
= ifaces
['uart']
395 print (ifaceuart
.ifacedef(0))
396 print (uartinterface_decl
.ifacedef(0))
397 assert ifaceuart
.ifacedef(0) == uartinterface_decl
.ifacedef(0)
399 ifacetwi
= ifaces
['twi']
400 print (ifacetwi
.ifacedef(0))
401 print (twiinterface_decl
.ifacedef(0))
402 assert ifacetwi
.ifacedef(0) == twiinterface_decl
.ifacedef(0)