1.用case语句:
module MUX41a(a,b,c,d,s1,s0,y);
input a,b,c,d;
input s1,s0;
output y;
reg y;
always @(a or b or c or d or s1 or s0)
begin : MUX41
case({s1,s0})
2'b00:y<=a;
2'b01:y<=b;
2'b10:y<=c;
2'b11:y<=d;
default:y<=a;
endcase
end
endmodule
2.用assign语句
module MUX41a(a,b,c,d,s1,s0,y);
input a,b,c,d,s1,s0;
output y;
wire [1:0] SEL;
wire AT,BT,CT,DT;
assign SEL={s1,s0};
assign AT=(SEL==2'D0);
assign BT=(SEL==2'D1);
assign CT=(SEL==2'D2);
assign DT=(SEL==2'D3);
assign y=(a&AT)|(b&BT)|(c&CT)|(d&DT);
endmodule
3.用三目语句:
module MUX41a(a,b,c,d,s1,s0,y);
input a,b,c,d,s1,s0;
output y;
wire AT=s0?d:c;
wire BT=s0?b:a;
wire y=(s1?AT:BT);
endmodule
=号是阻塞式赋值,语句执行时马上赋值;而<=是非阻塞式赋值,得等语句块(beign end或fork join)执行完再赋值。
关于reg,wire,always@(*)的用法