Combinational Designs: Verilog
Combinational Designs: Verilog
Combinational Designs: Verilog
Combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 1
Verilog HDL-combinational Designs
Contents
• Part 1- 4:1 Multiplexer
• Part 2- Encoders and Decoder
• Part 3- 4-bit Ripple Carry Adder
• Part 4- Magnitude Comparator
A.Prathiba/SENSE/VIT CHENNAI 2
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 3
Verilog HDL-combinational Designs
4-to-1 Multiplexer
• Structural or Gate Level Design
A.Prathiba/SENSE/VIT CHENNAI 4
Verilog HDL-combinational Designs
4-to-1 Multiplexer
//Structural or Gate Level Design
module m41(out, a, b, c, d, s0, s1);
output out;
input a, b, c, d, s0, s1;
wire sobar, s1bar, T1, T2, T3, T4;
not (s0bar, s0);
not (s1bar, s1);
and (T1, a, s0bar, s1bar);
and (T2, b, s0bar, s1);
and (T3, c, s0, s1bar);
and (T4, d, s0, s1);
or(out, T1, T2, T3, T4);
endmodule
A.Prathiba/SENSE/VIT CHENNAI 5
Verilog HDL-combinational Designs
4-to-1 Multiplexer
// Gate Level Modeling Testbench
module mux_2_tb;
wire out;
reg a,b,c,d,s0,s1;
m41 uut(out,a,b,c,d,s0,s1);
initial
begin
$dumpfile("dump.vcd");
$dumpvars(1, out);
$dumpvars(1, a);
$dumpvars(1, b);
$dumpvars(1, c);
$dumpvars(1, d);
$dumpvars(1, s0);
$dumpvars(1, s1);
A.Prathiba/SENSE/VIT CHENNAI 6
Verilog HDL-combinational Designs
#5 a=0;b=1;c=0;d=1;s0=0;s1=0;
#5 $display("a=%b b=%b c=%b d=%b s0=%b s1=%b out=%b",a,b,c,d,s0,s1,out);
#5 s0=0;s1=1;
#5 $display("a=%b b=%b c=%b d=%b s0=%b s1=%b out=%b",a,b,c,d,s0,s1,out);
#5 s0=1;s1=0;
#5 $display("a=%b b=%b c=%b d=%b s0=%b s1=%b out=
%b",a,b,c,d,s0,s1,out);
#5 s0=1;s1=1;
#5 $display("a=%b b=%b c=%b d=%b s0=%b s1=%b out=
%b",a,b,c,d,s0,s1,out);
end
endmodule
A.Prathiba/SENSE/VIT CHENNAI 7
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 8
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 9
Verilog HDL-combinational Designs
4-to-1 Multiplexer
out = (a. s1′.s0′) + (b.s1′.s0) + (c.s1.s0′) + (d. s1.s0)
//4X 1 multiplexer data flow modeling
module m41_df(
input a,
input b,
input c,
input d,
input [1:0]select,
output out);
endmodule
A.Prathiba/SENSE/VIT CHENNAI 10
Verilog HDL-combinational Designs
4-to-1 Multiplexer
// DATA FLOW MODELING 4:1 MULTIPLEXER TESTBENCH
module m41_df_tb;
wire out;
reg a,b,c,d;
reg [1:0] select;
m41_df uut(a,b,c,d,select,out);
initial
begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1, out);
$dumpvars(1, a);
$dumpvars(1, b);
$dumpvars(1, c);
$dumpvars(1, d);
$dumpvars(1, select);
A.Prathiba/SENSE/VIT CHENNAI 11
Verilog HDL-combinational Designs
4-to-1 Multiplexer
#5 a=0;b=1;c=0;d=1;select=2'b00;
#5 $display("a=%b b=%b c=%b d=%b select=%b out=%b",a,b,c,d,select,out);
#5 select=2'b01;
#5 $display("a=%b b=%b c=%b d=%b select=%b out=
%b",a,b,c,d,select,out);
#5 select=2'b10;
#5 $display("a=%b b=%b c=%b d=%b select=%b out=
%b",a,b,c,d,select,out);
#5 select=2'b11;
#5 $display("a=%b b=%b c=%b d=%b select=%b out=
%b",a,b,c,d,select,out);
end
endmodule
A.Prathiba/SENSE/VIT CHENNAI 12
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 13
Verilog HDL-combinational Designs
4-to-1 Multiplexer
A.Prathiba/SENSE/VIT CHENNAI 14
Verilog HDL-combinational Designs
4-to-1 Multiplexer
// conditional statement
module mux_df_cond(
input a,
input b,
input c,
input d,
input [1:0]select,
output out);
endmodule
A.Prathiba/SENSE/VIT CHENNAI 15
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 16
Verilog HDL-combinational Designs
#5 a=0;b=1;c=0;d=1;select=2'b00;
#5 $display("a=%b b=%b c=%b d=%b select=%b out=%b",a,b,c,d,select,out);
#5 select=2'b01;
#5 $display("a=%b b=%b c=%b d=%b select=%b out=%b",a,b,c,d,select,out);
#5 select=2'b10;
#5 $display("a=%b b=%b c=%b d=%b select=%b out=%b",a,b,c,d,select,out);
#5 select=2'b11;
#5 $display("a=%b b=%b c=%b d=%b select=%b out=%b",a,b,c,d,select,out);
end
endmodule
A.Prathiba/SENSE/VIT CHENNAI 17
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 18
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 19
Verilog HDL-combinational Designs
4-to-1 Multiplexer
I0
I1 4-TO-
1 Y S1 S0 Y
I2 MUX 0 0 I0
I3 0 1 I1
1 0 I2
1 1 I3
S1 s0
A.Prathiba/SENSE/VIT CHENNAI 20
Verilog HDL-combinational Designs
4-to-1 Multiplexer
//behavioral design : if else else if (sel == 2'b10)
statement
module mux4x1 (a, b, c, d, sel, out); out <= c;
input a, b, c, d; else if (sel == 2'b11)
input [1:0] sel;
out <= d;
output reg out;
always @ (a or b or c or d or sel) else
begin out<= 1'b0;
if (sel == 2'b00)
out <= a;
end
else if (sel == 2'b01) endmodule
out <= b;
A.Prathiba/SENSE/VIT CHENNAI 21
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 22
Verilog HDL-combinational Designs
#5 a=0;b=1;c=0;d=1;sel=2'b00;
#5 $display("a=%b b=%b c=%b d=%b sel=%b out=%b",a,b,c,d,sel,out);
#5 sel=2'b01;
#5 $display("a=%b b=%b c=%b d=%b sel=%b out=
%b",a,b,c,d,sel,out);
#5 sel=2'b10;
#5 $display("a=%b b=%b c=%b d=%b sel=%b out=
%b",a,b,c,d,sel,out);
#5 sel=2'b11;
#5 $display("a=%b b=%b c=%b d=%b sel=%b out=%b",a,b,c,d,sel,out);
end
endmodule
A.Prathiba/SENSE/VIT CHENNAI 23
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 24
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 25
Verilog HDL-combinational Designs
always @(*)
case (sel)
2'b00 : out<= a;
2'b01 : out<= b;
2'b10 : out <= c;
default : out <= d;
endcase
endmodule
A.Prathiba/SENSE/VIT CHENNAI 26
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 27
Verilog HDL-combinational Designs
#5 a=0;b=1;c=0;d=1;sel=2'b00;
#5 $display("a=%b b=%b c=%b d=%b sel=%b out=%b",a,b,c,d,sel,out);
#5 sel=2'b01;
#5 $display("a=%b b=%b c=%b d=%b sel=%b out=%b",a,b,c,d,sel,out);
#5 sel=2'b10;
#5 $display("a=%b b=%b c=%b d=%b sel=%b out=%b",a,b,c,d,sel,out);
#5 sel=2'b11;
#5 $display("a=%b b=%b c=%b d=%b sel=%b out=%b",a,b,c,d,sel,out);
// #5 sel=2'b11;
// $display("a=%b b=%b c=%b d=%b sel=%b out=%b",a,b,c,d,sel,out);
end
endmodule
A.Prathiba/SENSE/VIT CHENNAI 28
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 29
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 30
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 31
Verilog HDL-combinational Designs
Encoders (2:4)
A.Prathiba/SENSE/VIT CHENNAI 32
Verilog HDL-combinational Designs
endmodule
A.Prathiba/SENSE/VIT CHENNAI 33
Verilog HDL-combinational Designs
Test bench
module encoder_2_4_tb;
// Inputs
reg [3:0] in;
// Outputs
wire [1:0] y;
A.Prathiba/SENSE/VIT CHENNAI 34
Verilog HDL-combinational Designs
initial begin
// Initialize Inputs
$dumpfile("dump.vcd");
$dumpvars(1, in);
$dumpvars(1, y);
$monitor( "in=%b, y=%b",in, y);
endmodule
A.Prathiba/SENSE/VIT CHENNAI 35
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 36
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 37
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 38
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 39
Verilog HDL-combinational Designs
Test bench
module decoder_tb;
reg A;
reg B;
reg enable;
wire [0:3] D;
decoder uut (
.D(D),
.A(A),
.B(B),
.enable(enable)
);
initial begin
$dumpfile("dump.vcd");
$dumpvars(1, enable);
$dumpvars(1, A);
$dumpvars(1, B);
$dumpvars(1, D);
A.Prathiba/SENSE/VIT CHENNAI 40
Verilog HDL-combinational Designs
end
endmodule
A.Prathiba/SENSE/VIT CHENNAI 41
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 42
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 43
Verilog HDL-combinational Designs
endmodule
A.Prathiba/SENSE/VIT CHENNAI 44
Verilog HDL-combinational Designs
Test bench
module decoder_beh_tb;
// Inputs
reg [1:0] x;
reg enable_n;
// Outputs
wire [3:0] y;
initial begin
$monitor( ": enable_n=%b, x=%b, y=%b", enable_n, x, y);
A.Prathiba/SENSE/VIT CHENNAI 45
Verilog HDL-combinational Designs
// Initialize Inputs
$dumpfile("dump.vcd");
$dumpvars(1, enable_n);
$dumpvars(1, x);
$dumpvars(1, y);
enable_n = 1;
enable_n = 0;
// Wait 100 ns for global reset to finish
x = 2'b00;
#2 x = 2'b01;
#2 x = 2'b10;
#2 x = 2'b11;
end
endmodule
A.Prathiba/SENSE/VIT CHENNAI 46
Verilog HDL-combinational Designs
module decoder_2x4_df_tb;
// Inputs
reg A;
reg B;
reg enable;
// Outputs
wire [0:3] D;
A.Prathiba/SENSE/VIT CHENNAI 48
Verilog HDL-combinational Designs
initial begin
$monitor( ": enable=%b, A=%b, B=%b D=%b", enable, A, B,D);
$dumpfile("dump.vcd");
$dumpvars(1, enable);
$dumpvars(1, A);
$dumpvars(1, B);
$dumpvars(1, D);
// Initialize Inputs
enable = 1;
A = 0;
B = 0;
# 5 enable = 0;
A.Prathiba/SENSE/VIT CHENNAI 49
Verilog HDL-combinational Designs
#10 A = 1;
#10 B = 0;
#10 A = 1;
#10 B = 1;
// Add stimulus here
end
endmodule
A.Prathiba/SENSE/VIT CHENNAI 50
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 51
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 52
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 53
Verilog HDL-combinational Designs
Half adder
A.Prathiba/SENSE/VIT CHENNAI 54
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 55
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 58
Verilog HDL-combinational Designs
module testAdder;
reg [3:0] A;
reg [3:0] B;
reg carryIn;
wire [3:0] Sum;
wire carryOut;
Four_bit_adder adder( Sum, carryOut, A, B, carryIn );
initial
begin
$dumpfile("dump.vcd");
$dumpvars(1, A);
$dumpvars(1,B);
$dumpvars(1, carryIn);
$dumpvars(1, Sum);
$dumpvars(1, carryOut);
A.Prathiba/SENSE/VIT CHENNAI 59
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 61
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 62
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 63
Verilog HDL-combinational Designs
module mag_compare_tb;
// Inputs
reg [3:0] A;
reg [3:0] B;
// Outputs
wire A_lt_B;
wire A_eq_B;
wire A_gt_B;
A.Prathiba/SENSE/VIT CHENNAI 64
Verilog HDL-combinational Designs
initial begin
$monitor( ": A=%b, B=%b A_lt_B=%b A_eq_B=%b A_gt_B=%b", A,B,A_lt_B,A_eq_B,A_gt_B);
$dumpfile("dump.vcd");
$dumpvars(1, A);
$dumpvars(1, B);
$dumpvars(1, A_lt_B);
$dumpvars(1, A_eq_B);
$dumpvars(1, A_gt_B);
// Initialize Inputs
// Initialize Inputs
A = 4'b0000;
B = 4'b0000;
#100 A = 4'b0001;
#100 B = 4'b0000;
#100 A = 4'b0000;
#100 B = 4'b0001;
end
endmodule
A.Prathiba/SENSE/VIT CHENNAI 65
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 66
Verilog HDL-combinational Designs
A.Prathiba/SENSE/VIT CHENNAI 67