如何根据常量的对数设置 VHDL 矢量大小

How to set VHDL vector size based on the log of a constant

我想知道 $clog2(DATA_WIDTH) 对应的 VHDL 代码是什么,例如这一行:

parameter DATA_OUT_WIDTH = $clog2(DATA_WIDTH)

在这个例子中还有这个符号“-:”

if ( Pattern == In[i_count-:PATTERN_WIDTH] )

如果有人能帮助我,我将不胜感激。

你可以这样做

constant DATA_OUT_WIDTH : positive := positive(ceil(log2(real(DATA_WIDTH))));

或者定义一个封装该表达式的 clog2 函数。 ceil 和 log2 可以在 math_real

中找到
use ieee.math_real.all;

在 VHDL 中,您可以只指定整个范围,例如

foo(i_count to i_count + 7)
foo(i_count downto i_count - 7)

不要使用 In 作为标识符,它是 VHDL 中的保留字。

除了 Lars 示例之外,您还可以轻松编写一个函数来查找上限日志 2,以确定某些总线宽度所需的元素地址数 'bits'。一些供应商或验证支持库已经提供了一个。

Lars 回答中已经表达了 IEEE 库中没有预定义函数的原因,您往往不会经常使用它,您可以将值分配给常量,并且可以从现有的表达式中拼凑出一个表达式职能。

示例 clog2 函数

从 IEEE 包中借用并转换的 log2 例程 float_generic:

  function clog2 (A : NATURAL) return INTEGER is
    variable Y : REAL;
    variable N : INTEGER := 0;
  begin
    if  A = 1 or A = 0 then  -- trivial rejection and acceptance
      return A;
    end if;
    Y := real(A);
    while Y >= 2.0 loop
      Y := Y / 2.0;
      N := N + 1;
    end loop;
    if Y > 0.0 then
      N := N + 1;  -- round up to the nearest log2
    end if;
   return N;
  end function clog2;

参数 A 类型 NATURAL 阻止传递负整数值。四舍五入是严格的,任何低于 2.0 的余数都会导致四舍五入。

请注意,因为它使用 REAL 并使用除法,所以它只适合在分析和阐述期间使用。这是一个纯函数。

你可以记下 Lars 的例子:

constant DATA_OUT_WIDTH : positive := positive(ceil(log2(real(DATA_WIDTH))));

对用于分析(局部静态)和精化(全局静态)的使用具有相同的限制。综合通常不支持 REAL 类型,浮点运算会占用大量空间。

if条件

if ( Pattern == In[i_count-:PATTERN_WIDTH] )

是一个基本索引(一个 lsb 或 msb,取决于升序或降序声明的位顺序)和一个宽度。

参见 IEEE Std 1800-2012 (SystemVerilog),11.5.1 矢量位-select 和部分-select 寻址。

An indexed part-select is given with the following syntax:

logic [15:0] down_vect;  
logic [0:15] up_vect;  

down_vect[lsb_base_expr +: width_expr]  
up_vect[msb_base_expr +: width_expr]  
down_vect[msb_base_expr -: width_expr]  
up_vect[lsb_base_expr -: width_expr]  

The msb_base_expr and lsb_base_expr shall be integer expressions, and the width_expr shall be a positive constant integer expression. Each of these expressions shall be evaluated in a self-determined context. The lsb_base_expr and msb_base_expr can vary at run time. The first two examples select bits starting at the base and ascending the bit range. The number of bits selected is equal to the width expression. The second two examples select bits starting at the base and descending the bit range.

在 VHDL 术语中,这将是一个切片,其边界由高索引确定,宽度由减法确定。

PATTERN_WIDTH 可以是全局静态的(如在通用常量中),也可以是局部静态的(非延迟常量)。 i_count 可变。

取决于In的声明范围 例如:

constant DATAWIDTH:  natural := 8;
signal In_in:        std_logic_vector (31 downto 0);

等效表达式为

if Pattern = In_in(i_count downto i_count - DATAWIDTH - 1) then

请注意,如果切片长度或 i_count 小于 DATAWIDTH - 1,您将收到 运行 时间错误。 - 1 是因为 In_in'RIGHT = 0.

如果不提供 In(或 Pattern)和 DATAWIDTH 的声明,就无法提供更好的答案。它真的很想被重写为 VHDL 友好的。

注意 Lars 指出 in 是保留字(VHDL 在这里不区分大小写)并且名称已更改。