Go Back   Velocity Reviews > Newsgroups > VHDL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

VHDL - IMPLEMENTING ALU WITH OVERFLOW DETECTION ABILITY

 
Thread Tools Search this Thread
Old 01-18-2007, 11:24 PM   #1
Default IMPLEMENTING ALU WITH OVERFLOW DETECTION ABILITY


Hi all,

I am trying to implement a very simple 32-bit ALU capable of overflow
detection. Hence if the result of any computation (especially addition
and subtraction) results in a value > X'FFFFFFFF', then the system
should halt, freeze or a similar behaviour. Here is my code fragment
but it currently doesnt give expected behaviour - any suggestions
please?

LIBRARY IEEE;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_signed.ALL;
USE ieee.std_logic_arith.ALL;



-- The ALU unit

ENTITY ALU_32 IS
PORT (a,b : IN STD_LOGIC_VECTOR(31 DOWNTO 0);
opcode : IN STD_LOGIC_VECTOR(5 DOWNTO 0);
result : OUT STD_LOGIC_VECTOR(31 DOWNTO 0));

END ENTITY ALU_32;


ARCHITECTURE complex of ALU_32 is
signal undbit : bit;
signal overbit : bit;
BEGIN

-- Process is entered whenever the value of a, b or opcode changes
PROCESS(a,b,opcode)
VARIABLE alu_result : INTEGER; -- declares alu_result for result;
to be used within PROCESS only


BEGIN

CASE opcode is
--When addition
WHEN "001000" =>
alu_result:= CONV_INTEGER(a) + CONV_INTEGER(b); -- get the decimal
value of addition of a + b

--check for positive overflow
IF (alu_result > 31) THEN
overbit <= '1'; -- ALU does nothing
ELSE
overbit <= '0';
-- legal addition, it takes place
result <= CONV_STD_LOGIC_VECTOR(alu_result,32);
END IF;
-- check for negative overflow
IF (alu_result< -32) THEN
undbit <= '1'; -- ALU does nothing
ELSE
undbit <= '0';
-- legal negative addition, it takes place
result <= CONV_STD_LOGIC_VECTOR(alu_result,32);
END IF;



chibiks@googlemail.com
  Reply With Quote
Old 01-19-2007, 03:38 PM   #2
Dave Pollum
 
Posts: n/a
Default Re: IMPLEMENTING ALU WITH OVERFLOW DETECTION ABILITY
wrote:
> Hi all,
>
> I am trying to implement a very simple 32-bit ALU capable of overflow
> detection. Hence if the result of any computation (especially addition
> and subtraction) results in a value > X'FFFFFFFF', then the system
> should halt, freeze or a similar behaviour. Here is my code fragment
> but it currently doesnt give expected behaviour - any suggestions
> please?
>
> LIBRARY IEEE;
> USE ieee.std_logic_1164.ALL;
> USE ieee.std_logic_signed.ALL;
> USE ieee.std_logic_arith.ALL;
>
>
>
> -- The ALU unit
>
> ENTITY ALU_32 IS
> PORT (a,b : IN STD_LOGIC_VECTOR(31 DOWNTO 0);
> opcode : IN STD_LOGIC_VECTOR(5 DOWNTO 0);
> result : OUT STD_LOGIC_VECTOR(31 DOWNTO 0));
>
> END ENTITY ALU_32;
>
>
> ARCHITECTURE complex of ALU_32 is
> signal undbit : bit;
> signal overbit : bit;
> BEGIN
>
> -- Process is entered whenever the value of a, b or opcode changes
> PROCESS(a,b,opcode)
> VARIABLE alu_result : INTEGER; -- declares alu_result for result;
> to be used within PROCESS only
>
>
> BEGIN
>
> CASE opcode is
> --When addition
> WHEN "001000" =>
> alu_result:= CONV_INTEGER(a) + CONV_INTEGER(b); -- get the decimal
> value of addition of a + b
>
> --check for positive overflow
> IF (alu_result > 31) THEN
> overbit <= '1'; -- ALU does nothing
> ELSE
> overbit <= '0';
> -- legal addition, it takes place
> result <= CONV_STD_LOGIC_VECTOR(alu_result,32);
> END IF;
> -- check for negative overflow
> IF (alu_result< -32) THEN
> undbit <= '1'; -- ALU does nothing
> ELSE
> undbit <= '0';
> -- legal negative addition, it takes place
> result <= CONV_STD_LOGIC_VECTOR(alu_result,32);
> END IF;


I did a search within comp.lang.vhdl for "ALU" and "overflow". You may
find the following thread helpful:

http://groups.google.com/group/comp.... 4da52a605bcc1

Overflow and Carry flags are also discussed in "Max" Maxfield's book
"How Computers Do Math".
HTH
-Dave Pollum



Dave Pollum
  Reply With Quote
Old 01-22-2007, 10:48 AM   #3
Wolfgang Grafen
 
Posts: n/a
Default Re: IMPLEMENTING ALU WITH OVERFLOW DETECTION ABILITY
Hi,

please don't use the header capitalized as it means "shouting out loud"
which is generally considered as rude and annoying.

I see two potential problems with your code:
(1) integers are limited to the word length of your host processor. On a
32 bit machine you won't get results bigger or lower than 32 bits you
compare with. Even you are working at a 64 bit host it is better not
to use integers with more than 32 bit in your code (tests could be
performed at a lower specd machine).

I would use following template for declaring alu_result:

"""
LIBRARY ieee ;
USE ieee.std_logic_arith.all;


variable alu_result : signed(31 downto 0);
"""

(2) You check underflow and overflow against 31 but should be -(2**32) and
(2**32)-1

Regards

Wolfgang


wrote:
> Hi all,
>
> I am trying to implement a very simple 32-bit ALU capable of overflow
> detection. Hence if the result of any computation (especially addition
> and subtraction) results in a value > X'FFFFFFFF', then the system
> should halt, freeze or a similar behaviour. Here is my code fragment
> but it currently doesnt give expected behaviour - any suggestions
> please?
>
> LIBRARY IEEE;
> USE ieee.std_logic_1164.ALL;
> USE ieee.std_logic_signed.ALL;
> USE ieee.std_logic_arith.ALL;
>
>
>
> -- The ALU unit
>
> ENTITY ALU_32 IS
> PORT (a,b : IN STD_LOGIC_VECTOR(31 DOWNTO 0);
> opcode : IN STD_LOGIC_VECTOR(5 DOWNTO 0);
> result : OUT STD_LOGIC_VECTOR(31 DOWNTO 0));
>
> END ENTITY ALU_32;
>
>
> ARCHITECTURE complex of ALU_32 is
> signal undbit : bit;
> signal overbit : bit;
> BEGIN
>
> -- Process is entered whenever the value of a, b or opcode changes
> PROCESS(a,b,opcode)
> VARIABLE alu_result : INTEGER; -- declares alu_result for result;
> to be used within PROCESS only
>
>
> BEGIN
>
> CASE opcode is
> --When addition
> WHEN "001000" =>
> alu_result:= CONV_INTEGER(a) + CONV_INTEGER(b); -- get the decimal
> value of addition of a + b
>
> --check for positive overflow
> IF (alu_result > 31) THEN
> overbit <= '1'; -- ALU does nothing
> ELSE
> overbit <= '0';
> -- legal addition, it takes place
> result <= CONV_STD_LOGIC_VECTOR(alu_result,32);
> END IF;
> -- check for negative overflow
> IF (alu_result< -32) THEN
> undbit <= '1'; -- ALU does nothing
> ELSE
> undbit <= '0';
> -- legal negative addition, it takes place
> result <= CONV_STD_LOGIC_VECTOR(alu_result,32);
> END IF;
>



Wolfgang Grafen
  Reply With Quote
Old 01-22-2007, 11:03 AM   #4
Wolfgang Grafen
 
Posts: n/a
Default Re: IMPLEMENTING ALU WITH OVERFLOW DETECTION ABILITY
Typo, sorry
>
> (2) You check underflow and overflow against 31 but should be -(2**32) and
> (2**32)-1
>

2) You check underflow and overflow against 31 but should be -(2**31) and
> (2**31)-1



Wolfgang Grafen
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Computer Security aldrich.chappel.com.use@gmail.com A+ Certification 0 11-27-2007 02:11 AM
"Insane" "Defined" By Criminal Minds As 'Ability To Perceive Them' {HRI 20040422-V2.1} - (Version 2.1 on 24 June 2005) Leonardo Been DVD Video 1 08-28-2005 11:49 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46