如何为一位随机变量定义约束?

问题描述 投票:0回答:1

在我的情况下,如果某个位的值为“1”,那么我的约束对“1”的权重会更高,如果该位为“0”,那么我的约束对于“0”将具有更高的权重。如何约束呢?

我得到这段代码的语法错误

rand bit value;
bit x; // Has either 1 or 0 depending on external signal
constraint constraint_c { value dist { x := 3, ~x := 1};};

请帮帮我。谢谢 :)

constraints system-verilog
1个回答
1
投票

您可以在约束中放入if-else

rand bit value;
bit x;
constraint c {
    if(x)
       value dist {1 := 3, 0 := 1};
    else
       value dist {1 := 1, 0 := 3};
}

你的权重也可以是变量

int weight0, weight1;
 constraint c {
       value dist {1 := weight1, 0 := weight0};

// set before calling randomize
if (x) begin
    weight1 = 3; weight0 =1;
end else begin
    weight1 = 1; weight0 =3;
end

或表达

 constraint c {
           value dist {1 := x?3:1, 0 := x?1:3};
© www.soinside.com 2019 - 2024. All rights reserved.