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

Reply

VHDL - Queston about addition in Maxplus II

 
Thread Tools Search this Thread
Old 03-01-2004, 04:56 AM   #1
Default Queston about addition in Maxplus II


Hi,
I encounter some question about addition in Maxplus II,
Some addition result is wrong. A,B,D in either signed or
std_logic_vector are wrong. What is the problem?
Thanks

library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_signed.all;
use IEEE.std_logic_arith.all;

entity test1 is
port(
clk :in std_logic;
A :in signed(4 downto 0);
B :in signed(4 downto 0);
D ut signed(5 downto 0)
);
end test1;
architecture behave of test1 is
begin
process(clk)
begin
if falling_edge(clk) then
D<=A+B;
end if;
end process;
end behave;


lezah
  Reply With Quote
Old 03-01-2004, 02:54 PM   #2
Jonathan Bromley
 
Posts: n/a
Default Re: Queston about addition in Maxplus II
On 29 Feb 2004 20:56:33 -0800, (lezah)
wrote:

>Hi,
>I encounter some question about addition in Maxplus II,
>Some addition result is wrong. A,B,D in either signed or
>std_logic_vector are wrong. What is the problem?
>Thanks
>
>library IEEE;
>use IEEE.std_logic_1164.all;
>use IEEE.std_logic_signed.all;
>use IEEE.std_logic_arith.all;
>
>entity test1 is
>port(
> clk :in std_logic;
> A :in signed(4 downto 0);
> B :in signed(4 downto 0);
> D ut signed(5 downto 0)
> );


[...]

>D<=A+B;


[...]

A and B are both 5-bit vectors, and the addition will return a
5-bit result. This result cannot be copied to the 6-bit target D.

I guess you want to preserve the carry bit. In this case you must
do 6-bit arithmetic. This you do by sign-extending either or both
of A, B because std_logic_arith."+" returns a value as wide as its
wider operand.

The usual solution would be

D <= A + signed'(B(4) & B);

or, if you wished to be pedantic, you could sign-extend BOTH
operands:

D <= signed'(A(4) & A) + signed'(B(4) & B);

Alternatively you may wish to throw away the carry bit:

D(4 downto 0) <= A + B;

but in that case you need to decide rather carefully what
you want to happen to D(5).
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL, Verilog, SystemC, Perl, Tcl/Tk, Verification, Project Services

Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, BH24 1AW, UK
Tel: +44 (0)1425 471223 mail:
Fax: +44 (0)1425 471573 Web: http://www.doulos.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.


Jonathan Bromley
  Reply With Quote
Old 03-01-2004, 02:57 PM   #3
Ralf Hildebrandt
 
Posts: n/a
Default Re: Queston about addition in Maxplus II
lezah wrote:

> I encounter some question about addition in Maxplus II,
> Some addition result is wrong. A,B,D in either signed or
> std_logic_vector are wrong. What is the problem?
> Thanks
>
> library IEEE;
> use IEEE.std_logic_1164.all;



> use IEEE.std_logic_signed.all;
> use IEEE.std_logic_arith.all;


Don't use these 2 libraries, because the implementation of the packages
differs on different platforms.

use IEEE.Numeric_std.all;

instead.


> entity test1 is
> port(
> clk :in std_logic;
> A :in signed(4 downto 0);
> B :in signed(4 downto 0);
> D ut signed(5 downto 0)
> );


....
> D<=A+B;


The main problem is the different bit width of both summands and result.
Use the same bitdwidth for "D" too (if you don't need the carry-out) or

D<= resize(A,6) + resize(B,6);


The function "resize" is defined in IEEE.numerc_std.all. A similar
function may be provided by your libraries.
"Resize" got 2 arguments: the signal to resize and the number of bits of
the result (6 in our case).


An other way (manually expanding a signed number 1 Bit) would be

D<= (A(A'high) & A) + (B(B'high) & B);


A'high results the position of the MSB of A (in our case: 4).
A(A'high) is the MSB (in our case: A(4) )
The & Operator concatenates bits and vectors.



Ralf



Ralf Hildebrandt
  Reply With Quote
Old 03-01-2004, 03:33 PM   #4
Jonathan Bromley
 
Posts: n/a
Default Re: Queston about addition in Maxplus II
On Mon, 01 Mar 2004 15:57:36 +0100, Ralf Hildebrandt
<Ralf-> wrote:

> D <= (A(A'high) & A) + (B(B'high) & B);


I think I'd prefer A'left, Ralf

But it's still prettier than the simple solution I offered.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL, Verilog, SystemC, Perl, Tcl/Tk, Verification, Project Services

Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, BH24 1AW, UK
Tel: +44 (0)1425 471223 mail:
Fax: +44 (0)1425 471573 Web: http://www.doulos.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.


Jonathan Bromley
  Reply With Quote
Old 03-01-2004, 10:24 PM   #5
Mike Treseler
 
Posts: n/a
Default Re: Queston about addition in Maxplus II
Ralf Hildebrandt wrote:

> use IEEE.Numeric_std.all;


> The main problem is the different bit width of both summands and result.
> Use the same bitdwidth for "D" too (if you don't need the carry-out) or


> D<= resize(A,6) + resize(B,6);


Yes. I like this method.
Add some generics and you have:

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

entity test1 is -- Mike Treseler Mon Mar 1 13:27:16 2004
generic (result_len : natural := 6;
add_len : natural := 5);
port(
clk : in std_logic;
A : in signed(add_len-1 downto 0);
B : in signed(add_len-1 downto 0);
D : out signed(result_len-1 downto 0)
);
end test1;
architecture behave of test1 is

begin
clked : process(clk) is
begin
if rising_edge(clk) then
D <= resize(A, D'length) + resize(B, D'length);
end if;
end process clked;
end behave;

-- Mike Treseler


Mike Treseler
  Reply With Quote
Old 03-02-2004, 06:42 AM   #6
lezah
 
Posts: n/a
Default Re: Queston about addition in Maxplus II
Thanks all for your suggestions,
But my old version Maxplus II doesn't support ieee.numeric_std.all;
In fact, I would like to perform 3 addition with carry bits, eg:
D<=A+B+C;
Have you got any idea without useing the numeric_std library?
Thanks again


lezah
  Reply With Quote
Old 03-02-2004, 08:50 AM   #7
Jonathan Bromley
 
Posts: n/a
Default Re: Queston about addition in Maxplus II
On 1 Mar 2004 22:42:00 -0800, (lezah)
wrote:

>Thanks all for your suggestions,
>But my old version Maxplus II doesn't support ieee.numeric_std.all;
>In fact, I would like to perform 3 addition with carry bits, eg:
>D<=A+B+C;
>Have you got any idea without useing the numeric_std library?


The solution I posted doesn't require numeric_std
(manual sign-extension of at least one operand to
the required width). Ralf offered a cleaner version, which
also does not require numeric_std.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL, Verilog, SystemC, Perl, Tcl/Tk, Verification, Project Services

Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, BH24 1AW, UK
Tel: +44 (0)1425 471223 mail:
Fax: +44 (0)1425 471573 Web: http://www.doulos.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.


Jonathan Bromley
  Reply With Quote
Old 03-02-2004, 06:11 PM   #8
Mike Treseler
 
Posts: n/a
Default Re: Queston about addition in Maxplus II
lezah wrote:

> But my old version Maxplus II doesn't support ieee.numeric_std.all;


Quartus has the library.
You can get it free at:
http://www.altera.com/products/softw...arwebmain.html


> In fact, I would like to perform 3 addition with carry bits, eg:
> D<=A+B+C;


then declare:

ci : in signed(0 downto 0);

and subtract ci:

D <= resize(A, D'length) + resize(B, D'length) - ci;

carry out is D(D'left)

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




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