当前位置: 首页 > news >正文

Verilog基础:时序调度中的竞争(一)

相关阅读

Verilog基础icon-default.png?t=N7T8https://blog.csdn.net/weixin_45791458/category_12263729.html?spm=1001.2014.3001.5482


        作为一个硬件描述语言,Verilog HDL常常需要使用语句描述并行执行的电路,但其实在仿真器的底层,这些并行执行的语句是有先后顺序的,然而Verilog标准并没有将这些事件调度的顺序定死,而是给予了仿真器厂商一定的自由去实现自己的产品,这就导致了设计者如果不遵循一定的编程习惯,会导致意想不到的仿真结果,下面是一些相关的规则。

1、不要在两个及以上的always或initial结构中对同一个变量赋值

        当两个以上的过程结构同时执行时,它们之间的执行顺序是不一定的,Verilog标准只规定了在一个顺序块(begin end)中的所有语句是按照其先后顺序执行的,但并没有规定同一个时间执行的多个结构的执行顺序,下面举例详细说明。

//例1
`timescale 1ns/1ps
module test();initial $display("The initial_0 execute");initial $display("The initial_1 execute");initial $display("The initial_2 execute");
endmodule

        在上例中,三个initial结构在0ns时同时执行,仿真器执行他们的顺序是不定的,但测试表明,许多仿真器都选择顺序执行这三个initial结构,如下所示。

对于Mentor Modelsim SE,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execute对于Aldec Riviera Pro,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execute对于Cadence Xcelium,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execute对于Mentor Questa,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execute对于Synopsys VCS,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execute对于Icarus Verilog,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execute

         虽然看起来仿真器的行为都是一致的,但其实这只是一个巧合,下面的例子就体现出了各种仿真器对同一段代码之间的不同行为。

//例2
`timescale 1ns/1ps
module test();reg a;initial #5 a = 1;initial @(a) $display("The initial_0 execute");initial @(a) $display("The initial_1 execute");initial @(a) $display("The initial_2 execute");
endmodule

        这段代码的三个initial结构都对事件a敏感,所以在5ns时,三个initial结构同时被触发,而他们的执行顺序是不定的,如下所示。

对于Mentor Modelsim SE,输出结果为
The initial_2 execute
The initial_1 execute
The initial_0 execute对于Aldec Riviera Pro,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execute对于Cadence Xcelium,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execute对于Mentor Questa,输出结果为
The initial_2 execute
The initial_1 execute
The initial_0 execute对于Synopsys VCS,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execut对于Icarus Verilog,输出结果为
The initial_2 execute
The initial_1 execute
The initial_0 execute

        下面的代码中仍然体现了同一时间的多个结构的执行顺序是不定的,即使触发这些结构的事件在同一时间是有前后关系的。

//例3
`timescale 1ns/1ps
module test();reg a, b, c;initial begin#5;a = 1;b = 1;c = 1;endalways @(a) $display("The initial_0 execute");always @(b) $display("The initial_1 execute");always @(c) $display("The initial_2 execute");
endmodule

        在5ns时,a、b、c先后被赋值为1,Verilog标准保证了begin end块中的赋值顺序从上到下。但由这些赋值语句触发的其他always结构呢?是否也会按照a、b、c的顺序执行呢?

对于Mentor Modelsim SE,输出结果为
The initial_2 execute
The initial_1 execute
The initial_0 execute对于Aldec Riviera Pro,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execute对于Cadence Xcelium,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execute对于Mentor Questa,输出结果为
The initial_2 execute
The initial_1 execute
The initial_0 execute对于Synopsys VCS,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execut对于Icarus Verilog,输出结果为
The initial_0 execute
The initial_1 execute
The initial_2 execute

        依然可以注意到不同仿真器之间的差异,为什么不都是按照a赋值,always @(a)执行,b赋值,always @(b)执行,c赋值,always @(c)执行的顺序执行呢?还是那句话,Verilog标准只规定了一个顺序块(begin end)中的所有语句是按照其先后顺序执行的,并没有保证其他行为。当a赋值后,always @(a)被调度了,但他不一定会在b赋值之前执行,有可能在执行完b赋值、c赋值之后再来执行always @(a),此时always @(b)和always @(c)也应该在队列中,他们执行的先后顺序是标准没有保证的(但需要注意的是,最基本的调度者和被调度者的先后关系是无法改变的,即always @(a)一定在a赋值后,b、c亦然)。下面是一个经典的两个结构交错执行的例子。

//例4
`timescale 1ns/1ps
module test();reg a;wire b;initial begin#1;a = 1;#1;a = 0;$display("b is %b", b);endassign b = a;endmodule

         当时间来到1ns,a被赋值为1,同时触发assign结构,对b连续赋值为1。1ns后,a被更改为0,紧接着打印b的值,结果是1还是0呢?

对于Mentor Modelsim SE,输出结果为
b is 1对于Aldec Riviera Pro,输出结果为
b is 1对于Cadence Xcelium,输出结果为
b is 1对于Mentor Questa,输出结果为
b is 1对于Synopsys VCS,输出结果为
b is 0对于Icarus Verilog,输出结果为
b is 0

        上面的结果显示,仿真器可能在执行了a=0的赋值后立刻执行被调度的assign连续赋值,再返回initial结构执行显示语句,也可能继续执行下面的显示语句,再去执行被调度的assign连续赋值。

        这就给了我们一个启示,在改变了一个全组合逻辑的电路的输入后,紧接着显示输出,可能无法得到更新后的输出。一个更好的做法是给$display语句一定的延时,这样就能确保在执行$display前,连续赋值已经执行完毕,因为不同时间的语句执行是不会产生竞争的。

//例5
`timescale 1ns/1ps
module test();reg a;wire b;initial begin#1;a = 1;#1;a = 0;#0.01 $display("b is %b", b);endassign b = a;endmodule

相关文章:

  • ElasticSearch之cat aliases API
  • Redis中文结果查看方式
  • 【Python 千题 —— 基础篇】删除列表值
  • Nginx模块开发之http过滤器filter
  • MySQL面试,MySQL事务,MySQL锁,MySQL集群,主从,MySQL分区,分表,InnoDB
  • 蓝桥杯每日一题2023.11.23
  • 【算法专题】滑动窗口—无重复字符的最长子串
  • Django项目window环境部署
  • Python之Pygame游戏编程详解
  • 音视频项目—基于FFmpeg和SDL的音视频播放器解析(二十一)
  • Missing file libarclite_iphoneos.a 问题解决方案
  • Halcon Solution Guide I basics(4): Blob Analysis(连通性解析)
  • 【Java】认识异常
  • 数据提取PDF SDK的对比推荐
  • Photoshop下载秘籍:附送7款不用下载的在线PS工具!
  • [NodeJS] 关于Buffer
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • Apache Spark Streaming 使用实例
  • DOM的那些事
  • Electron入门介绍
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • golang中接口赋值与方法集
  • java小心机(3)| 浅析finalize()
  • Kibana配置logstash,报表一体化
  • Leetcode 27 Remove Element
  • SQL 难点解决:记录的引用
  • swift基础之_对象 实例方法 对象方法。
  • 使用 QuickBI 搭建酷炫可视化分析
  • 线上 python http server profile 实践
  • 写代码的正确姿势
  • 学习HTTP相关知识笔记
  • 由插件封装引出的一丢丢思考
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • # 透过事物看本质的能力怎么培养?
  • #Linux(make工具和makefile文件以及makefile语法)
  • #pragam once 和 #ifndef 预编译头
  • #pragma 指令
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (27)4.8 习题课
  • (4) PIVOT 和 UPIVOT 的使用
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (TOJ2804)Even? Odd?
  • (动态规划)5. 最长回文子串 java解决
  • (二)测试工具
  • (六)Flink 窗口计算
  • (三)mysql_MYSQL(三)
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (五)activiti-modeler 编辑器初步优化
  • (一) springboot详细介绍
  • (原創) 博客園正式支援VHDL語法著色功能 (SOC) (VHDL)
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • ****Linux下Mysql的安装和配置
  • *算法训练(leetcode)第三十九天 | 115. 不同的子序列、583. 两个字符串的删除操作、72. 编辑距离