VHDL 将独占或数据作为函数实现

VHDL Implementing exclusive or data as a function

我正在尝试将简单代码打包到一个函数中。 这是 VHDL 代码:

process(CLK, RST)
    variable newdata : std_logic_vector(7 downto 0)  := (others => '0');
    variable tempdata : std_logic_vector(7 downto 0) := (others => '0');
begin

    if (RST = '1') then
       tempdata := (others => '0');
       newdata  := (others => '0');
    elsif rising_edge(CLK) then
        tempdata := DIN;    
        newdata  := newdata XOR tempdata;
    end if;
    DOUT <= newdata;
end process;

Code simulation

RTL model

一切如我所愿。 但是如果尝试将此代码作为函数实现,它将失败

function f_MOD2ALG (foo : std_logic_vector) 
return std_logic_vector;
...
function f_MOD2ALG (foo : std_logic_vector) 
return std_logic_vector is
    variable tempdata : std_logic_vector(foo'range);
    variable newdata  : std_logic_vector(foo'range);
begin
    tempdata := foo;
    newdata := newdata xor tempdata;
    return newdata;
end function;

Fail simulation

Fail RTL model

没有错误,没有警告。

在 RTL 模型上看起来他是空数据输入,但我不明白为什么。 有人可以向我解释为什么会这样吗?以及如何正确创建实现我的代码的函数? 谢谢!

函数调用如下:

...
signal temp     : std_logic_vector(7 downto 0) := (others => '0');

begin

process(CLK, RST)
begin
    if (RST = '1') then
        temp <= (others => '0');
    elsif rising_edge(CLK) then
        temp <= f_MOD2ALG(DIN);
    end if;
end process;
DOUT <= temp;
...

正如我在评论中所写的那样,您的代码存在的问题是函数内的变量 newdata 将始终为 "UUUUUUUU" 因为它永远不会被初始化并且它不会在两者之间保留其值两个函数调用。与 U 进行异或运算总是会得到 U.

如果将 newdata 初始化为全零,则输出将对应于输入,因为对 foo0 进行异或运算将得到 foo.

我知道 Easics 的 CRC 生成器。在它们的函数中,它们将先前的 CRC 值作为参数传递。你可以做类似的事情:

function f_MOD2ALG (foo : std_logic_vector; last_bar: std_logic_vector) 
return std_logic_vector is
    variable new_bar  : std_logic_vector(foo'range);
begin
    new_bar := foo xor last_bar;
    return new_bar;
end function;

然后这样称呼它:

signal bar : std_logic_vector(31 downto 0) := (others=>'0');
...
process(clk)
begin
   if rising_edge(clk) then
      bar <= f_MOD2ALG(foo, bar);
   end if;
end process;