6-Mux & Decoder Verilog Code Using If-Else Statement
6-Mux & Decoder Verilog Code 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
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
81
Testbench of 2-to-1 Multiplexer
82
GMR Institute of Technology
Software Demonstration
83
System Design using Verilog
Example:
Write behavior model of 2 to 4 Decoder
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
86
GMR Institute of Technology
Software Demonstration
87