溢出位 32Bit ALU VHDL

发布于 2024-08-11 03:50:06 字数 111 浏览 5 评论 0原文

我目前正在用 VHDL 编写 32 位 ALU(Add/Sub)。我遇到了溢出位的问题。 我看不到何时根据运算(加法、减法)和输入值设置溢出。

你能帮助我吗 ?

此致, 安德烈

I'm currently writing a 32Bit ALU (Add/Sub) in VHDL. I've got a problem with the overflow bit.
I can't see when to set the overflow depending on the operation (addition, subtraction) and the input values.

Can you help me ?

best regards,
Andre

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半边脸i 2024-08-18 03:50:06

只是为了向 Martin 的答案添加一些额外的信息,您需要小心检测 2 的补码算术的溢出。

例如,如果您有以二进制补码格式表示的 3 位有符号值,并且想要检测溢出,则需要将符号扩展到额外位,然后查看额外位是否与最高有效位不同:

例如,如果要计算a = b + c:

--Declare the signals
signal overflow : std_logic;
signal a : signed(2 downto 0);
signal b : signed(2 downto 0);
signal c : signed(2 downto 0);

-- Declare some additional signals with one more MSB than the original signals
signal a_extended : std_logic(3 downto 0);
signal b_extended : std_logic(3 downto 0);
signal c_extended : std_logic(3 downto 0);

-- Sign extend the MSB
b_extended <= b(2) & b;
c_extended <= c(2) & c;

-- Perform the addition
a_extended <= b_extended + c_extended;

-- Detect the overflow case
overflow <= '1' when a_extended(3) /= a_extended(2) else '0';

-- Calculate the answer
-- Limit to 100 (-4) or 011 (+3) in the case of overflow
process(a_extended)
begin
   if a_extended(3) /= a_extended(2) then
      if a_extended(3) = '1' then
         a <= "100";
      else
         a <= "011";
      end if;
   else
      a <= a_extended(2 downto 0);
   end if;
end process;

Just to add a bit of extra information to Martin's answer, you need to be careful about detecting the overflow with 2's complement arithmetic.

For example, if you have 3-bit signed values represented in two's complement format and you want to detect an overflow, you need to sign extend into the extra bit and then look to see if the extra bit is different from the most significant wanted bit:

For example, if you want to calculate a = b + c:

--Declare the signals
signal overflow : std_logic;
signal a : signed(2 downto 0);
signal b : signed(2 downto 0);
signal c : signed(2 downto 0);

-- Declare some additional signals with one more MSB than the original signals
signal a_extended : std_logic(3 downto 0);
signal b_extended : std_logic(3 downto 0);
signal c_extended : std_logic(3 downto 0);

-- Sign extend the MSB
b_extended <= b(2) & b;
c_extended <= c(2) & c;

-- Perform the addition
a_extended <= b_extended + c_extended;

-- Detect the overflow case
overflow <= '1' when a_extended(3) /= a_extended(2) else '0';

-- Calculate the answer
-- Limit to 100 (-4) or 011 (+3) in the case of overflow
process(a_extended)
begin
   if a_extended(3) /= a_extended(2) then
      if a_extended(3) = '1' then
         a <= "100";
      else
         a <= "011";
      end if;
   else
      a <= a_extended(2 downto 0);
   end if;
end process;
巴黎夜雨 2024-08-18 03:50:06

规范说它应该做什么?应该描述在什么条件下应该设置溢出标志。

传统上,当输出对于存储来说太大时,就会设置溢出位。您可以将其视为对两个 32 位数字求和的答案中的第 33 位。在有符号算术中,如果运算结果的大小太大(无论符号如何),就会发生这种情况。对于 2-s 补码算术,您必须小心一点,因为最大负数比您可以在给定位数中表示的最大正数稍微负一些。

就实际操作而言,只需创建一个比输入宽 1 位的 numeric_std 向量,执行

a<=b+c;

并让合成器创建逻辑。那么您就不必担心细节。

可以去掉“a”的MSB(使用a(a'high)并将其用作溢出。

What does the spec say it should do? There should be a description of under what conditions the overflow flag should be set.

Conventionally, the overflow bit is set when the output is too big for the storage. You could think of it as a 33rd bit in the answer of summing two 32 bit numbers. In signed arithmetic, this can happen if the magnitude of the result of the operation is too big, irrespective of sign. With 2-s complement arithmetic, you have to be a bit careful as the biggest negative number is slightly more -negative than the biggest positive number that you can represent in a given number of bits.

In terms of actually doing it, just create a numeric_std vector that is 1 bit wider than the inputs, do

a<=b+c;

and let the synthesizer create the logic. You then don't have to worry about the details.

The MSB of "a" can be taken off (using a(a'high) and use it as the overflow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文