首页 > 解决方案 > 使用无符号向量减法时如何检查结转?

问题描述

我尝试实现一个只有 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”信号。

标签: vhdl

解决方案


您需要将两个操作数都扩展一位,因此进位被收集在结果的 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 代码。


推荐阅读