0% found this document useful (0 votes)
45 views

6-Mux & Decoder Verilog Code Using If-Else Statement

The document discusses the design of a 2 to 4 decoder using Verilog. It provides the code for the behaviour model and testbench of the decoder. It also mentions a software demonstration.

Uploaded by

manu tyagi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

6-Mux & Decoder Verilog Code Using If-Else Statement

The document discusses the design of a 2 to 4 decoder using Verilog. It provides the code for the behaviour model and testbench of the decoder. It also mentions a software demonstration.

Uploaded by

manu tyagi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

System Design using Verilog

Contents of the lecture


(1) “if….else” statement
(2) Write behaviour model of 4 to 1 multiplexer
GMR Institute of Technology

using “if….else” statement


(3) Test bench of 4 to 1 multiplexer
(4) Software Demonstration
Verilog code of 4 to 1 multiplexer using “if-else” Statement

Example:
Write behaviour model of 4 to 1 multiplexer

module mux4to1(i,s,y);
GMR Institute of Technology

input [0:3]i;
input [1:0]s;
output y;
reg y;
always @(i,s)
if (s==2'b00)
y=i[0]; S[1] s[0] y
else if (s==2'b01)
0 0 i[0]
y=i[1];
else if (s==2'b10) 0 1 i[1]
y=i[2]; 1 0 i[2]
else if (s==2'b11)
y=i[3]; 1 1 i[3]
endmodule

78
Test Bench of 4 to 1 Mux

module mux4to1 (i,s,y);


input [0:3]i; module testbench;
input [1:0]s; reg [0:3]i;reg [1:0]s; wire y;
output y; mux4to1 uut (.i(i),.s(s),.y(y));
reg y; initial
always @(i,s) begin
if (s==2'b00) $monitor($time,“i[0]=%b,i[1]=%b, i[2]=%b,i[3]=%b,
s[1]=%b,s[0]=%b,y=%b",i[0],i[1],i[2],i[3],s[1],s[0],y);
GMR Institute of Technology

y=i[0];
else if (s==2'b01) #5 i=4'b0101;s=2’b00;
y=i[1]; #5 i=4'b0101;s=2’b01;
else if (s==2'b10) #5 i=4'b0101;s=2’b10;
y=i[2]; #5 i=4'b0101;s=2’b11;
else if (s==2'b11) #5 i=4'b1100;s=2’b00;
y=i[3]; #5 i=4'b1100;s=2’b01;
endmodule #5 i=4'b1100;s=2’b10;
#5 i=4'b1100;s=2’b11;
#5 $finish;
end
endmodule

79
System Design using Verilog

Lecture 25
Structure Modeling (2 to 1 Multiplexer)
GMR Institute of Technology

Contents of the lecture


(1) Structure model of 2-to-1 Multiplexer
(2) Test bench of 2-to-1 Multiplexer
(3) Software Demonstration
Structure model of 2-to-1 Multiplexer

module mux_2_1 (a,s,z);


input [0:1]a;
input s;
output z;
wire sbar, t1, t2;
GMR Institute of Technology

not g1 (sbar, s);


and g2 (t1, a[0], sbar);
and g3 (t2, a[1], s);
or g4 (z, t1, t2);
endmodule

81
Testbench of 2-to-1 Multiplexer

module mux_2_1 (a,s,z);


input [0:1]a; module testbench;
input s; reg [0:1]a;reg s; wire z;
output z; mux_2_1 uut (.a(a),.s(s),.z(z));
wire sbar, t1, t2; initial
begin
not g1 (sbar, s);
$monitor($time,"a[0]=%b,a[1]=%b,s=%b,z=%b",a[0],a[1]
GMR Institute of Technology

and g2 (t1, a[0], sbar); ,s,z);


and g3 (t2, a[1], s); #5 a=2'b01;s=0;
or g4 (z, t1, t2); #5 a=2'b01;s=1;
endmodule #5 a=2'b10;s=0;
#5 a=2'b10;s=1;
#5 $finish;
end
endmodule

82
GMR Institute of Technology

Software Demonstration

83
System Design using Verilog

Contents of the lecture


(1) Write behaviour model of 2 to 4 Decoder
using “if….else” statement
GMR Institute of Technology

(2) Test bench of 2 to 4 Decoder


(3) Software Demonstration
Verilog code of 2 to 4 Decoder using “if-else” Statement

Example:
Write behavior model of 2 to 4 Decoder

module decoder_2_to_4 (a,y);


input [2:0]a;
GMR Institute of Technology

output [0:3]y;
reg [0:3]y;
always @(a)
if (a==3'b100)
y= 4'b1000;
else if (a==3'b101)
y= 4'b0100; a[2] a[1] a[0] y[0] y[1] y[2] y[3]
else if (a==3'b110)
y= 4'b0010; 0 x x 0 0 0 0
else if (a==3'b111) 1 0 0 1 0 0 0
y= 4'b0001; 1 0 1 0 1 0 0
else
y= 4'b0000; 1 1 0 0 0 1 0
endmodule 1 1 1 0 0 0 1

85
Test Bench of 2 to 4 Decoder

module decoder_2_to_4 (a,y);


module testbench;
input [2:0]a;
output [0:3]y; reg [2:0]a; wire [0:3]y;
reg [0:3]y; decoder_2_to_4 uut (.a(a),.y(y));
always @(a) initial
if (a==3'b100) begin
y= 4'b1000; $monitor($time,"a[2]=%b,a[1]=%b,
GMR Institute of Technology

else if (a==3'b101) a[0]=%b,y[0]=%b,y[1]=%b,y[2]=%b, ,y[3]=%b",


y= 4'b0100;
a[2],a[1], a[0],y[0],y[1],y[2],y[3]);
else if (a==3'b110)
y= 4'b0010; #5 a=3'b100;
else if (a==3'b111) #5 a=3'b101;
y= 4'b0001; #5 a=3'b110;
else #5 a=3'b111;
y= 4'b0000; #5 a=3'b000;
endmodule #5 a=3'b101;
#5 $finish;
end
endmodule

86
GMR Institute of Technology

Software Demonstration

87

You might also like