module test_ex1( input a,b,c,d,sel0,sel1,sel2,sel3, output reg z );
always @(a or b or c or d or sel0 or sel1 or sel2 or sel3) begin z=0; if (sel0) z=a; if (sel1) z=b; if (sel2) z=c; if (sel3) z=d; end
endmodule
图1:example1
example2
module test_ex2( input a,b,c,d,sel0,sel1,sel2,sel3, output reg z );
always@(a or b or c or d or sel0 or sel1 or sel2 or sel3) begin if(sel0) z = d; elseif(sel1) z = c; elseif(sel2) z = b; elseif(sel3) z = a; else z = 0; end
endmodule
图2:example2
example3
module test_ex3( input a,b,c,d,sel0,sel1,sel2,sel3, output reg z );
always@(a or b or c or d or sel0 or sel1 or sel2 or sel3) begin if(sel0) z = d; elseif(sel1) z = c; elseif(sel2) z = b; elseif(sel3) z = a; end
endmodule
图3:example3
example4
module test_ex4( input a,b,c,d,sel0,sel1,sel2,sel3, output reg [3:0] z );
always @(a or b or c or d or sel0 or sel1 or sel2 or sel3) begin z=0; if (sel0) z[0]=a; if (sel1) z[1]=b; if (sel2) z[2]=c; if (sel3) z[3]=d; end
always_comb begin error = '0; case (d_in) inside 4'b1???: d_out = 2'h3; // bit 3 is set 4'b01??: d_out = 2'h2; // bit 2 is set 4'b001?: d_out = 2'h1; // bit 1 is set 4'b0001: d_out = 2'h0; // bit 0 is set 4'b0000: begin // no bits set d_out = 2'b0; error = '1; end endcase end endmodule: test_ex6