Verilog Code For 2 - 1 Multiplexer (MUX) - All Modeling Styles
Verilog Code For 2 - 1 Multiplexer (MUX) - All Modeling Styles
B
Get knowledge on different styles of modeling in Verilog
HDL
C
Design the 2:1 MUX in Verilog with all abstraction layers
(modeling styles).
Generate RTL Schematic and simulate the 2:1 MUX
using testbench.
What is a multiplexer?
A multiplexer is a device that selects one output from multiple inputs.
It is also known as a data selector. Multiplexers are used in
communication systems to increase the amount of data that can be
sent over a network within a certain amount of time and bandwidth. It
allows us to ‘squeeze’ multiple data lines into one data line.
S
The multiplexer (MUX) functions as a multi-input and single-output
switch. The selection of the input is done using select lines. A MUX
3. Behavioral modeling
4. Structural modeling
In this article, we’ll write the Verilog code for the simplest multiplexer,
i.e. a 2:1 MUX.
Contents
Gate Level Modeling
Logic circuit
Verilog code for 2:1 MUX using gate-level modeling
RTL Schematic
Data flow modeling
lowest level modules, as the switch level modeling isn’t that common.
The prerequisite for this style is knowing the basic logic diagram of
the digital circuit that you wish to code.
Since we’re concerned about designing the Verilog code for a 2:1
MUX, have a look at its circuit diagram.
Logic circuit
output Y;
input D0, D1, S;
wire T1, T2, Sbar;
Next comes the instantiation part for gates. and not and or are the
predefined built-in gates, and we’re instantiating these gates with
their respective input-output ports.
For example for not gate, Sbar is the output and S is the input.
Similarly for and gate, T1, D1, and T2, D2 are inputs to two and gates
and S and Sbar are their respective output.
output Y;
input D0, D1, S;
wire T1, T2, Sbar;
endmodule
RTL Schematic
This is the design abstraction, which shows the internal circuitry
involved. It is the hardware implementation of a system.
Final code:
output Y;
input D0, D1, S;
assign Y=(S)?D1:D0;
endmodule
RTL Schematic
The hardware schematic for a 2:1 multiplexer in dataflow level
modeling is shown below. You will notice that this schematic is
different from that of the gate-level. It involves the symbol of a
multiplexer rather than showing up the logic gates involved, unlike
gate-level modeling.
This level describes the behavior of a digital system. In most of the
cases, we code the behavioral model using the truth table of the
circuit.
Now to find the expression, we will use K- map for final output Y.
Don’t forget to mention the data- type of the ports. Since it is the
behavioral modeling, we will declare the output Y as reg while the
rest of the inputs as wire.
always @(D0 or D1 or S)
begin
if(S)
Y= D1;
else
Y=D0;
end
endmodule
RTL Schematic
Hardware schematic for 2:1 MUX:
defined here. There’s a proper definition for the expression of the
digital system within the module itself. It includes module declaration
and instantiation, port-list and it’s associates.
Logic circuit
Now the logical diagram for a 2:1 MUX shows that we need two AND
gates, one OR gate and one NOT gate. We’ll structurize for each of
the gates separately.
Now using the assign statement, write the function of the logic gate
module or_gate(output l, input m, n);
assign l = m | n;
endmodule
NOTE: use a different variable name for each input and output signal.
First, write the name of the module you need. Then give the instance
a name. The association list will contain the output signal first,
followed by the input ones.
RTL schematic
module top;
wire out;
reg d0, d1, s;
m21 name(.Y(out), .D0(d0), .D1(d1), .S(s));
initial
begin
d0=1'b0;
d1=1'b0;
s=1'b0;
#100 $finish;
end
always #40 d0=~d0;
always #20 d1=~d1;
always #10 s=~s;
always@(d0 or d1 or s)
$monitor("At time = %t, Output = %d", $time,
out);
endmodule;
Simulation Waveform
Here is the final simulated waveform for the 2X1 MUX circuit.
designing of logic taught from the basics in
circuits using the CMOS an easy to understand
inverter. manner.