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 mkslow_peripheral(self
):
48 def mk_connection(self
, count
):
49 aname
= self
.axi_slave_name(self
.name
, count
)
50 txt
= " mkConnection (slow_fabric.v_to_slaves\n" + \
51 " [fromInteger(valueOf({1}))],\n" + \
54 con
= self
._mk
_connection
().format(count
, aname
)
57 return txt
.format(con
, aname
)
59 def _mk_connection(self
):
66 return " import Uart16550 :: *;"
69 return " interface RS232_PHY_Ifc uart{0}_coe;\n" + \
70 " method Bit#(1) uart{0}_intr;"
72 def num_axi_regs32(self
):
75 def mkslow_peripheral(self
):
76 return " Uart16550_AXI4_Lite_Ifc uart{0} <- \n" + \
77 " mkUart16550(clocked_by uart_clock,\n" + \
78 " reset_by uart_reset, sp_clock, sp_reset);"
80 def _mk_connection(self
):
81 return "uart{0}.slave_axi_uart"
88 return " import Uart_bs::*;\n" + \
89 " import RS232_modified::*;"
92 return " interface RS232 uart{0}_coe;"
94 def num_axi_regs32(self
):
97 def mkslow_peripheral(self
):
98 return " //Ifc_Uart_bs uart{0} <-" + \
99 " // mkUart_bs(clocked_by uart_clock,\n" + \
100 " // reset_by uart_reset,sp_clock, sp_reset);" +\
101 " Ifc_Uart_bs uart{0} <-" + \
102 " mkUart_bs(clocked_by sp_clock,\n" + \
103 " reset_by sp_reset, sp_clock, sp_reset);"
105 def _mk_connection(self
):
106 return "uart{0}.slave_axi_uart"
111 def slowimport(self
):
112 return " import I2C_top :: *;"
114 def slowifdecl(self
):
115 return " interface I2C_out i2c{0}_out;\n" + \
116 " method Bit#(1) i2c{0}_isint;"
118 def num_axi_regs32(self
):
121 def mkslow_peripheral(self
):
122 return " I2C_IFC i2c{0} <- mkI2CController();"
124 def _mk_connection(self
):
125 return "i2c{0}.slave_i2c_axi"
130 def slowimport(self
):
131 return " import qspi :: *;"
133 def slowifdecl(self
):
134 return " interface QSPI_out qspi{0}_out;\n" + \
135 " method Bit#(1) qspi{0}_isint;"
137 def num_axi_regs32(self
):
140 def mkslow_peripheral(self
):
141 return " Ifc_qspi qspi{0} <- mkqspi();"
143 def _mk_connection(self
):
144 return "qspi{0}.slave"
149 def slowimport(self
):
150 return " import pwm::*;"
152 def slowifdecl(self
):
153 return " interface PWMIO pwm{0}_o;"
155 def num_axi_regs32(self
):
158 def mkslow_peripheral(self
):
159 return " Ifc_PWM_bus pwm_bus <- mkPWM_bus(sp_clock);"
164 def slowimport(self
):
165 return " import pinmux::*;\n" + \
166 " import mux::*;\n" + \
169 def slowifdecl(self
):
170 return " interface GPIO_config#({1}) pad_config{0};"
172 def num_axi_regs32(self
):
175 def axi_slave_idx(self
, idx
, name
, ifacenum
):
176 """ generates AXI slave number definition, except
177 GPIO also has a muxer per bank
180 (ret
, x
) = PBase
.axi_slave_idx(self
, idx
, name
, ifacenum
)
181 (ret2
, x
) = PBase
.axi_slave_idx(self
, idx
, "mux", ifacenum
)
182 return ("%s\n%s" % (ret
, ret2
), 2)
184 def mkslow_peripheral(self
):
185 return " MUX#({1}) mux{0} <- mkmux();\n" + \
186 " GPIO#({1}) gpio{0} <- mkgpio();"
189 axi_slave_declarations
= """\
190 typedef 0 SlowMaster;
192 typedef TAdd#(LastGen_slave_num,`ifdef CLINT 1 `else 0 `endif )
194 typedef TAdd#(CLINT_slave_num ,`ifdef PLIC 1 `else 0 `endif )
196 typedef TAdd#(Plic_slave_num ,`ifdef AXIEXP 1 `else 0 `endif )
198 typedef TAdd#(AxiExp1_slave_num,1) Num_Slow_Slaves;
202 class CallFn(object):
203 def __init__(self
, peripheral
, name
):
204 self
.peripheral
= peripheral
207 def __call__(self
, *args
):
208 #print "__call__", self.name, args
209 if not self
.peripheral
.slow
:
211 return getattr(self
.peripheral
.slow
, self
.name
)(*args
[1:])
214 class PeripheralIface(object):
215 def __init__(self
, ifacename
):
217 slow
= slowfactory
.getcls(ifacename
)
219 self
.slow
= slow(ifacename
)
220 for fname
in ['slowimport', 'slowifdecl', 'mkslow_peripheral',
222 fn
= CallFn(self
, fname
)
223 setattr(self
, fname
, types
.MethodType(fn
, self
))
225 #print "PeripheralIface"
228 def axi_reg_def(self
, start
, count
):
231 return self
.slow
.axi_reg_def(start
, self
.ifacename
, count
)
233 def axi_slave_idx(self
, start
, count
):
236 return self
.slow
.axi_slave_idx(start
, self
.ifacename
, count
)
238 def axi_addr_map(self
, count
):
241 return self
.slow
.axi_addr_map(self
.ifacename
, count
)
244 class PeripheralInterfaces(object):
248 def slowimport(self
, *args
):
250 for (name
, count
) in self
.ifacecount
:
251 #print "slowimport", name, self.data[name].slowimport
252 ret
.append(self
.data
[name
].slowimport())
253 return '\n'.join(list(filter(None, ret
)))
255 def slowifdecl(self
, *args
):
257 for (name
, count
) in self
.ifacecount
:
258 for i
in range(count
):
259 ret
.append(self
.data
[name
].slowifdecl().format(i
, name
))
260 return '\n'.join(list(filter(None, ret
)))
262 def axi_reg_def(self
, *args
):
264 start
= 0x00011100 # start of AXI peripherals address
265 for (name
, count
) in self
.ifacecount
:
266 for i
in range(count
):
267 x
= self
.data
[name
].axi_reg_def(start
, i
)
268 #print ("ifc", name, x)
272 return '\n'.join(list(filter(None, ret
)))
274 def axi_slave_idx(self
, *args
):
277 for (name
, count
) in self
.ifacecount
:
278 for i
in range(count
):
279 (rdef
, offs
) = self
.data
[name
].axi_slave_idx(start
, i
)
280 #print ("ifc", name, rdef, offs)
283 ret
.append("typedef %d LastGen_slave_num" % (start
- 1))
284 decls
= '\n'.join(list(filter(None, ret
)))
285 return axi_slave_declarations
.format(decls
)
287 def axi_addr_map(self
, *args
):
289 for (name
, count
) in self
.ifacecount
:
290 for i
in range(count
):
291 ret
.append(self
.data
[name
].axi_addr_map(i
))
292 return '\n'.join(list(filter(None, ret
)))
294 def mkslow_peripheral(self
, *args
):
296 for (name
, count
) in self
.ifacecount
:
297 for i
in range(count
):
298 ret
.append(self
.data
[name
].mkslow_peripheral().format(i
))
299 return '\n'.join(list(filter(None, ret
)))
301 def mk_connection(self
, *args
):
303 for (name
, count
) in self
.ifacecount
:
304 for i
in range(count
):
305 txt
= self
.data
[name
].mk_connection(i
)
307 return '\n'.join(list(filter(None, ret
)))
310 class PFactory(object):
311 def getcls(self
, name
):
312 return {'uart': uart
,
321 slowfactory
= PFactory()
323 if __name__
== '__main__':