【问题标题】:VHDL: setting a constant conditionally based on another constant's valueVHDL:根据另一个常量的值有条件地设置一个常量
【发布时间】:2019-08-05 16:25:53
【问题描述】:

我需要使用“if-else”或“case”设置一个常量的值,并根据另一个常量的值选择不同的常量值。这在VHDL中可能吗?这将是在模拟开始时常数值的一次更改......示例:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity bridge is
    generic (
        burst_mode    :std_logic  := '0'
    );
end entity;

architecture rtl of bridge is

    constant  fifo_aw_wdata  :natural := 2 when (burst_mode = '0') else 5; 

begin

--   fifo: entity work myfifo
--      generic map(
--          aw => fifo_aw_wdata
--      )
--      port map(
--          ...
--      );

end architecture;

上面的VHDL代码给出了错误信息:

Error ';' is expected instead of 'when'

在 Verilog 中,做这种事情很容易……所以我认为 VHDL 也可以做到这一点? Verilog 示例:

    module #(parameter burst_mode = 1'b0) bridge;

    localparam fifo_aw_wdata = (!burst_mode) ? 2 : 5;

    // fifo myfifo #(.aw(fifo_aw_wdata)) ( ...);

    endmodule;

【问题讨论】:

  • Vhdl 2019 将(最终)允许此代码。

标签: vhdl


【解决方案1】:

这个解决方案有点奇怪,但确实有效:

architecture rtl of bridge is

    function setup1(s:std_logic; i0:integer; i1:integer) 
        return integer is
    begin
        if s = '1' then
            return i1;
        else
            return i0;
        end if;
    end function;

    constant  fifo_aw_wdata  :natural := setup1(burst_mode, 2, 5);

begin

--   fifo: entity work myfifo
--      generic map(
--          aw => fifo_aw_wdata
--      )
--      port map(
--          ...
--      );

end architecture;

【讨论】:

  • 是的,就是这样做的,VHDL 重载允许您声明多个具有相同名称但参数和结果类型不同的函数,从而 VHDL 编译器将像魔术一样应用正确的函数; -)
【解决方案2】:

已发布正确答案,但也有单行替代方案。

最简单的解决方案是将burst_mode的数据类型更改为整数,范围为0到1,然后使用一些数学运算:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity bridge is
    generic (
        burst_mode    :integer range 0 to 1 := 0
    );
end entity;

architecture rtl of bridge is
    constant  fifo_aw_wdata  :natural := 2 + burst_mode * 3; 
begin

如果burst_mode的数据类型不能改变,也可以通过类型转换来实现:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity bridge is
    generic (
        burst_mode    :std_logic  := '0'
    );
end entity;

architecture rtl of bridge is
    constant  fifo_aw_wdata  :natural := 2 + to_integer(unsigned('0' & burst_mode)) * 3; 
begin

【讨论】:

    【解决方案3】:

    虽然 pico 已经发布了答案,但这是一个非常相关的问题,值得细说。

    函数的声明可能类似于其他语言中的“条件表达式”或“三元 if”,例如带有 res_true if cond else res_false 的 Python 或带有 cond ? res_true : res_false 的 C。

    条件是一个布尔值,true 的结果在false 的结果之前。声明可能是这样的:

    function tif(cond : boolean; ref_true, ref_false : integer) return integer is
    begin
      if cond then
        return res_true;
      else
        return res_false;
      end if;
    end function;
    

    并且具有不同结果类型的多个函数,real 的版本也可以定义为:

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

    【讨论】:

      猜你喜欢
      • 2014-10-06
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2021-09-18
      • 1970-01-01
      • 2010-12-01
      相关资源
      最近更新 更多