亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Verilog

掌握 Verilog Timescale:时间单位、精度与精准仿真时序

Verilog 仿真依赖于时间的定义,因为仿真器需要知道 #1 在时间上代表什么。`timescale 编译指令指定后续模块的时间单位和精度。

语法

 `timescale <time_unit>/<time_precision> // 示例 `timescale 1ns/1ps `timescale 10us/100ns `timescale 10ns/1ns 

time_unit 是延迟和仿真时间的度量单位,time_precision 指定延迟值在仿真中使用前的舍入精度。

使用以下 timescale 结构可在同一设计中使用不同时间单位。记住设计中的延迟规范不可综合,无法转换为硬件逻辑。

字符单位
s秒
ms毫秒
us微秒
ns纳秒
ps皮秒
fs飞秒

这些规范中的整数只能是 1、10 或 100,字符串单位可取上表任意值。

示例 1:1ns/1ns

 // time_unit=1ns,time_precision=1ns `timescale 1ns/1ns module tb; reg val; initial begin val <= 0; #1 $display ("T=%0t At time #1", $realtime); val <= 1; #0.49 $display ("T=%0t At time #0.49", $realtime); val <= 0; #0.50 $display ("T=%0t At time #0.50", $realtime); val <= 1; #0.51 $display ("T=%0t At time #0.51", $realtime); val <= 0; #5 $display ("T=%0t End of simulation", $realtime); end endmodule 

第一个延迟 #1 使仿真器等待恰好 1 个时间单位(1ns)。第二个 #0.49 小于半个时间单位,但精度为 1ns,仿真器无法表示更小值,将其舍入为 0ns,故第二个延迟未推进仿真时间。

第三个 #0.5 正好半个时间单位,仿真器舍入为 #1,即一个完整时间单位,故在 T=2ns 打印。第四个 #0.51 大于半个时间单位,同样舍入为 1 个时间单位,在 T=3ns 打印。

仿真日志

 ncsim> run T=1 At time #1 T=1 At time #0.49 T=2 At time #0.50 T=3 At time #0.51 T=8 End of simulation ncsim: *W,RNQUIE: Simulation is complete. 

仿真运行 8ns 符合预期,但波形在每纳秒间无更小分度,因精度与时间单位相同。

掌握 Verilog Timescale:时间单位、精度与精准仿真时序

示例 2:10ns/1ns

唯一变化是 timescale 从 1ns/1ns 改为 10ns/1ns。时间单位 10ns,精度 1ns。

 `timescale 10ns/1ns module tb; reg val; initial begin val <= 0; #1 $display ("T=%0t At time #1", $realtime); val <= 1; #0.49 $display ("T=%0t At time #0.49", $realtime); val <= 0; #0.50 $display ("T=%0t At time #0.50", $realtime); val <= 1; #0.51 $display ("T=%0t At time #0.51", $realtime); val <= 0; #5 $display ("T=%0t End of simulation", $realtime); end endmodule 

实际仿真时间 = # 指定延迟 × 时间单位,再按精度舍入。第一个延迟得 10ns,第二个 0.49×10=4.9ns 舍入为 5ns,总时间 15ns。第三个加 0.5×10=5ns 总时间 20ns。第四个加 0.51×10=5.1ns 舍入 5ns 总时间 25ns。

仿真日志

 ncsim> run T=10 At time #1 T=15 At time #0.49 T=20 At time #0.50 T=25 At time #0.51 T=75 End of simulation ncsim: *W,RNQUIE: Simulation is complete. 

波形基准单位为十纳秒,精度 1ns。

掌握 Verilog Timescale:时间单位、精度与精准仿真时序

示例 3:1ns/1ps

timescale 改为 1ns/1ps。时间单位 1ns,精度 1ps。

 `timescale 1ns/1ps module tb; reg val; initial begin val <= 0; #1 $display ("T=%0t At time #1", $realtime); val <= 1; #0.49 $display ("T=%0t At time #0.49", $realtime); val <= 0; #0.50 $display ("T=%0t At time #0.50", $realtime); val <= 1; #0.51 $display ("T=%0t At time #0.51", $realtime); val <= 0; #5 $display ("T=%0t End of simulation", $realtime); end endmodule 

时间单位缩放匹配新精度 1ps。注意时间以最小分辨率皮秒表示。

仿真日志

 ncsim> run T=1000 At time #1 T=1490 At time #0.49 T=1990 At time #0.50 T=2500 At time #0.51 T=7500 End of simulation ncsim: *W,RNQUIE: Simulation is complete. 

掌握 Verilog Timescale:时间单位、精度与精准仿真时序

Verilog

  1. Verilog 入门:数字电路设计完整教程
  2. 掌握 Verilog 拼接操作:实用指南
  3. 深入理解 Verilog 赋值语句:类型、语法与最佳实践
  4. Verilog阻塞与非阻塞赋值:关键区别详解
  5. 掌握Verilog函数:高效RTL设计的复用利器
  6. 理解Verilog任务:函数vs任务、语法与用法全解析
  7. 理解 Verilog 调度语义:时序、事件与执行顺序
  8. Verilog 时钟发生器设计与实现指南
  9. 掌握 Verilog 数学函数:高效硬件设计的利器
  10. 掌握 Verilog $timeformat:精度控制与显示格式化
  11. 理解与打印 Verilog 任意作用域下的 Timescale
  12. 掌握 Verilog 文件 I/O:打开、读取、写入和关闭