Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

possible to reference attributes of function return type?

Tags:

vhdl

I'm wondering if it's possible to somehow reference attributes of the return type/value from inside a function returning an unconstrained type. (Do the constraints even propagate up into the function at all? Something tells me they don't, and that the return type's constraints are determined by whatever is actually returned inside the function) For example:

function f return std_logic_vector is begin
    -- how do we access attributes of what we are returning?
    -- "return_type" is a placeholder for it here, it would be nice if
    -- this information propagated up into the function based on the way
    -- the function's result is used when invoked
    return (return_type'high - 2 downto return_type'low + 1 => '0', others => '1');
end function;
signal x: std_logic_vector(7 downto 0);
signal y: std_logic_vector(11 downto 0);
[...]
x <= f; -- expect x = "11000001"
y <= f; -- expect y = "110000000001"

I know this could be hacked around by passing something of desired type to the function as a parameter, like so, but I'd like to avoid it if possible:

function f(hint: std_logic_vector) return std_logic_vector is
    variable retval: std_logic_vector(hint'range);
begin
    retval := (hint'high - 2 downto hint'low + 1 => '0', others => '1');
    return retval;
end function;
signal x: std_logic_vector(7 downto 0);
signal y: std_logic_vector(11 downto 0);
[...]
x <= f(x); -- x = "11000001"
y <= f(y); -- y = "110000000001"

If it matters, I'm using Quartus and this needs to synthesize. The second block of code SHOULD work fine (assuming I haven't made any mistakes), but I wonder if there is a better way to accomplish this.

Note that these are contrived examples and of course there are much simpler ways to assign those values; I am asking if in general there is a nice way to avoid the hack of passing an extra parameter.

like image 769
Will Simoneau Avatar asked Jul 06 '26 19:07

Will Simoneau


1 Answers

With an unconstrained array as a return type, your function is responsible for deciding what it returns, typically based in some way on the range(s) of your input(s). The return range may be the same as your input ranges, or different if desired (perhaps you are concatenating multiple arrays, increasing bit-width to avoid overflow of the result, or whatever).

You can't easily do this in your contrived example because your function has no inputs...which I guess would make it a constant. :)

It's a little more obvious what you need to do if your code changes:

-- from:
x <= f; 

-- to:
x <= f(x);
like image 174
Charles Steinkuehler Avatar answered Jul 13 '26 09:07

Charles Steinkuehler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!