1 from UserDict
import UserDict
3 from wire_def
import generic_io
# special case
4 from wire_def
import muxwire
# special case
7 """ pin interface declaration.
8 * name is the name of the pin
9 * ready, enabled and io all create a (* .... *) prefix
10 * action changes it to an "in" if true
13 def __init__(self
, name
,
21 self
.enabled
= enabled
24 self
.bitspec
= bitspec
if bitspec
else '1'
26 def ifacefmt(self
, fmtfn
=None):
30 status
.append('always_ready')
32 status
.append('always_enabled')
34 status
.append('result="io"')
37 res
+= ','.join(status
)
42 name
= fmtfn(self
.name
)
46 res
+= ' (Bit#(%s) in)' % self
.bitspec
48 res
+= " Bit#(%s) " % self
.bitspec
53 def ifacedef(self
, fmtoutfn
=None, fmtinfn
=None, fmtdecfn
=None):
56 fmtname
= fmtinfn(self
.name
)
58 res
+= fmtdecfn(self
.name
)
59 res
+= '(Bit#(%s) in);\n' % self
.bitspec
60 res
+= ' %s<=in;\n' % fmtname
63 fmtname
= fmtoutfn(self
.name
)
64 res
+= "%s=%s;" % (self
.name
, fmtname
)
67 def wirefmt(self
, fmtoutfn
=None, fmtinfn
=None, fmtdecfn
=None):
68 res
= ' Wire#(Bit#(%s)) ' % self
.bitspec
70 res
+= '%s' % fmtinfn(self
.name
)
72 res
+= '%s' % fmtoutfn(self
.name
)
73 res
+= "<-mkDWire(0);"
77 class Interface(object):
78 """ create an interface from a list of pinspecs.
79 each pinspec is a dictionary, see Pin class arguments
82 def __init__(self
, ifacename
, pinspecs
):
83 self
.ifacename
= ifacename
85 self
.pinspecs
= pinspecs
89 if p
.get('outen') is True: # special case, generate 3 pins
91 for psuffix
in ['out', 'outen', 'in']:
92 _p
['name'] = "%s_%s" % (self
.pname(p
['name']), psuffix
)
93 _p
['action'] = psuffix
!= 'in'
94 self
.pins
.append(Pin(**_p
))
96 _p
['name'] = self
.pname(p
['name'])
97 self
.pins
.append(Pin(**_p
))
99 def pname(self
, name
):
100 return '%s{0}_%s' % (self
.ifacename
, name
)
102 def wirefmt(self
, *args
):
103 res
= '\n'.join(map(self
.wirefmtpin
, self
.pins
)).format(*args
)
105 for p
in self
.pinspecs
:
106 name
= self
.pname(p
['name']).format(*args
)
107 res
+= " GenericIOType %s_io = GenericIOType{\n" % name
109 if p
.get('outen') is True:
110 outname
= self
.ifacefmtoutfn(name
)
111 params
.append('outputval:%s_out,' % outname
)
112 params
.append('output_en:%s_outen,' % outname
)
113 params
.append('input_en:~%s_outen,' % outname
)
114 elif p
.get('action'):
115 outname
= self
.ifacefmtoutfn(name
)
116 params
.append('outputval:%s,' % outname
)
117 params
.append('output_en:1,')
118 params
.append('input_en:0,')
120 params
.append('outputval:0,')
121 params
.append('output_en:0,')
122 params
.append('input_en:1,')
123 params
+= ['pullup_en:0,', 'pulldown_en:0,',
124 'pushpull_en:0,', 'drivestrength:0,',
127 res
+= ' %s\n' % param
131 def ifacefmt(self
, *args
):
132 res
= '\n'.join(map(self
.ifacefmtdecpin
, self
.pins
)).format(*args
)
135 def ifacefmtdecfn(self
, name
):
138 def ifacefmtdecfn2(self
, name
):
141 def ifacefmtoutfn(self
, name
):
144 def ifacefmtinfn(self
, name
):
147 def wirefmtpin(self
, pin
):
148 return pin
.wirefmt(self
.ifacefmtoutfn
, self
.ifacefmtinfn
,
151 def ifacefmtdecpin(self
, pin
):
152 return pin
.ifacefmt(self
.ifacefmtdecfn
)
154 def ifacefmtpin(self
, pin
):
155 return pin
.ifacedef(self
.ifacefmtoutfn
, self
.ifacefmtinfn
,
158 def ifacedef(self
, *args
):
159 res
= '\n'.join(map(self
.ifacefmtpin
, self
.pins
))
160 res
= res
.format(*args
)
161 return '\n' + res
+ '\n'
163 class MuxInterface(Interface
):
165 def wirefmt(self
, *args
):
166 return muxwire
.format(*args
)
168 class IOInterface(Interface
):
170 def ifacefmtoutfn(self
, name
):
171 """ for now strip off io{0}_ part """
172 return "cell{0}_mux_out.%s" % name
[6:]
174 def ifacefmtinfn(self
, name
):
175 return "cell{0}_mux_in"
177 def wirefmt(self
, *args
):
178 return generic_io
.format(*args
)
181 class Interfaces(UserDict
):
182 """ contains a list of interface definitions
187 UserDict
.__init
__(self
, {})
188 with
open('interfaces.txt', 'r') as ifile
:
189 for l
in ifile
.readlines():
194 spec
= self
.read_spec(name
)
195 self
.ifaceadd(name
, count
, Interface(name
, spec
))
197 def ifaceadd(self
, name
, count
, iface
, at
=None):
199 at
= len(self
.ifacecount
)
200 self
.ifacecount
.insert(at
, (name
, count
))
203 def read_spec(self
, name
):
205 with
open('%s.txt' % name
, 'r') as sfile
:
206 for l
in sfile
.readlines():
212 elif l
[1] == 'inout':
217 def ifacedef(self
, f
, *args
):
218 for (name
, count
) in self
.ifacecount
:
219 for i
in range(count
):
220 f
.write(self
.data
[name
].ifacedef(i
))
222 def ifacefmt(self
, f
, *args
):
224 // interface declaration between %s-{0} and pinmux'''
225 for (name
, count
) in self
.ifacecount
:
226 for i
in range(count
):
227 c
= comment
% name
.upper()
229 f
.write(self
.data
[name
].ifacefmt(i
))
231 def wirefmt(self
, f
, *args
):
232 comment
= '\n // following wires capture signals ' \
233 'to IO CELL if %s-{0} is\n' \
235 for (name
, count
) in self
.ifacecount
:
236 for i
in range(count
):
239 f
.write(self
.data
[name
].wirefmt(i
))
242 # ========= Interface declarations ================ #
244 mux_interface
= MuxInterface('cell', [{'name': 'mux', 'ready': False,
246 'bitspec': '{1}', 'action': True}])
248 io_interface
= IOInterface('io',
249 [{'name': 'outputval', 'enabled': False},
250 {'name': 'output_en', 'enabled': False},
251 {'name': 'input_en', 'enabled': False},
252 {'name': 'pullup_en', 'enabled': False},
253 {'name': 'pulldown_en', 'enabled': False},
254 {'name': 'drivestrength', 'enabled': False},
255 {'name': 'pushpull_en', 'enabled': False},
256 {'name': 'opendrain_en', 'enabled': False},
257 {'name': 'inputval', 'action': True, 'io': True},
260 # == Peripheral Interface definitions == #
261 # these are the interface of the peripherals to the pin mux
262 # Outputs from the peripherals will be inputs to the pinmux
263 # module. Hence the change in direction for most pins
265 # ======================================= #
268 if __name__
== '__main__':
270 uartinterface_decl
= Interface('uart',
272 {'name': 'tx', 'action': True},
275 twiinterface_decl
= Interface('twi',
276 [{'name': 'sda', 'outen': True},
277 {'name': 'scl', 'outen': True},
280 def _pinmunge(p
, sep
, repl
, dedupe
=True):
281 """ munges the text so it's easier to compare.
282 splits by separator, strips out blanks, re-joins.
287 p
= filter(lambda x
: x
, p
) # filter out blanks
291 """ munges the text so it's easier to compare.
293 # first join lines by semicolons, strip out returns
295 p
= map(lambda x
: x
.replace('\n', ''), p
)
297 # now split first by brackets, then spaces (deduping on spaces)
298 p
= _pinmunge(p
, "(", " ( ", False)
299 p
= _pinmunge(p
, ")", " ) ", False)
300 p
= _pinmunge(p
, " ", " ")
306 for p1
, p2
in zip(l1
, l2
):
312 ifaces
= Interfaces()
314 ifaceuart
= ifaces
['uart']
315 print ifaceuart
.ifacedef(0)
316 print uartinterface_decl
.ifacedef(0)
317 assert ifaceuart
.ifacedef(0) == uartinterface_decl
.ifacedef(0)
319 ifacetwi
= ifaces
['twi']
320 print ifacetwi
.ifacedef(0)
321 print twiinterface_decl
.ifacedef(0)
322 assert ifacetwi
.ifacedef(0) == twiinterface_decl
.ifacedef(0)