Damien said:
I would like to add two 4 bits number, and get the result and the carry. Is
there any way too do that with the '+' operator ? Otherwise, what is the
best way to solve this problem ?
If you concatenate a "0" on the front of each number, then add them,
the low four bits of the result are your sum, and the high bit is
the carry out.
Example below; I haven't actually compiled this exact code, but it's
basically a simplified version of something I actually use.
If you also want a signed arithmetic overflow output (which is NOT the
same thing as carry out), you can get that by using a two bit extension
rather than a single bit.
Lately I've been having fun with BCD/binary adder/subtractors, using
carry select and carry lookahead. With careful desgin (but FPGA vendor
neutral and not floorplanned), I was able to get a 56 bit version with
minimum cycle time of just over 10 ns.
Eric
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity binary_adder_with_carry_out is
generic (width: integer := 4);
port (operand_a: in std_logic_vector (width - 1 downto 0);
operand_b: in std_logic_vector (width - 1 downto 0);
carry_in: in std_logic;
result: out std_logic_vector (width - 1 downto 0);
carry_out: out std_logic);
end binary_adder_with_carry_out;
architecture behavioral of binary_adder_with_carry_out is
signal a_ext: unsigned (width downto 0);
signal b_ext: unsigned (width downto 0);
signal c_ext: unsigned (0 downto 0);
signal temp: unsigned (width downto 0);
begin
a_ext <= unsigned ("0" & operand_a);
b_ext <= unsigned ("0" & operand_b);
c_ext (0) <= carry_in;
temp <= a_ext + b_ext + c_ext;
result <= temp (width - 1 downto 0);
carry_out <= temp (width);
end behavioral;