VHDL if 语句的奇怪行为

Strange behavior of VHDL if statement

当我在做我的项目时,我遇到了 VHDL if 语句的一个奇怪问题。虽然我修复了它,但我仍然不明白为什么会这样。我使用 ModelSPIM 刺激了我的代码。在我更改代码之前,我期望 rd <= inst (20 downto 16);RegDst = '1',但它给了我 rd <= inst (15 downto 11);。我检查过 RegDst 确实等于 0,但它给了我错误的分配。我改了代码之后,一切都正常了。它们有什么区别?

之前:

fetch: process(inst)
begin
if( inst = x"0000_0000" ) then -- end of program
    endRun <= '1';
else
    endRun <= '0';
    opcode <= inst (31 downto 26);
    rs <= inst (25 downto 21);
    rt <= inst (20 downto 16);
    if( RegDst = '1' ) then
       rd <= inst (15 downto 11);
    else
       rd <= inst (20 downto 16);
    end if;
    funct <= inst (5 downto 0);
    offset <= inst (15 downto 0);
    jsec <= inst (25 downto 0);
end if;
end process fetch;      

之后:

fetch: process(inst)
begin
if( inst = x"0000_0000" ) then -- end of program
    endRun <= '1';
else
    endRun <= '0';
    opcode <= inst (31 downto 26);
    rs <= inst (25 downto 21);
    rt <= inst (20 downto 16);
    funct <= inst (5 downto 0);
    offset <= inst (15 downto 0);
    jsec <= inst (25 downto 0);
end if;
end process fetch;      
   rd <= inst (15 downto 11) when (RegDst = '1') else
      inst(20 downto 16); -- RegDst mux

您的敏感度列表有问题。敏感列表是处理后括号中的信号列表。当一个事件发生在它的敏感列表中的任何信号上时,一个过程就会被执行。

在您的情况下,您的敏感列表中只有 inst。因此,当 regDst 从 '0' 变为 '1' 时,该过程将不会执行(如果 inst 没有改变)并且rd 不会更新。

在你的第二种方法中,语句不在进程中,因此不受敏感列表的影响(准确地说,进程外语句中涉及的所有信号都被认为是敏感列表)。如果您在敏感度列表中添加 redDst,您将得到相同的结果:

process(inst, regDst)

请注意,敏感度列表中缺失的信号是模拟和实现之间不匹配的一个非常常见的来源,因为我知道的所有工具都会忽略它们来实现。如果你使用 VHDL-2008,你可以在敏感度列表中使用关键字 all,这完全符合你的想法。