3 from UserDict
import UserDict
5 from wire_def
import generic_io
# special case
6 from wire_def
import muxwire
# special case
10 """ pin interface declaration.
11 * name is the name of the pin
12 * ready, enabled and io all create a (* .... *) prefix
13 * action changes it to an "in" if true
16 def __init__(self
, name
,
24 self
.enabled
= enabled
27 self
.bitspec
= bitspec
if bitspec
else '1'
29 def ifacefmt(self
, fmtfn
=None):
33 status
.append('always_ready')
35 status
.append('always_enabled')
37 status
.append('result="io"')
40 res
+= ','.join(status
)
45 name
= fmtfn(self
.name
)
49 res
+= ' (Bit#(%s) in)' % self
.bitspec
51 res
+= " Bit#(%s) " % self
.bitspec
56 def ifacedef(self
, fmtoutfn
=None, fmtinfn
=None, fmtdecfn
=None):
59 fmtname
= fmtinfn(self
.name
)
61 res
+= fmtdecfn(self
.name
)
62 res
+= '(Bit#(%s) in);\n' % self
.bitspec
63 res
+= ' %s<=in;\n' % fmtname
66 fmtname
= fmtoutfn(self
.name
)
67 res
+= "%s=%s;" % (self
.name
, fmtname
)
70 def wirefmt(self
, fmtoutfn
=None, fmtinfn
=None, fmtdecfn
=None):
71 res
= ' Wire#(Bit#(%s)) ' % self
.bitspec
73 res
+= '%s' % fmtinfn(self
.name
)
75 res
+= '%s' % fmtoutfn(self
.name
)
76 res
+= "<-mkDWire(0);"
80 class Interface(object):
81 """ create an interface from a list of pinspecs.
82 each pinspec is a dictionary, see Pin class arguments
85 def __init__(self
, ifacename
, pinspecs
):
86 self
.ifacename
= ifacename
88 self
.pinspecs
= pinspecs
92 if p
.get('outen') is True: # special case, generate 3 pins
94 for psuffix
in ['out', 'outen', 'in']:
95 _p
['name'] = "%s_%s" % (self
.pname(p
['name']), psuffix
)
96 _p
['action'] = psuffix
!= 'in'
97 self
.pins
.append(Pin(**_p
))
99 _p
['name'] = self
.pname(p
['name'])
100 self
.pins
.append(Pin(**_p
))
102 def pname(self
, name
):
103 return '%s{0}_%s' % (self
.ifacename
, name
)
105 def wirefmt(self
, *args
):
106 res
= '\n'.join(map(self
.wirefmtpin
, self
.pins
)).format(*args
)
108 for p
in self
.pinspecs
:
109 name
= self
.pname(p
['name']).format(*args
)
110 res
+= " GenericIOType %s_io = GenericIOType{\n" % name
112 if p
.get('outen') is True:
113 outname
= self
.ifacefmtoutfn(name
)
114 params
.append('outputval:%s_out,' % outname
)
115 params
.append('output_en:%s_outen,' % outname
)
116 params
.append('input_en:~%s_outen,' % outname
)
117 elif p
.get('action'):
118 outname
= self
.ifacefmtoutfn(name
)
119 params
.append('outputval:%s,' % outname
)
120 params
.append('output_en:1,')
121 params
.append('input_en:0,')
123 params
.append('outputval:0,')
124 params
.append('output_en:0,')
125 params
.append('input_en:1,')
126 params
+= ['pullup_en:0,', 'pulldown_en:0,',
127 'pushpull_en:0,', 'drivestrength:0,',
130 res
+= ' %s\n' % param
134 def ifacefmt(self
, *args
):
135 res
= '\n'.join(map(self
.ifacefmtdecpin
, self
.pins
)).format(*args
)
138 def ifacefmtdecfn(self
, name
):
141 def ifacefmtdecfn2(self
, name
):
144 def ifacefmtoutfn(self
, name
):
147 def ifacefmtinfn(self
, name
):
150 def wirefmtpin(self
, pin
):
151 return pin
.wirefmt(self
.ifacefmtoutfn
, self
.ifacefmtinfn
,
154 def ifacefmtdecpin(self
, pin
):
155 return pin
.ifacefmt(self
.ifacefmtdecfn
)
157 def ifacefmtpin(self
, pin
):
158 return pin
.ifacedef(self
.ifacefmtoutfn
, self
.ifacefmtinfn
,
161 def ifacedef(self
, *args
):
162 res
= '\n'.join(map(self
.ifacefmtpin
, self
.pins
))
163 res
= res
.format(*args
)
164 return '\n' + res
+ '\n'
167 class MuxInterface(Interface
):
169 def wirefmt(self
, *args
):
170 return muxwire
.format(*args
)
173 class IOInterface(Interface
):
175 def ifacefmtoutfn(self
, name
):
176 """ for now strip off io{0}_ part """
177 return "cell{0}_mux_out.%s" % name
[6:]
179 def ifacefmtinfn(self
, name
):
180 return "cell{0}_mux_in"
182 def wirefmt(self
, *args
):
183 return generic_io
.format(*args
)
186 class Interfaces(UserDict
):
187 """ contains a list of interface definitions
190 def __init__(self
, pth
):
193 UserDict
.__init
__(self
, {})
194 ift
= 'interfaces.txt'
196 ift
= os
.path
.join(pth
, ift
)
197 with
open(ift
, 'r') as ifile
:
198 for ln
in ifile
.readlines():
203 spec
= self
.read_spec(pth
, name
)
204 self
.ifaceadd(name
, count
, Interface(name
, spec
))
206 def ifaceadd(self
, name
, count
, iface
, at
=None):
208 at
= len(self
.ifacecount
)
209 self
.ifacecount
.insert(at
, (name
, count
))
212 def read_spec(self
, pth
, name
):
214 fname
= '%s.txt' % name
216 ift
= os
.path
.join(pth
, fname
)
217 with
open(ift
, 'r') as sfile
:
218 for ln
in sfile
.readlines():
224 elif ln
[1] == 'inout':
229 def ifacedef(self
, f
, *args
):
230 for (name
, count
) in self
.ifacecount
:
231 for i
in range(count
):
232 f
.write(self
.data
[name
].ifacedef(i
))
234 def ifacefmt(self
, f
, *args
):
236 // interface declaration between %s-{0} and pinmux'''
237 for (name
, count
) in self
.ifacecount
:
238 for i
in range(count
):
239 c
= comment
% name
.upper()
241 f
.write(self
.data
[name
].ifacefmt(i
))
243 def wirefmt(self
, f
, *args
):
244 comment
= '\n // following wires capture signals ' \
245 'to IO CELL if %s-{0} is\n' \
247 for (name
, count
) in self
.ifacecount
:
248 for i
in range(count
):
251 f
.write(self
.data
[name
].wirefmt(i
))
254 # ========= Interface declarations ================ #
256 mux_interface
= MuxInterface('cell', [{'name': 'mux', 'ready': False,
258 'bitspec': '{1}', 'action': True}])
260 io_interface
= IOInterface('io',
261 [{'name': 'outputval', 'enabled': False},
262 {'name': 'output_en', 'enabled': False},
263 {'name': 'input_en', 'enabled': False},
264 {'name': 'pullup_en', 'enabled': False},
265 {'name': 'pulldown_en', 'enabled': False},
266 {'name': 'drivestrength', 'enabled': False},
267 {'name': 'pushpull_en', 'enabled': False},
268 {'name': 'opendrain_en', 'enabled': False},
269 {'name': 'inputval', 'action': True, 'io': True},
272 # == Peripheral Interface definitions == #
273 # these are the interface of the peripherals to the pin mux
274 # Outputs from the peripherals will be inputs to the pinmux
275 # module. Hence the change in direction for most pins
277 # ======================================= #
280 if __name__
== '__main__':
282 uartinterface_decl
= Interface('uart',
284 {'name': 'tx', 'action': True},
287 twiinterface_decl
= Interface('twi',
288 [{'name': 'sda', 'outen': True},
289 {'name': 'scl', 'outen': True},
292 def _pinmunge(p
, sep
, repl
, dedupe
=True):
293 """ munges the text so it's easier to compare.
294 splits by separator, strips out blanks, re-joins.
299 p
= filter(lambda x
: x
, p
) # filter out blanks
303 """ munges the text so it's easier to compare.
305 # first join lines by semicolons, strip out returns
307 p
= map(lambda x
: x
.replace('\n', ''), p
)
309 # now split first by brackets, then spaces (deduping on spaces)
310 p
= _pinmunge(p
, "(", " ( ", False)
311 p
= _pinmunge(p
, ")", " ) ", False)
312 p
= _pinmunge(p
, " ", " ")
318 for p1
, p2
in zip(l1
, l2
):
324 ifaces
= Interfaces()
326 ifaceuart
= ifaces
['uart']
327 print ifaceuart
.ifacedef(0)
328 print uartinterface_decl
.ifacedef(0)
329 assert ifaceuart
.ifacedef(0) == uartinterface_decl
.ifacedef(0)
331 ifacetwi
= ifaces
['twi']
332 print ifacetwi
.ifacedef(0)
333 print twiinterface_decl
.ifacedef(0)
334 assert ifacetwi
.ifacedef(0) == twiinterface_decl
.ifacedef(0)