讲个SystemVerilog disable语句的坑

2024-09-01 13:20

本文主要是介绍讲个SystemVerilog disable语句的坑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

记录个使用SystemVerilog disable语句时遇到的坑,这个坑有点反直觉,以至于我当时有点不信,觉得可能是EDA仿真工具的问题。后来查看了SystemVerilog手册和使用不同EDA工具进行验证,才慢慢接受了。结论是:SystemVerilog disable block_name或task时,会把hierarchy一致的block_name或task的线程都停掉。

正文

为了阐述这个问题,先看以下代码例子:

class disable_class extends uvm_object;int gcnt = 10;function new(string name = "disable_class");super.new(name);endfunction : newtask abc();begin : thread1$display("%m %s time[%0t] thread1 begin new", get_name(), $time);#100ns$display("%m %s time[%0t] thread1 end new", get_name(), $time);endendtasktask main();forkbegin : thread1$display("%m %s time[%0t] thread1 begin", get_name(), $time);#6ns;$display("%m %s time[%0t] thread1 end", get_name(), $time);endbegin : thread2#gcnt;disable thread1;$display("%s time[%0t] thread2 end, %m", get_name(), $time);endabc();join$display("%s time[%0t] main task end", get_name(), $time);endtask : mainendclass : disable_class

disable_class类中main task()使用fork…join启动了3个线程:

  • thread1为延迟6ns退出;
  • thread2延迟gcnt时间后,使用disable把thread1停掉,至于thread2有没有机会把thread1停掉,得看变量gcnt的值。如果gcnt小于6ns,那么在执行到disable thread1语句时,thread1早就结束了。如果gcnt大于6ns,那么thread2还是可以把thread1停掉的;
  • thread3调用task abc(),abc()任务里面定义了名为thread1的block块,延迟100ns后退出;

测试代码如下:

disable_class dc1 = new("dc1");
disable_class dc2 = new("dc2");
dc1.gcnt = 2;
dc2.gcnt = 8;
forkdc1.main();dc2.main();begin : thread1$display("m example time[%0t] thread1 begin", $time);#20ns$display("%m example time[%0t] thread1 end", $time);end
join

disable_class类例化了两次,使用dc1和dc2句柄指向它们。dc1句柄的gcnt为2,dc2句柄的gcnt为8。然后使用fork启动了三个线程,前两个线程分别调用了dc1和dc2的main() task。第三个定义了名称thread1的block块,延迟20ns后退出。

仿真结果如下:

example_pkg::disable_class.main.thread1 dc1 time[0.000ns] thread1 begin
example_pkg::disable_class.abc.thread1 dc1 time[0.000ns] thread1 begin new
example_pkg::disable_class.main.thread1 dc2 time[0.000ns] thread1 begin
example_pkg::disable_class.abc.thread1 dc2 time[0.000ns] thread1 begin new
example_pkg::example_agent.run_phase.thread1 example time[0.000ns] thread1 begin
dc1 time[2.000ns] thread2 end, example_pkg::disable_class.main.thread2
dc2 time[8.000ns] thread2 end, example_pkg::disable_class.main.thread2
example_pkg::example_agent.run_phase.thread1 example time[20.000ns] thread1 end
example_pkg::disable_class.abc.thread1 dc1 time[100.000ns] thread1 end new
dc1 time[100.000ns] main task end
example_pkg::disable_class.abc.thread1 dc2 time[100.000ns] thread1 end new
dc2 time[100.000ns] main task end

在0ns时,dc1的main()/abc() task、dc2的main()/abc() task和测试代码thread1其开始执行并打印出响应消息。

在2ns时,dc1的thread2结束,根据disable_class类可知,这时候thread1被disable了,因此dc1的thread1的“$display("%m %s time[%0t] thread1 end", get_name(), $time);”代码无法被执行到。这里就有个疑问了:dc2的thread1是否也会被disable吗?答案是也会被disable。因此仿真结果中,虽然dc1 thread2执行了disable thread1,但dc2中thread1也无法打印出”thread1 end”相关的消息了。这里就是本文的重点了,在$display打印中我们用%m,把thread1的hierarchy也打印出来,我们可以发现,dc1和dc2虽然是两个不同的句柄,但是它们thread1的层次还是一模一样的,都是example_pkg::disable_class.main.thread1。SystemVerilog disable block_name的方式会把所有hierarchy一致的block_name都停掉的,故而我们可以看到20ns和100ns的example_pkg::example_agent.run_phase.thread1和example_pkg::disable_class.abc.thread1都没有被disable掉(因为虽然它们block_name都是thread1,但是它们hierarchy不一致的)。

总结

SystemVerilog disable block_name或task时,会把hierarchy一致的block_name或task的线程都停掉。不管一个class例化多少次,这些句柄内部的block_name和task的hierarchy还是一样的,因此只要其中任何一个句柄调用了disable block_name或task,那么其它句柄的对应线程也会被disable掉。这些行为如果我们提前知道,并就想利用这个特性做达成某些功能倒还好,但如果提前不了解,可能会产生很多意想不到的错误行为。其实为了精准的控制线程状态,推荐大家可以用process的内置方法(self(), status(), kill(), await()等等)来进行。

这篇关于讲个SystemVerilog disable语句的坑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1127049

相关文章

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

python之流程控制语句match-case详解

《python之流程控制语句match-case详解》:本文主要介绍python之流程控制语句match-case使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录match-case 语法详解与实战一、基础值匹配(类似 switch-case)二、数据结构解构匹

grom设置全局日志实现执行并打印sql语句

《grom设置全局日志实现执行并打印sql语句》本文主要介绍了grom设置全局日志实现执行并打印sql语句,包括设置日志级别、实现自定义Logger接口以及如何使用GORM的默认logger,通过这些... 目录gorm中的自定义日志gorm中日志的其他操作日志级别Debug自定义 Loggergorm中的

在MySQL执行UPDATE语句时遇到的错误1175的解决方案

《在MySQL执行UPDATE语句时遇到的错误1175的解决方案》MySQL安全更新模式(SafeUpdateMode)限制了UPDATE和DELETE操作,要求使用WHERE子句时必须基于主键或索引... mysql 中遇到的 Error Code: 1175 是由于启用了 安全更新模式(Safe Upd

封装MySQL操作时Where条件语句的组织

在对数据库进行封装的过程中,条件语句应该是相对难以处理的,毕竟条件语句太过于多样性。 条件语句大致分为以下几种: 1、单一条件,比如:where id = 1; 2、多个条件,相互间关系统一。比如:where id > 10 and age > 20 and score < 60; 3、多个条件,相互间关系不统一。比如:where (id > 10 OR age > 20) AND sco

【Python知识宝库】上下文管理器与with语句:资源管理的优雅方式

🎬 鸽芷咕:个人主页  🔥 个人专栏: 《C++干货基地》《粉丝福利》 ⛺️生活的理想,就是为了理想的生活! 文章目录 前言一、什么是上下文管理器?二、上下文管理器的实现三、使用内置上下文管理器四、使用`contextlib`模块五、总结 前言 在Python编程中,资源管理是一个重要的主题,尤其是在处理文件、网络连接和数据库

FPGA开发:条件语句 × 循环语句

条件语句 if_else语句 if_else语句,用来判断是否满足所给定的条件,根据判断的结果(真或假)决定执行给出的两种操作之一。 if(表达式)语句; 例如: if(a>b) out1=int1; if(表达式)         语句1; else         语句2; 例如: if(a>b)out1=int1;elseout1=int2; if(表达式1) 语句1; els

mysql 修改表结构语句

主要还是要参考mysql的官方网站 http://dev.mysql.com/doc/refman/5.7/en/alter-table.html 简单例子: alter table_name alter column old_column_name new_column_name int unsigned;

MySQL学习笔记-join语句类型

join从句的类型:内链接(inner) 全外连接(full outer) 左外连接(left outer) 右外连接(right outer) 交叉链接(cross) 连接条件:使用ON设定连接条件,也可以用WHERE代替 · ON:设定连接条件 · WHERE:进行结果集记录的过滤 一,内连接inner join:  内连接是返回左表及右表符合连接条件的记录,在MySQL中JO

Oracle和Sql_Server 部分sql语句的区别

比如:A表中, 字段:gxmlflag  number;  比如数据:20210115 字段:gxmldate date ;    比如数据:2021-01-15 09:50:50 一、在Oracle数据库中: 1、insert 和 update 语句: t.gxmlflag = to_char(sysdate,'yyyymmdd'),t.gxmldate=sysdate 比如:update f