English 中文(简体)
Overflow bit 32Bit ALU VHDL
原标题:

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

问题回答

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;

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.





相关问题
override overflow:hidden with z-index

Am using coda_bubble jquery plugin, and i need to make my bubble pop out within an overflow hidden div. here is my sample code. <html> <head> <title>Override overflow:hidden</...

Negative Absolute Positioning, Removing Scrollbar

I am working on this site: http://waterwing.waterwing.ca/ The top right corner animation is perfect except for that it creates a horizontal scrollbar with the way it s positioned. It s absolutely ...

textarea scrollbars overflow and hide div border

I ve tried this on many browsers ( via browsershots ) and universally the scrollbars overflow and hide the green border. I don t want overflow: hidden because that clips the scrollbars. How do I get ...

CSS Parent DIV Overflow

I have a problem with a website I m putting together. I have a simple div layout. Which is as follows: <body> <div id="Container"> <div id="Logo"></div> &...

Horizontal menu with overflow-x:auto

Is it possible to have a horizontal menu that can scroll overflowed menuitems horizontally, but also display the vertical overflow when a submenu is to appear?

Overflow bit 32Bit ALU VHDL

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 ...

CSS Overflow with div

I want to use overflow on a div to show all div and image, and text to but for this example i used only images. i need a horizontal scroll, if i only use image its work well with the white-space: ...

热门标签