一、 mux2_1.v
module mux2_1
(
input wire in_1,
input wire in_2,
input wire sel,
output reg out
);
//always赋值的变量必须是reg型,所以选用out为reg类型
always@(*)
if(sel == 1'b0)
//组合逻辑使用阻塞赋值
out = in_1;
else
out = in_2;
endmodule
二、 mux2_1_test.v
`timescale 1ns/100ps
module mux2_1_test();
//==========================================
//定义reg型输入变量
reg in_1;
reg in_2;
reg sel;
wire out;
initial
begin
in_1 <= 1'b0;
in_2 <= 1'b0;
sel <= 1'b0;
end
//in_1,in_2,sel产生随机信号
always #10 in_1 <= {$random} % 2;
always #10 in_2 <= {$random} % 2;
always #10 sel <= {$random} % 2;
initial
begin
//$timeformat设置时间格式的系统函数
//-9表示显示时间的单位是ns
//0表示精确到小数点后的位数
//ns需要与-9对应
//打印的最小数字字符是6个
$timeformat(-9,0,"ns",6);
$monitor("@time %t:in_1=%b in_2=%b sel=%b out=%b",$time,in_1,in_2,sel,out);
end
mux2_1 mux2_1_inst
(
.in_1(in_1),
.in_2(in_2),
.sel(sel),
.out(out)
);
endmodule