【问题标题】:cyclic randomization for a group of variables in SystemVerilogSystemVerilog中一组变量的循环随机化
【发布时间】:2016-06-29 17:06:59
【问题描述】:

我正在尝试以循环方式随机化系统 verilog 中的 3 个不同变量。我的意思是,我有以下 3 个变量

rand int a;
rand int b;
rand int c;

constraint c_a{
  a inside {1,2};     
}
constraint c_b{
  b inside {1,2,3,4,5,6};
}
constraint c_c{
  c inside {1,2,3}
}

有了上述约束,所有 3 个变量的组合共有 36 种 (2x6x3)。

但是如果我们运行一个 36 的循环,像这样:

repeat(36) begin
  this.randomize(a,b,c);
  $display("%d   %d   %d", a,b,c);
end

我们不会击中所有可能的组合,因为某些组合可能会重复。因此,我正在寻找一种方法,通过准确地运行循环 36 次来达到所有这些组合。

我通过声明另一个 rand 变量来表示每个组合并像这样使用 randc 来编写一个蛮力方法来做到这一点:

int a;
int b;
int c;
randc int k;

constraint c_k{
  k inside {[1:36]};
}

repeat(36) begin
  this.randomize(k);
  // randomizing variable 'a' to one of the 2 values.
  if(k<9)
    a = 1;
  else
    a = 2;
  // randomizing variable 'b' to one of the 6 values.
  case(k)
    1,2,3,19,20,21 : b = 1;
    4,5,6,22,23,24 : b = 2;
    7,8,9,25,26,27 : b = 3;
    //
    //   finishing the sequence
    // 
  endcase  

  case(k)
     // similar case statement for the final variable
  endcase

  $display("%d, %d, %d", a,b,c);
end

上述方式效果很好,但对我来说,这似乎是一种忙碌的方式(也不能应用于大型组合),因此想知道是否有更优雅的方法来实现这一点。

感谢您的帮助。

【问题讨论】:

    标签: constraints verilog system-verilog


    【解决方案1】:

    你可以做的是将你的变量连接到一个打包的结构中,并使其成为一个 randc 变量。

    module top;
    class A;
        typedef struct packed {
        bit [1:0]   a;
        bit [2:0]   b;
        bit [1:0]   c;
        } abc_t;
    randc abc_t k;
    constraint c_a{
      k.a inside {1,2};     
    }
    constraint c_b{
      k.b inside {1,2,3,4,5,6};
    }
    constraint c_c{
      k.c inside {1,2,3};
    }
    endclass
       A h = new;
       initial 
         repeat(40) begin
           h.randomize();
           $display("%0p",h.k);
         end
    endmodule
    

    请注意,randc 变量允许的总位数可能会受到模拟器的限制

    【讨论】:

    • 谢谢戴夫。在尝试这个时,我发现我使用的模拟器最多允许 32 位或仅 1 'int' 类型。这似乎很小。我明白您为什么将数据类型从“int”(在我的示例中)更改为“bit”。
    • 想想你需要什么来处理6位的循环随机性。您基本上必须绘制出所有可能的解决方案并随机选择其中一个。约束求解器必须做类似的事情,并且随着位数的增加,问题呈指数增长。解决这个问题的任何其他方法都需要知道可能解决方案的确切数量,而随着约束变得越来越复杂,这变得非常困难。
    猜你喜欢
    • 2012-10-29
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    相关资源
    最近更新 更多