1 from UserDict
import UserDict
3 from wire_def
import generic_io
# special case
4 from wire_def
import muxwire
# special case
8 """ pin interface declaration.
9 * name is the name of the pin
10 * ready, enabled and io all create a (* .... *) prefix
11 * action changes it to an "in" if true
14 def __init__(self
, name
,
22 self
.enabled
= enabled
25 self
.bitspec
= bitspec
if bitspec
else '1'
27 def ifacefmt(self
, fmtfn
=None):
31 status
.append('always_ready')
33 status
.append('always_enabled')
35 status
.append('result="io"')
38 res
+= ','.join(status
)
43 name
= fmtfn(self
.name
)
47 res
+= ' (Bit#(%s) in)' % self
.bitspec
49 res
+= " Bit#(%s) " % self
.bitspec
54 def ifacedef(self
, fmtoutfn
=None, fmtinfn
=None, fmtdecfn
=None):
57 fmtname
= fmtinfn(self
.name
)
59 res
+= fmtdecfn(self
.name
)
60 res
+= '(Bit#(%s) in);\n' % self
.bitspec
61 res
+= ' %s<=in;\n' % fmtname
64 fmtname
= fmtoutfn(self
.name
)
65 res
+= "%s=%s;" % (self
.name
, fmtname
)
68 def wirefmt(self
, fmtoutfn
=None, fmtinfn
=None, fmtdecfn
=None):
69 res
= ' Wire#(Bit#(%s)) ' % self
.bitspec
71 res
+= '%s' % fmtinfn(self
.name
)
73 res
+= '%s' % fmtoutfn(self
.name
)
74 res
+= "<-mkDWire(0);"
78 class Interface(object):
79 """ create an interface from a list of pinspecs.
80 each pinspec is a dictionary, see Pin class arguments
83 def __init__(self
, ifacename
, pinspecs
):
84 self
.ifacename
= ifacename
86 self
.pinspecs
= pinspecs
90 if p
.get('outen') is True: # special case, generate 3 pins
92 for psuffix
in ['out', 'outen', 'in']:
93 _p
['name'] = "%s_%s" % (self
.pname(p
['name']), psuffix
)
94 _p
['action'] = psuffix
!= 'in'
95 self
.pins
.append(Pin(**_p
))
97 _p
['name'] = self
.pname(p
['name'])
98 self
.pins
.append(Pin(**_p
))
100 def pname(self
, name
):
101 return '%s{0}_%s' % (self
.ifacename
, name
)
103 def wirefmt(self
, *args
):
104 res
= '\n'.join(map(self
.wirefmtpin
, self
.pins
)).format(*args
)
106 for p
in self
.pinspecs
:
107 name
= self
.pname(p
['name']).format(*args
)
108 res
+= " GenericIOType %s_io = GenericIOType{\n" % name
110 if p
.get('outen') is True:
111 outname
= self
.ifacefmtoutfn(name
)
112 params
.append('outputval:%s_out,' % outname
)
113 params
.append('output_en:%s_outen,' % outname
)
114 params
.append('input_en:~%s_outen,' % outname
)
115 elif p
.get('action'):
116 outname
= self
.ifacefmtoutfn(name
)
117 params
.append('outputval:%s,' % outname
)
118 params
.append('output_en:1,')
119 params
.append('input_en:0,')
121 params
.append('outputval:0,')
122 params
.append('output_en:0,')
123 params
.append('input_en:1,')
124 params
+= ['pullup_en:0,', 'pulldown_en:0,',
125 'pushpull_en:0,', 'drivestrength:0,',
128 res
+= ' %s\n' % param
132 def ifacefmt(self
, *args
):
133 res
= '\n'.join(map(self
.ifacefmtdecpin
, self
.pins
)).format(*args
)
136 def ifacefmtdecfn(self
, name
):
139 def ifacefmtdecfn2(self
, name
):
142 def ifacefmtoutfn(self
, name
):
145 def ifacefmtinfn(self
, name
):
148 def wirefmtpin(self
, pin
):
149 return pin
.wirefmt(self
.ifacefmtoutfn
, self
.ifacefmtinfn
,
152 def ifacefmtdecpin(self
, pin
):
153 return pin
.ifacefmt(self
.ifacefmtdecfn
)
155 def ifacefmtpin(self
, pin
):
156 return pin
.ifacedef(self
.ifacefmtoutfn
, self
.ifacefmtinfn
,
159 def ifacedef(self
, *args
):
160 res
= '\n'.join(map(self
.ifacefmtpin
, self
.pins
))
161 res
= res
.format(*args
)
162 return '\n' + res
+ '\n'
165 class MuxInterface(Interface
):
167 def wirefmt(self
, *args
):
168 return muxwire
.format(*args
)
171 class IOInterface(Interface
):
173 def ifacefmtoutfn(self
, name
):
174 """ for now strip off io{0}_ part """
175 return "cell{0}_mux_out.%s" % name
[6:]
177 def ifacefmtinfn(self
, name
):
178 return "cell{0}_mux_in"
180 def wirefmt(self
, *args
):
181 return generic_io
.format(*args
)
184 class Interfaces(UserDict
):
185 """ contains a list of interface definitions
190 UserDict
.__init
__(self
, {})
191 with
open('interfaces.txt', 'r') as ifile
:
192 for l
in ifile
.readlines():
197 spec
= self
.read_spec(name
)
198 self
.ifaceadd(name
, count
, Interface(name
, spec
))
200 def ifaceadd(self
, name
, count
, iface
, at
=None):
202 at
= len(self
.ifacecount
)
203 self
.ifacecount
.insert(at
, (name
, count
))
206 def read_spec(self
, name
):
208 with
open('%s.txt' % name
, 'r') as sfile
:
209 for l
in sfile
.readlines():
215 elif l
[1] == 'inout':
220 def ifacedef(self
, f
, *args
):
221 for (name
, count
) in self
.ifacecount
:
222 for i
in range(count
):
223 f
.write(self
.data
[name
].ifacedef(i
))
225 def ifacefmt(self
, f
, *args
):
227 // interface declaration between %s-{0} and pinmux'''
228 for (name
, count
) in self
.ifacecount
:
229 for i
in range(count
):
230 c
= comment
% name
.upper()
232 f
.write(self
.data
[name
].ifacefmt(i
))
234 def wirefmt(self
, f
, *args
):
235 comment
= '\n // following wires capture signals ' \
236 'to IO CELL if %s-{0} is\n' \
238 for (name
, count
) in self
.ifacecount
:
239 for i
in range(count
):
242 f
.write(self
.data
[name
].wirefmt(i
))
245 # ========= Interface declarations ================ #
247 mux_interface
= MuxInterface('cell', [{'name': 'mux', 'ready': False,
249 'bitspec': '{1}', 'action': True}])
251 io_interface
= IOInterface('io',
252 [{'name': 'outputval', 'enabled': False},
253 {'name': 'output_en', 'enabled': False},
254 {'name': 'input_en', 'enabled': False},
255 {'name': 'pullup_en', 'enabled': False},
256 {'name': 'pulldown_en', 'enabled': False},
257 {'name': 'drivestrength', 'enabled': False},
258 {'name': 'pushpull_en', 'enabled': False},
259 {'name': 'opendrain_en', 'enabled': False},
260 {'name': 'inputval', 'action': True, 'io': True},
263 # == Peripheral Interface definitions == #
264 # these are the interface of the peripherals to the pin mux
265 # Outputs from the peripherals will be inputs to the pinmux
266 # module. Hence the change in direction for most pins
268 # ======================================= #
271 if __name__
== '__main__':
273 uartinterface_decl
= Interface('uart',
275 {'name': 'tx', 'action': True},
278 twiinterface_decl
= Interface('twi',
279 [{'name': 'sda', 'outen': True},
280 {'name': 'scl', 'outen': True},
283 def _pinmunge(p
, sep
, repl
, dedupe
=True):
284 """ munges the text so it's easier to compare.
285 splits by separator, strips out blanks, re-joins.
290 p
= filter(lambda x
: x
, p
) # filter out blanks
294 """ munges the text so it's easier to compare.
296 # first join lines by semicolons, strip out returns
298 p
= map(lambda x
: x
.replace('\n', ''), p
)
300 # now split first by brackets, then spaces (deduping on spaces)
301 p
= _pinmunge(p
, "(", " ( ", False)
302 p
= _pinmunge(p
, ")", " ) ", False)
303 p
= _pinmunge(p
, " ", " ")
309 for p1
, p2
in zip(l1
, l2
):
315 ifaces
= Interfaces()
317 ifaceuart
= ifaces
['uart']
318 print ifaceuart
.ifacedef(0)
319 print uartinterface_decl
.ifacedef(0)
320 assert ifaceuart
.ifacedef(0) == uartinterface_decl
.ifacedef(0)
322 ifacetwi
= ifaces
['twi']
323 print ifacetwi
.ifacedef(0)
324 print twiinterface_decl
.ifacedef(0)
325 assert ifacetwi
.ifacedef(0) == twiinterface_decl
.ifacedef(0)