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

Reply

VHDL - MSB in std_logic_vector

 
Thread Tools Search this Thread
Old 11-26-2007, 11:48 PM   #1
Default MSB in std_logic_vector


Hi,

I would like to get the most significant bit from a std_logic_vector, so
that I can deduce if it's a signed or unsigned binary number. How can I do
this?



Dan
  Reply With Quote
Old 11-27-2007, 12:03 AM   #2
Eric Smith
 
Posts: n/a
Default Re: MSB in std_logic_vector
Dan wrote:
> I would like to get the most significant bit from a std_logic_vector,
> so that I can deduce if it's a signed or unsigned binary number. How
> can I do this?


If you know how the signal/type is declared, just index the appropriate
bit. For isntance, if it is:

signal foo: std_logic_vector (11 downto 0);

you could use foo (11).

In a situation where the declaration may not be visible, you can
use the 'left attribute, e.g., foo (foo'left). That might be a good
idea even when you know the index, as it makes it more clear that
you're selecting the leftmost bit, and it won't break if the width
of the signal changes.

You can also use IEEE.numeric_std, and cast the std_logic_vector
into the type "signed", and then use a numeric comparison against
0 (or other numeric literals).



Eric Smith
  Reply With Quote
Old 11-27-2007, 12:35 AM   #3
KJ
 
Posts: n/a
Default Re: MSB in std_logic_vector

"Dan" <> wrote in message
news:fifm0r$nvq$...
> Hi,
>
> I would like to get the most significant bit from a std_logic_vector, so
> that I can deduce if it's a signed or unsigned binary number. How can I do
> this?


If the vector is representing a signed number then you'll be much farther
ahead if you use the ieee.numeric_std package to work with instead.

But the straightforward way to get the MSB of a vector 'vec' without having
to hardcode a particular bit number (which is subject to change) is the
following

vec_msb <= vec(vec'left);

KJ




KJ
  Reply With Quote
Old 11-27-2007, 01:11 AM   #4
Mike Treseler
 
Posts: n/a
Default Re: MSB in std_logic_vector
Dan wrote:

> I would like to get the most significant bit from a std_logic_vector, so
> that I can deduce if it's a signed or unsigned binary number. How can I
> do this?


A std_logic_vector is just an array of bits.
If I happen to know that the value is signed, the
msb is usually the sign bit, but I certainly can't
determine the type by the value of the msb.

-- Mike Treseler


Mike Treseler
  Reply With Quote
Old 11-27-2007, 03:34 PM   #5
Andy
 
Posts: n/a
Default Re: MSB in std_logic_vector
On Nov 26, 7:11 pm, Mike Treseler <mike_trese...@comcast.net> wrote:
> Dan wrote:
> > I would like to get the most significant bit from a std_logic_vector, so
> > that I can deduce if it's a signed or unsigned binary number. How can I
> > do this?

>
> A std_logic_vector is just an array of bits.
> If I happen to know that the value is signed, the
> msb is usually the sign bit, but I certainly can't
> determine the type by the value of the msb.
>
> -- Mike Treseler


Dan,

What Mike is trying to say, is that your original question states you
want to determine whether a value is signed or unsigned. Those are
representations (types), not values. If you meant "negative or
positive", and you know the representation is signed (obviously,
otherwise there would be no negative values to consider), then, yes
the MSB will tell you that. Otherwise, the MSB value tells you nothing
about representation.

This is the reasoning behind the numeric_std package and its
definitions of types signed and unsigned: because it is impossible for
VHDL to know the numeric representation of an SLV. Defining the data
as signed or unsigned (by putting it in the appropriate type of signal
or variable) allows VHDL to automatically select the appropriate
operator version to ensure that the results are arithmetically
correct.

Andy


Andy
  Reply With Quote
Old 11-30-2007, 12:32 AM   #6
Dan
 
Posts: n/a
Default Re: MSB in std_logic_vector
Thanks all

Dan,


"Andy" <> wrote in message
news:83f993af-ac2d-47de-928d-...
> On Nov 26, 7:11 pm, Mike Treseler <mike_trese...@comcast.net> wrote:
>> Dan wrote:
>> > I would like to get the most significant bit from a std_logic_vector,
>> > so
>> > that I can deduce if it's a signed or unsigned binary number. How can I
>> > do this?

>>
>> A std_logic_vector is just an array of bits.
>> If I happen to know that the value is signed, the
>> msb is usually the sign bit, but I certainly can't
>> determine the type by the value of the msb.
>>
>> -- Mike Treseler

>
> Dan,
>
> What Mike is trying to say, is that your original question states you
> want to determine whether a value is signed or unsigned. Those are
> representations (types), not values. If you meant "negative or
> positive", and you know the representation is signed (obviously,
> otherwise there would be no negative values to consider), then, yes
> the MSB will tell you that. Otherwise, the MSB value tells you nothing
> about representation.
>
> This is the reasoning behind the numeric_std package and its
> definitions of types signed and unsigned: because it is impossible for
> VHDL to know the numeric representation of an SLV. Defining the data
> as signed or unsigned (by putting it in the appropriate type of signal
> or variable) allows VHDL to automatically select the appropriate
> operator version to ensure that the results are arithmetically
> correct.
>
> Andy




Dan
  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