conversion

S

spartan

Guest
Hi all;
I use the "numeric_std". how can I conver an "unsigned 8 bit" to a "signed
9-bit".

a :unsigned(7 down to 0);
aa, b,c :signed(8 down to 0);
if i write:
aa<= to_signed(a,9);
then i got wrong result for
c<= aa-b;
when aa is negative.

thanks.
 
spartan a écrit:
Hi all;
I use the "numeric_std". how can I conver an "unsigned 8 bit" to a "signed
9-bit".

a :unsigned(7 down to 0);
aa, b,c :signed(8 down to 0);
if i write:
aa<= to_signed(a,9);
then i got wrong result for
c<= aa-b;
when aa is negative.

thanks.
Signed & unsigned types are std_logic vectors. Function to_signed only
converts from integer to signed vectors.
Your problem is easily solved this way:
aa <= signed('0' & a);
(add the sign bit and cast the result to signed)

--
____ _ __ ___
| _ \_)/ _|/ _ \ Adresse de retour invalide: retirez le -
| | | | | (_| |_| | Invalid return address: remove the -
|_| |_|_|\__|\___/
 
You are right but this work just when "a" is possitive. suppose that a is
1000-0001(-127) aa would be 0-1000-0001 while we expected it to be
1-1000-0001.
when i use arith_std_logic and conv_signed(a,9)it works, but i nees to use
numeric.
Any idea?
Thanks a lot.
 
You are right but this work just when "a" is possitive. suppose that a is
1000-0001(-127) aa would be 0-1000-0001 while we expected it to be
1-1000-0001.
when i use arith_std_logic and conv_signed(a,9)it works, but i nees to use
numeric.
Any idea?
I think you need to rethink your problem.

If a is unsigned, then it is never negative and
the following is correct:
signal a :unsigned(7 downto 0);
aa <= signed('0' & a);

However, the following is not correct when a is a large
positive number and b is a large negative number.
signal a : unsigned(7 downto 0);
signal b,c : signed(8 downto 0);

c <= signed('0' & a) - b ;

C needs to be one bit bigger. The following should work.
signal a : unsigned(7 downto 0);
signal b : signed(8 downto 0);
signal c : signed(9 downto 0);

c <= signed("00" & a) - b ;

You could also extend b, however, only one of the operands
needs to match the size of the result:
c <= signed("00" & a) - (b(8) & b) ;


On the otherhand, if a is signed (as indicated by your
concern about it being negative), then you need either of
the following two assignments:

signal a : signed(7 downto 0);
signal b : signed(8 downto 0);
signal c1, c2 : signed(9 downto 0);

c1 <= a - (b(8) & b) ;
c2 <= (a(7) & a(7) & a) - (b(8) & b) ;


Cheers,
Jim
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jim Lewis
Director of Training mailto:Jim@SynthWorks.com
SynthWorks Design Inc. http://www.SynthWorks.com
1-503-590-4787

Expert VHDL Training for Hardware Design and Verification
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
"spartan" <startix_tm@nospam.yahoo.ca> wrote in message
news:3f555b77b28cc28060d61e7f5d80c73c@localhost.talkaboutprogramming.com
....
Hi all;
I use the "numeric_std". how can I conver an "unsigned 8 bit" to a
"signed
9-bit".

a :unsigned(7 down to 0);
aa, b,c :signed(8 down to 0);
if i write:
aa<= to_signed(a,9);
then i got wrong result for
c<= aa-b;
when aa is negative.

thanks.

If "a" is unsigned then it is always non-negative. So, to convert it to
9-bit long signed, just pad a '0' on the left. The following statement
should do the job:
aa <= signed(resize(a,9));


If, on the other hand, you really want "a" to be treated as signed in
this case, then try the following:
aa <= resize(signed(a),9);

This will extend the sign bit to the left instead of padding with '0';
 
Assuming you want to interpret the 8 bit unsigned as a signed value, and sign
extend it then to 9 bits:

aa<= resize(signed(a),9);



spartan wrote:

Hi all;
I use the "numeric_std". how can I conver an "unsigned 8 bit" to a "signed
9-bit".

a :unsigned(7 down to 0);
aa, b,c :signed(8 down to 0);
if i write:
aa<= to_signed(a,9);
then i got wrong result for
c<= aa-b;
when aa is negative.

thanks.
--
--Ray Andraka, P.E.
President, the Andraka Consulting Group, Inc.
401/884-7930 Fax 401/884-7950
email ray@andraka.com
http://www.andraka.com

"They that give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety."
-Benjamin Franklin, 1759
 

Welcome to EDABoard.com

Sponsor

Back
Top