【问题标题】:Does VHDL have a ternary operator?VHDL有三元运算符吗?
【发布时间】:2013-04-13 07:26:40
【问题描述】:

我喜欢三元运算符与 if 子句的简洁性。

这个操作符是否存在于 vhdl 中?我的搜索结果正好相反。我还检查了when语句,但它不是运算符,我也希望能够在进程中使用它...

【问题讨论】:

    标签: vhdl ternary-operator


    【解决方案1】:

    不是您从 C/C++ 中了解的那种,但您可以使用:

    destination <= signal1 when condition else signal2;
    

    【讨论】:

      【解决方案2】:

      没有。它已针对 VHDL-2008 进行了讨论,但没有进入。您有几个选择。如果您的工具支持 VHDL-2008,则现在支持条件赋值作为顺序语句(它们以前只是并发的),因此您可以编写如下内容:

      process(clock)
      begin
        if rising_edge(clock) then
          q <= '0' when reset else d; -- ie. much like q <= reset? '0':d;
        end if;
      end process;
      

      如果你没有 2008,就写一个函数 (q &lt;= sel(reset, '0', d))。不过,您必须为您感兴趣的每种类型编写它。

      【讨论】:

      • 如何添加多个 when/else 子句?我似乎无法让(cond when x else y) + (cond when z else w) 工作。
      • 不要在赛灵思 Vivado 中使用它。 ISIM 模拟器直到 2019.2 版本才支持此构造!难以置信。
      【解决方案3】:

      在我的实用程序包中,我有这两个功能。对我来说,它们非常方便

        function ternu(cond : boolean; res_true, res_false : unsigned) return unsigned is
        begin
           if cond then return res_true;
           else      return res_false;
           end if;
        end function;
      
        function terni(cond : boolean; res_true, res_false : integer) return integer is
        begin
           if cond then return res_true;
           else      return res_false;
           end if;
        end function;
      

      【讨论】:

        猜你喜欢
        • 2023-03-11
        • 2022-01-06
        • 2017-02-08
        相关资源
        最近更新 更多