HDL Program for 2-to-1 Multiplexer

Post date: Nov 1, 2017 6:53:12 AM

2-to-1 Multiplexer

A multiplexer (or mux) is a common digital circuit used to mix a lot of signals into just one. If you want multiple sources of data to share a single, common data line, you’d use a multiplexer to run them into that line. Multiplexers come in all sorts of shapes and sizes, but they’re all made out of logic gates.

Every multiplexer has at least one select line, which is used to select which input signal gets relayed to the output. In a 2-to-1 multiplexer, there’s just one select line. More inputs means more select lines: a 4-to-1 multiplexer would have 2 select lines, an 8-to-1 has 3, and so on (2n inputs requires n select lines).

Think of a mux as a “digital switch”. The select line is the throw on the switch, it chooses which of the many inputs get to be the output.

Here’s how you might make a 2-to-1 multiplexer out of logic gates. A and B are the two inputs, X is the select input, and Y is the output.

Here’s what a truth table would look like for such a circuit:

//Behavior Description of Two-to-one-line Multiplexer

module Test(out,A,B,S);

input A,B,S;

output out;

wire n1,a1,a2;

not (n1,S);

and (a1,A,n1);

and (a2,B,S);

or(out,a1,a2);

endmodule

Test Bench File

module muxtest;

reg A,B,S;

wire out;

Test t2(out,A,B,S);

initial

begin

S=0; A=0;B=0;

#20

S=0;A=0;B=1;

#20

S=0;A=1;B=0;

#20

S=0;A=1;B=1;

#20

S=1;A=0;B=0;

#20

S=1;A=0;B=1;

#20

S=1;A=1;B=0;

#20

S=1;A=1;B=1;

#20

S=0;A=0;B=0;

end

initial

$monitor("A=%b B=%b S=%b out=%b time=%0d",

A,B,S,out,$time);

endmodule

===========================================================================================

2nd Method for MUX_2_to_1 Multiplexer

===========================================================================================

Description File

[Mux_2_to_1.v]

module mux(A,B,select,out);

input A,B,select;

output out;

assign out = select ?A:B;

endmodule

[Mux_2_to_1_Testbench.v]

module muxtest;

reg TA,TB,TS;

wire Y;

mux mx2X1(TA,TB,TS,Y);

initial

begin

TS=1;TA=0;TB=1;

#50 TA=0;TB=0;

#50 TS=0;

#50 TA=0;TB=1;

#50 TA=1;TB=0;

#50 TS=1;

#50 TA=1;TB=1;

#50 TA=0;TB=0;

end

initial

$monitor("select =%b A=%b B=%b out=%b time = %0d",TS,TA,TB,Y,$time);

endmodule