[Verilog][SystemVerilog] $random使用時の失敗
$randomを使うと、テストベンチの修正や変数の追加の際に、
前のランダム値が再現できなくなるということだったので、
では、SystemVerilogはどうなのか見てみました。
コード1
class hoge; rand int temp; endclass: hoge module testbench(); hoge hoge_a = new(); hoge hoge_b = new(); initial begin repeat(10) begin hoge_a.randomize(); hoge_b.randomize(); $display("--- temp_a = %0d ---", hoge_a.temp); $display("--- temp_b = %0d ---", hoge_b.temp); end $finish(2); end endmodule
コード2
class hoge; rand int temp; endclass: hoge module testbench(); hoge hoge_a = new(); hoge hoge_b = new(); initial begin repeat(10) begin hoge_b.randomize(); // ここだけ順序逆 hoge_a.randomize(); $display("--- temp_a = %0d ---", hoge_a.temp); $display("--- temp_b = %0d ---", hoge_b.temp); end $finish(2); end endmodule
コード3
class hoge; rand int temp; endclass: hoge module testbench(); hoge hoge_a = new(); hoge hoge_b = new(); initial begin repeat(10) begin #1; hoge_a.randomize(); $display("--- temp_a = %0d ---", hoge_a.temp); end $finish(2); end initial begin repeat(10) begin #1; hoge_b.randomize(); $display("--- temp_b = %0d ---", hoge_b.temp); end end endmodule
これらのコードすべて、hoge_a.temp, hoge_b.tempの値は
同じ値になるかと思います。SystemVerilog良い。
- 関連記事