其他分享
首页 > 其他分享> > constraint的on与off以及with约束

constraint的on与off以及with约束

作者:互联网

module crv;
  class PCIBus;
    rand bit[15:0] addr;
    rand bit[31:0] data;
    constraint addrw {addr[7:0] == 'h01;}
    constraint dataw {data[15:0] == 'hffff;}
  endclass
  logic [31:0] result;
  initial begin
    PCIBus pBus = new ( ); //Instantiate PCIBus
    $display("\n pBus randomize");
    repeat (2) begin
      pBus.randomize; //randomize pBus
      $display ("addr = %h data = %h", pBus.addr, pBus.data);
    end
    $display("\n pBus randomize with constraint 'addrw' and 'dataw' turned OFF");
    pBus.addrw.constraint_mode(0); //turn OFF constraint
    pBus.dataw.constraint_mode(0); //turn OFF constraint
    repeat (2) begin
      pBus.randomize;
      $display ("addr = %h data = %h", pBus.addr, pBus.data);
    end
    $display("\n pBus randomize with constraint 'addrw' and 'dataw' turned ON");
    pBus.addrw.constraint_mode(1); //turn ON constraint
    pBus.dataw.constraint_mode(1); //turn ON constraint
    repeat (2) begin
      pBus.randomize;
      $display ("addr = %h data = %h", pBus.addr, pBus.data);
    end
    $display("\n constraint 'data' using the 'with' clause");
    repeat (10) begin
      pBus.randomize ( ) with {pBus.data >= 'hffff && pBus.data <= 
'hf_ffff;};
      $display ("data = %h", pBus.data);
    end
  end
endmodule

仿真结果

pBus randomize
addr = 3501 data = ad77ffff
addr = f401 data = 3de0ffff

pBus randomize with constraint 'addrw' and 'dataw' turned OFF
addr = 6f68 data = bef096ce
addr = ff96 data = 7ebf2e4c

pBus randomize with constraint 'addrw' and 'dataw' turned ON
addr = 7f01 data = 4bb7ffff
addr = e401 data = cb1cffff

constraint 'data' using the 'with' clause
data = 0000ffff
data = 0002ffff
data = 0005ffff
data = 000dffff
data = 0001ffff
data = 000effff
data = 000dffff
data = 0003ffff
data = 0001ffff
data = 000bffff
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.500 seconds; Data structure size: 0.0Mb

标签:off,addr,constraint,data,约束,addrw,randomize,pBus
来源: https://www.cnblogs.com/xiangtianxiao/p/16592873.html