system verilog 路科验证每周练习第2210期部分代码讲解

1.sv_dyn_que_array_random.sv

package data_pkg;
  typedef struct {
    rand bit [31:0] addr;
    rand bit [31:0] data;
  } data_pair_t;
  class packet;
    rand data_pair_t pair_arr[];//动态数组不需要new
    constraint cstr {
      pair_arr.size() inside {[10:15]};
      foreach(pair_arr[i]) {
        pair_arr[i].addr inside {['h20 : 'h40]};
        pair_arr[i].addr[1:0] == 0 ;
        ^(pair_arr[i].data) == 1;//让自身的1 个数为奇
        //xored(pair_arr[i].data) == 1;//问题:为什么这句不行?
      }
    }
    function bit xored(bit[31:0] d);
      return ^d;
    endfunction
  endclass

endpackage

module tb;
  import data_pkg::*;
  packet p;
  initial begin
    p = new();
    void'(p.randomize() with {
                              pair_arr.size() == 12;
                              foreach(pair_arr[i]) {
                                pair_arr[i].data[31:8] == 0;
                              }
                            });
    $finish();
  end
endmodule
 

2.sv_constraint_inheritance.sv

package data_pkg;
  class packet;
    rand bit[31:0] addr;
    rand bit[31:0] data;
    constraint cstr {
      addr inside {['h20: 'h40]};
      soft addr[1:0] == 0;
      soft data inside {['h50: 'h60]};
    }
  endclass

  class user_packet extends packet;
    bit bad_packet;
    function new();
      bad_packet = 0;
    endfunction
    constraint cstr {//会覆盖掉父类的,使其无效,哪怕p = up(理论应该调用父类本身)、up.cstr.constraint_mode(0),也还是用子类的cstr
      if(bad_packet) 
        addr[0] == 1;
    }
  endclass
endpackage

module tb;
  import data_pkg::*;
  user_packet up;
  packet p;
  initial begin
    up = new();
    up.bad_packet = 1;
    up.cstr.constraint_mode(0);
    if(!up.randomize())
      $error("UP RANDOMIZATION FAILRUE!!");
    p = up;
    if(!p.randomize())
      $error("P RANDOMIZATION FAILRUE!!");
    $finish();
  end
endmodule


你可能感兴趣的:(system,verilog,fpga开发)