module mux2_1
(
input wire in1 ,//输入只能是wire型
input wire in2 ,
input wire sel ,
//输出可以使wire型或者reg型
//output wire out 在assign中赋值(=)使用wire型
output reg out //在always中赋值(<=)要用reg型
);
//assign out = (sel == 1'b1)? in1:in2;
always@(*)
if(sel == 1)
//out <= in1;always块中表达组合逻辑时使用=赋值
out = in1;
else
out = in2;
/*
always@(*)
case(sel)
1'b1 : out <= in1;
1'b0 : out <= in2;
endcase
*/
endmodule
if-else和条件运算符综合后的RTL视图
使用case语句的RTL Viewer
Technology Map Viewer
`timescale 1ns/1ns
module tb_mux2_1();
reg in1, in2, sel;
wire out;
initial begin
in1 = 1;
in2 = 0;
sel = 1;
end
//在initial和always块中被赋值的变量一定要是reg型
//在testbeech中待测试模块的输入永远是reg型
always #10 in1 <= {$random} % 2;
always #10 in2 <= {$random} % 2;
always #10 sel <= {$random} % 2;
mux2_1 mux2_1_inst
(
.in1 (in1),
.in2 (in2),
.sel (sel),
.out (out) //在testbeech中待测试模块的输出永远为wire型
);
endmodule