b8b6b78482cd145e777d628db5c5110a62f96fb2
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(npins
=2, nfns
=4):
54 from myhdl import block
56 def test(testfn, clk, num_pins, num_fns, {0}):
58 return testfn(clk, 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 def test2(clk
, num_pins
, num_fns
, args
):
95 for i
in range(num_pins
):
96 muxes
.append(args
.pop(0))
97 pins
.append(args
.pop(0))
98 for i
in range(num_fns
):
99 fns
.append(args
.pop(0))
105 inputs
.append(fns
[i
].inp
)
107 for i
in range(len(muxes
)):
110 inst
= mux4(clk
, inputs
, mux
.sel
, pin
.out
)
135 muxvals
.append(m
.sel
)
137 pin
= IO("inout", "name%d" % i
)
142 dirs
.append(pin
.dirn
)
145 fn
= IO("inout", "fnname%d" % i
)
149 fdirs
.append(fn
.dirn
)
151 clk
= Signal(bool(0))
153 mux_inst
= test(test2
, clk
, 2, 4, *args
)
159 yield delay(period
// 2)
164 # print.format is not supported in MyHDL 1.0
165 for i
in range(len(muxes
)):
168 print ("%d: %s %s" % (i
, sel
, out
))
193 muxvals
.append(m
.sel
)
195 pin
= IO("inout", "name%d" % i
)
200 dirs
.append(pin
.dirn
)
203 fn
= IO("inout", "fnname%d" % i
)
207 fdirs
.append(fn
.dirn
)
209 clk
= Signal(bool(0))
211 mux_inst
= test(test2
, clk
, 2, 4, *args
)
212 mux_inst
.convert(hdl
="Verilog", initial_values
=True, testbench
=False)
213 #mux_inst = Test(clk, muxes, pins, fns)
214 #toVerilog(mux_inst, clk, muxes, pins, fns)
216 #b = _Block(mux_inst, deco, "test", "test.py", 1, clk, muxes, pins, fns)
217 #b.convert(hdl="Verilog", name="test", initial_values=True)
218 #mux_inst.convert(hdl="Verilog", initial_values=True)
219 #block(mux_inst).convert(hdl="Verilog", initial_values=True)
223 tb
.convert(hdl
="Verilog", initial_values
=True, testbench
=True)
224 # keep following lines below the 'tb.convert' line
225 # otherwise error will be reported
226 tb
.config_sim(trace
=True)
227 tb
.run_sim(66 * period
) # run for 15 clock cycle
232 if __name__
== '__main__':