利用Verilog HDL设计八选一数据选择器及其激励代码
时间: 2023-06-14 14:07:11 浏览: 569
下面是一个基于 Verilog HDL 的 8选1数据选择器的设计,包括激励代码:
```
module mux8to1(sel, in0, in1, in2, in3, in4, in5, in6, in7, out);
input [2:0] sel;
input in0, in1, in2, in3, in4, in5, in6, in7;
output out;
assign out = (sel == 3'b000) ? in0 :
(sel == 3'b001) ? in1 :
(sel == 3'b010) ? in2 :
(sel == 3'b011) ? in3 :
(sel == 3'b100) ? in4 :
(sel == 3'b101) ? in5 :
(sel == 3'b110) ? in6 :
in7;
endmodule
module mux8to1_tb;
reg [2:0] sel;
reg in0, in1, in2, in3, in4, in5, in6, in7;
wire out;
mux8to1 UUT(sel, in0, in1, in2, in3, in4, in5, in6, in7, out);
initial begin
$monitor("sel=%b, in0=%b, in1=%b, in2=%b, in3=%b, in4=%b, in5=%b, in6=%b, in7=%b, out=%b", sel, in0, in1, in2, in3, in4, in5, in6, in7, out);
sel = 3'b000; in0 = 1'b1; in1 = 1'b0; in2 = 1'b0; in3 = 1'b0; in4 = 1'b0; in5 = 1'b0; in6 = 1'b0; in7 = 1'b0;
#10;
sel = 3'b001; in0 = 1'b0; in1 = 1'b1; in2 = 1'b0; in3 = 1'b0; in4 = 1'b0; in5 = 1'b0; in6 = 1'b0; in7 = 1'b0;
#10;
sel = 3'b010; in0 = 1'b0; in1 = 1'b0; in2 = 1'b1; in3 = 1'b0; in4 = 1'b0; in5 = 1'b0; in6 = 1'b0; in7 = 1'b0;
#10;
sel = 3'b011; in0 = 1'b0; in1 = 1'b0; in2 = 1'b0; in3 = 1'b1; in4 = 1'b0; in5 = 1'b0; in6 = 1'b0; in7 = 1'b0;
#10;
sel = 3'b100; in0 = 1'b0; in1 = 1'b0; in2 = 1'b0; in3 = 1'b0; in4 = 1'b1; in5 = 1'b0; in6 = 1'b0; in7 = 1'b0;
#10;
sel = 3'b101; in0 = 1'b0; in1 = 1'b0; in2 = 1'b0; in3 = 1'b0; in4 = 1'b0; in5 = 1'b1; in6 = 1'b0; in7 = 1'b0;
#10;
sel = 3'b110; in0 = 1'b0; in1 = 1'b0; in2 = 1'b0; in3 = 1'b0; in4 = 1'b0; in5 = 1'b0; in6 = 1'b1; in7 = 1'b0;
#10;
sel = 3'b111; in0 = 1'b0; in1 = 1'b0; in2 = 1'b0; in3 = 1'b0; in4 = 1'b0; in5 = 1'b0; in6 = 1'b0; in7 = 1'b1;
#10;
$finish;
end
endmodule
```
在上述代码中, `mux8to1` 模块实现了一个8选1数据选择器,它有9个输入端口和1个输出端口。其中, `sel` 输入是3位宽的选择信号, `in0` 到 `in7` 输入是8位宽的数据信号, `out` 输出是选择后的结果。
激励代码 `mux8to1_tb` 模块对 `mux8to1` 模块进行了测试。在测试中,我们通过改变输入数据和选择信号,来观察输出结果。通过 `$monitor` 函数,我们可以在模拟过程中实时查看模块的输入和输出信号。运行模拟时,我们可以看到模块的输出结果按照选择信号对应的输入信号进行了切换。
阅读全文