1
\$\begingroup\$

I want to add A, B and the carry in. This should be done using only one big adder. But when I look at the circuit generated, there is an extra adder for the carry in. How can I solve this problem?

Picture:

enter image description here

Code:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity adder is
    port(
        a, b : in unsigned(3 downto 0);
        result : out unsigned(3 downto 0);
        clk : in STD_LOGIC;
        cin : in integer
    );
end entity;
architecture behavioral of adder is
    signal a_r, b_r, result_r : unsigned(3 downto 0);
    signal cin_r : integer;
begin
    process(clk)
    begin
        if rising_edge(clk) then
            a_r <= a;
            b_r <= b;
            cin_r <= cin;
            result_r <= a_r + b_r + to_unsigned(cin_r, 4);
        end if;
        result <= result_r;
    end process;
end behavioral;
\$\endgroup\$
  • \$\begingroup\$ speculation here, so not an answer. Cin is an integer, you may need to extract bit 4 external to your adder process and forward the single bit. But, it looks like it has synthesised what you have asked for, which is two adders, two plus signs in the expression. What width does the function to_unsigned expand up to? If you have a specific FPGA vendor in mind, then they will provide libraries from which you can directly generate full adders. The generic logic you are generating in this step may be synthesised correctly by a vendor's software. \$\endgroup\$ – Neil_UK Aug 27 '16 at 16:06
  • 1
    \$\begingroup\$ What does the technology map output look like. RTL is only half the story - even in Verilog you can end up with the same "two adders" in RTL, but there is only one in the technology map. But either way, your signals seem to be all the wrong size as the others have pointed out, \$\endgroup\$ – Tom Carpenter Aug 27 '16 at 16:39
  • 1
    \$\begingroup\$ cin, cin_r S.B. std_logic (1 bit), result_r <= a_r + b_r + ("" & cin_r); Converts cin_r to unsigned, length 1. \$\endgroup\$ – user8352 Aug 27 '16 at 16:52
  • \$\begingroup\$ You have removed the problem from your question. The good news is it can be recovered from the edit history. You could show your modifications and results in a separate answer. It might even get up votes. \$\endgroup\$ – user8352 Aug 28 '16 at 0:41
1
\$\begingroup\$

Why are cin and cin_r of type integer? You can see in the Altera RTL view that it's trying to synthesize a 32-bit wide signal!

Typically, a VHDL adder would take two unsigned or signed inputs of appropriate width and the cin input of type std_logic or similar one-bit-wide signal. Try changing the two integers into std_logics and seeing if the result_r <= a_r + b_r + ("" & cin_r); (suggested by user8352) can correctly infer an adder with carry-in.

Also, your adder does not appear to correctly handle overflow/carry.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ When changing cin and cin_r to STD_LOGIC I get the following error: Error (10476): VHDL error at adder.vhd(21): type of identifier "cin_r" does not agree with its usage as "natural" type \$\endgroup\$ – gilianzz Aug 27 '16 at 19:38
  • \$\begingroup\$ I don't have any VHDL tools handy, but I changed the form to user8352's comment suggestion. \$\endgroup\$ – user2943160 Aug 27 '16 at 23:19
0
\$\begingroup\$

To solve this problem you have to change cin and cin_r to STD_LOGIC. The line

result_r <= a_r + b_r + to_unsigned(cin_r, 4);

becomes

result_r <= a_r + b_r + ("" & cin_r);

The new code is:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity adder is
    port(
        a, b : in unsigned(3 downto 0);
        result : out unsigned(3 downto 0);
        clk, cin : in STD_LOGIC
    );
end entity;
architecture behavioral of adder is
    signal a_r, b_r, result_r : unsigned(3 downto 0);
    signal cin_r : STD_LOGIC;
begin
    process(clk)
    begin
        if rising_edge(clk) then
            a_r <= a;
            b_r <= b;
            cin_r <= cin;
            result_r <= a_r + b_r + ("" & cin_r);
        end if;
        result <= result_r;
    end process;
end behavioral;
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

All current answers are bad design. a b and c should all be either integers or unsigneds. Correctly defining the ranges will solve the problem.

Either cin and cin_r become

unsigned(0 downto 0)

and a and b stay as they are now or you put

a : natural range 0 to 7
b : natural range 0 to 7
a_r : natural range 0 to 7
b_r : natural range 0 to 7
cin : natural range 0 to 1
cin_r : natural range 0 to 1
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.