systemverilog-内嵌约束randomize()with{}、soft 约束、local::

内嵌约束
SV允许使用 randomize()with{} 来增加额外的约束,这和在类里增加约束是等效的。
当没有soft修饰时,约束发生冲突则报错;当soft修饰时,则以外部约束的优先级高于内部

class chnl_generator extends uvm_component;
    rand int pkt_id = 0;
    rand int ch_id = -1;
    rand int data_nidles = -1;
    rand int pkt_nidles = -1;
    rand int data_size = -1;
    rand int ntrans = 10;

    constraint cstr{
      soft ch_id == -1;
      soft pkt_id == 0;
      soft data_size == -1;
      soft data_nidles == -1;
      soft pkt_nidles == -1;
      soft ntrans == 10;
    }
    场景1:
    task send_trans();
    chnl_trans req;
    req = chnl_trans::type_id::create("req");
      assert(req.randomize with {local::ch_id >= 0 -> ch_id == local::ch_id; 
                                 local::pkt_id >= 0 -> pkt_id == local::pkt_id;
                                 local::data_nidles >= 0 -> data_nidles == local::data_nidles;
                                 local::pkt_nidles >= 0 -> pkt_nidles == local::pkt_nidles;
                                 local::data_size >0 -> data.size() == local::data_size; 
     endtask
     场景2:
     task send_trans( int pkt_id, int ch_id, int data_nidles, int pkt_nidles, int data_size, int ntrans );
    chnl_trans req;
    req = chnl_trans::type_id::create("req");
      assert(req.randomize with {local::ch_id >= 0 -> ch_id == local::ch_id; 
                                 local::pkt_id >= 0 -> pkt_id == local::pkt_id;
                                 local::data_nidles >= 0 -> data_nidles == local::data_nidles;
                                 local::pkt_nidles >= 0 -> pkt_nidles == local::pkt_nidles;
                                 local::data_size >0 -> data.size() == local::data_size; 
     endtask
endclass

data表示变量

  • 当task没有传递参数时,local::data表示chnl_generator::data
  • 当task有传递参数时,local::data表示任务传递进来的参数
    local:: 表示“域”,而不是句柄,可以用local::this表示调用randmize() 函数的对象句柄。
    约束体with{约束}中的变量名查找顺序默认是从被随机化对象开始查找。如果调用randomize()函数局部域中也有同名变量,那就需要使用 local:: 来显式声明该变量来源于外部函数,而非被随机化的对象。

参考: https://blog.csdn.net/lbt_dvshare/article/details/107280692
  • 6
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在SystemVerilog中,虽然它是一种硬件描述语言,但实际上可以和Python结合使用。这主要是因为SystemVerilog是一种功能强大的语言,可以用于设计和验证硬件。Python是一种通用的编程语言,具有强大的文本处理、数据分析和自动化能力。 在SystemVerilog中使用Python的主要目的是为了辅助设计和验证任务。例如,可以使用Python来处理和分析仿真输出文件,从中提取和验证关键参数。此外,Python还可以用于生成测试用例或配置文件,以便自动化测试流程。 要在SystemVerilog中使用Python,一种常见的方法是使用外部调用(External Call)的方式。通过使用SystemVerilog中的$system函数或$systemtask任务,可以从SystemVerilog中调用外部Python脚本,然后将结果返回到SystemVerilog中。 此外,还可以使用Verilog PLI(Programming Language Interface)来实现SystemVerilog和Python之间的交互。通过使用PLI,可以在SystemVerilog代码中嵌入Python函数,并通过PLI接口和SystemVerilog进行通信。 另外,Python还有一些库和工具可以与SystemVerilog配合使用。例如,可以使用pyvpi库来与Verilog的VPI(Verilog Procedural Interface)进行交互,使用cocotb库来编写Python测试框架,从而实现SystemVerilog代码的验证等等。 总而言之,SystemVerilog和Python的结合可以为硬件设计和验证流程提供更多的便利和灵活性。通过使用Python的文本处理、数据分析和自动化能力,可以提高设计和验证的效率,并实现更复杂的功能。 ### 回答2: System Verilog本身并不支持直接使用Python编程语言,因为它是一种硬件描述语言,主要用于硬件设计和验证。然而,在System Verilog中可以通过SVPython接口与Python进行交互,从而利用Python的强大功能。 SVPython接口是一种跨语言的接口,通过它,我们可以从System Verilog代码中调用Python函数,并传递参数或者获取返回值。这为System Verilog的用户提供了更多灵活性和功能扩展的可能性。 使用SVPython接口,我们可以在System Verilog中调用Python的标准库函数,如math、string等,或者调用第三方库进行数据处理、图像处理、机器学习等。例如,如果需要对System Verilog中的数据进行复杂运算或统计,可以调用Python的math库函数来完成。 另外,Python在机器学习和人工智能领域非常流行和强大。我们可以使用Python编写机器学习模型,并通过SVPython接口将其集成到System Verilog的验证环境中。这样,我们可以利用机器学习技术来进行高级分析、自动化测试和优化。 总结来说,虽然System Verilog本身不支持直接使用Python,但通过SVPython接口,我们可以在System Verilog中调用Python的函数和库,从而利用Python的强大功能来增强System Verilog的设计和验证能力。这种集成可以提高开发效率,拓宽了System Verilog的应用范围。 ### 回答3: 在SystemVerilog中使用Python可以通过使用PLI(Programming Language Interface)实现。PLI是SystemVerilog提供的一种机制,允许开发者使用其他语言(如C++、Python等)与SystemVerilog交互。 首先,需要在SystemVerilog代码中添加`import "DPI-C" function`语句,以导入Python的函数。然后,可以使用`$dlopen(<library>)`函数加载Python库,并使用`$dlsym(<library>, <symbol>)`函数连接Python函数。 接下来,可以定义一个SystemVerilog的函数,并将其与Python函数进行绑定。如下所示: ```systemverilog import "DPI-C" function void my_python_function(); import "DPI-C" context python_function = begin initial $dlopen("my_python_module.so"); my_python_function = $dlsym("my_python_function"); end module my_module; initial begin my_python_function(); end endmodule ``` 在Python中,首先需要定义一个用于SystemVerilog调用的函数,并将其编译为动态链接库(.so文件)。可以使用`ctypes`库加载SystemVerilog中的函数,并与Python函数进行关联。 下面是一个Python的示例代码: ```python from ctypes import * # 定义SystemVerilog调用的函数 def my_python_function(): print("Hello from Python!") # 将Python函数编译为动态链接库 lib = CDLL("my_python_module.so") lib.my_python_function.restype = None lib.my_python_function.argtypes = [] # 将SystemVerilog函数与Python函数进行关联 lib.my_python_function() ``` 以上就是在SystemVerilog中使用Python的基本方法。通过PLI,可以实现SystemVerilog和Python之间的数据交互与函数调用。然而,需要注意的是,PLI通常与特定的编译器和操作系统密切相关,可能需要针对不同的平台进行调整和配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值