"when others" VHDL case 语句中的行?

"when others" line in VHDL case statement?

我是一名学习 VHDL 的学生,我教科书中的示例代码在几个地方显示了类似于以下内容的行;

when "000" => tmp_result <= a and b;
when "001" => tmp_result <= a or b;
...
when others => tmp_result <= (others => '0');

我发现 VHDL 的语法总体上非常不直观,但我真的完全不"get"这一行。

我真的很困惑为什么上面的行不只是:

when others => tmp_result <= '0'

为什么会这样?

我试过谷歌搜索,但没能找到解释。

这是因为 tmp_result 被定义为 std_logic_vector(而不是简单地 std_logic ).

tmp_result <= '0';             -- tmp_result is std_logic (single-quotes)
tmp_result <= "0000000";       -- tmp_result is std_logic_vector (double quotes)
tmp_result <= (others => '0'); -- same as previous line, but does not need to know length of tmp_result

当将 std_logic_vector 的所有位分配给相同的值时('0',在这种情况下),通常的做法是使用语法 (others => '0'),基本上转换为 "give me a std_logic_vector the same length as tmp_result filled with '0'"。这样更好,因为当 tmp_result 的长度发生变化时,该行仍然有效,例如它的长度取决于泛型。

STD_LOGIC_VECTOR 具有固定大小。因此,当您为其分配一个值时,您可以只使用

而不是明确定义每个位
(others => '0')

表示您希望将剩余的位设置为 0。由于变量具有固定大小,您的编译器将知道要设置多少位。您可以将其与一堆其他语句混合使用,例如

tmp_result <= (1=>'1', OTHERS => '0');

一个可以派上用场的情况是:

ENTITY test IS
    GENERIC (n : INTEGER := 7);
    PORT (a : OUT STD_LOGIC_VECTOR (n DOWNTO 0)
          );
END test;

您知道,我们可能每次都必须更改大小,这就是我们定义 generic 变量的原因。使用 (others => '0') 将其设置为 0 将使我们不必重新更改整个程序。