Verilog 4bits 4x1 MUX (Data flow level)
module MUX_4x1_4bit(A,B,C,D,s0,s1,Y) ;
input [3:0]A,B,C,D;
input s0,s1 ;
output [3:0]Y;
wire [3:0]w1,w2,w3,w4;
assign w1[0]=A[0] & (~s1) & (~s0);
assign w1[1]=A[1] & (~s1) & (~s0);
assign w1[2]=A[2] & (~s1) & (~s0);
assign w1[3]=A[3] & (~s1) & (~s0);
assign w2[0]=B[0] & (~s1) & (s0);
assign w2[1]=B[1] & (~s1) & (s0);
assign w2[2]=B[2] & (~s1) & (s0);
assign w2[3]=B[3] & (~s1) & (s0);
assign w3[0]=C[0] & (s1) & (~s0);
assign w3[1]=C[1] & (s1) & (~s0);
assign w3[2]=C[2] & (s1) & (~s0);
assign w3[3]=C[3] & (s1) & (~s0);
assign w4[0]=D[0] & (s1) & (s0);
assign w4[1]=D[1] & (s1) & (s0);
assign w4[2]=D[2] & (s1) & (s0);
assign w4[3]=D[3] & (s1) & (s0);
assign Y= w1 | w2 | w3 | w4;
endmodule
/*
module mux_4to1_assign ( input [3:0] a, // 4-bit input called a
input [3:0] b, // 4-bit input called b
input [3:0] c, // 4-bit input called c
input [3:0] d, // 4-bit input called d
input [1:0] sel, // input sel used to select between a,b,c,d
output [3:0] out); // 4-bit output based on input sel
// When sel[1] is 0, (sel[0]? b:a) is selected and when sel[1] is 1, (sel[0] ? d:c) is taken
// When sel[0] is 0, a is sent to output, else b and when sel[0] is 0, c is sent to output, else d
assign out = sel[1] ? (sel[0] ? d : c) : (sel[0] ? b : a);
endmodule
*/
`timescale 10ns/10ps
module Test_bench;
reg [3:0] A = 4'b0101;
reg [3:0] B = 4'b0101;
reg [3:0] C = 4'b0101;
reg [3:0] D = 4'b0101;
reg s0 = 1'b0;
reg s1 = 1'b0;
wire [3:0] Y;
//mux_4x1(S0,S1,A,B,C,D,Y);
MUX_4x1_4bit UUT (
.s0(s0),
.s1(s1),
.A(A),
.B(B),
.C(C),
.D(D),
.Y(Y));
initial
begin
#800; // Final time: 1000 ns
$stop;
end
initial
begin
// ------------- Current Time: 100ns
#100;
s1=1'b0; s0=1'b0; A = 4'b1010;B = 4'b0101; C = 4'b0101;D = 4'b0101;
// -------------------------------------
// ------------- Current Time: 200ns
#100;
s1=1'b0; s0=1'b1; A = 4'b0101;B = 4'b0101; C = 4'b0101;D = 4'b0101;
// -------------------------------------
// ------------- Current Time: 300ns
#100;
s1=1'b0; s0=1'b1; A = 4'b0101;B = 4'b1010; C = 4'b0101;D = 4'b0101;
// -------------------------------------
// ------------- Current Time: 400ns
#100;
s1=1'b1; s0=1'b0; A = 4'b0101;B = 4'b0101; C = 4'b0101;D = 4'b0101;
// -------------------------------------
// ------------- Current Time: 500ns
#100;
s1=1'b1; s0=1'b0; A = 4'b0101;B = 4'b0101; C = 4'b1010;D = 4'b0101;
// -------------------------------------
// ------------- Current Time: 600ns
#100;
s1=1'b1; s0=1'b1; A = 4'b0101;B = 4'b0101; C = 4'b0101;D = 4'b0101;
// -------------------------------------
// ------------- Current Time: 700ns
#100;
s1=1'b1; s0=1'b1; A = 4'b0101;B = 4'b0101; C = 4'b0101;D = 4'b1010;
// -------------------------------------
// ------------- Current Time: 1000ns
end
endmodule