4x1 Using 2x1
4x1 Using 2x1
com/x/4qJM
// Design of 4x1 mux using 2x1 mux
module mux_4x1_using_2x1 (I,S,f);
//input declaration
input [3:0] I;
input [1:0] S;
//output declaration
output f;
//implicit declaration
wire [3:0] I;
wire [1:0] S;
wire f;
wire g1,g2;
mux_2X1 M3 (.I({I[3],I[2]}),
.s(S[0]),
.y(g2));
mux_2X1 M2 (.I({I[1],I[0]}),
.s(S[0]),
.y(g1));
mux_2X1 M1 (.I({g2,g1}),
.s(S[1]),
.y(f));
endmodule
//input declaration
input [1:0] I;
input s;
//output declaration
output y;
//implicit declaration
wire [1:0] I;
wire s;
wire y;
endmodule
/******* TB *******/
// Testbench for design of 4x1 mux using 2x1
module Top;
//implicit declaration
reg [3:0] I;
reg [1:0] S;
wire f;
reg exp_f;
event sim_done;
//stimulus
initial
begin
#10;I=4'b0000;S=2'b00;
#10;I=4'b0001;S=2'b00;
#10;I=4'b0010;S=2'b01;
#10;I=4'b0100;S=2'b10;
#10;I=4'b1000;S=2'b11;
#10;I=4'b1110;S=2'b00;
#10;I=4'b1101;S=2'b01;
#10;I=4'b1011;S=2'b10;
#10;I=4'b0111;S=2'b11;
->sim_done;
end
initial
begin
@(sim_done);
#100;
$finish;
end
//checker
always@(I or S) begin
case(S)
2'b00:begin exp_f=I[0]; end
2'b01:begin exp_f=I[1]; end
2'b10:begin exp_f=I[2]; end
2'b11:begin exp_f=I[3]; end
default: exp_f='x;
endcase
end
always@(f) begin
if(exp_f===f) begin
$display("Matched: time=%t,I=%b,S=%b,f=%b,exp_f=%b",$time,I,S,f,exp_f);
end
else begin
$error("Not Matched: time=%t,I=%b,S=%b,f=%b,exp_f=%b",
$time,I,S,f,exp_f);
end
end
endmodule