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
):
47 for p
in self
.peripheral
.pinspecs
:
50 #n = "{0}{1}".format(self.name, self.mksuffix(name, count))
51 n
= name
#"{0}{1}".format(self.name, self.mksuffix(name, count))
52 ret
.append(" //%s %s" % (n
, str(p
)))
53 sname
= self
.peripheral
.pname(pname
).format(count
)
54 ps
= "pinmux.peripheral_side.%s" % sname
55 if typ
== 'out' or typ
== 'inout':
56 ret
.append(" rule con_%s%d_%s_out" % (name
, count
, pname
))
57 fname
= self
.pinname_out(pname
)
59 ret
.append(" {0}_out({1}.{2});".format(ps
, n
, fname
))
62 fname
= self
.pinname_outen(pname
)
64 fname
= "{0}{1}.{2}".format(n
, count
, fname
)
65 fname
= self
.pinname_tweak(pname
, 'outen', fname
)
66 ret
.append(" {0}_outen({1});".format(ps
, fname
))
67 ret
.append(" endrule")
68 if typ
== 'in' or typ
== 'inout':
69 fname
= self
.pinname_in(pname
)
71 ret
.append(" rule con_%s%d_%s_in" % (name
, count
, pname
))
72 ret
.append(" {1}.{2}({0}_in);".format(ps
, n
, fname
))
73 ret
.append(" endrule")
76 def mk_cellconn(self
, *args
):
79 def mkslow_peripheral(self
):
82 def mksuffix(self
, name
, i
):
85 def __mk_connection(self
, con
, aname
):
86 txt
= " mkConnection (slow_fabric.v_to_slaves\n" + \
87 " [fromInteger(valueOf({1}))],\n" + \
90 print "PBase __mk_connection", self
.name
, aname
93 return txt
.format(con
, aname
)
95 def mk_connection(self
, count
, name
=None):
98 print "PBase mk_conn", self
.name
, count
99 aname
= self
.axi_slave_name(name
, count
)
100 #dname = self.mksuffix(name, count)
101 #dname = "{0}{1}".format(name, dname)
102 con
= self
._mk
_connection
(name
, count
).format(count
, aname
)
103 return self
.__mk
_connection
(con
, aname
)
105 def _mk_connection(self
, name
=None, count
=0):
108 def pinname_out(self
, pname
):
111 def pinname_in(self
, pname
):
114 def pinname_outen(self
, pname
):
117 def pinname_tweak(self
, pname
, typ
, txt
):
122 def slowimport(self
):
123 return " import Uart16550 :: *;"
125 def slowifdecl(self
):
126 return " interface RS232_PHY_Ifc uart{0}_coe;\n" + \
127 " method Bit#(1) uart{0}_intr;"
129 def num_axi_regs32(self
):
132 def mkslow_peripheral(self
):
133 return " Uart16550_AXI4_Lite_Ifc uart{0} <- \n" + \
134 " mkUart16550(clocked_by uart_clock,\n" + \
135 " reset_by uart_reset, sp_clock, sp_reset);"
137 def _mk_connection(self
, name
=None, count
=0):
138 return "uart{0}.slave_axi_uart"
140 def pinname_out(self
, pname
):
141 return {'tx': 'coe_rs232.sout'}.get(pname
, '')
143 def pinname_in(self
, pname
):
144 return {'rx': 'coe_rs232.sin'}.get(pname
, '')
149 def slowimport(self
):
150 return " import Uart_bs::*;\n" + \
151 " import RS232_modified::*;"
153 def slowifdecl(self
):
154 return " interface RS232 uart{0}_coe;"
156 def num_axi_regs32(self
):
159 def mkslow_peripheral(self
):
160 return " //Ifc_Uart_bs uart{0} <-" + \
161 " // mkUart_bs(clocked_by uart_clock,\n" + \
162 " // reset_by uart_reset,sp_clock, sp_reset);" +\
163 " Ifc_Uart_bs uart{0} <-" + \
164 " mkUart_bs(clocked_by sp_clock,\n" + \
165 " reset_by sp_reset, sp_clock, sp_reset);"
167 def _mk_connection(self
, name
=None, count
=0):
168 return "uart{0}.slave_axi_uart"
170 def pinname_out(self
, pname
):
171 return {'tx': 'coe_rs232.sout'}.get(pname
, '')
173 def pinname_in(self
, pname
):
174 return {'rx': 'coe_rs232.sin'}.get(pname
, '')
179 def slowimport(self
):
180 return " import I2C_top :: *;"
182 def slowifdecl(self
):
183 return " interface I2C_out twi{0}_out;\n" + \
184 " method Bit#(1) twi{0}_isint;"
186 def num_axi_regs32(self
):
189 def mkslow_peripheral(self
):
190 return " I2C_IFC twi{0} <- mkI2CController();"
192 def _mk_connection(self
, name
=None, count
=0):
193 return "twi{0}.slave_i2c_axi"
195 def pinname_out(self
, pname
):
196 return {'sda': 'out.sda_out',
197 'scl': 'out.scl_out'}.get(pname
, '')
199 def pinname_in(self
, pname
):
200 return {'sda': 'out.sda_in',
201 'scl': 'out.scl_in'}.get(pname
, '')
203 def pinname_outen(self
, pname
):
204 return {'sda': 'out.sda_outen',
205 'scl': 'out.scl_outen'}.get(pname
, '')
207 def pinname_tweak(self
, pname
, typ
, txt
):
209 return "pack({0})".format(txt
)
215 def slowimport(self
):
216 return " import qspi :: *;"
218 def slowifdecl(self
):
219 return " interface QSPI_out qspi{0}_out;\n" + \
220 " method Bit#(1) qspi{0}_isint;"
222 def num_axi_regs32(self
):
225 def mkslow_peripheral(self
):
226 return " Ifc_qspi qspi{0} <- mkqspi();"
228 def _mk_connection(self
, name
=None, count
=0):
229 return "qspi{0}.slave"
234 def slowimport(self
):
235 return " import pwm::*;"
237 def slowifdecl(self
):
238 return " interface PWMIO pwm{0}_o;"
240 def num_axi_regs32(self
):
243 def mkslow_peripheral(self
):
244 return " Ifc_PWM_bus pwm{0}_bus <- mkPWM_bus(sp_clock);"
246 def _mk_connection(self
, name
=None, count
=0):
247 return "pwm{0}_bus.axi4_slave"
252 def slowimport(self
):
253 return " import pinmux::*;\n" + \
254 " import mux::*;\n" + \
257 def slowifdecl(self
):
258 return " interface GPIO_config#({1}) pad_config{0};"
260 def num_axi_regs32(self
):
263 def axi_slave_idx(self
, idx
, name
, ifacenum
):
264 """ generates AXI slave number definition, except
265 GPIO also has a muxer per bank
268 (ret
, x
) = PBase
.axi_slave_idx(self
, idx
, name
, ifacenum
)
269 (ret2
, x
) = PBase
.axi_slave_idx(self
, idx
, "mux", ifacenum
)
270 return ("%s\n%s" % (ret
, ret2
), 2)
272 def mkslow_peripheral(self
):
273 return " MUX#(%(name)s) mux{0} <- mkmux();\n" + \
274 " GPIO#(%(name)s) gpio{0} <- mkgpio();" % \
277 def mk_connection(self
, count
):
278 print "GPIO mk_conn", self
.name
, count
280 dname
= self
.mksuffix(self
.name
, count
)
281 for i
, n
in enumerate(['gpio' + dname
, 'mux' + dname
]):
282 res
.append(PBase
.mk_connection(self
, count
, n
))
283 return '\n'.join(res
)
285 def _mk_connection(self
, name
=None, count
=0):
286 n
= self
.mksuffix(name
, count
)
287 if name
.startswith('gpio'):
288 return "gpio{0}.axi_slave".format(n
)
289 if name
.startswith('mux'):
290 return "mux{0}.axi_slave".format(n
)
292 def mksuffix(self
, name
, i
):
293 if name
.startswith('mux'):
297 def mk_cellconn(self
, cellnum
, name
, count
):
299 bank
= self
.mksuffix(name
, count
)
300 txt
= " pinmux.mux_lines.cell{0}_mux(mux{1}.mux_config.mux[{2}]);"
301 for p
in self
.peripheral
.pinspecs
:
302 ret
.append(txt
.format(cellnum
, bank
, p
['name'][1:]))
304 return ("\n".join(ret
), cellnum
)
306 def pinname_out(self
, pname
):
307 return "func.gpio_out[{0}]".format(pname
[1:])
309 def pinname_outen(self
, pname
):
310 return {'sda': 'out.sda_outen',
311 'scl': 'out.scl_outen'}.get(pname
, '')
313 def mk_pincon(self
, name
, count
):
314 ret
= [PBase
.mk_pincon(self
, name
, count
)]
315 # special-case for gpio in, store in a temporary vector
316 plen
= len(self
.peripheral
.pinspecs
)
317 ret
.append(" rule con_%s%d_in" % (name
, count
))
318 ret
.append(" Vector#({0},Bit#(1)) temp;".format(plen
))
319 for p
in self
.peripheral
.pinspecs
:
324 sname
= self
.peripheral
.pname(pname
).format(count
)
325 ps
= "pinmux.peripheral_side.%s_in" % sname
326 ret
.append(" temp[{0}]={1};".format(idx
, ps
))
327 ret
.append(" {0}.func.gpio_in(temp);".format(name
))
328 ret
.append(" endrule")
329 return '\n'.join(ret
)
332 axi_slave_declarations
= """\
333 typedef 0 SlowMaster;
335 typedef TAdd#(LastGen_slave_num,`ifdef CLINT 1 `else 0 `endif )
337 typedef TAdd#(CLINT_slave_num ,`ifdef PLIC 1 `else 0 `endif )
339 typedef TAdd#(Plic_slave_num ,`ifdef AXIEXP 1 `else 0 `endif )
341 typedef TAdd#(AxiExp1_slave_num,1) Num_Slow_Slaves;
344 pinmux_cellrule
= """\
345 rule connect_select_lines_pinmux;
351 class CallFn(object):
352 def __init__(self
, peripheral
, name
):
353 self
.peripheral
= peripheral
356 def __call__(self
, *args
):
357 #print "__call__", self.name, self.peripheral.slow, args
358 if not self
.peripheral
.slow
:
360 return getattr(self
.peripheral
.slow
, self
.name
)(*args
[1:])
363 class PeripheralIface(object):
364 def __init__(self
, ifacename
):
366 slow
= slowfactory
.getcls(ifacename
)
367 print "Iface", ifacename
, slow
369 self
.slow
= slow(ifacename
)
370 self
.slow
.peripheral
= self
371 for fname
in ['slowimport', 'slowifdecl', 'mkslow_peripheral',
372 'mk_connection', 'mk_cellconn', 'mk_pincon']:
373 fn
= CallFn(self
, fname
)
374 setattr(self
, fname
, types
.MethodType(fn
, self
))
376 #print "PeripheralIface"
379 def mksuffix(self
, name
, i
):
380 if self
.slow
is None:
382 return self
.slow
.mksuffix(name
, i
)
384 def axi_reg_def(self
, start
, count
):
387 return self
.slow
.axi_reg_def(start
, self
.ifacename
, count
)
389 def axi_slave_idx(self
, start
, count
):
392 return self
.slow
.axi_slave_idx(start
, self
.ifacename
, count
)
394 def axi_addr_map(self
, count
):
397 return self
.slow
.axi_addr_map(self
.ifacename
, count
)
400 class PeripheralInterfaces(object):
404 def slowimport(self
, *args
):
406 for (name
, count
) in self
.ifacecount
:
407 #print "slowimport", name, self.data[name].slowimport
408 ret
.append(self
.data
[name
].slowimport())
409 return '\n'.join(list(filter(None, ret
)))
411 def slowifdecl(self
, *args
):
413 for (name
, count
) in self
.ifacecount
:
414 for i
in range(count
):
415 ret
.append(self
.data
[name
].slowifdecl().format(i
, name
))
416 return '\n'.join(list(filter(None, ret
)))
418 def axi_reg_def(self
, *args
):
420 start
= 0x00011100 # start of AXI peripherals address
421 for (name
, count
) in self
.ifacecount
:
422 for i
in range(count
):
423 x
= self
.data
[name
].axi_reg_def(start
, i
)
424 #print ("ifc", name, x)
428 return '\n'.join(list(filter(None, ret
)))
430 def axi_slave_idx(self
, *args
):
433 for (name
, count
) in self
.ifacecount
:
434 for i
in range(count
):
435 (rdef
, offs
) = self
.data
[name
].axi_slave_idx(start
, i
)
436 #print ("ifc", name, rdef, offs)
439 ret
.append("typedef %d LastGen_slave_num" % (start
- 1))
440 decls
= '\n'.join(list(filter(None, ret
)))
441 return axi_slave_declarations
.format(decls
)
443 def axi_addr_map(self
, *args
):
445 for (name
, count
) in self
.ifacecount
:
446 for i
in range(count
):
447 ret
.append(self
.data
[name
].axi_addr_map(i
))
448 return '\n'.join(list(filter(None, ret
)))
450 def mkslow_peripheral(self
, *args
):
452 for (name
, count
) in self
.ifacecount
:
453 for i
in range(count
):
454 print "mkslow", name
, count
455 x
= self
.data
[name
].mkslow_peripheral()
457 suffix
= self
.data
[name
].mksuffix(name
, i
)
458 ret
.append(x
.format(suffix
))
459 return '\n'.join(list(filter(None, ret
)))
461 def mk_connection(self
, *args
):
463 for (name
, count
) in self
.ifacecount
:
464 for i
in range(count
):
465 print "mk_conn", name
, i
466 txt
= self
.data
[name
].mk_connection(i
)
469 print self
.data
[name
].mk_connection
471 return '\n'.join(list(filter(None, ret
)))
473 def mk_cellconn(self
):
476 for (name
, count
) in self
.ifacecount
:
477 for i
in range(count
):
478 res
= self
.data
[name
].mk_cellconn(cellcount
, name
, i
)
481 (txt
, cellcount
) = res
483 ret
= '\n'.join(list(filter(None, ret
)))
484 return pinmux_cellrule
.format(ret
)
488 for (name
, count
) in self
.ifacecount
:
489 for i
in range(count
):
490 txt
= self
.data
[name
].mk_pincon(name
, i
)
492 return '\n'.join(list(filter(None, ret
)))
494 class PFactory(object):
495 def getcls(self
, name
):
496 for k
, v
in {'uart': uart
,
503 if name
.startswith(k
):
508 slowfactory
= PFactory()
510 if __name__
== '__main__':
514 i
= PeripheralIface('uart')
516 i
= PeripheralIface('gpioa')