randomize 制約 with

randomize with

classの中の変数をランダム生成する場合にrandomize()を実行します。randomize()の後にwithを追加し制約を記述することができます。

記述例

ランダマイズ実行時に制約としてa00>b00 と b00>c00をwithで設定しています。

class rand_with ;
  rand bit [7:0] a00, b00, c00 ;
endclass

module test () ;
  rand_with classs00 ;
 
  initial begin 
    class00 = new() ;
    for (int iii=0;iii<10;iii++) begin 
      class00.randomize() with {a00>b00; b00>c00;} ;
      $display("a00=%3d b00=%3d c00=%3d",class00.a00,class00.b00,class00.c00) ;
    end 
  end 
endmodule 

実行結果

randomize() withの後ろに設定したa00>b00 と b00>c00の制約通りの結果となりました。

a00=210 b00=199 c00= 20
a00=254 b00=182 c00= 84
a00=175 b00=160 c00= 25
a00=229 b00=194 c00=125
a00=236 b00=101 c00= 21
a00=193 b00= 68 c00=  6
a00=124 b00=106 c00=100
a00=147 b00=127 c00=114
a00=108 b00=102 c00= 16
a00=220 b00=217 c00= 95

複数制約

randomize() withの後には複数の制約を同時に記載することもできます。

記述例

a00にdistで重み設定、b00に”->”でa00が200より大きい場合はb00=10、c00にはif文でb00=10のときはc00=111と制約を付加した例です。

class rand_with ;
  rand bit [7:0] a00, b00, c00 ;
endclass

module test () ;
  rand_with classs00 ;
 
  initial begin 
    class00 = new() ;
    for (int iii=0;iii<10;iii++) begin 
      class00.randomize() with { a00 dist{[0:200]/1,250:=1} ;
                                 a00>200 -> b00==10 ;
                                 if(b00==10) c000==111;  } ;
      $display("a00=%3d b00=%3d c00=%3d",class00.a00,class00.b00,class00.c00) ;
    end 
  end 
endmodule 

実行結果

a00= 67 b00=165 c00=  7
a00=250 b00= 10 c00=111
a00=250 b00= 10 c00=111
a00=160 b00=178 c00=194
a00=135 b00=241 c00=  9
a00= 55 b00=  2 c00=243
a00=135 b00= 78 c00=164
a00= 39 b00=226 c00=138
a00= 57 b00= 13 c00= 11
a00= 73 b00= 52 c00=144

コメント

Copied title and URL