conversion

S

spartan

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

Nicolas Matringe

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

spartan

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

Jim Lewis

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:[email protected]
SynthWorks Design Inc. http://www.SynthWorks.com
1-503-590-4787

Expert VHDL Training for Hardware Design and Verification
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
C

Charles Bailey

....
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';
 
R

Ray Andraka

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


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 (e-mail address removed)
http://www.andraka.com

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top