gpt4 book ai didi

logic - 计算 ALU 中的溢出标志

转载 作者:行者123 更新时间:2023-12-02 03:45:05 30 4
gpt4 key购买 nike

首先,如果这不是发布此问题的正确位置,请原谅我,但我不确定它应该去哪里。我目前正在使用 VHDL 在 Xilinx 中模拟 ALU。 ALU 具有以下输入和输出:

输入

  • AB:两个 8 位操作数
  • Ci:一位进位
  • Op:多路复用器的 4 位操作码

输出

  • Y:8位输出操作数
  • Co:一位进位
  • V:溢出标志(如果溢出则为1,否则为0)
  • Z:零标志(如果为零则为 1,否则为 0)
  • S:符号标志(如果 -ve 则为 1,如果 +ve 则为 0)

ALU 执行下表中详细说明的操作:

table here

我使用多路复用器和加法器实现了它,如下图所示:

here

我的问题是:

How do I calculate the value of the overflow flag, V?

我知道:

  • 如果将正数加负数,则不会发生溢出
  • 如果没有进位/借位,则可以通过计算表达式来计算溢出
(not A(7) and not B(7) and Y(7)) or (A(7) and B(7) and not Y(7))

其中 A(7)B(7)Y(7) 的第 8 位分别为 ABY

  • 在进位/借位的情况下,当且仅当最高有效位的进位和出位不同时才会发生溢出。

但是,我不知道如何在 VHDL 代码中逻辑地实现这一点 - 特别是在进位的情况下。

最佳答案

您发布的解决方案

v <= (not A(7) and not B(7) and Y(7)) or (A(7) and B(7) and not Y(7))

对于有符号操作数的加法是正确的,并且与进位无关。

编辑要将此也用于减法,您必须使用实际的加法器输入,即:

v <= (not add_A(7) and not add_B(7) and Y(7)) or (add_A(7) and add_B(7) and not Y(7))

以上方法适用于加法和减法,与进位或借位无关。 (顺便说一句,对于真正的实现,您应该使用 add_Y 而不是 Y 来缩短关键路径。)

如果你想通过对最高位和位的进位进位和出位位进行异或来实现,那么你必须先计算最低7位的部分和。这使您可以访问位 6 的进位输出,即位 7 的进位输入。然后只需附加一个全加器即可获取位 7 和进位输出。这是代码:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity adder_overflow is
port (
a : in unsigned(7 downto 0);
b : in unsigned(7 downto 0);
y : out unsigned(7 downto 0);
v : out std_logic;
co : out std_logic);
end;

architecture rtl of adder_overflow is
signal psum : unsigned(7 downto 0); -- partial sum
signal c7_in : std_logic;
signal c7_out : std_logic;
begin

-- add lowest 7 bits together
psum <= ("0" & a(6 downto 0)) + b(6 downto 0);

-- psum(7) is the carry-out of bit 6 and will be the carry-in of bit 7
c7_in <= psum(7);
y(6 downto 0) <= psum(6 downto 0);

-- add most-signifcant operand bits and carry-in from above together using a full-adder
y(7) <= a(7) xor b(7) xor c7_in;
c7_out <= ((a(7) xor b(7)) and c7_in) or a(7);

-- carry and overflow
co <= c7_out;
v <= c7_in xor c7_out;
end rtl;

关于logic - 计算 ALU 中的溢出标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34534304/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com