【问题标题】:Parameterized constants in VHDLVHDL 中的参数化常量
【发布时间】:2018-09-20 16:16:34
【问题描述】:

我正在设计一个通用数据路径,其中有不同解码器使用的恒定模式。例如:

NBITS = 4 -> 常数为“1000”

NBITS = 5 -> 常数为“10000”

NBITS = 16 -> 常数为“1000_0000_0000_0000”

我认为类似以下的方法可以解决问题: constant NaR : std_logic_vector(NBITS - 1 downto 0) := (NBITS - 1 => '1', others => '0');

但是可惜没有编译并生成错误:非静态选择排除其他选择。

如何指定作为泛型参数函数的常量位模式?

【问题讨论】:

    标签: generics vhdl


    【解决方案1】:

    当有多个选择时,聚合具有本地静态选择的要求。 (确定表达式值的代码是在分析时构建的。)

    IEEE Std 1076-2008 9.3.3.3 数组聚合第 6 段:

    除了与单个选择others的最终元素关联外,数组聚合的其余元素关联(如果有)应全部为位置或全部命名。数组聚合的命名关联允许有一个不是局部静态的选择,或者同样是一个空范围的选择,只有当聚合包括单个元素关联并且这个元素关联有一个选择时。如果适用的索引约束是本地静态的,则其他选择是本地静态的。

    可以在细化时通过函数调用(表达式,9.3.4 函数调用)提供常量的值表达式(6.4.2.2 常量声明)或变量或信号的初始表达式(14.4.2.5 对象声明) )。该函数可能是不纯的(4. 子程序和包,4.1 通用,9.4.3 全局静态主,注 2)。

    构造一个Minimal, Complete, and Verifiable example 可以证明这一点:

    library ieee;
    use ieee.std_logic_1164.all;
    
    ravenwater_mcve is
        generic (NBITS: natural := 42);
    end entity;
    
    architecture foo of ravenwater_mcve is
        impure function leftbit return std_logic_vector is
            variable retv:  std_logic_vector (NBITS - 1 downto 0) := 
                              (others => '0');
        begin
            retv(retv'LEFT) := '1';
            return retv;
        end function;
        constant NaR: std_logic_vector(NBITS - 1 downto 0) := leftbit;
        -- (NBITS - 1 => '1', others => '0');
    begin
    end architecture;
    

    MCVe 分析、阐述和模拟。

    您可以添加一个过程来确定值表达式是否正确:

    RESULT:
        process
        begin
            report "NaR = " & to_string(NaR);
            wait;
        end process;
    

    如果使用 -2008 之前的 VHDL 标准修订版,请提供 to_string 函数:

        function to_string (inp: std_logic_vector) return string is
                variable image_str: string (1 to inp'length);
                alias input_str:  std_logic_vector (1 to inp'length) is inp;
        begin
            for i in input_str'range loop
                image_str(i) := character'VALUE(std_ulogic'IMAGE(input_str(i)));
            end loop;
            return image_str;
        end function;
    

    这会产生:

    ghdl -r ravenwater_mcve
    ravenwater_mcve.vhdl:33:9:@0ms:(报告说明):NaR = 100000000000000000000000000000000000000000

    (也许我应该为通用常量使用较小的默认值。)

    您也可以将泛型的值作为参数传递给纯函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多