Convert enumeration to std_logic_vector

Discussion in 'VHDL' started by Stef, May 16, 2008.

  1. Stef

    Stef Guest

    I have a problem converting an enum with known encoding to a
    std_logic_vector. Here's some fragments of the code:

    entity controller is
    port (
    ...
    ledsel : out std_logic_vector(1 downto 0);
    ...
    );


    ctrl_proc : process(clock)
    type opmode_t is (OPMODE_IDENT, OPMODE_CONFIG, OPMODE_ACQ);
    attribute enum_encoding : string;
    attribute enum_encoding of opmode_t : type is "00 01 10";
    variable opmode_v : opmode_t;


    I now want to assign opmode_v to the ledsel output and thought to do it
    like this:

    ledsel <= std_logic_vector(opmode_v);

    That didn't work: " Illegal type conversion from opmode_t to
    ieee.std_logic_1164.std_logic_vector (non-numeric to array)."

    Is there another way to do it? Didn't find anythmh yet wityh google.


    --
    Stef (remove caps, dashes and .invalid from e-mail address to reply by mail)
    Stef, May 16, 2008
    #1
    1. Advertising

  2. Stef

    Stef Guest

    In comp.lang.vhdl,
    Stef <> wrote:
    > I have a problem converting an enum with known encoding to a
    > std_logic_vector. Here's some fragments of the code:
    >
    > entity controller is
    > port (
    > ...
    > ledsel : out std_logic_vector(1 downto 0);
    > ...
    > );
    >
    >
    > ctrl_proc : process(clock)
    > type opmode_t is (OPMODE_IDENT, OPMODE_CONFIG, OPMODE_ACQ);
    > attribute enum_encoding : string;
    > attribute enum_encoding of opmode_t : type is "00 01 10";
    > variable opmode_v : opmode_t;
    >
    >
    > I now want to assign opmode_v to the ledsel output and thought to do it
    > like this:
    >
    > ledsel <= std_logic_vector(opmode_v);
    >
    > That didn't work: " Illegal type conversion from opmode_t to
    > ieee.std_logic_1164.std_logic_vector (non-numeric to array)."


    Just after hitting the send button, I had a thought. I don't even
    need the encoding if I do it like this:

    ledsel <= std_logic_vector(to_unsigned(opmode_t'pos(opmode_v), ledsel'length));

    Problem solved for now, but I would still like to know how to do it with
    use of the encoding as this solution only works for straight forward
    encoding.

    --
    Stef (remove caps, dashes and .invalid from e-mail address to reply by mail)
    Stef, May 16, 2008
    #2
    1. Advertising

  3. Stef

    KJ Guest

    On May 16, 7:09 am, Stef <>
    wrote:
    > In comp.lang.vhdl,
    >
    >
    >
    >
    >
    > Stef <> wrote:
    > > I have a problem converting an enum with known encoding to a
    > > std_logic_vector. Here's some fragments of the code:

    >
    > >   entity controller is
    > >     port (
    > >       ...
    > >       ledsel : out std_logic_vector(1 downto 0);
    > >       ...
    > >     );

    >
    > >   ctrl_proc : process(clock)
    > >     type opmode_t is (OPMODE_IDENT, OPMODE_CONFIG, OPMODE_ACQ);
    > >     attribute enum_encoding : string;
    > >     attribute enum_encoding of opmode_t : type is "00 01 10";
    > >     variable opmode_v : opmode_t;

    >
    > > I now want to assign opmode_v to the ledsel output and thought to do it
    > > like this:

    >
    > >       ledsel <= std_logic_vector(opmode_v);

    >
    > > That didn't work: " Illegal type conversion from opmode_t to
    > > ieee.std_logic_1164.std_logic_vector (non-numeric to array)."

    >
    > Just after hitting the send button, I had a thought. I don't even
    > need the encoding if I do it like this:
    >
    >    ledsel <= std_logic_vector(to_unsigned(opmode_t'pos(opmode_v), ledsel'length));
    >
    > Problem solved for now, but I would still like to know how to do it with
    > use of the encoding as this solution only works for straight forward
    > encoding.
    >
    > --
    > Stef    (remove caps, dashes and .invalid from e-mail address to reply by mail)- Hide quoted text -
    >
    > - Show quoted text -


    Enumerated types do not have any encoding. Say that 100 times. Now
    ask yourself, since enumerated types do not have any encoding then how
    would can you convert from an enumerated type to a std_logic_vector?
    (Hint: The answer is you have to write your own conversion function).

    The 'enum_encoding' attribute may (or may not) get used by a synthesis
    tool to encode your enumerated type but that will not affect the
    language syntax in any way.

    There is nothing built into the language or any of the standard
    packages that will convert an enumerated type that you create into a
    std_logic_vector. (If at this point you feel yourself ready to say
    "But I told it what the encoding was with the enum_encoding
    attribute..." then go back to the first sentence of this post and
    start over.

    So now go about writing your conversion function (Hint: A good name
    to use might be 'to_sdd_logic_vector'...conversions the other way
    might well be called 'from_std_logic_vector). For your particular
    case, this can be the one liner that you posted with the 'pos
    attribute, but it doesn't have to be.

    Kevin Jennings
    KJ, May 16, 2008
    #3
  4. Stef

    Stef Guest

    In comp.lang.vhdl,
    KJ <> wrote:
    > On May 16, 7:09 am, Stef <>
    > wrote:
    >> In comp.lang.vhdl,
    >> Stef <> wrote:


    >> >     type opmode_t is (OPMODE_IDENT, OPMODE_CONFIG, OPMODE_ACQ);
    >> >     attribute enum_encoding : string;
    >> >     attribute enum_encoding of opmode_t : type is "00 01 10";
    >> >     variable opmode_v : opmode_t;

    >>
    >> > I now want to assign opmode_v to the ledsel output and thought to do it
    >> > like this:

    >>
    >> >       ledsel <= std_logic_vector(opmode_v);

    >>
    >> > That didn't work: " Illegal type conversion from opmode_t to
    >> > ieee.std_logic_1164.std_logic_vector (non-numeric to array)."

    >>
    >> Just after hitting the send button, I had a thought. I don't even
    >> need the encoding if I do it like this:
    >>
    >>    ledsel <= std_logic_vector(to_unsigned(opmode_t'pos(opmode_v), ledsel'length));
    >>
    >> Problem solved for now, but I would still like to know how to do it with
    >> use of the encoding as this solution only works for straight forward
    >> encoding.
    >>

    >
    > Enumerated types do not have any encoding. Say that 100 times. Now


    My understanding has always been that there will be some sort of
    encoding and that that encoding can be 'forced' by the encoding
    attribute. So that is wrong from when talking about the language
    alone? Ok, then I say that 0x4 times.


    > ask yourself, since enumerated types do not have any encoding then how
    > would can you convert from an enumerated type to a std_logic_vector?
    > (Hint: The answer is you have to write your own conversion function).


    That is clear as soon as you know there's no encoding.

    > The 'enum_encoding' attribute may (or may not) get used by a synthesis
    > tool to encode your enumerated type but that will not affect the
    > language syntax in any way.


    Are you aware of any tools that do not use the enum_encoding
    attribute? I once wrote a state machine and encoded the states so
    that each transition would only be a single-bit change. I would
    gave been upset if the tool just ignored my carefull state layout,
    but fortunately it didn't

    > There is nothing built into the language or any of the standard
    > packages that will convert an enumerated type that you create into a
    > std_logic_vector. (If at this point you feel yourself ready to say
    > "But I told it what the encoding was with the enum_encoding
    > attribute..." then go back to the first sentence of this post and
    > start over.


    But..., okay, I'll try to surpress it. But there would still be
    dissapointment if the synthesis tool had just ignored my suggested
    encoding.

    > So now go about writing your conversion function (Hint: A good name
    > to use might be 'to_sdd_logic_vector'...conversions the other way
    > might well be called 'from_std_logic_vector). For your particular
    > case, this can be the one liner that you posted with the 'pos
    > attribute, but it doesn't have to be.


    Ok, I'll keep it in mind for the next time I need a less than straight
    forward conversion.

    Thanks for clearing the matter up.

    --
    Stef (remove caps, dashes and .invalid from e-mail address to reply by mail)
    Stef, May 16, 2008
    #4
  5. Stef

    Stef Guest

    In comp.lang.vhdl,
    Brian Drummond <> wrote:

    > -- You can separate the encoding from the enumeration
    > type encoding_array is array(opmode_t) of std_logic_vector(1 downto 0);
    > -- constant encode : encoding_array := ("00", "01", "10");
    > -- or for easier maintenance
    > constant encode : encoding_array :=
    > ( OPMODE_IDENT => "00",
    > OPMODE_CONFIG => "01",
    > OPMODE_ACQ => "10");
    >


    > ledsel <= encode(opmode_v);


    Nice solution, thanks. And usable to implement that conversion function
    as well. :)

    --
    Stef (remove caps, dashes and .invalid from e-mail address to reply by mail)
    Stef, May 16, 2008
    #5
  6. Stef

    KJ Guest

    "Stef" <> wrote in message
    news:e37a4$482d7ee4$54f63171$-service.com...
    > In comp.lang.vhdl,
    > KJ <> wrote:
    >> On May 16, 7:09 am, Stef <>
    >> wrote:
    >>> In comp.lang.vhdl,
    >>> Stef <> wrote:

    >
    >
    >> The 'enum_encoding' attribute may (or may not) get used by a synthesis
    >> tool to encode your enumerated type but that will not affect the
    >> language syntax in any way.

    >
    > Are you aware of any tools that do not use the enum_encoding
    > attribute? I once wrote a state machine and encoded the states so
    > that each transition would only be a single-bit change. I would
    > gave been upset if the tool just ignored my carefull state layout,
    > but fortunately it didn't
    >


    I'm not aware of any but that's also because I don't use enum_encoding
    because there are no guarantees that
    - It will work
    - It is portable

    >> There is nothing built into the language or any of the standard
    >> packages that will convert an enumerated type that you create into a
    >> std_logic_vector. (If at this point you feel yourself ready to say
    >> "But I told it what the encoding was with the enum_encoding
    >> attribute..." then go back to the first sentence of this post and
    >> start over.

    >
    > But..., okay, I'll try to surpress it. But there would still be
    > dissapointment if the synthesis tool had just ignored my suggested
    > encoding.
    >


    But now that you know the proper way to avoid possible future
    disappointments with that approach.

    KJ
    KJ, May 17, 2008
    #6
  7. Stef

    Thomas Rouam Guest

    Hi Guys,

    I wanted to give a little comment on Kevin's statement :
    > I'm not aware of any but that's also because I don't use enum_encoding
    > because there are no guarantees that
    > - It will work
    > - It is portable


    A synthesis tool that does not handle the enum_encoding attribute, is
    then not compliant with the IEEE Standard for VHDL Register Tranfer
    Level (RTL) Synthesis (IEEE Std 1076.6). It is said in there :

    An attribute named ENUM_ENCODING shall provide a means of encoding
    enumeration type values. The attribute specification for this
    attribute shall specify the encoding of the enumeration type literals
    in the form of a string. This string shall be made up of tokens
    separated by one or more spaces. There shall be as many tokens as
    there are literals in the enumeration type, with the first token
    corresponding to the first enumeration literal, the second token
    corresponding to the second enumeration literal, and so on.

    The word shall here is equivalent to required to.

    Hope this helps as well.

    Thomas.
    Thomas Rouam, May 18, 2008
    #7
  8. Stef

    rickman Guest

    On May 18, 4:09 am, Thomas Rouam <> wrote:
    > Hi Guys,
    >
    > I wanted to give a little comment on Kevin's statement :
    >
    > > I'm not aware of any but that's also because I don't use enum_encoding
    > > because there are no guarantees that
    > > - It will work
    > > - It is portable

    >
    > A synthesis tool that does not handle the enum_encoding attribute, is
    > then not compliant with the IEEE Standard for VHDL Register Tranfer
    > Level (RTL) Synthesis (IEEE Std 1076.6). It is said in there :
    >
    > An attribute named ENUM_ENCODING shall provide a means of encoding
    > enumeration type values. The attribute specification for this
    > attribute shall specify the encoding of the enumeration type literals
    > in the form of a string. This string shall be made up of tokens
    > separated by one or more spaces. There shall be as many tokens as
    > there are literals in the enumeration type, with the first token
    > corresponding to the first enumeration literal, the second token
    > corresponding to the second enumeration literal, and so on.
    >
    > The word shall here is equivalent to required to.
    >
    > Hope this helps as well.
    >
    > Thomas.


    Maybe I am missing something, but I don't see where your information
    contradicts Kevin's statements. I don't see where the nature of the
    "token" is specified. That means that the tokens are not standardized
    (Kevin's second point) and therefor may not work (Kevin's second
    point).

    Unless the IEEE standard specifies the *method* of describing the
    enumeration encoding it has not done a lot of good regardless of
    whether it is "required" or not.

    Rick
    rickman, May 18, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Kindt
    Replies:
    5
    Views:
    2,014
    Allan Herriman
    Sep 12, 2003
  2. Olaf Petzold
    Replies:
    4
    Views:
    5,381
    Mike Treseler
    Nov 28, 2005
  3. Thomas Rouam
    Replies:
    6
    Views:
    1,113
  4. puvit82
    Replies:
    4
    Views:
    743
    puvit82
    Feb 1, 2008
  5. Jan Burse
    Replies:
    11
    Views:
    1,533
    Daniele Futtorovic
    Dec 1, 2012
Loading...

Share This Page