6 period
= 20 # clk frequency = 50 MHz
10 def __init__(self
, ins
):
18 class Selectors(object):
19 def __init__(self
, sels
):
31 (in_a
, in_b
, in_c
, in_d
) = ins
32 print repr(clk
), ins
, repr(selector
), repr(out
)
34 @always(selector
, in_a
, in_b
, in_c
, in_d
)
36 out
.next
= bool(in_a
if selector
== 0 else False) | \
37 bool(in_b
if selector
== 1 else False) | \
38 bool(in_c
if selector
== 2 else False) | \
39 bool(in_d
if selector
== 3 else False)
41 return instances() # return all instances
56 return instances() # return all instances
60 def pmux2(clk
, in_a
, in_b
,
73 return instances() # return all instances
77 def pmux3(clk
, in_a
, in_b
, in_c
,
78 sel_a
, sel_b
, sel_c
, out
):
80 @always(sel_a
, sel_b
, sel_c
,
92 return instances() # return all instances
96 def pmux4(clk
, ins
, sels
, out
):
98 @always(*list(sels
.sels
) + list(ins
.ins
))
113 return i
# return all instances
120 clk
= Signal(bool(0))
121 in_a
= Signal(bool(0))
122 in_b
= Signal(bool(0))
123 in_c
= Signal(bool(0))
124 in_d
= Signal(bool(0))
125 sel_a
= Signal(bool(0))
126 sel_b
= Signal(bool(0))
127 sel_c
= Signal(bool(0))
128 sel_d
= Signal(bool(0))
129 out
= Signal(bool(0))
131 sels
= Selectors((sel_a
, sel_b
, sel_c
, sel_d
))
132 ins
= Inputs((in_a
, in_b
, in_c
, in_d
))
133 mux_inst
= pmux4(clk
, ins
, sels
, out
)
151 sel_a
.next
= not sel_a
153 sel_b
.next
= not sel_b
155 sel_c
.next
= not sel_c
157 sel_d
.next
= not sel_d
158 yield delay(period
// 2)
160 # print simulation data on screen and file
161 file_data
= open("pmux.csv", 'w') # file for saving data
162 # # print header on screen
163 s
= "{0},{1},{2},{3},{4},{5},{6},{7},{8}".format(
164 "in_a", "in_b", "in_c", "in_d",
165 "sel_a", "sel_b", "sel_c", "sel_d",
168 # # print header to file
170 # print data on each clock
175 # print.format is not supported in MyHDL 1.0
176 print ("%s,%s,%s,%s,%s,%s,%s,%s,%s" %
191 # print.format is not supported in MyHDL 1.0
192 #file_data.write(s + "\n")
202 clk
= Signal(bool(0))
203 in_a
= Signal(bool(0))
204 in_b
= Signal(bool(0))
205 in_c
= Signal(bool(0))
206 in_d
= Signal(bool(0))
207 selector
= Signal(intbv(0)[2:0])
208 out
= Signal(bool(0))
210 mux_inst
= mux4(clk
, in_a
, in_b
, in_c
, in_d
, selector
, out
)
228 selector
.next
= selector
+ 1
229 yield delay(period
// 2)
231 # print simulation data on screen and file
232 file_data
= open("mux.csv", 'w') # file for saving data
233 # # print header on screen
234 s
= "{0},{1},{2},{3},{4},{5}".format("in_a", "in_b", "in_c", "in_d",
237 # # print header to file
239 # print data on each clock
244 # print.format is not supported in MyHDL 1.0
245 print ("%s,%s,%s,%s,%s,%s" %
259 # print.format is not supported in MyHDL 1.0
260 #file_data.write(s + "\n")
267 clk
= Signal(bool(0))
268 in_a
= Signal(bool(0))
269 in_b
= Signal(bool(0))
270 in_c
= Signal(bool(0))
271 in_d
= Signal(bool(0))
272 selector
= Signal(intbv(0)[2:0])
273 out
= Signal(bool(0))
275 mux_v
= mux4(clk
, in_a
, in_b
, in_c
, in_d
, selector
, out
)
276 mux_v
.convert(hdl
="Verilog", initial_values
=True)
280 tb
.convert(hdl
="Verilog", initial_values
=True)
281 # keep following lines below the 'tb.convert' line
282 # otherwise error will be reported
283 tb
.config_sim(trace
=True)
284 tb
.run_sim(66 * period
) # run for 15 clock cycle
289 clk
= Signal(bool(0))
290 in_a
= Signal(bool(0))
291 in_b
= Signal(bool(0))
292 in_c
= Signal(bool(0))
293 in_d
= Signal(bool(0))
294 sel_a
= Signal(bool(0))
295 sel_b
= Signal(bool(0))
296 sel_c
= Signal(bool(0))
297 sel_d
= Signal(bool(0))
298 out
= Signal(bool(0))
300 sels
= Selectors((sel_a
, sel_b
, sel_c
, sel_d
))
301 ins
= Inputs((in_a
, in_b
, in_c
, in_d
))
302 pmux_v
= pmux4(clk
, ins
, sels
, out
)
303 pmux_v
.convert(hdl
="Verilog", initial_values
=True)
307 tb
.convert(hdl
="Verilog", initial_values
=True)
308 # keep following lines below the 'tb.convert' line
309 # otherwise error will be reported
310 tb
.config_sim(trace
=True)
311 tb
.run_sim(4 * 66 * period
) # run for 15 clock cycle
314 if __name__
== '__main__':