SystemVerilog - 使用枚举可以有一个范围吗?

SystemVerilog - With an enum can you have a range?

使用 typedef 枚举,您可以为未分配的值设置一个范围吗?例如:

typedef bit [3:0] enum {BLUE = 4'h0, RED = 4'h1, OTHERS = 4'h2 to 4'hF};

或者类似的东西?如果用户选择了一个未分配的值,会发生什么情况?

枚举中不能有范围。但是,其余的取决于您将如何使用它。例如,在 case 语句中:

case(sel)
  BLUE: do-blue-function;
  RED: do-red-function;
  default: do-other-function;
endcase

System-Verilog 中的枚举值可以超出范围。如果您选择 四态 类型作为 基本类型 ,这在使用枚举描述 FSM 时特别有用,例如:

//           a four-state base type (with default value 3'bxxx)
//               |
//               |      the base value of IDLE will be 3'b000
//               |        |
//               V        V
typedef enum logic[2:0] {IDLE, GO1, GO2} state_type;
state_type state;

logic 的默认类型是 x,因此默认情况下上面的变量 state 将具有值 3'bxxx。因此,通过使用四状态基本类型,我们可以对未初始化状态建模,检查 FSM 是否已正确重置。

因此,您要求 "What will happen if an user choose a value that is unassigned also?" 变量将具有该值,并且在 Serge 的 case 示例中,in 将不等于任何已定义的枚举值。