二选一多路开关的Verilog源码及测试源码
时间: 2023-12-17 16:04:18 浏览: 47
以下是一个2选1多路开关的Verilog代码示例:
```
module mux2to1(input in0, in1, sel, output reg out);
always @ (in0, in1, sel) begin
if (sel == 1'b0) begin
out = in0;
end else begin
out = in1;
end
end
endmodule
```
在这个代码中, `in0` 和 `in1` 输入是两个数据, `sel` 输入是一个选择信号, `out` 输出是一个数据。`always` 代码块中的 `if-else` 语句用于选择输入数据中的一个作为输出数据。如果 `sel` 为0,则选择 `in0` 作为输出数据;否则选择 `in1` 作为输出数据。
以下是一个测试代码的示例:
```
module mux2to1_tb;
reg in0, in1, sel;
wire out;
mux2to1 dut(.in0(in0), .in1(in1), .sel(sel), .out(out));
initial begin
in0 = 1'b0;
in1 = 1'b0;
sel = 1'b0;
#5;
in0 = 1'b1;
#5;
sel = 1'b1;
#5;
in1 = 1'b1;
#5;
sel = 1'b0;
#5;
$finish;
end
endmodule
```
在这个测试代码中, `in0` 和 `in1` 以及 `sel` 信号都是由寄存器 `reg` 定义的, `out` 信号是由线 `wire` 定义的。然后使用 `mux2to1` 模块的例化 `dut`,连接输入和输出信号。在 `initial` 代码块中,对输入信号进行初始化,并通过 `#5` 延迟指令等待5个时钟周期。然后更改输入信号并等待5个时钟周期,直到完全测试完毕。最后使用 `$finish` 指令结束仿真。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)