0

what's the best way to return an unconstrained vector in vhdl?

function func(selector : natural) return std_logic_vector is
begin
    case selector is
        when 3 => return std_logic_vector("11");
        when 4 => return std_logic_vector("101");
        when others => return std_logic_vector("0");
    end case;
end function;

In this case i get string literal cannot be a type conversion operand, so it doesn't work. The signal selector is generic, so it don't have to be synthesizeable.

4
  • 4
    You have syntax errors after numeric literals 3 and 4. Do you mean to use a qualified expression which explicitly specifies a type? Qualified expressions: std_logic_vector'("11"), std_logic_vector'("101"); and std_logic_vector'("0"). The error message you haven't shown would tell you that you can't type convert a string to std_logic_vector, their element types aren't the same (character and std_ulogic) . Those ':'s should be "=>". Commented Oct 10, 2017 at 13:14
  • The : instead of <= was a mistake in here, i typed this part new for stackexchange, without unnecessary code. In the original code there're also "<=". However, it works with the ' you mentioned before the brackets.Thanks a lot. Can you give me a hint about how's this ' function called in this case? Copy this in an answer, i'll mark as accepted! Commented Oct 10, 2017 at 13:40
  • Then edit your question... Commented Oct 10, 2017 at 17:23
  • You still don't have a viable code snippet in your question. You should be using right arrow => for associating a choice with the following return statement. when 3 => return std_logic_vector'("11");, etc. Please supply a Minimal, Complete and Verifiable example. Include a complete error message. Your question is not useful as it stands. Note that the return value type is std_logic_vector and can provide the type of the string literals without the qualified expressions as Matthew Taylor commented to his answer. Commented Oct 10, 2017 at 19:49

1 Answer 1

1

You can't do this. The return value from your function will need to be associated with something when it is called and that something will have to be of a fixed width. In other words, you'll have to say thing like:

s <= func(n);

and s will have a fixed width, so all the return values from your function will have to have the same width.

Sign up to request clarification or add additional context in comments.

4 Comments

My function return value is used in a generic map with an unconstrained array type. Like generic map(func(n)). The width of the generic is evaluated in lower hierarchy levels.
@FranzForstmayr In which case, I don't understand what you're asking. You can just say this when 3 => return "11";.
Ok, i didn't knew that this is possible. I saw, i just thought much too complicated!
"The return value from your function will need to be associated with something when it is called and that something will have to be of a fixed width." No, the return value must be evaluated, as it would be in the OP's intended use as an initial value for a (generic) constant where the subtype indication does not supply a constraint. His MCVe could also provide usage for func.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.