4 from myhdl
._block
import _Block
6 from functools
import wraps
, partial
9 period
= 20 # clk frequency = 50 MHz
13 def __init__(self
, typ
, name
):
16 if typ
== 'in' or typ
== 'inout':
17 self
.inp
= Signal(bool(0))
18 if typ
== 'out' or typ
== 'inout':
19 self
.out
= Signal(bool(0))
21 self
.dirn
= Signal(bool(0))
25 def __init__(self
, bwidth
=2):
26 self
.sel
= Signal(intbv(0)[bwidth
:0])
30 print('attr =', obj
.attr
)
34 def cvt(self
, *args
, **kwargs
):
35 print('args', self
, args
, kwargs
)
36 return block(test2
)(*self
._args
).convert(*args
, **kwargs
)
52 def create_test(fncls
, npins
=2, nfns
=4):
54 from myhdl import block
56 def test(testfn, clk, fncls, num_pins, num_fns, {0}):
58 return testfn(clk, fncls, num_pins, num_fns, args)
62 for pnum
in range(npins
):
63 args
.append("sel%d" % pnum
)
64 args
.append("pin%d" % pnum
)
65 #for pnum in range(nfns):
66 # args.append("fn%d" % pnum)
71 with
open("testmod.py", "w") as f
:
73 x
= "from testmod import test"
74 code
= compile(x
, '<string>', 'exec')
86 return func(args
[0], args
[1], args
[2], args
[3])
91 self
.attrs
= ['uart', 'i2c', 'spi', 'gpio']
93 def setfn(self
, idx
, fn
):
94 return setattr(self
, self
.attrs
[idx
], fn
)
97 return getattr(self
, self
.attrs
[idx
])
100 def muxer(clk
, p
, ifaces
, args
):
104 for i
in range(len(p
.muxed_cells
)):
105 pins
.append(args
.pop(0))
106 muxes
.append(args
.pop(0))
107 kl
= sorted(ifaces
.keys())
108 for i
in range(len(p
.myhdlifaces
)):
109 fns
.append(args
.pop(0))
115 x
= getattr(fns
[i
], fns
[i
].pnames
[0])
117 inputs
.append(getattr(fns
[i
], fns
[i
].pnames
[0]).out
)
118 inputs
.append(getattr(fns
[i
], fns
[i
].pnames
[0]).out
)
120 print "inputs", inputs
122 for i
in range(len(muxes
)):
127 inst
= mux4(clk
, inputs
, mux
.sel
, pin
.out
)
134 def test2(clk
, fncls
, num_pins
, num_fns
, args
):
137 for i
in range(num_pins
):
138 muxes
.append(args
.pop(0))
139 pins
.append(args
.pop(0))
144 inputs
.append(fncls
.uart
.out
)
145 inputs
.append(fncls
.i2c
.out
)
146 inputs
.append(fncls
.spi
.out
)
147 inputs
.append(fncls
.gpio
.out
)
149 #inputs.append(fncls.getfn(i).out)
151 for i
in range(len(muxes
)):
154 inst
= mux4(clk
, inputs
, mux
.sel
, pin
.out
)
176 muxvals
.append(m
.sel
)
178 pin
= IO("inout", "name%d" % i
)
183 dirs
.append(pin
.dirn
)
185 clk
= Signal(bool(0))
187 mux_inst
= test(test2
, clk
, fncls
, 2, 4, *args
)
193 yield delay(period
// 2)
198 # print.format is not supported in MyHDL 1.0
199 for i
in range(len(muxes
)):
202 print ("%d: %s %s" % (i
, sel
, out
))
227 muxvals
.append(m
.sel
)
229 pin
= IO("inout", "name%d" % i
)
234 dirs
.append(pin
.dirn
)
235 clk
= Signal(bool(0))
237 mux_inst
= test(test2
, clk
, fncls
, 2, 4, *args
)
238 mux_inst
.convert(hdl
="Verilog", initial_values
=True, testbench
=False)
239 #mux_inst = Test(clk, muxes, pins, fns)
240 #toVerilog(mux_inst, clk, muxes, pins, fns)
242 #b = _Block(mux_inst, deco, "test", "test.py", 1, clk, muxes, pins, fns)
243 #b.convert(hdl="Verilog", name="test", initial_values=True)
244 #mux_inst.convert(hdl="Verilog", initial_values=True)
245 #block(mux_inst).convert(hdl="Verilog", initial_values=True)
249 tb
.convert(hdl
="Verilog", initial_values
=True, testbench
=True)
250 # keep following lines below the 'tb.convert' line
251 # otherwise error will be reported
252 tb
.config_sim(trace
=True)
253 tb
.run_sim(66 * period
) # run for 15 clock cycle
256 def muxgen(fn
, p
, ifaces
):
258 for i
in range(len(p
.muxed_cells
)):
259 args
.append(p
.muxers
[i
])
260 args
.append(p
.muxsel
[i
])
261 for i
in p
.myhdlifaces
:
263 clk
= Signal(bool(0))
265 mux_inst
= fn(muxer
, clk
, p
, ifaces
, *args
)
266 mux_inst
.convert(hdl
="Verilog", initial_values
=True, testbench
=False)
271 if __name__
== '__main__':
274 for i
in range(num_fns
):
275 fn
= IO("inout", fncls
.attrs
[i
])
277 test
= create_test(fncls
)