gpt4 book ai didi

system-verilog - 在 SystemVerilog 中约束整个对象

转载 作者:行者123 更新时间:2023-12-04 06:47:54 59 4
gpt4 key购买 nike

我试图基于其他一些对象来约束整个对象(不仅仅是对象的字段)。这是我的生产代码的精简版本:

我有以下类(class):

class some_class;
bit[7:0] some_field;
bit[3:0] some_other_field;


// this function would do some complex procedural
// operations on the fields of the object
function void do_some_op();
bit[3:0] tmp = some_field[3:0];
some_field[3:0] = some_other_field;
some_other_field = some_field[7:4];
some_field[7:4] = tmp;
endfunction

function some_class some_function(bit some_param);
some_function = new this;
$display("foo"); // this print here to see that method is executed

if (some_param)
some_function.do_some_op();
endfunction

function void print();
$display("some_field = %x", some_field);
$display("some_other_field = %x", some_other_field);
endfunction
endclass // some_class

这个类包含一些完整的字段。它还有一个方法可以对该类的字段执行一些复杂的程序。在示例中,我对其进行了简化。我还有另一个类,它返回一个已执行操作的新对象。

我有另一个类与 some_class 一起运行实例。根据 Dave 的输入,我让它首先创建对象(因为 randomize() 不创建对象)。
class some_shuffler;
rand bit params[];
rand some_class objects[];

constraint size_c {
params.size() == objects.size() - 1;
params.size() <= 10;
};

constraint shuffle_c {
// not allowed by standard
// foreach (params[i])
// objects[i+1].some_field == objects[i].some_function(params[i]);

foreach (params[i])
objects[i+1].some_field ==
objects[i].some_function(params[i]).some_field &&
objects[i+1].some_other_field ==
objects[i].some_function(params[i]).some_other_field;
};

function new();
objects = new[10]; // create more objects than needed
foreach (objects[i])
objects[i] = new();

// initialize first object
objects[0].some_field = 8'hA5;
endfunction // new

function void post_randomize();
foreach (objects[i]) begin
$display("objects[%0d]:", i);
objects[i].print();
$display("");
end
endfunction

endclass

这个类有两个数组,一个执行的操作和一个中间状态。有一个初始对象。关于这一点, some_function被执行并产生下一个对象。

这是我想测试它的方式:
module top;
import some_pkg::*;

initial begin
static some_shuffler shuffler = new();
bit rand_ok;
rand_ok = shuffler.randomize() with {
params.size() == 1;
};
assert (rand_ok);
end

endmodule

当试图直接约束对象时,我立即得到约束违规。模拟器似乎试图使 2 个 handle 相等。无论如何,这是标准禁止的,我不再这样做了(尽管编译失败会很好)。我已经按照 Dave 和 Greg 的建议解开了约束(我认为做 some_function().some_field 是非标准的,但它在 Questa 中编译)。

即使是现在, foo print 没有出现在命令行上( some_function() 没有被执行)。我看到的是 objects[1]包含初始值(两个字段都为 0)。

我不能只生成参数列表,然后在每次迭代时按程序随机化对象,因为我希望能够将最后一个对象约束为具有特定值 - 基本上为约束求解器提供起点和终点,然后让它找出到达那里的方法。

最佳答案

SystemVerilog 中不允许对象与对象约束,因为它们不是整数类型。见 IEEE Std 1800-2012第 18.3 条:

  • Constraints can be any SystemVerilog expression with variables and constants of integral type (e.g., bit, reg, logic, integer, enum, packed struct).


如果组件是 rand,您可以约束类对象的整体组件。 (例如 obj[1].value == obj[0].value+1; )。

约束中允许函数,但有限制。见 IEEE Std 1800-2012 § 18.5.12 约束中的函数的完整细节。限制包括:
  • 函数不能包含 outputref参数
  • 功能应该是自动的,没有副作用
  • 函数参数具有隐式优先级(例如 x<=F(y) 推断 solve y before x )
  • 循环依赖会导致错误


  • 更新:

    看起来唯一真正被随机化的是 params . some_field 的值和 some_other_field are calculations. So it makes more sense to move the loop for shuffling into the post_randomize` 函数。
    constraint size_c {
    params.size() == objects.size() - 1;
    params.size() <= 10;
    };

    function void postrand_shuffle();
    foreach (params[i])
    objects[i+1] = objects[i].some_function(params[i]);
    endfunction

    function void post_randomize();
    postrand_shuffle();
    // ... your other post_rand code...
    endfunction

    SystemVerilog 的随机约束求解器将在至少有一个解决方案时起作用。但是,当解决方案空间小且难以确定或链很长时,模拟器性能会下降。对于这些场景,最好将一对一的顺序计算移到 post_randomize 中。 .

    关于system-verilog - 在 SystemVerilog 中约束整个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24340246/

    59 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com