SystemVerilog - 如何在编译时获取枚举类型的数量

SystemVerilog - How to get the number of enumerated types at compile time

我试图找到一种方法来在编译时获取 enum 类型中可能的枚举数。我需要它来初始化使用枚举类型的模板 class。

我很好奇是否有实用函数(或系统任务)提供此功能。它类似于 $size() 但用于枚举类型。但是,我似乎找不到相应的功能。查了很多,好像不行。

这是我正在尝试做的一个例子:

typedef enum {RANDOM, STICKY, SWEEP} bias_t;

// can be parameterized to pick another enum type at random
class enum_picker #(type T = bias_t); //type must be an enumerated type
    local T current_type;
    local const int weights[$size(T)]; //<--- How do I get the number of enumerated types?

    function T pick_type();
        ... some code ...
    endfunction
endclass

所以对于变量weights来说,它是一个权重数组,其大小为枚举类型的个数。现在是 32 因为 $size() 调用但这是错误的;在此特定代码示例中,数组大小应为 3.

有办法吗?或者这在 SystemVerilog 中根本不允许吗?

您可能不想将 weights 设置为 const;您将无法为其设置值。可以使用num()方法获取枚举数。

class enum_picker #(type T = bias_t); //type must be an enumerated type
  local T current_type;
  local int weights[]; 
  function new;
    weights = new[current_type.num()];
    foreach (weights[i]) weights[i] = $urandom_range(10);
  endfunction

    function T pick_type();
       
    endfunction
endclass