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

  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.
     
    MK, Jul 24, 2003
    #2
    1. Advertisements

  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;
    ....
     
    William Wallace, Aug 17, 2003
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.