在这个程序中,o1.randomize()的随机化失败了。 如果我为o2采用单独的变量并使用内联约束(如o2.a == o2_local.a),那么它可以工作。
有没有其他方法来进行这种类型的随机化,因为我的原始类包含近38个变量,并且个别分配会很麻烦。
class c2; rand bit[1:0] a; rand bit[1:0] b; function void my_print(); $display("a = %b", a); $display("b = %b", b); endfunction endclass class c1; rand bit[1:0] i; rand bit[1:0] j; rand c2 o2; function new(); o2 = new(); endfunction function void my_print(); $display("i = %b", i); $display("j = %b", j); endfunction endclass program p1; c1 o1 = new; c2 o2_local = new; initial begin if (o1.randomize() with {o2 == o2_local;}) begin o1.my_print(); o2.my_print(); end endprogramIn this program, randomization is getting failed for o1.randomize(). If I take individual variables for o2 and use inline constraint with that (like o2.a == o2_local.a), then it works.
Is there any alternative way to do this type of randomization, as my original class contains almost 38 variable, and individual assignments would be troublesome.
最满意答案
应用于对象时, ==运算符不会执行您认为的操作。 它只比较手柄。 解算器失败,因为它看到o1.o2和o2_local是不同的对象,因此不是“相等”。
如果不应该通过随机化更改o2_local ,那么您可以执行以下操作:
o1.o2 = o2_local; o1.o2.rand_mode(0); if (o1.randomize()) begin o1.print(); o1.o2.print(); end o1.o2.rand_mode(1);这样你就已经为o2分配了o2_local ,如果你有任何引用o2和o1字段的约束,它们就会被解决。
对象等价的问题是所有OOP语言的症状。 您需要一个函数来考虑对象的所有字段以确定两个对象是否相等:
class c2; // ... function bit equals(c2 obj); return a == obj.a && b == obj.b; endfunction endclass此函数在过程代码中工作正常,但它不会帮助您进行随机化,因为在约束中使用函数比较棘手。
一个丑陋的解决方案也是声明一个扩展到equals(...)函数体的宏,因为展开==语句将在约束中起作用。
The == operator when applied to object doesn't do what you think it does. It only compares the handles. The solver is failing because it sees that o1.o2 and o2_local are different objects and thus aren't "equal".
If o2_local isn't supposed to be changed by the randomization then you can just do the following:
o1.o2 = o2_local; o1.o2.rand_mode(0); if (o1.randomize()) begin o1.print(); o1.o2.print(); end o1.o2.rand_mode(1);This way you've assigned o2_local to o2 and if you were to have any constraints that referenced fields of o2 and of o1 they would be solved.
The problem with object equivalence is symptomatic to all OOP languages. You'd need a function that takes all fields of the object into account to determine if two objects are equal:
class c2; // ... function bit equals(c2 obj); return a == obj.a && b == obj.b; endfunction endclassThis function works fine in procedural code, but it won't help you for randomization as using functions in constraints is trickier.
An ugly solution would also be to declare a macro that expands to the body of the equals(...) function, because having the == statements unrolled will work in a constraint.
更多推荐
发布评论