Saturday 4 February 2012

Verilog code for adders/subtractors

Following is the Verilog code for an unsigned 8-bit adder with carry in.
 module adder(a, b, ci, sum);
 input  [7:0] a;
 input  [7:0] b;
 input        ci;
 output [7:0] sum;
        
    assign sum = a + b + ci;

        endmodule 
Verilog code for an unsigned 8-bit adder with carry out.
 module adder(a, b, sum, co);
 input  [7:0] a;
 input  [7:0] b;
 output [7:0] sum;
 output       co;
 wire   [8:0] tmp;

    assign tmp = a + b;
    assign sum = tmp [7:0];
    assign co  = tmp [8];

        endmodule
        

Verilog code for an unsigned 8-bit adder with carry in and carry out.
        module adder(a, b, ci, sum, co);
        input        ci;
        input  [7:0] a;
        input  [7:0] b;
        output [7:0] sum;
        output       co;
        wire   [8:0] tmp;

           assign tmp = a + b + ci;
           assign sum = tmp [7:0];
           assign co  = tmp [8];

        endmodule
        

Verilog code for an unsigned 8-bit adder/subtractor.
 module addsub(a, b, oper, res);
 input        oper;
 input  [7:0] a;
 input  [7:0] b;
 output [7:0] res;
 reg    [7:0] res;
 always @(a or b or oper)
 begin
    if (oper == 1’b0)
       res = a + b;
    else
       res = a - b;
        end
        endmodule
        

5 comments:

  1. Dear,
    You can help me for code verilog and testbench of this topic "signed adder 16 bit have carry in.out flag}
    my email: ndphong18@gmail.com
    thanks so much,

    ReplyDelete
  2. Hello, can you help me with my verilog program and testbench. Really need a help. My email is emyraclarasalvatore@gmail.com

    ReplyDelete
  3. Hi
    Can help me
    Verilog code for adder/subtractor comparator

    ReplyDelete
  4. can u give me testbench for adder/subtractor

    ReplyDelete