将 std_logic 位连接成无符号数

Concatenate std_logic bits into unsigned number

在下面的代码中,我尝试连接三个 std_logic 输入以生成三位无符号输出。执行此操作所需的语法似乎不直观,我不明白。有人解释这是怎么回事吗?

当我在评论中说 "fails" 时,我的意思是合成会产生以下错误消息:found '4' definitions of operator "&", cannot determine exact overloaded matching definition。然后它在 numeric_std 中给出 2 个行号,在 std_1164 中给出 2 个行号(但我没有要检查的源文件的那些特定版本)。

use IEEE.NUMERIC_STD.ALL;

entity Thingy is
    Port ( a : in  STD_LOGIC;
           b : in  STD_LOGIC;
           c : in  STD_LOGIC;
           clk : in  STD_LOGIC;
           decoded : out  UNSIGNED (2 downto 0));
end Thingy;

architecture Behavioral of Thingy is
begin

    process (clk)
        variable dec : STD_LOGIC_VECTOR(2 downto 0) := (others => '0');
    begin
        if rising_edge(clk) then
            -- Intermediate variable, works ok.
            dec := a & b & c;
            decoded <= unsigned(dec);

            -- Also ok. Implicit conversion from std_logic to unsigned?
            -- decoded <= a & b & c;

            -- No intermediate variable, fails.
            -- decoded <= unsigned(std_logic_vector(a & b & c));

            -- Fails.
            -- decoded <= unsigned(a & b & c);
        end if;
    end process;

end Behavioral;

让我们看看你的各种情况。

首先,您只是将串联的 std_logic 分配给一个 unsigned 信号,正如您所说,它有效!

decoded <= a & b & c;

所以,您是说这工作正常(应该!)并且简单明了,那么您为什么对它有疑问?根据定义,unsigned's 字面上是由 std_logic's 组成的。这里没有什么奇怪的。这是最好的写法。


此处您正在尝试进行一系列转换:

decoded <= unsigned(std_logic_vector(a & b & c));

它失败了,因为它无法将 std_logic 的某些数组类型转换为 std_logic_vector,它还不能推断(& 运算符的结果) .但是这个语法,虽然看起来几乎一样,应该做你想做的,因为它没有做转换,只是简单地告诉编译器表达式的类型:

decoded <= unsigned(std_logic_vector'(a & b & c));

同样,可以用同样的方法解决这个问题:

decoded <= unsigned(a & b & c);

改用这个:

decoded <= unsigned'(a & b & c);

我看到的主要误解是您可能认为 std_logic 连接在一起在某种程度上与 std_logic_vector 相同。这根本 不正确。 std_logic_vector 只是一种由 std_logic 元素构建的特定数组类型。其他示例是 unsignedsigned 以及您可以创建的任何其他用户定义类型。

当您将 std_logic 元素与 & 连接时,它们具有一种 "universal" 类型,可以在赋值时推断出来,或者可以显式标记类型,但不能转换,因为它们还没有已知类型!这就是为什么 ' 的语法有效,而你原来的尝试没有。

希望对您有所帮助。祝你好运!