Function 在多个函数中使用变量?
可能是因为我是新手。无论如何,我想定义一些变量用于多个函数(比如C中的全局变量)。我决定使用共享变量,但它给了我一个错误Function 在多个函数中使用变量?,function,variables,process,vhdl,modelsim,Function,Variables,Process,Vhdl,Modelsim,可能是因为我是新手。无论如何,我想定义一些变量用于多个函数(比如C中的全局变量)。我决定使用共享变量,但它给了我一个错误无法在纯函数“y”中引用共享变量“x”。如果我在一个进程中定义变量,那么它会初始化(擦除值)每个进程激活 Architecture SV_example of example is shared variable temp:std_logic_vector(18 downto 0):= "0000000000000000000"; SIGNAL gct: STD_LOGIC_
无法在纯函数“y”中引用共享变量“x”
。如果我在一个进程中定义变量,那么它会初始化(擦除值)每个进程激活
Architecture SV_example of example is
shared variable temp:std_logic_vector(18 downto 0):= "0000000000000000000";
SIGNAL gct: STD_LOGIC_VECTOR(18 DOWNTO 0);
function register_adder( load_value:std_logic ) return std_logic_vector is
begin
for i in size downto 0 loop
temp(i) := load_value;
end loop;
return temp;
end register_adder;
p1 : process (CLK)
begin
if rising_edge(CLK) then
gct <= register_loader('1');
end if;
end process;
end SV_example;
Architecture SV\u示例的示例为
共享变量temp:std_logic_vector(18到0):=“0000000000000000000”;
信号gct:STD_逻辑_向量(18向下至0);
函数寄存器加法器(加载值:标准逻辑)返回标准逻辑向量为
开始
对于大小降至0的循环中的i
温度(i):=荷载值;
端环;
返回温度;
端寄存器加法器;
p1:过程(CLK)
开始
如果上升沿(CLK),则
gct在VHDL中(至少在VHDL'93及更高版本中),默认情况下函数是纯函数。纯
功能是一种没有副作用的功能。它只依赖于输入,不依赖于(非静态)外部信息
因此,您的解决方案是声明函数不纯
。我不得不对你的例子进行一些编辑,使之成为一个好例子。修复方法:
library ieee;
use ieee.std_logic_1164.all;
entity example is
port
(
CLK : in std_logic
);
end entity example;
Architecture SV_example of example is
shared variable temp:std_logic_vector(18 downto 0):= "0000000000000000000";
SIGNAL gct: STD_LOGIC_VECTOR(18 DOWNTO 0);
impure function register_adder( load_value:std_logic ) return std_logic_vector is
begin
for i in temp'length-1 downto 0 loop
temp(i) := load_value;
end loop;
return temp;
end register_adder;
begin
p1 : process (CLK)
begin
if rising_edge(CLK) then
gct <= register_adder('1');
end if;
end process;
end SV_example;
ieee库;
使用ieee.std_logic_1164.all;
实体示例是
港口
(
时钟:在标准逻辑中
);
终端实体示例;
架构SV_示例的示例是
共享变量temp:std_logic_vector(18到0):=“0000000000000000000”;
信号gct:STD_逻辑_向量(18向下至0);
不纯函数寄存器加法器(加载值:标准逻辑)返回标准逻辑向量为
开始
对于临时长度为1到0的循环中的i
温度(i):=荷载值;
端环;
返回温度;
端寄存器加法器;
开始
p1:过程(CLK)
开始
如果上升沿(CLK),则
gct共享变量通常与受保护类型一起使用,这些类型可以处理数据的封装。使用受保护类型的示例如下所示:
architecture SV_example of example is
signal gct : std_logic_vector(18 downto 0);
type temp_t is protected
impure function register_adder(load_value : std_logic) return std_logic_vector;
end protected;
type temp_t is protected body
variable temp : std_logic_vector(18 downto 0) := (others => '0');
impure function register_adder(load_value : std_logic) return std_logic_vector is
begin
for i in temp'range loop
temp(i) := load_value;
end loop;
return temp;
end function;
end protected body;
shared variable temp_sv : temp_t;
begin
p1 : process (CLK)
begin
if rising_edge(CLK) then
gct <= temp_sv.register_adder('1');
end if;
end process;
end SV_example;
architecture SV\u示例的示例为
信号gct:std_逻辑_向量(18向下至0);
类型temp\u t受保护
不纯函数寄存器加法器(加载值:标准逻辑)返回标准逻辑向量;
末端保护;
类型temp\u t是受保护的主体
变量温度:标准逻辑向量(18到0):=(其他=>“0”);
不纯函数寄存器加法器(加载值:标准逻辑)返回标准逻辑向量为
开始
对于温度范围循环中的i
温度(i):=荷载值;
端环;
返回温度;
末端功能;
末端保护体;
共享变量temp_sv:temp_t;
开始
p1:过程(CLK)
开始
如果上升沿(CLK),则
gct我给出了如何消除错误的答案。但我不确定你在这里的目标。上面的示例不需要temp
,因为您使用函数的返回值。发布一个更完整的示例,我们可以帮助您更好地分析您的用例。您是对的。上面的例子不清楚我的要求。我只需要一个全局变量就可以从体系结构中的每个函数访问。您需要一个体系结构范围的变量,而不是一个信号
?通过共享变量
共享信息,您试图实现什么目的。如果您需要某种状态信息,我建议您查看VHDL-2002及更高版本中的受保护的
类型。谢谢您的推荐。关于封装的一些东西对未来的工作会更有用。