ad9f92c5f2c66b65c2db4fb25fc4990b1eff7c51
6 period
= 20 # clk frequency = 50 MHz
9 def __init__(self
, ins
):
16 class Selectors(object):
17 def __init__(self
, sels
):
25 def mux4(clk
, in_a
, in_b
, in_c
, in_d
,
28 @always(selector
, in_a
, in_b
, in_c
, in_d
)
30 out
.next
= bool(in_a
if selector
== 0 else False) | \
31 bool(in_b
if selector
== 1 else False) | \
32 bool(in_c
if selector
== 2 else False) | \
33 bool(in_d
if selector
== 3 else False)
35 return instances() # return all instances
50 return instances() # return all instances
54 def pmux2(clk
, in_a
, in_b
,
67 return instances() # return all instances
71 def pmux3(clk
, in_a
, in_b
, in_c
,
72 sel_a
, sel_b
, sel_c
, out
):
74 @always(sel_a
, sel_b
, sel_c
,
86 return instances() # return all instances
90 def pmux4(clk
, ins
, sels
, out
):
92 @always(*list(sels
.sels
) + list(ins
.ins
))
107 return i
# return all instances
114 clk
= Signal(bool(0))
115 in_a
= Signal(bool(0))
116 in_b
= Signal(bool(0))
117 in_c
= Signal(bool(0))
118 in_d
= Signal(bool(0))
119 sel_a
= Signal(bool(0))
120 sel_b
= Signal(bool(0))
121 sel_c
= Signal(bool(0))
122 sel_d
= Signal(bool(0))
123 out
= Signal(bool(0))
125 sels
= Selectors((sel_a
, sel_b
, sel_c
, sel_d
))
126 ins
= Inputs((in_a
, in_b
, in_c
, in_d
))
127 mux_inst
= pmux4(clk
, ins
, sels
, out
)
145 sel_a
.next
= not sel_a
147 sel_b
.next
= not sel_b
149 sel_c
.next
= not sel_c
151 sel_d
.next
= not sel_d
152 yield delay(period
// 2)
154 # print simulation data on screen and file
155 file_data
= open("pmux.csv", 'w') # file for saving data
156 # # print header on screen
157 s
= "{0},{1},{2},{3},{4},{5},{6},{7},{8}".format(
158 "in_a", "in_b", "in_c", "in_d",
159 "sel_a", "sel_b", "sel_c", "sel_d",
162 # # print header to file
164 # print data on each clock
169 # print.format is not supported in MyHDL 1.0
170 print ("%s,%s,%s,%s,%s,%s,%s,%s,%s" %
185 # print.format is not supported in MyHDL 1.0
186 #file_data.write(s + "\n")
196 clk
= Signal(bool(0))
197 in_a
= Signal(bool(0))
198 in_b
= Signal(bool(0))
199 in_c
= Signal(bool(0))
200 in_d
= Signal(bool(0))
201 selector
= Signal(intbv(0)[2:0])
202 out
= Signal(bool(0))
204 mux_inst
= mux4(clk
, in_a
, in_b
, in_c
, in_d
, selector
, out
)
222 selector
.next
= selector
+ 1
223 yield delay(period
// 2)
225 # print simulation data on screen and file
226 file_data
= open("mux.csv", 'w') # file for saving data
227 # # print header on screen
228 s
= "{0},{1},{2},{3},{4},{5}".format("in_a", "in_b", "in_c", "in_d",
231 # # print header to file
233 # print data on each clock
238 # print.format is not supported in MyHDL 1.0
239 print ("%s,%s,%s,%s,%s,%s" %
253 # print.format is not supported in MyHDL 1.0
254 #file_data.write(s + "\n")
261 clk
= Signal(bool(0))
262 in_a
= Signal(bool(0))
263 in_b
= Signal(bool(0))
264 in_c
= Signal(bool(0))
265 in_d
= Signal(bool(0))
266 selector
= Signal(intbv(0)[2:0])
267 out
= Signal(bool(0))
269 mux_v
= mux4(clk
, in_a
, in_b
, in_c
, in_d
, selector
, out
)
270 mux_v
.convert(hdl
="Verilog", initial_values
=True)
274 tb
.convert(hdl
="Verilog", initial_values
=True)
275 # keep following lines below the 'tb.convert' line
276 # otherwise error will be reported
277 tb
.config_sim(trace
=True)
278 tb
.run_sim(66 * period
) # run for 15 clock cycle
283 clk
= Signal(bool(0))
284 in_a
= Signal(bool(0))
285 in_b
= Signal(bool(0))
286 in_c
= Signal(bool(0))
287 in_d
= Signal(bool(0))
288 sel_a
= Signal(bool(0))
289 sel_b
= Signal(bool(0))
290 sel_c
= Signal(bool(0))
291 sel_d
= Signal(bool(0))
292 out
= Signal(bool(0))
294 sels
= Selectors((sel_a
, sel_b
, sel_c
, sel_d
))
295 ins
= Inputs((in_a
, in_b
, in_c
, in_d
))
296 pmux_v
= pmux4(clk
, ins
, sels
, out
)
297 pmux_v
.convert(hdl
="Verilog", initial_values
=True)
301 tb
.convert(hdl
="Verilog", initial_values
=True)
302 # keep following lines below the 'tb.convert' line
303 # otherwise error will be reported
304 tb
.config_sim(trace
=True)
305 tb
.run_sim(4 * 66 * period
) # run for 15 clock cycle
308 if __name__
== '__main__':