2 from copy
import deepcopy
6 def __init__(self
, name
):
9 def axibase(self
, name
, ifacenum
):
11 return "%(name)s%(ifacenum)dBase" % locals()
13 def axiend(self
, name
, ifacenum
):
15 return "%(name)s%(ifacenum)dEnd" % locals()
17 def axi_reg_def(self
, start
, name
, ifacenum
):
19 offs
= self
.num_axi_regs32() * 4 * 16
20 end
= start
+ offs
- 1
21 bname
= self
.axibase(name
, ifacenum
)
22 bend
= self
.axiend(name
, ifacenum
)
23 comment
= "%d 32-bit regs" % self
.num_axi_regs32()
24 return (" `define%(bname)s 'h%(start)08X\n"
25 " `define%(bend)s 'h%(end)08X // %(comment)s" % locals(),
28 def axi_slave_name(self
, name
, ifacenum
):
30 return "{0}{1}_slave_num".format(name
, ifacenum
)
32 def axi_slave_idx(self
, idx
, name
, ifacenum
):
33 name
= self
.axi_slave_name(name
, ifacenum
)
34 return ("typedef {0} {1};".format(idx
, name
), 1)
36 def axi_addr_map(self
, name
, ifacenum
):
37 bname
= self
.axibase(name
, ifacenum
)
38 bend
= self
.axiend(name
, ifacenum
)
39 name
= self
.axi_slave_name(name
, ifacenum
)
41 if(addr>=`{0} && addr<=`{1})
42 return tuple2(True,fromInteger(valueOf({2})));
43 else""".format(bname
, bend
, name
)
45 def mk_pincon(self
, name
, count
):
46 # TODO: really should be using bsv.interface_decl.Interfaces
47 # pin-naming rules.... logic here is hard-coded to duplicate
48 # it (see Interface.__init__ outen)
50 for p
in self
.peripheral
.pinspecs
:
53 #n = "{0}{1}".format(self.name, self.mksuffix(name, count))
54 n
= name
#"{0}{1}".format(self.name, self.mksuffix(name, count))
55 ret
.append(" //%s %s" % (n
, str(p
)))
56 sname
= self
.peripheral
.pname(pname
).format(count
)
57 ps
= "pinmux.peripheral_side.%s" % sname
58 if typ
== 'out' or typ
== 'inout':
59 ret
.append(" rule con_%s%d_%s_out" % (name
, count
, pname
))
60 fname
= self
.pinname_out(pname
)
66 n_
= "{0}{1}".format(n
, count
)
67 ret
.append(" {0}({1}.{2});".format(ps_
, n_
, fname
))
70 fname
= self
.pinname_outen(pname
)
72 fname
= "{0}{1}.{2}".format(n
, count
, fname
)
73 fname
= self
.pinname_tweak(pname
, 'outen', fname
)
74 ret
.append(" {0}_outen({1});".format(ps
, fname
))
75 ret
.append(" endrule")
76 if typ
== 'in' or typ
== 'inout':
77 fname
= self
.pinname_in(pname
)
83 ret
.append(" rule con_%s%d_%s_in" % (name
, count
, pname
))
84 ret
.append(" {1}.{2}({0});".format(ps_
, n
, fname
))
85 ret
.append(" endrule")
88 def mk_cellconn(self
, *args
):
91 def mkslow_peripheral(self
):
94 def mksuffix(self
, name
, i
):
97 def __mk_connection(self
, con
, aname
):
98 txt
= " mkConnection (slow_fabric.v_to_slaves\n" + \
99 " [fromInteger(valueOf({1}))],\n" + \
102 print "PBase __mk_connection", self
.name
, aname
105 return txt
.format(con
, aname
)
107 def mk_connection(self
, count
, name
=None):
110 print "PBase mk_conn", self
.name
, count
111 aname
= self
.axi_slave_name(name
, count
)
112 #dname = self.mksuffix(name, count)
113 #dname = "{0}{1}".format(name, dname)
114 con
= self
._mk
_connection
(name
, count
).format(count
, aname
)
115 return self
.__mk
_connection
(con
, aname
)
117 def _mk_connection(self
, name
=None, count
=0):
120 def pinname_out(self
, pname
):
123 def pinname_in(self
, pname
):
126 def pinname_outen(self
, pname
):
129 def pinname_tweak(self
, pname
, typ
, txt
):
134 def slowimport(self
):
135 return " import Uart16550 :: *;"
137 def slowifdecl(self
):
138 return " interface RS232_PHY_Ifc uart{0}_coe;\n" + \
139 " method Bit#(1) uart{0}_intr;"
141 def num_axi_regs32(self
):
144 def mkslow_peripheral(self
):
145 return " Uart16550_AXI4_Lite_Ifc uart{0} <- \n" + \
146 " mkUart16550(clocked_by uart_clock,\n" + \
147 " reset_by uart_reset, sp_clock, sp_reset);"
149 def _mk_connection(self
, name
=None, count
=0):
150 return "uart{0}.slave_axi_uart"
152 def pinname_out(self
, pname
):
153 return {'tx': 'coe_rs232.sout'}.get(pname
, '')
155 def pinname_in(self
, pname
):
156 return {'rx': 'coe_rs232.sin'}.get(pname
, '')
161 def slowimport(self
):
162 return " import Uart_bs::*;\n" + \
163 " import RS232_modified::*;"
165 def slowifdecl(self
):
166 return " interface RS232 uart{0}_coe;"
168 def num_axi_regs32(self
):
171 def mkslow_peripheral(self
):
172 return " //Ifc_Uart_bs uart{0} <-" + \
173 " // mkUart_bs(clocked_by uart_clock,\n" + \
174 " // reset_by uart_reset,sp_clock, sp_reset);" +\
175 " Ifc_Uart_bs uart{0} <-" + \
176 " mkUart_bs(clocked_by sp_clock,\n" + \
177 " reset_by sp_reset, sp_clock, sp_reset);"
179 def _mk_connection(self
, name
=None, count
=0):
180 return "uart{0}.slave_axi_uart"
182 def pinname_out(self
, pname
):
183 return {'tx': 'coe_rs232.sout'}.get(pname
, '')
185 def pinname_in(self
, pname
):
186 return {'rx': 'coe_rs232.sin'}.get(pname
, '')
191 def slowimport(self
):
192 return " import I2C_top :: *;"
194 def slowifdecl(self
):
195 return " interface I2C_out twi{0}_out;\n" + \
196 " method Bit#(1) twi{0}_isint;"
198 def num_axi_regs32(self
):
201 def mkslow_peripheral(self
):
202 return " I2C_IFC twi{0} <- mkI2CController();"
204 def _mk_connection(self
, name
=None, count
=0):
205 return "twi{0}.slave_i2c_axi"
207 def pinname_out(self
, pname
):
208 return {'sda': 'out.sda_out',
209 'scl': 'out.scl_out'}.get(pname
, '')
211 def pinname_in(self
, pname
):
212 return {'sda': 'out.sda_in',
213 'scl': 'out.scl_in'}.get(pname
, '')
215 def pinname_outen(self
, pname
):
216 return {'sda': 'out.sda_outen',
217 'scl': 'out.scl_outen'}.get(pname
, '')
219 def pinname_tweak(self
, pname
, typ
, txt
):
221 return "pack({0})".format(txt
)
227 def slowimport(self
):
228 return " import qspi :: *;"
230 def slowifdecl(self
):
231 return " interface QSPI_out qspi{0}_out;\n" + \
232 " method Bit#(1) qspi{0}_isint;"
234 def num_axi_regs32(self
):
237 def mkslow_peripheral(self
):
238 return " Ifc_qspi qspi{0} <- mkqspi();"
240 def _mk_connection(self
, name
=None, count
=0):
241 return "qspi{0}.slave"
246 def slowimport(self
):
247 return " import pwm::*;"
249 def slowifdecl(self
):
250 return " interface PWMIO pwm{0}_o;"
252 def num_axi_regs32(self
):
255 def mkslow_peripheral(self
):
256 return " Ifc_PWM_bus pwm{0}_bus <- mkPWM_bus(sp_clock);"
258 def _mk_connection(self
, name
=None, count
=0):
259 return "pwm{0}_bus.axi4_slave"
261 def pinname_out(self
, pname
):
262 return {'out': 'pwm_io.pwm_o'}.get(pname
, '')
267 def slowimport(self
):
268 return " import pinmux::*;\n" + \
269 " import mux::*;\n" + \
272 def slowifdecl(self
):
273 return " interface GPIO_config#({1}) pad_config{0};"
275 def num_axi_regs32(self
):
278 def axi_slave_idx(self
, idx
, name
, ifacenum
):
279 """ generates AXI slave number definition, except
280 GPIO also has a muxer per bank
283 (ret
, x
) = PBase
.axi_slave_idx(self
, idx
, name
, ifacenum
)
284 (ret2
, x
) = PBase
.axi_slave_idx(self
, idx
, "mux", ifacenum
)
285 return ("%s\n%s" % (ret
, ret2
), 2)
287 def mkslow_peripheral(self
):
288 return " MUX#(%(name)s) mux{0} <- mkmux();\n" + \
289 " GPIO#(%(name)s) gpio{0} <- mkgpio();" % \
292 def mk_connection(self
, count
):
293 print "GPIO mk_conn", self
.name
, count
295 dname
= self
.mksuffix(self
.name
, count
)
296 for i
, n
in enumerate(['gpio' + dname
, 'mux' + dname
]):
297 res
.append(PBase
.mk_connection(self
, count
, n
))
298 return '\n'.join(res
)
300 def _mk_connection(self
, name
=None, count
=0):
301 n
= self
.mksuffix(name
, count
)
302 if name
.startswith('gpio'):
303 return "gpio{0}.axi_slave".format(n
)
304 if name
.startswith('mux'):
305 return "mux{0}.axi_slave".format(n
)
307 def mksuffix(self
, name
, i
):
308 if name
.startswith('mux'):
312 def mk_cellconn(self
, cellnum
, name
, count
):
314 bank
= self
.mksuffix(name
, count
)
315 txt
= " pinmux.mux_lines.cell{0}_mux(mux{1}.mux_config.mux[{2}]);"
316 for p
in self
.peripheral
.pinspecs
:
317 ret
.append(txt
.format(cellnum
, bank
, p
['name'][1:]))
319 return ("\n".join(ret
), cellnum
)
321 def pinname_out(self
, pname
):
322 return "func.gpio_out[{0}]".format(pname
[1:])
324 def pinname_outen(self
, pname
):
325 return {'sda': 'out.sda_outen',
326 'scl': 'out.scl_outen'}.get(pname
, '')
328 def mk_pincon(self
, name
, count
):
329 ret
= [PBase
.mk_pincon(self
, name
, count
)]
330 # special-case for gpio in, store in a temporary vector
331 plen
= len(self
.peripheral
.pinspecs
)
332 ret
.append(" rule con_%s%d_in" % (name
, count
))
333 ret
.append(" Vector#({0},Bit#(1)) temp;".format(plen
))
334 for p
in self
.peripheral
.pinspecs
:
339 sname
= self
.peripheral
.pname(pname
).format(count
)
340 ps
= "pinmux.peripheral_side.%s_in" % sname
341 ret
.append(" temp[{0}]={1};".format(idx
, ps
))
342 ret
.append(" {0}.func.gpio_in(temp);".format(name
))
343 ret
.append(" endrule")
344 return '\n'.join(ret
)
347 axi_slave_declarations
= """\
348 typedef 0 SlowMaster;
350 typedef TAdd#(LastGen_slave_num,`ifdef CLINT 1 `else 0 `endif )
352 typedef TAdd#(CLINT_slave_num ,`ifdef PLIC 1 `else 0 `endif )
354 typedef TAdd#(Plic_slave_num ,`ifdef AXIEXP 1 `else 0 `endif )
356 typedef TAdd#(AxiExp1_slave_num,1) Num_Slow_Slaves;
359 pinmux_cellrule
= """\
360 rule connect_select_lines_pinmux;
366 class CallFn(object):
367 def __init__(self
, peripheral
, name
):
368 self
.peripheral
= peripheral
371 def __call__(self
, *args
):
372 #print "__call__", self.name, self.peripheral.slow, args
373 if not self
.peripheral
.slow
:
375 return getattr(self
.peripheral
.slow
, self
.name
)(*args
[1:])
378 class PeripheralIface(object):
379 def __init__(self
, ifacename
):
381 slow
= slowfactory
.getcls(ifacename
)
382 print "Iface", ifacename
, slow
384 self
.slow
= slow(ifacename
)
385 self
.slow
.peripheral
= self
386 for fname
in ['slowimport', 'slowifdecl', 'mkslow_peripheral',
387 'mk_connection', 'mk_cellconn', 'mk_pincon']:
388 fn
= CallFn(self
, fname
)
389 setattr(self
, fname
, types
.MethodType(fn
, self
))
391 #print "PeripheralIface"
394 def mksuffix(self
, name
, i
):
395 if self
.slow
is None:
397 return self
.slow
.mksuffix(name
, i
)
399 def axi_reg_def(self
, start
, count
):
402 return self
.slow
.axi_reg_def(start
, self
.ifacename
, count
)
404 def axi_slave_idx(self
, start
, count
):
407 return self
.slow
.axi_slave_idx(start
, self
.ifacename
, count
)
409 def axi_addr_map(self
, count
):
412 return self
.slow
.axi_addr_map(self
.ifacename
, count
)
415 class PeripheralInterfaces(object):
419 def slowimport(self
, *args
):
421 for (name
, count
) in self
.ifacecount
:
422 #print "slowimport", name, self.data[name].slowimport
423 ret
.append(self
.data
[name
].slowimport())
424 return '\n'.join(list(filter(None, ret
)))
426 def slowifdecl(self
, *args
):
428 for (name
, count
) in self
.ifacecount
:
429 for i
in range(count
):
430 ret
.append(self
.data
[name
].slowifdecl().format(i
, name
))
431 return '\n'.join(list(filter(None, ret
)))
433 def axi_reg_def(self
, *args
):
435 start
= 0x00011100 # start of AXI peripherals address
436 for (name
, count
) in self
.ifacecount
:
437 for i
in range(count
):
438 x
= self
.data
[name
].axi_reg_def(start
, i
)
439 #print ("ifc", name, x)
443 return '\n'.join(list(filter(None, ret
)))
445 def axi_slave_idx(self
, *args
):
448 for (name
, count
) in self
.ifacecount
:
449 for i
in range(count
):
450 (rdef
, offs
) = self
.data
[name
].axi_slave_idx(start
, i
)
451 #print ("ifc", name, rdef, offs)
454 ret
.append("typedef %d LastGen_slave_num" % (start
- 1))
455 decls
= '\n'.join(list(filter(None, ret
)))
456 return axi_slave_declarations
.format(decls
)
458 def axi_addr_map(self
, *args
):
460 for (name
, count
) in self
.ifacecount
:
461 for i
in range(count
):
462 ret
.append(self
.data
[name
].axi_addr_map(i
))
463 return '\n'.join(list(filter(None, ret
)))
465 def mkslow_peripheral(self
, *args
):
467 for (name
, count
) in self
.ifacecount
:
468 for i
in range(count
):
469 print "mkslow", name
, count
470 x
= self
.data
[name
].mkslow_peripheral()
472 suffix
= self
.data
[name
].mksuffix(name
, i
)
473 ret
.append(x
.format(suffix
))
474 return '\n'.join(list(filter(None, ret
)))
476 def mk_connection(self
, *args
):
478 for (name
, count
) in self
.ifacecount
:
479 for i
in range(count
):
480 print "mk_conn", name
, i
481 txt
= self
.data
[name
].mk_connection(i
)
484 print self
.data
[name
].mk_connection
486 return '\n'.join(list(filter(None, ret
)))
488 def mk_cellconn(self
):
491 for (name
, count
) in self
.ifacecount
:
492 for i
in range(count
):
493 res
= self
.data
[name
].mk_cellconn(cellcount
, name
, i
)
496 (txt
, cellcount
) = res
498 ret
= '\n'.join(list(filter(None, ret
)))
499 return pinmux_cellrule
.format(ret
)
503 for (name
, count
) in self
.ifacecount
:
504 for i
in range(count
):
505 txt
= self
.data
[name
].mk_pincon(name
, i
)
507 return '\n'.join(list(filter(None, ret
)))
509 class PFactory(object):
510 def getcls(self
, name
):
511 for k
, v
in {'uart': uart
,
518 if name
.startswith(k
):
523 slowfactory
= PFactory()
525 if __name__
== '__main__':
529 i
= PeripheralIface('uart')
531 i
= PeripheralIface('gpioa')