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
, aname
, count
):
49 txt
= " mkConnection (slow_fabric.v_to_slaves\n" + \
50 " [fromInteger(valueOf({1}))],\n" + \
53 con
= self
._mk
_connection
().format(count
, aname
)
56 return txt
.format(con
, aname
)
58 def mk_connection(self
, count
):
59 aname
= self
.axi_slave_name(self
.name
, count
)
60 return :elf
.__mk
_connection
(aname
, count
)
62 def _mk_connection(self
):
69 return " import Uart16550 :: *;"
72 return " interface RS232_PHY_Ifc uart{0}_coe;\n" + \
73 " method Bit#(1) uart{0}_intr;"
75 def num_axi_regs32(self
):
78 def mkslow_peripheral(self
):
79 return " Uart16550_AXI4_Lite_Ifc uart{0} <- \n" + \
80 " mkUart16550(clocked_by uart_clock,\n" + \
81 " reset_by uart_reset, sp_clock, sp_reset);"
83 def _mk_connection(self
):
84 return "uart{0}.slave_axi_uart"
91 return " import Uart_bs::*;\n" + \
92 " import RS232_modified::*;"
95 return " interface RS232 uart{0}_coe;"
97 def num_axi_regs32(self
):
100 def mkslow_peripheral(self
):
101 return " //Ifc_Uart_bs uart{0} <-" + \
102 " // mkUart_bs(clocked_by uart_clock,\n" + \
103 " // reset_by uart_reset,sp_clock, sp_reset);" +\
104 " Ifc_Uart_bs uart{0} <-" + \
105 " mkUart_bs(clocked_by sp_clock,\n" + \
106 " reset_by sp_reset, sp_clock, sp_reset);"
108 def _mk_connection(self
):
109 return "uart{0}.slave_axi_uart"
114 def slowimport(self
):
115 return " import I2C_top :: *;"
117 def slowifdecl(self
):
118 return " interface I2C_out i2c{0}_out;\n" + \
119 " method Bit#(1) i2c{0}_isint;"
121 def num_axi_regs32(self
):
124 def mkslow_peripheral(self
):
125 return " I2C_IFC i2c{0} <- mkI2CController();"
127 def _mk_connection(self
):
128 return "i2c{0}.slave_i2c_axi"
133 def slowimport(self
):
134 return " import qspi :: *;"
136 def slowifdecl(self
):
137 return " interface QSPI_out qspi{0}_out;\n" + \
138 " method Bit#(1) qspi{0}_isint;"
140 def num_axi_regs32(self
):
143 def mkslow_peripheral(self
):
144 return " Ifc_qspi qspi{0} <- mkqspi();"
146 def _mk_connection(self
):
147 return "qspi{0}.slave"
152 def slowimport(self
):
153 return " import pwm::*;"
155 def slowifdecl(self
):
156 return " interface PWMIO pwm{0}_o;"
158 def num_axi_regs32(self
):
161 def mkslow_peripheral(self
):
162 return " Ifc_PWM_bus pwm{0}_bus <- mkPWM_bus(sp_clock);"
164 def _mk_connection(self
):
165 return "pwm{0}_bus.axi4_slave"
170 def slowimport(self
):
171 return " import pinmux::*;\n" + \
172 " import mux::*;\n" + \
175 def slowifdecl(self
):
176 return " interface GPIO_config#({1}) pad_config{0};"
178 def num_axi_regs32(self
):
181 def axi_slave_idx(self
, idx
, name
, ifacenum
):
182 """ generates AXI slave number definition, except
183 GPIO also has a muxer per bank
186 (ret
, x
) = PBase
.axi_slave_idx(self
, idx
, name
, ifacenum
)
187 (ret2
, x
) = PBase
.axi_slave_idx(self
, idx
, "mux", ifacenum
)
188 return ("%s\n%s" % (ret
, ret2
), 2)
190 def mkslow_peripheral(self
):
191 return " MUX#({1}) mux{0} <- mkmux();\n" + \
192 " GPIO#({1}) gpio{0} <- mkgpio();"
195 axi_slave_declarations
= """\
196 typedef 0 SlowMaster;
198 typedef TAdd#(LastGen_slave_num,`ifdef CLINT 1 `else 0 `endif )
200 typedef TAdd#(CLINT_slave_num ,`ifdef PLIC 1 `else 0 `endif )
202 typedef TAdd#(Plic_slave_num ,`ifdef AXIEXP 1 `else 0 `endif )
204 typedef TAdd#(AxiExp1_slave_num,1) Num_Slow_Slaves;
208 class CallFn(object):
209 def __init__(self
, peripheral
, name
):
210 self
.peripheral
= peripheral
213 def __call__(self
, *args
):
214 #print "__call__", self.name, args
215 if not self
.peripheral
.slow
:
217 return getattr(self
.peripheral
.slow
, self
.name
)(*args
[1:])
220 class PeripheralIface(object):
221 def __init__(self
, ifacename
):
223 slow
= slowfactory
.getcls(ifacename
)
225 self
.slow
= slow(ifacename
)
226 for fname
in ['slowimport', 'slowifdecl', 'mkslow_peripheral',
228 fn
= CallFn(self
, fname
)
229 setattr(self
, fname
, types
.MethodType(fn
, self
))
231 #print "PeripheralIface"
234 def axi_reg_def(self
, start
, count
):
237 return self
.slow
.axi_reg_def(start
, self
.ifacename
, count
)
239 def axi_slave_idx(self
, start
, count
):
242 return self
.slow
.axi_slave_idx(start
, self
.ifacename
, count
)
244 def axi_addr_map(self
, count
):
247 return self
.slow
.axi_addr_map(self
.ifacename
, count
)
250 class PeripheralInterfaces(object):
254 def slowimport(self
, *args
):
256 for (name
, count
) in self
.ifacecount
:
257 #print "slowimport", name, self.data[name].slowimport
258 ret
.append(self
.data
[name
].slowimport())
259 return '\n'.join(list(filter(None, ret
)))
261 def slowifdecl(self
, *args
):
263 for (name
, count
) in self
.ifacecount
:
264 for i
in range(count
):
265 ret
.append(self
.data
[name
].slowifdecl().format(i
, name
))
266 return '\n'.join(list(filter(None, ret
)))
268 def axi_reg_def(self
, *args
):
270 start
= 0x00011100 # start of AXI peripherals address
271 for (name
, count
) in self
.ifacecount
:
272 for i
in range(count
):
273 x
= self
.data
[name
].axi_reg_def(start
, i
)
274 #print ("ifc", name, x)
278 return '\n'.join(list(filter(None, ret
)))
280 def axi_slave_idx(self
, *args
):
283 for (name
, count
) in self
.ifacecount
:
284 for i
in range(count
):
285 (rdef
, offs
) = self
.data
[name
].axi_slave_idx(start
, i
)
286 #print ("ifc", name, rdef, offs)
289 ret
.append("typedef %d LastGen_slave_num" % (start
- 1))
290 decls
= '\n'.join(list(filter(None, ret
)))
291 return axi_slave_declarations
.format(decls
)
293 def axi_addr_map(self
, *args
):
295 for (name
, count
) in self
.ifacecount
:
296 for i
in range(count
):
297 ret
.append(self
.data
[name
].axi_addr_map(i
))
298 return '\n'.join(list(filter(None, ret
)))
300 def mkslow_peripheral(self
, *args
):
302 for (name
, count
) in self
.ifacecount
:
303 for i
in range(count
):
304 ret
.append(self
.data
[name
].mkslow_peripheral().format(i
))
305 return '\n'.join(list(filter(None, ret
)))
307 def mk_connection(self
, *args
):
309 for (name
, count
) in self
.ifacecount
:
310 for i
in range(count
):
311 txt
= self
.data
[name
].mk_connection(i
)
313 return '\n'.join(list(filter(None, ret
)))
316 class PFactory(object):
317 def getcls(self
, name
):
318 return {'uart': uart
,
327 slowfactory
= PFactory()
329 if __name__
== '__main__':