如何在VHDL中添加两个16位STD_LOGIC_VECTOR和一个进位到17位?

时间:2015-06-16 14:41:51

标签: vhdl

我有两个输入16位LOGIC_VECTOR A,B和一个Cin:

A,B : in STD_LOGIC_VECTOR(15 DOWNTO 0);
Cin : in STD_LOGIC;
F : out STD_LOGIC_VECTOR(15 downto 0);
Cout : out STD_LOGIC 

和17位LOGIC_VECTOR结果信号:

signal result : STD_LOGIC_VECTOR(16 DOWNTO 0);

和以下代码添加A和B以及Cin:

result <= ('0' & A) + ('0' & B) + Cin;
F <= result(15 DOWNTO 0);
Cout <= result(16);

但它只是添加A,B和Cin被忽略。 我怎么能做到这一点?

测试平台:

A <= "1010100101110011";
B <= "0001111101010101";
Cin <= '1';

但是F是:

1100100011001000

1 个答案:

答案 0 :(得分:1)

验证您的连接,您的代码按写入方式工作。我尝试了以下方法:

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity adder is
    port (
        A, B : in  std_logic_vector(15 downto 0);
        Cin  : in  std_logic;
        F    : out std_logic_vector(15 downto 0);
        Cout : out std_logic
    );
end entity adder;

architecture rtl of adder is
    signal result : std_logic_vector(16 downto 0);
begin
    result <= ('0' & A) + ('0' & B) + Cin;
    F      <= result(15 downto 0);
    Cout   <= result(16);
end architecture rtl;

library ieee;
use ieee.std_logic_1164.all;

entity adder_tb is
end entity adder_tb;

architecture behavioral of adder_tb is
    signal A, B, F : std_logic_vector(15 downto 0);
    signal Cin, Cout : std_logic;
begin

    DUT: entity work.adder
    port map (
        A    => A,
        B    => B,
        Cin  => Cin,
        F    => F,
        Cout => Cout
    );

    A   <= "1010100101110011";
    B   <= "0001111101010101";
    Cin <= '1';

end architecture behavioral;

得到了:

Simulation result

F = 1100100011001001,正如所料。