使用无符号矢量减法时,如何检查进位?

时间:2019-02-28 19:38:27

标签: vhdl

我尝试实现仅具有8位加减法器的体系结构。但是有一个我无法解决的问题。当我使用减法器体系结构时,我需要计算结转,但不能。

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;

entity sub is
            port ( a  : in std_logic_vector(7 downto 0);
                   b  : in std_logic_vector(7 downto 0);
                   o  : out std_logic_vector(7 downto 0)
                );
end sub;

architecture Behavioral of sub is
  signal a2,b2 : unsigned (7 downto 0);
begin
            a2<=unsigned(a);
            b2<=unsigned(b);

            o<=std_logic_vector(a2-b2);
 end Behavioral;

编辑:我正在谈论图片中的“ c1”和“ c5”信号。

1 个答案:

答案 0 :(得分:0)

您需要将两个操作数扩展一位,以便将进位值收集到结果的MSB中。结果分解为进位位c和减法结果o

library IEEE;
use     IEEE.STD_LOGIC_1164.ALL;
use     IEEE.numeric_std.all;

entity sub is
  port (
    a  : in  std_logic_vector(7 downto 0);
    b  : in  std_logic_vector(7 downto 0);
    o  : out std_logic_vector(7 downto 0);
    c  : out std_logic
  );
end entity;

architecture rtl of sub is
begin
  (c, o) <= std_logic_vector(unsigned('0' & a) - unsigned('0' & b));
end architecture;

注意:这是VHDL-2008代码。