if-elseを用いることでrandomizeによる乱数生成において、条件分岐による制約(constraint)を記述することが出来ます。
複数制約の重ね掛けも可能という点はimplication(->)を用いた制約と使い方が類似しているようにも思います。
if-elseを用いた制約例
if-elseの記述においてbegin-endではなく、{ }で囲む点に使用上の注意が必要です。
if-elseを用いることで、複数の制約を条件分岐で使い分けることが可能となります。
条件分岐制約:SystemVerilog
class Transaction;
rand bit x;
rand bit [(8-1):0] y;
rand bit [(8-1):0] z;
// random constraint
constraint c1 {
if (x == 0) {
y == 0;
z inside {0};
} else {
(y > 100) & (y < 200);
z inside {[100:200]};
}
}
endclass
Transaction tr = new();
initial begin
repeat(10) begin
if(!tr.randomize()) $finish;
$display("x : %d, y : %d, z : %d", tr.x, tr.y, tr.z);
end
$finish();
end
条件分岐制約:実行結果
if (x == 0) {
y == 0;
z inside {0};
} else {
(y > 100) & (y < 200);
z inside {[100:200]};
}
----------------------------
x : 0, y : 0, z : 0
x : 1, y : 123, z : 198
x : 0, y : 0, z : 0
x : 0, y : 0, z : 0
x : 1, y : 124, z : 147
x : 1, y : 162, z : 193
x : 1, y : 114, z : 141
x : 0, y : 0, z : 0
x : 1, y : 171, z : 124
x : 0, y : 0, z : 0
- xが0のとき、y, zは0
- xが1のとき、100<y<200で100<z<200