SystemVerilog constraints:约束求解的基本模型
约束是随机化的“规则集”。求解器在所有约束同时成立的前提下为 rand 变量赋值。理解约束的关系运算符与优先级,是写出可满足约束的前提。
1. 约束表达式
1 | constraint c { |
约束支持 inside、dist、->、算术与逻辑运算。
2. 求解模型
求解器把约束转换为可满足性问题,为每个 rand 变量找一组值。约束冲突时 randomize 返回 0。
3. 优先级与软硬约束
硬约束必须满足;soft 约束可被更强的硬约束覆盖。
1 | constraint c { soft len == 4; } |
4. 常见误区
- 约束互相矛盾:randomize 恒失败。
- 忘记约束优先级:soft 被误当硬约束。
- 在约束中使用非 rand 变量做边界:合法但不会随机。
小结
约束是“必须同时成立的规则”。写完后先做一次 randomize 检查可满足性,再扩大范围。
参考文献:ChipVerify SystemVerilog Tutorial — SystemVerilog Constraints 章节(https://chipverify.com/systemverilog/systemverilog-constraints)。本文为原创中文讲解。