我正在尝试对必须发送到 DUT 的一些随机数据包(我可以应用于固定数据包)应用约束

I am trying to apply constraints on some random packets (I could apply to fixed packets) that I have to send to DUT

下面的代码有两个 classes - packet 和 packet_1; packet class 具有长度和模式属性,packet class 具有长度和模式所需的约束。在 packet_1 class 中,我想创建 30 个这样的数据包,并想对 10 个随机数据包应用长度 = 6 的约束,我可以为一个特定的(非随机的)数据包做到这一点,请告诉我如果这是正确的方法,如何处理 10 个随机数据包?

以下代码生成了 30 个数据包,其中第 4 个数据包的长度为 6 并符合其他约束条件,但是如何对 1 个随机数据包(不是固定的第 4 个数据包)或 30 个中的 10 个随机数据包做同样的事情。

 class packet;
 rand bit[1:0] mode;
 rand int length;
 int num;
 constraint mod_len {mode==0 -> length inside {[1:5]};
                  mode==1 -> length inside {[6:10]};
                  mode==2 -> length inside {[11:15]};
                  mode==3 -> length inside {[16:20]};};
 endclass

 class packet_1;  
  packet p1;
  task pack(); 
  p1=new;
  p1.num=0;

  repeat(30)
  begin 
  p1.num++;
  if(p1.num==4)
    p1.randomize with {(p1.length==6);} ;
  else
  p1.randomize ;
  $display("packet is %p",p1);
  end
  endtask
 endclass


  module a;
  packet_1 p2;
  initial 
  begin
  p2=new();
  p2.pack();
  end
  endmodule

如果您想要确切的特定数量的数据包,那么我们可以使用一个 queue/array 来存储要存储的约束值的 随机索引

class packet_1;  
   packet p1;
   int NUM_CONSTRAINED_PKTS=10; // Number of pkts to be constrained
   int NUM_TOTAL_PKTS=30; // total number of pkts to be generated
   rand int q[$];
   constraint c1{
     q.size == NUM_CONSTRAINED_PKTS; // random indices = total constrained pkts generated
     foreach(q[i]){q[i]>=0; q[i]<NUM_TOTAL_PKTS;} // indices from 0-total_pkts
       unique {q}; // unique indices
   };
  task pack(); 
    p1=new;
    p1.num=0;
    this.randomize(); // randomize indices in q
    for(int i=0;i<NUM_TOTAL_PKTS;i++) begin 
      if(i inside {q}) begin // check if index matches
        p1.randomize with {(p1.length==6);} ;
        p1.num++; // debug purpose
      end
      else
        p1.randomize ;    
      $display("packet is %p",p1);
    end
    if(p1.num != NUM_CONSTRAINED_PKTS) $error("Error: p1.num=%0d",p1.num); // 10 pkts with constrained value
    else $display("Pass!!");
  endtask
endclass

另一种方法是在 length 变量上使用 dist 运算符和一些胶合逻辑在输出中生成 weighted distribution .