function declaration help

Discussion in 'VHDL' started by Greg Dunn, Jul 24, 2003.

  1. Greg Dunn

    Greg Dunn Guest

    I'm trying to create a function that will convert state_value types (see
    below) to std_logic_vectors so i can monitor state transitions, but I keep
    getting the following error:

    ERROR ... LINE 35 The type of the operand of type conversion must be
    determinable independent of the context

    Here's a stripped version of my broken code:
    ----------------------------------------------------------------------------
    -------------
    architecture state_machine of logic_unit is
    type state_value is (S0_WAIT_FOR_ACQ, S1_INIT, S2_WAIT_VE,
    S3_RD_RAM, S4_WAIT, S5_WR_RAM, S6_CLR_VE);

    function stateToVec (st : state_value)
    return std_logic_vector(3 downto 0) is -- LINE 35
    begin
    return "0000";
    end stateToVec;
    begin
    ....
    ----------------------------------------------------------------------------
    -------------

    If I change the return type to std_logic, it works just fine, but isn't of
    much use to me. Can anyone tell me what I'm doing wrong or a better way to
    do this?

    Thanks,
    Greg
    Greg Dunn, Jul 24, 2003
    #1
    1. Advertising

  2. Greg Dunn

    MK Guest

    You need declare subtype first:

    subtype std_logic_vector4 is std_logic_vector(3 downto 0);
    function stateToVec (st : state_value) return std_logic_vector4 is
    begin
    return "0000";
    end stateToVec;

    or return simply std_logic_vector without constraint:

    function stateToVec (st : state_value) return std_logic_vector is
    begin
    return "0000";
    end stateToVec;


    regards,
    MK.

    "Greg Dunn" <> wrote in message
    news:MWETa.644$...
    > I'm trying to create a function that will convert state_value types (see
    > below) to std_logic_vectors so i can monitor state transitions, but I keep
    > getting the following error:
    >
    > ERROR ... LINE 35 The type of the operand of type conversion must be
    > determinable independent of the context
    >
    > Here's a stripped version of my broken code:
    > --------------------------------------------------------------------------

    --
    > -------------
    > architecture state_machine of logic_unit is
    > type state_value is (S0_WAIT_FOR_ACQ, S1_INIT, S2_WAIT_VE,
    > S3_RD_RAM, S4_WAIT, S5_WR_RAM,

    S6_CLR_VE);
    >
    > function stateToVec (st : state_value)
    > return std_logic_vector(3 downto 0) is -- LINE 35
    > begin
    > return "0000";
    > end stateToVec;
    > begin
    > ...
    > --------------------------------------------------------------------------

    --
    > -------------
    >
    > If I change the return type to std_logic, it works just fine, but isn't of
    > much use to me. Can anyone tell me what I'm doing wrong or a better way

    to
    > do this?
    >
    > Thanks,
    > Greg
    >
    >
    MK, Jul 24, 2003
    #2
    1. Advertising

  3. If the sub-type suggestion doesn't help, try a variable.

    ....
    function stateToVec (st : state_value) return std_logic_vector(3 downto 0) is
    variable result : std_logic_vector(3 downto 0);
    begin
    case st is
    when others =>
    result := X"0"; -- requires 1993, or use result := "0000";
    end case;
    return result;
    ....

    "Greg Dunn" <> wrote in message news:<MWETa.644$>...
    > I'm trying to create a function that will convert state_value types (see
    > below) to std_logic_vectors so i can monitor state transitions, but I keep
    > getting the following error:
    >
    > ERROR ... LINE 35 The type of the operand of type conversion must be
    > determinable independent of the context
    >
    > Here's a stripped version of my broken code:
    > ----------------------------------------------------------------------------
    > -------------
    > architecture state_machine of logic_unit is
    > type state_value is (S0_WAIT_FOR_ACQ, S1_INIT, S2_WAIT_VE,
    > S3_RD_RAM, S4_WAIT, S5_WR_RAM, S6_CLR_VE);
    >
    > function stateToVec (st : state_value)
    > return std_logic_vector(3 downto 0) is -- LINE 35
    > begin
    > return "0000";
    > end stateToVec;
    > begin
    > ...
    > ----------------------------------------------------------------------------
    > -------------
    >
    > If I change the return type to std_logic, it works just fine, but isn't of
    > much use to me. Can anyone tell me what I'm doing wrong or a better way to
    > do this?
    >
    > Thanks,
    > Greg
    William Wallace, Aug 17, 2003
    #3
    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. Noah
    Replies:
    5
    Views:
    947
  2. Ovidesvideo
    Replies:
    4
    Views:
    482
    Andrey Tarasevich
    Dec 10, 2004
  3. Replies:
    4
    Views:
    1,051
    Richard Tobin
    Dec 12, 2006
  4. Bolin
    Replies:
    4
    Views:
    401
  5. Luca Forlizzi
    Replies:
    4
    Views:
    447
    Luca Forlizzi
    Nov 14, 2010
Loading...

Share This Page