在 systemverilog 中有没有一种方法可以对类型进行条件处理?

In systemverilog is there a way to condition on a type?

所以我在公共模块中使用参数化类型。

有没有办法说: if( type == TYPE1 ) 以一种方式分配结构 else if( type == TYPE2 ) 分配另一种方式

我在生成块中想象这个。

type() 运算符在 IEEE1800-2012 § 6.23 中描述。来自 LRM 的示例用法:

bit[12:0] A_bus, B_bus;
parameter typebus_t = type(A_bus);
generate
  case(type(bus_t))
    type(bit[12:0]): addfixed_int #(bus_t) (A_bus,B_bus);
    type(real): add_float #(type(A_bus)) (A_bus,B_bus);
  endcase
endgenerate

$typename() IEEE1800-2012 § 20.6.1 中也有描述。 $typename() return s 类型的字符串。来自 LRM 的示例用法:

// source code            // $typename would return
typedef bitnode;          // "bit"
node [2:0] X;             // "bit [2:0]"
int signedY;              // "int"
packageA;
enum{A,B,C=99} X;         // "enum{A=32'sd0,B=32'sd1,C=32'sd99}A::e"
typedef bit[9:1'b1] word; // "A::bit[9:1]" 
endpackage: A
importA::*;
moduletop;
typedef struct{node A,B;} AB_t;
AB_t AB[10];              // "struct{bit A;bit B;}top.AB_t$[0:9]"
...
endmodule

是的,您可以使用类型运算符做一个 generate-if/case,或程序 if/case 如:

real r;

if ( type(r) == type(real) ) ...

但不幸的是,无论条件如何,所有分支中的代码仍然必须成功编译。您将无法引用不存在的结构成员。

  typedef struct {int a;} s1_t;
  typedef struct {int a;int b;} s2_t;
  s1_t s;
 initial
      #1 // procedural-if
    if (type(s) == type(s1_t))
      $display("%m s.a = %0d",s.a);
    else if (type(s) == type(s2_t))
      $display("%m s.b ==%0d",s.b); // this will not compile