题解 | #四选一多路器#
四选一多路器
http://www.nowcoder.com/practice/cba4617e1ef64e9ea52cbb400a0725a3
由于要求输出是线网类型。所以不可以使用always等过程语句模块。
- 使用verilog语句提供的代码如下:
`timescale 1ns/1ns
module mux4_1(
input [1:0]d1,d2,d3,d0,
input [1:0]sel,
output[1:0]mux_out
);
//*************code***********//
wire[1:0] a1,a2;
assign a1 = (sel[1]) ? d0:d2;
assign a2 = (sel[1]) ? d1:d3;
assign mux_out = (sel[0]) ? a1: a2;
//*************code***********//
endmodule
其实这个网站莫过于操作倒骚的就是自测运行的TestBench语句需要自己输入,虽然说这对于能力提升有作用,但这对于纯小白确实不太友好,差点直接劝退。
`timescale 1ns/1ns
module testbench();
reg clk=0;
always #5 clk = ~clk; // Create clock with period=10
// A testbench
--------------需自行补充的语句--------------
wire[1:0] d1,d2,d3,d4;
wire[1:0] sel;
wire[1:0] mux_out;
发现这个可以不需要
-------------------------------------
//end
initial begin
$dumpfile("out.vcd");
// This will dump all signal, which may not be useful
//$dumpvars;
// dumping only this module
//$dumpvars(1, testbench);
// dumping only these variable
// the first number (level) is actually useless
$dumpvars(0, testbench);
-----------------
$finish; // 这个一定要有,不然一运行就是超时了。
end
endmodule
好吧,好像就是在tb里加一个finish就可以解决问题了。加油!