VHDL when-else错误

VHDL when-else error

我是 VHDL 的新手,有一些简单的错误。我正在尝试使用 when else 构造创建 MUX。错误有两种类型:

Error (10500): VHDL syntax error at lab13.vhd(21) near text "when"; expecting ";"

Error (10500): VHDL syntax error at lab13.vhd(21) near text "else"; expecting ":=", or "<="

这些错误是针对每个带有 when else.

的字符串

这是代码:

entity lab13 is
port (SW : in  STD_LOGIC_VECTOR (17 downto 0);
LEDG : out STD_LOGIC_VECTOR (2 downto 0);
LEDR : out STD_LOGIC_VECTOR (17 downto 0));
end lab13;



architecture logicFunc of lab13 is
begin  
    process
variable a, b, c : STD_LOGIC_VECTOR (2 downto 0) :=0;
begin
    a(0) := SW(0) when (SW(15) = '0') else SW(3);
    b(0) := SW(6) when (SW(15) = '0') else SW(9);
    c(0) := a(0) when (SW(16) = '0') else b(0);
    LEDG(0) <= c(0) when (SW(17) = '0') else SW(12);

    a(1) := SW(1) when (SW(15) = '0') else SW(4);
    b(1) := SW(7) when (SW(15) = '0') else SW(10);
    c(1) := a(1) when (SW(16) = '0') else b(1);
    LEDG(1) <= c(1) when (SW(17) = '0') else SW(13);

    a(2) := SW(2) when (SW(15) = '0') else SW(5);
    b(2) := SW(8) when (SW(15) = '0') else SW(11);
    c(2) := a(2) when (SW(16) = '0') else b(2);
    LEDG(2) <= c(2) when (SW(17) = '0') else SW(14);
end process;
   LEDR <= SW;
end logicFunc;

那么,如何解决这些问题呢?

VHDL-2008中引入了条件变量或信号赋值的顺序语句when,Altera Quartus不完全支持

可以使用信号而不是进程来实现,例如:

architecture logicFunc of lab13 is
  signal a, b, c : STD_LOGIC_VECTOR (2 downto 0);
begin

  a(0) <= SW(0) when (SW(15) = '0') else SW(3);
  b(0) <= SW(6) when (SW(15) = '0') else SW(9);
  c(0) <= a(0) when (SW(16) = '0') else b(0);
  LEDG(0) <= c(0) when (SW(17) = '0') else SW(12);

  a(1) <= SW(1) when (SW(15) = '0') else SW(4);
  b(1) <= SW(7) when (SW(15) = '0') else SW(10);
  c(1) <= a(1) when (SW(16) = '0') else b(1);
  LEDG(1) <= c(1) when (SW(17) = '0') else SW(13);

  a(2) <= SW(2) when (SW(15) = '0') else SW(5);
  b(2) <= SW(8) when (SW(15) = '0') else SW(11);
  c(2) <= a(2) when (SW(16) = '0') else b(2);
  LEDG(2) <= c(2) when (SW(17) = '0') else SW(14);

  LEDR <= SW;

end architecture;

abc的初始化值不需要,否则必须使用:

variable a, b, c : std_logic_vector (2 downto 0) := (others => '0');

如果像 when 这样的东西在 VHDL-2008 之前很方便,那么 tern 函数可以写成:

function tern(cond : boolean; res_true, res_false : std_logic) return std_logic is
begin
  if cond then
    return res_true;
  else
    return res_false;
  end if;
end function;

然后用作:

a(0) := tern(SW(15) = '0', SW(0), SW(3));

'WHEN' 关键字在 VHDL 中有 2 个上下文,并且从 VHDL 1993 开始都适用: 1. 它被用作 process/procedure 中 'CASE' 语句的一部分:

CASE xyz IS
  WHEN val1 =>  some sequential statements;
  WHEN val2 =>  some sequential statements;
  WHEN OTHERS => NULL;
END CASE;

通过用此替换您处理的 BEGIN/END 之间的代码,无论您使用的是变量还是信号,都会产生正确的结果。

  1. 它可以用作 'WHEN-ELSE' 并发语句(不在 process/procedure 中):

    result <= val1 WHEN 某些条件为真 ELSE val2 WHEN 其他一些条件为真 ELSE (OTHERS => '0');

这也将 return 要求的结果,但由于不在 process/procedure 内,将要求 'result' 是信号或共享变量。