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

Reply

VHDL - Get the carry with add operator

 
Thread Tools Search this Thread
Old 03-20-2006, 11:16 AM   #1
Default Get the carry with add operator



Hello,

I would like to add two 4 bits number, and get the result and the carry. Is
there any way too do that with the '+' operator ? Otherwise, what is the
best way to solve this problem ?

Thank you I search a lot without any success.

Damien_


Damien Bardon
  Reply With Quote
Old 03-20-2006, 02:12 PM   #2
Mike Treseler
 
Posts: n/a
Default Re: Get the carry with add operator
Damien Bardon wrote:

> I would like to add two 4 bits number, and get the result and the carry. Is
> there any way too do that with the '+' operator ?


http://groups.google.com/groups/sear...unsigned_carry


Mike Treseler
  Reply With Quote
Old 03-20-2006, 07:25 PM   #3
Eric Smith
 
Posts: n/a
Default Re: Get the carry with add operator
Damien Bardon wrote:
> I would like to add two 4 bits number, and get the result and the carry. Is
> there any way too do that with the '+' operator ? Otherwise, what is the
> best way to solve this problem ?


If you concatenate a "0" on the front of each number, then add them,
the low four bits of the result are your sum, and the high bit is
the carry out.

Example below; I haven't actually compiled this exact code, but it's
basically a simplified version of something I actually use.

If you also want a signed arithmetic overflow output (which is NOT the
same thing as carry out), you can get that by using a two bit extension
rather than a single bit.

Lately I've been having fun with BCD/binary adder/subtractors, using
carry select and carry lookahead. With careful desgin (but FPGA vendor
neutral and not floorplanned), I was able to get a 56 bit version with
minimum cycle time of just over 10 ns.

Eric


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity binary_adder_with_carry_out is
generic (width: integer := 4);
port (operand_a: in std_logic_vector (width - 1 downto 0);
operand_b: in std_logic_vector (width - 1 downto 0);
carry_in: in std_logic;
result: out std_logic_vector (width - 1 downto 0);
carry_out: out std_logic);
end binary_adder_with_carry_out;

architecture behavioral of binary_adder_with_carry_out is
signal a_ext: unsigned (width downto 0);
signal b_ext: unsigned (width downto 0);
signal c_ext: unsigned (0 downto 0);
signal temp: unsigned (width downto 0);
begin
a_ext <= unsigned ("0" & operand_a);
b_ext <= unsigned ("0" & operand_b);
c_ext (0) <= carry_in;

temp <= a_ext + b_ext + c_ext;

result <= temp (width - 1 downto 0);
carry_out <= temp (width);
end behavioral;


Eric Smith
  Reply With Quote
Old 03-20-2006, 09:55 PM   #4
Andy
 
Posts: n/a
Default Re: Get the carry with add operator
Using integer types, and subtypes thereof, you can do this:

signal a,b,sum : natural range 0 to 2**N-1; -- N-bit unsigned values
signal carry_out : natural range 0 to 1;
....
sum <= (a + b) mod 2**N;
carry_out <= ((a + b) / 2**N) mod 2;

Operations on all integer types/subtypes are 32 bits (31 bits unsigned)
wide, only the assignments are restricted to the range of the subtype,
which determines data widths of registers. The synthesis tool will
optimize out any unused bits.

Note that the / and mod operators are just shift/truncate in hardware,
and both will execute MUCH faster in simulation than vector types (SLV,
unsigned, etc.)

I only wish vhdl supported 64 and/or 128 bit integer types.

Andy


Damien Bardon wrote:
> Hello,
>
> I would like to add two 4 bits number, and get the result and the carry. Is
> there any way too do that with the '+' operator ? Otherwise, what is the
> best way to solve this problem ?
>
> Thank you I search a lot without any success.
>
> Damien_




Andy
  Reply With Quote
Old 03-21-2006, 08:15 AM   #5
Damien Bardon
 
Posts: n/a
Default Re: Get the carry with add operator

Thank you for your answers, I like Eric's solution, as Andy's proposal is
too complicated for me yet I hope I will understand such a solution in
a few time !

Actually I had already try something similar to Eric's solution but although
it was working, I was wondering if it is a good practice, as the
synthesizer generate a 5 bits adder instead of a 4 bits adder with carry
out.

Damien_


Damien Bardon wrote:

>
> Hello,
>
> I would like to add two 4 bits number, and get the result and the carry.
> Is there any way too do that with the '+' operator ? Otherwise, what is
> the best way to solve this problem ?
>
> Thank you I search a lot without any success.
>
> Damien_




Damien Bardon
  Reply With Quote
Old 03-21-2006, 01:27 PM   #6
Mike Treseler
 
Posts: n/a
Default Re: Get the carry with add operator
Damien Bardon wrote:

> Actually I had already try something similar to Eric's solution but although
> it was working, I was wondering if it is a good practice, as the
> synthesizer generate a 5 bits adder instead of a 4 bits adder with carry
> out.


Use that fifth bit as your carry out.
That's what the resize is for in my example.

Better yet, just use the full width
that you really need and don't describe
any carry.

-- Mike Treseler


Mike Treseler
  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
Re: Carry on dvds ^^artnada^^ DVD Video 8 04-05-2008 07:36 PM
DVD's For Sale Galloping DVD Video 3 09-06-2004 05:11 AM
For Sale Galloping DVD Video 0 08-28-2004 10:08 AM
DVDs for sale Galloping DVD Video 0 08-23-2004 08:17 PM
DVD's For Sale Galloping DVD Video 0 08-23-2004 07:57 PM




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