Electronic – Add with carry in VHDL + operator

adderlibraryvhdl

As I checked some documents about library ieee.std_logic_arith it seems resulting length of A+B will be 64-bit when both A and B are 64-bits.

I want to know if ieee.std_logic_arith have an add operator which generates carry (so that operator generate 65-bit output for adding two 64-bit operands)?

Best Answer

ieee.numeric_std will provide a result the length of the left operand. You could concatenate a leading sign bit or zero bit (for unsigned) with the left operand to produce your 65 bit result.

A look through the source for Synopsys' std_logic_arith shows it's "+" does the same thing.

Library ieee;
use ieee.std_logic_1164.all;
-- use ieee.numeric_std.all;
use ieee.std_logic_arith.all;

entity adder65bit is
    port (
        a,b:    in  unsigned(63 downto 0);
        carry:  out std_logic;
        sum:    out unsigned (63 downto 0)
    );
end entity;

    architecture foo of adder65bit is 
    signal temp:  unsigned(64 downto 0);
    begin
        temp <= '0' & a +  b;
        sum <= temp (63 downto 0);
        carry <= temp(64);
end architecture;

You didn't specify signed or unsigned, this is unsigned, the operands and the results can be ether signed or unsigned instead.

This code analyzes, elaborates and simulates. It works be setting the left operand to be 65 bits long. You mentioned carry so it's shown with one in a method compatible with earlier VHDL tool implementations.

Note the "&" and "+" operators have the same priority, they will be executed in the order they are found left to right.

adder65bit_tb.png

Related Topic