繁体   English   中英

声明类型取决于参数的 SystemVerilog 变量

[英]Declare a SystemVerilog variable with type depending on a parameter

我想编写可重用的 SystemVerilog 代码,所以我需要在模块中有一个类型的变量,具体取决于参数(不是不同的总线宽度,而是不同的结构)。 也许有一个参数化结构或不同的结构是可能的,但在这种情况下如何声明变量选择所需的结构作为一种类型。 可以在 generte-if 块中声明不同的结构,但它们对于 it 块来说是本地的,因此外部无法获取它们。 也许类型转换可能会有所帮助。

PS 接口不允许使用和定义(允许使用宏)。

代码示例,我想实现:

typedef struct packed
{
  logic a;
  logic b;
} str_s;

typedef struct packed
{
  logic a;
  logic b;
  logic c;
} str_s_with_c;

// A is a parameter
generate
if (A == "ON")
   str_s_with_c array;

if (A == "OFF")
   str_s array;
endgenerate

assign array.a = 1'b1;

logic t;

assign t = array.a;

或者

generate
if (A == "ON")
begin
   typedef struct packed
   {
     logic a;
     logic b;
   } str_s;
end

if (A == "OFF")
begin
   typedef struct packed
   {
     logic a;
     logic b;
     logic c;
   } str_s;
end
endhenerate

str_s array;

array.a = 1.b1;

通常的做法是将 struct 作为参数类型传递给模块实例。

module inst#(type struct_t=int)();
    struct_t v;  
endmodule

typedef struct packed
{
  logic a;
  logic b;
} str_s;

typedef struct packed
{
  logic a;
  logic b;
  logic c;
} str_s_with_c;

module top;
  inst #(.struct_t(str_s)) inst1();
  inst #(.struct_t(str_s_with_c)) inst2();
endmodule

为单个实例回答下面的评论。 在这种情况下,您可以使用generate块,有条件地实例化模块:

module top #(ON=1)
    typedef struct packed
    {
        logic a;
        logic b;
    } str_s;
    typedef struct packed
    {
        logic a;
        logic b;
        logic c;
    } str_s_with_c;

  if (ON == 1) begin: ON1
    inst #(.struct_t(str_s)) inst1();
  end
  else begin: ON0
    inst #(.struct_t(str_s_with_c)) inst2();
  end
endmodule

一个警告。 您最好在生成语句之外声明结构。 LRM 不会阻止您在内部声明它们,但至少在某些编译器中未实现此功能。

您可以声明局部变量以生成块并使用块名称访问它们。

typedef struct packed
{
  logic a;
  logic b;
} str_s;

typedef struct packed
{
  logic a;
  logic b;
  logic c;
} str_s_with_c;

// A is a parameter
if (A == "ON") begin : bname
   str_s_with_c array;
end 
else if (A == "OFF") begin :  bname
   str_s array;
end

assign bname.array.a = 1'b1;

logic t;

assign t = bname.array.a;

请注意,如果您想访问array.c ,则还必须使用 generate-if 进行保护。

if (A == "ON") begin 
  always @(posedge clk)
    x = bname.array.c;
end 
else if (A == "OFF") begin 
  always @(posedge clk)
     x = something_else;
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM