【问题标题】:Need work-around for "ternary operator" in VHDL constants需要解决 VHDL 常量中“三元运算符”的问题
【发布时间】:2019-04-26 19:24:05
【问题描述】:

我有一些 Verilog 代码如下:

module bus_fifo #(
    parameter DEPTH_WIDTH = 0,
    parameter DATA_WIDTH = 0
) (
    input  wire                    clk,
    input  wire                    rst,

    input  wire [DATA_WIDTH-1:0]       wr_data_i,
    input  wire                wr_en_i,

    output wire [DATA_WIDTH-1:0]       rd_data_o,
    input  wire                        rd_en_i,

    output wire                full_o,
    output wire                        empty_o
);

localparam DW = (DATA_WIDTH  == 10) ? 4 : DATA_WIDTH;
localparam AW = DEPTH_WIDTH>>2;

endmodule

我想要做同样事情的语法有效的 VHDL:

library ieee;
use ieee.std_logic_1164.all;

entity bus_fifo is
    generic(
        DEPTH_WIDTH               : integer := 0;
        DATA_WIDTH                : integer := 0
    );
    port(
        clk                       : in    std_logic;
        rst                       : in    std_logic;
        wr_data_i                 : in    std_logic_vector(DATA_WIDTH-1 downto 0);
        wr_en_i                   : in    std_logic;
        rd_data_o                 : out   std_logic_vector(DATA_WIDTH-1 downto 0);
        rd_en_i                   : in    std_logic;
        full_o                    : out   std_logic;
        empty_o                   : out   std_logic        
    );
end entity;

architecture bus_fifo of bus_fifo is
    constant   DW         : integer := (DATA_WIDTH  == 10) ? 4 : DATA_WIDTH;
    constant   AW         : integer := DEPTH_WIDTH>>2;
    signal     write_pointer;            : std_logic_vector(AW downto 0);
    signal     read_pointer;             : std_logic_vector(AW downto 0);
    signal     empty_int                 : std_logic;
    signal     full_or_empty             : std_logic;
begin

end architecture;

我遇到的问题是 VHDL 不支持三元运算符并生成错误消息。我想知道是否有 VHDL 解决方案可以做类似于我在 Verilog 中所做的事情?

【问题讨论】:

  • 函数是在 VHDL 2008 及以前版本中执行此操作的唯一方法。
  • 我质疑这项任务是否完全有意义。如果DATA_WIDTH 是9 或11,DW 也是9 或11,但如果DATA_WIDTH 是10,DW 应该是4!?
  • VHDL - IF alternative的可能重复

标签: vhdl verilog


【解决方案1】:

我通常使用一个函数(名为“If Then Else”=ite)来解决 VHDL 中缺少三元运算符的问题。

function ite(b: boolean; x, y: integer) return integer is begin
    if (b) then
        return x;
    else
        return y;
    end if;
end function ite;

这样的用法:

constant foo : integer := ite(SOME_INTEGER = 42, 1337, 4711);

你可以为不同的返回类型重载这个函数。请注意,当用于初始化常量时,布尔表达式在求值时必须是常量。
一个更现实的例子,一个我经常使用的食谱:

entity foo is
  generic (
    DEBUG_LVL : integer := 0
  )
...
attribute MARK_DEBUG : string;
...
signal my_signal : std_logic;
attribute MARK_DEBUG of my_signal : signal is ite(DEBUG_LVL >= 1, "TRUE", "FALSE");

最后一个例子当然需要一个带有签名的“ite 函数”

function ite(b: boolean; x, y: string) return string;

【讨论】:

    【解决方案2】:

    VHDL 2019 增加了初始值的条件赋值:

    constant DW : integer := 4 when (DATA_WIDTH = 10) else DATA_WIDTH;
    

    预计到 2029 年提供供应商支持(尤其是 Xilinx)。

    【讨论】:

    • 没有供应商参与标准化过程,这让我想起了一个关于制作English the standard language of the European Union 的笑话。龙虾的豆子计数器注意到修复变红的外皮的成本,因为 VHDL 慢慢地转换为 SystemVerilog。会不会先遇到热死?
    • @user1155120 我刚开始在一家使用 VHDL 但允许我使用 Verilog 的公司工作。两个月后,他们正在考虑更换。它让我想起了所有程序员不得不学习 Pascal 而整个行业使用 C。
    • 我有点相信特定的 HDL 设计输入、设计验证和验证方法从长远来看不会占主导地位。它们不能很好地随着设计和设备尺寸的增加而扩展。如果 VHDL 没有“赶上”System Verilog,最终结果将是相同的。
    • 我有一些印象,对某些人来说,完全使用 SV 太过分了,他们正在推动更正式。它可能是销售记录。
    【解决方案3】:

    您可以创建一个函数并在评估泛型时调用该函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-13
      • 2021-12-06
      • 2011-10-24
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      相关资源
      最近更新 更多