Wednesday, February 29, 2012

4 Bit Carry Look Ahead Adder in Verilog


DataFlow Model : 4 Bit CLA

module CLA_4bit(
    output [3:0] S,
    output Cout,PG,GG,
    input [3:0] A,B,
    input Cin
    );
    wire [3:0] G,P,C;

    assign G = A & B; //Generate
    assign P = A ^ B; //Propagate
    assign C[0] = Cin;
    assign C[1] = G[0] | (P[0] & C[0]);
    assign C[2] = G[1] | (P[1] & G[0]) | (P[1] & P[0] & C[0]);
    assign C[3] = G[2] | (P[2] & G[1]) | (P[2] & P[1] & G[0]) |             (P[2] & P[1] & P[0] & C[0]);
    assign Cout = G[3] | (P[3] & G[2]) | (P[3] & P[2] & G[1]) | (P[3] & P[2] & P[1] & G[0]) |(P[3] & P[2] & P[1] & P[0] & C[0]);
    assign S = P ^ C;
    
    assign PG = P[3] & P[2] & P[1] & P[0];
    assign GG = G[3] | (P[3] & G[2]) | (P[3] & P[2] & G[1]) | (P[3] & P[2] & P[1] & G[0]);
endmodule

Test Bench : 4 Bit CLA

module Test_CLA_4bit;
    // Inputs
    reg [3:0] A;
    reg [3:0] B;
    reg Cin;

    // Outputs
    wire [3:0] S;
    wire Cout;
    wire PG;
    wire GG;

    // Instantiate the Unit Under Test (UUT)
    CLA_4bit uut (
    .S(S), 
    .Cout(Cout), 
    .PG(PG), 
    .GG(GG), 
    .A(A), 
    .B(B), 
    .Cin(Cin)
    );

    initial begin
    // Initialize Inputs
    A = 0; B = 0; Cin = 0;
    // Wait 100 ns for global reset to finish
    #100;
        
    // Add stimulus here
    A=4'b0001;B=4'b0000;Cin=1'b0;
    #10 A=4'b100;B=4'b0011;Cin=1'b0;
    #10 A=4'b1101;B=4'b1010;Cin=1'b1;
    #10 A=4'b1110;B=4'b1001;Cin=1'b0;
    #10 A=4'b1111;B=4'b1010;Cin=1'b0;
    end

    initial begin
$monitor("time=",$time,, "A=%b B=%b Cin=%b : Sum=%b Cout=%b PG=%b GG=%b",A,B,Cin,S,Cout,PG,GG);
    end      
endmodule

Simulation Results

time = 0 A=0000 B=0000 Cin=0 : Sum=0000 Cout=0 PG=0 GG=0
time = 100 A=0001 B=0000 Cin=0 : Sum=0001 Cout=0 PG=0 GG=0
time = 110 A=0100 B=0011 Cin=0 : Sum=0111 Cout=0 PG=0 GG=0
time = 120 A=1101 B=1010 Cin=1 : Sum=1000 Cout=1 PG=0 GG=1
time = 130 A=1110 B=1001 Cin=0 : Sum=0111 Cout=1 PG=0 GG=1
time = 140 A=1111 B=1010 Cin=0 : Sum=1001 Cout=1 PG=0 GG=1

42 comments:

  1. Can this block be used for 8 bit CLA? Thanks.

    ReplyDelete
    Replies
    1. Sure...
      CLA_4bit CLA1(S[3:0],c3,p3,g3,A[3:0],B[3:0],Cin);
      CLA_4bit CLA2(S[7:4],c7,p7,g7,A[7:4],B[7:4],c3);

      For more efficient implementation, you can compute c3 and c7 using the group carry logic.

      Delete
    2. Can you pls show me how to do that?...I'm not sure that I can do it all alone and...I'm a begginer user of verilog...10X

      Delete
    3. module CLA_8bit(
      output [7:0] Sum,
      output Cout,
      input [7:0] A,B,
      input Cin
      );
      wire c3,p3,g3,p7,g7;
      CLA_4bit CLA1(Sum[3:0],c3,p3,g3,A[3:0],B[3:0],Cin);
      CLA_4bit CLA2(Sum[7:4],Cout,p7,g7,A[7:4],B[7:4],c3);
      endmodule

      If you need more efficient implementation, you need to compute c3 and c7 using the group carry logic. Internet has many resources on group carry logic.

      Delete
    4. can you plz give me full verilog code for 8bit CLA??thanku

      Delete
  2. you have fan ins greater than 4 which isn't an efficient design

    ReplyDelete
    Replies
    1. This is actually a Data Flow Model, you don't need to consider the fan-in etc here!
      When you do the synthesis, you can specify electronic (no of fan-in etc), speed and area constrains to optimize your design. (The synthesis tool may automatically do the optimization for you)

      Thanks for the interest shown...

      Delete
  3. Thanks for writing the post, it's helpful to someone trying to learn Verilog!

    ReplyDelete
  4. Can someone help me with a booth multiplier using carry lookahead i need to multiply two 8 bit numbers and make them 16 bit. Thank u

    ReplyDelete
  5. i need simplified 4 bit carry look ahead adder

    ReplyDelete
  6. Hey, i need a 9-bit hybrid adder using a 4-bit CLA and 5-bit CLA. I can write both of them like you did above, but i'm not sure how to connect them. Please help me, it's for my homework project.

    ReplyDelete
  7. can any one give the code for merged arithmetic scheme

    ReplyDelete
  8. what is pg and gg in the above progrme are they really needed

    ReplyDelete
    Replies
    1. Not really needed. (They are there for completeness, needed when you need to propagate group carry logic)

      Delete
  9. there is a small mistake in this program in test bench if am not wrong.

    #10 A=4'b1000;B=4'b0011;Cin=1'b0;

    ReplyDelete
    Replies
    1. Do you mean #10 A=4'b100;B=4'b0011;Cin=1'b0;?
      It was done intentionally, it is same as assigning A = 4'b0100;.
      See the output line-3.

      Delete
  10. If I were made to do this using bit slices would I eliminate all the buses and make this a separate module?

    ReplyDelete
  11. Could you use PG or GG as a way to detect overflow errors?

    ReplyDelete
  12. thanxx a lot...i write this code in my samester exam and i got 15 marks out of 15...thanxx jeya

    ReplyDelete
  13. can u post squarer verilog program

    ReplyDelete
  14. Hi
    So I've been following your blog and find it extremely helpful. I'm presently working on a measuring the performance of an 8, 16, 32 bit CLA adder in 4 bit groups with the groups connected in ripple carry method.
    Could you kindly provide any help with that? It would be great if you could show it to me in the form of a verilog code.
    Thanks in advance!
    - Nikhila

    ReplyDelete
  15. hi..I am also currently working on 8,16,32 bit CLA adder in 4 bit groups with the groups connected in ripple carry method. Could you please me with the verilog code?

    Thanks!!

    ReplyDelete
  16. module fourbitCLA(S, Cout, PG, GG, A, B, Cin);
    output [3:0] S;
    output Cout,PG,GG;
    reg [3:0] S;
    reg Cout,PG,GG;
    input [3:0] A,B;
    input Cin;
    wire [3:0] A,B;
    wire Cin;
    reg[4:0] G,P,C;
    integer i;
    always @ (A or B or Cin) begin
    C[0] = Cin;
    for (i=0;i<=3;i=i+1) begin
    G[i] = A[i] & B[i];
    P[i] = A[i] ^ B[i];
    S[i] = P[i] ^ C[i];
    C[i+1] = G[i] | (P[i] & C[i]);
    end
    Cout = C[4];
    PG = P[3] & P[2] & P[1] & P[0]; // Group Propagate
    GG = G[3] | (P[3] & G[2]) | (P[3] & P[2] & G[1]) | (P[3] & P[2] & P[1] & G[0]); // Group Generate
    end
    endmodule

    BEHAVIORAL MODEL FOR THE SAME PROGRAM!!!!!!!!!!!!!!!!

    - FUAAD

    ReplyDelete
  17. module fourbitCLA(S, Cout, PG, GG, A, B, Cin, G, P, C, W1, W2, W3, W4, W5, W6, W7, W8, W9, W10, W11);
    output [3:0] S;
    output Cout,PG,GG;
    input [3:0] A,B;
    input Cin;
    inout [3:0]G,P,C;
    inout W1,W2,W3,W4,W5,W6,W7,W8,W9,W10,W11;
    and (G[3],A[3],B[3]);//Generate
    and (G[2],A[2],B[2]);
    and (G[1],A[1],B[1]);
    and (G[0],A[0],B[0]);
    xor (P[3],A[3],B[3]);//Propagate
    xor (P[2],A[2],B[2]);
    xor (P[1],A[1],B[1]);
    xor (P[0],A[0],B[0]);
    buf (C[0],Cin);
    and (W1,P[0],C[0]);
    or (C[1],G[0],W1);
    and (W2,P[1],C[1]);
    or (C[2],G[1],W2);
    and (W3,P[2],C[2]);
    or (C[3],G[2],W3);
    and (W4,P[3],C[3]);
    or (Cout,G[3],W4);//Cout
    xor (S[3],P[3],C[3]);// Sum
    xor (S[2],P[2],C[2]);
    xor (S[1],P[1],C[1]);
    xor (S[0],P[0],C[0]);
    and (W5,P[3],P[2]);
    and (W6,P[1],W5);
    and (PG,P[0],W6);// Group Propagate
    and (W7,G[0],W6);
    and (W8,G[1],W5);
    and (W9,G[2],P[3]);
    or (W10,W8,W7);
    or (W11,W10,W9);
    or (GG,W11,G[3]);// Group Generate
    endmodule

    STRUCTURAL MODEL FOR THE SAME PROGRAM!!!!!!!!!!!!!

    ReplyDelete
    Replies
    1. Hai, This one was really fantastic yaar. It helped me alot

      Delete
    2. Thanks a lot for posting this

      Delete
    3. Could you please post the structural model for the same program using NOR gates instead?

      Delete
  18. How to implement above code to create 64 bit CLA adder?

    ReplyDelete
  19. Can you please post the 4-bit carry lookahead adder using continuous assignment statements in behavioral model

    ReplyDelete
  20. we want verilog code for energy efficient hybrid adder which inculdes ripple carry adder and carry look ahead adder.
    http://www.sciencedirect.com/science/article/pii/S0167926014000480

    ReplyDelete
  21. we want verilog code for energy efficient hybrid adder which inculdes ripple carry adder and carry look ahead adder.
    http://www.sciencedirect.com/science/article/pii/S0167926014000480

    ReplyDelete
  22. anyone please tell me how to write behavioral code for 4bit,16bit cla,and how to write generic code for n bit

    ReplyDelete
  23. how would you write this using g0,g1,g2,g3,p0,p1,p2,p3 instead of [3:0] a,b?

    ReplyDelete
  24. could you please help me write a code for 4 bit vedic multiplier using carry look ahead adder?

    ReplyDelete
  25. I'm looking for ways on how to create a 8-bit hybrid CLA-CPA adder out of two 4-bit CLA adders. Could you provide me with code on how to do so in Verilog? Thank you Jeya

    ReplyDelete
  26. Hi Jeya, Can you please show me how to implement this for a 32 bit?

    ReplyDelete
  27. we want 8bit cla verilog code would you please help us...

    ReplyDelete
  28. sir shall u give me a code for booth multiplication using Verilog.please help me...

    ReplyDelete