SystemVerilog中类的内部类对象的随机化(Randomization of a class object inside a class in SystemVerilog)

编程入门 行业动态 更新时间:2024-10-12 16:24:06
SystemVerilog中类的内部类对象的随机化(Randomization of a class object inside a class in SystemVerilog) 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 endprogram

在这个程序中,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 endprogram

In 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 endclass

This 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.

更多推荐

本文发布于:2023-07-30 16:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338853.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部类   对象   随机化   SystemVerilog   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!