2012-01-28 34 views
3

我想知道是否有可能以某种方式引用返回类型/值从一个函数返回一个无约束类型的属性。 (做限制,甚至向上传递到所有的功能有个声音告诉我,他们不这样做,并返回类型的约束是由什么决定实际上是在函数内部返回?)例如:可能引用函数返回类型的属性吗?

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" 

我知道这可以通过将所需类型的东西给函数作为参数,像这样被黑身边,但我想,以避免可能的话:

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" 

如果它的事项,我使用的Quartus这需要综合。第二块代码应该可以正常工作(假设我没有犯任何错误),但是我想知道是否有更好的方法来完成这个任务。

请注意,这些是人为的例子,当然有更简单的方法来分配这些值;我在问,一般情况下是否有避免传入额外参数的方法。

+0

有没有人掏槽的LRM和找到相关的东西吗?当你尝试运行第一段代码时,你的模拟器会说什么? – Philippe 2012-01-30 19:17:03

回答

1

将无约束数组作为返回类型时,函数负责决定返回的内容,通常以某种方式基于输入的范围。返回范围可能与您的输入范围相同,或者根据需要设置不同(也许您将多个数组连接在一起,增加位宽以避免结果溢出或其他)。

在你设计的例子中,你不能轻易做到这一点,因为你的函数没有输入......我想这会使它成为一个常量。 :)

这一点更加明显,你需要做什么,如果你的代码更改:

-- from: 
x <= f; 

-- to: 
x <= f(x); 
+0

好的,这完全符合我怀疑的情况,谢谢:) – 2012-01-30 18:59:59

+0

请注意,没有参数的函数不是常量。 VHDL允许在函数中使用副作用! – Philippe 2012-01-30 19:13:01