gpt4 book ai didi

vhdl - 可以引用函数返回类型的属性吗?

转载 作者:行者123 更新时间:2023-12-04 22:48:07 24 4
gpt4 key购买 nike

我想知道是否有可能从返回无约束类型的函数内部以某种方式引用返回类型/值的属性。 (约束是否甚至会传播到函数中?有些东西告诉我它们没有,并且返回类型的约束由函数内部实际返回的任何内容决定)例如:

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,这需要综合。第二个代码块应该可以正常工作(假设我没有犯任何错误),但我想知道是否有更好的方法来实现这一点。

请注意,这些都是人为的示例,当然还有更简单的方法来分配这些值;我在问一般来说是否有一种很好的方法来避免传递额外参数的黑客行为。

最佳答案

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

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

如果您的代码发生更改,您需要做的事情会更加明显:

-- from:
x <= f;

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

关于vhdl - 可以引用函数返回类型的属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9048630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com