Verilog code for 8_1 Multiplexer (MUX) - All modeling styles
Verilog code for 8_1 Multiplexer (MUX) - All modeling styles
B
line and produce that in the output section. It is implemented using
combinational circuits and is very commonly used in digital systems.
C
Sending data over multiplexing reduces the cost of transmission
lines, and saves bandwidth.
A 2^n:1 multiplexer has 2^n input lines, n select lines, and a single
output line. You can find a detailed explanation and schematic
representation for multiplexers over here.
This article will deal with the modeling styles for an 8:1 multiplexer.
Verilog code for 8:1 mux using structural modeling
RTL schematic
Testbench for 8×1 Mux using Verilog
TCL Console
Simulation Waveforms
Logic circuit
The following figure is the 8×1 multiplexer. Now this 8×1 MUX is a
high-level multiplexer. For simplicity, the 8×1 mux can also be
implemented using 2×1 or 4×1 multiplexers.
You can observe that the input signals
are D0, D1, D2, D3, D4, D5, D6, D7, S0, S1, S2 and the output signal
is out.
`timescale 1ns/1ps
wire T1, T2, T3, T4, T5, T6, T7, T8, T9, T10,
T11;
and(T4, D0, T1, T2, T3);
or(out, T4, T5, T6, T7, T8, T9, T10, T11);
T4 is the output for AND gate, D0, T1, T2, and T3 are
the input variables.
For OR gate, the output is out and input is T4, T5, T6,
T7, T8, T9, T10 and T11.
If there exist more than two same gates, we can concatenate the
expression into one single statement.
Summing up, we will get the final gate-level modeling Verilog code:
`timescale 1ns/1ps
module m81(input D0, D1, D2, D3, D4, D5, D6,
D7, S0, S1, S2, output out);
wire T1, T2, T3, T4, T5, T6, T7, T8, T9, T10,
T11;
not(T1, S0);
not(T2, S1);
not(T3, S2);
and(T4, D0, T1, T2, T3), (T5, D1, S0, T2,
T3);
and(T6, D2, T1, S1, T3), (T7, D3, S0, S1,
T3);
and(T8, D4, T1, T2, S2), (T9, D5, S0, T2,
S2);
and(T10, D6, T1, S1, S2), (T11, D7, S0, S1,
S2);
RTL Schematic For Gate-level Modeling
where D0, D1, D2, D3, D4, D5, D6, D7, S0, S1, and S2 are the inout
variables and the output variable is out.
assign S0bar=~S0;
assign S1bar=~S1;
assign S2bar=~S2;
Behavioral modeling
This is the highest abstraction layer of all. It emphasizes the behavior
of the digital circuit. In most cases, implementing the truth table will
describe the behavior with no failure.
Truth table
Let’s write down the cases for each row of the truth table. For S0=0,
S1=0, S2=0, the input variable D0 will get transferred to the output
variable out.
3'b000: out=D0;
Similarly,
3'b001: out=D1;
3'b010: out=D2;
3'b011: out=D3;
3'b100: out=D4;
3'b101: out=D5;
3'b110: out=D6;
3'b111: out=D7;
3'b000 represents the 3- bit binary value for the expression inside
the case statement.
Design code:
module m81(out, D0, D1, D2, D3, D4, D5, D6,
D7, S0, S1, S2);
input wire D0, D1, D2, D3, D4, D5, D6, D7,
S0, S1, S2;
output reg out;
always@(*)
begin
case(S0 & S1 & S2)
3'b000: out=D0;
3'b001: out=D1;
3'b010: out=D2;
3'b011: out=D3;
3'b100: out=D4;
3'b101: out=D5;
3'b110: out=D6;
3'b111: out=D7;
default: out=1'b0;
endcase
end
endmodule
RTL Schematic
logic diagram for 8×1 MUX
Start with the name of the module you need. This will
work as an instance. Give this instance a name. Then
mention the port-list.
`timescale 1ns/1ps
module top;
wire out;
reg D0, D1, D2, D3, D4, D5, D6, D7, D8, S0,
S1, S2;
m81 name(.D0(D0), .D1(D1), .D2(D2), .D3(D3),
.D4(D4), .D5(D5), .D6(D6), .D7(D7), .S0(S0),
.S1(S1), .S2(S2), .out(out));
initial
begin
D0=1'b0; D1=1'b0; D2=1'b0; D3=1'b0; D4=1'b0;
D5=1'b0; D6=1'b0; D7=1'b0;S0=1'b0; S1=1'b0;
S2=1'b0;
#500 $finish;
end
always #1 D0=~D0;
always #2 D1=~D1;
always #3 D2=~D2;
always #4 D3=~D3;
always #5 D4=~D4;
always #6 D5=~D5;
always #7 D6=~D6;
always #8 D7=~D7;
always #9 S0=~S0;
always #10 S1=~S1;
always #11 S2=~S2;
always@(D0 or D1 or D2 or D3 or D4 or D5 or
TCL Console
Simulation Waveforms
The simulation waveform for 8X1 MUX is:
The above simulation result is the same for each of the abstraction
layers, truly satisfying the truth table.
5 thoughts on “Verilog code for 8:1 Multiplexer (MUX)
styles”
1. Bire says:
March 29, 2021 at 9:42 PM
Sir can u tell me how can write verilog cod and vhdl code foe 16-1mux and 1-16 dwmux
Reply
1. Umair Hussaini says:
April 5, 2021 at 11:53 AM
Hi Bire! You can easily change the code in the above post for a 16:1 mux by simply in
steps in this post for the VHDL coding of a demux.
Reply
2. Arpit says:
September 7, 2020 at 1:43 PM
This is with respect to behavioral style of modeling.
In the statement, case(S0 & S1 & S2), let us suppose values of S0, S1, S2 are 0,1,0 respectiv
so, S0 & S1 & S2 evaluates to 0&1&0 = 0.
And therefore the control switches to 1’b000: out=0; (i.e. 0),
instead of switching to 1’b010: out =D2;
can’t we simply leave case(S0 S1 S2) ??
Reply
1. Umair Hussaini says:
October 10, 2020 at 10:14 PM
Can you please reframe your question? I didn’t quite understand it.
Reply
1. James says:
January 2, 2021 at 10:44 PM
He’s pointing out a mistake. case(S0 & S1 & S2) should be replaced with
case({S2,S1,S0})
or
wire [2:0] select;
assign select = {S2,S1,S0};
case(select)
Reply