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

Reply

VHDL - conversion

 
Thread Tools Search this Thread
Old 05-24-2004, 08:52 AM   #1
Default conversion


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
  Reply With Quote
Old 05-24-2004, 10:46 AM   #2
Nicolas Matringe
 
Posts: n/a
Default Re: conversion
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 -
|_| |_|_|\__|\___/



Nicolas Matringe
  Reply With Quote
Old 05-25-2004, 01:46 AM   #3
spartan
 
Posts: n/a
Default Re: conversion
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.



spartan
  Reply With Quote
Old 05-25-2004, 02:19 AM   #4
Symon
 
Posts: n/a
Default Re: conversion
How about this
aa <= signed(a(7) & a);




Symon
  Reply With Quote
Old 05-25-2004, 03:44 AM   #5
Jim Lewis
 
Posts: n/a
Default Re: conversion
> 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( & 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( & b) ;
c2 <= (a(7) & a(7) & a) - (b( & b) ;


Cheers,
Jim
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~
Jim Lewis
Director of Training private.php?do=newpm&u=
SynthWorks Design Inc. http://www.SynthWorks.com
1-503-590-4787

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


Jim Lewis
  Reply With Quote
Old 05-25-2004, 06:47 AM   #6
Charles Bailey
 
Posts: n/a
Default Re: conversion

"spartan" <> wrote in message
news: lkaboutprogramming.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';




Charles Bailey
  Reply With Quote
Old 05-28-2004, 10:21 PM   #7
Ray Andraka
 
Posts: n/a
Default Re: conversion
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
http://www.andraka.com

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




Ray Andraka
  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
Error: expected constructor, destructor or type conversion before '(' token suse Software 0 03-09-2009 03:25 AM
Conversion tool:Flash2Video hely0123 Software 0 11-01-2007 03:30 AM
VOB Conversion Help Request Ryton DVD Video 1 03-19-2007 10:37 AM
VHS conversion: PC capture card, or standalone burner? Patrick Neve DVD Video 10 05-16-2006 09:34 AM
file conversion size/quality issues RichA DVD Video 0 03-02-2005 05:24 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