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

理解与打印 Verilog 任意作用域下的 Timescale

默认 timescale

虽然 Verilog 模块期望在模块前定义 timescale,但仿真器可能插入默认 timescale。Verilog 精细化层次中任意作用域的实际 timescale 可用系统任务 $printtimescale 打印,接受作用域作为参数。

 module tb; initial begin // 打印此模块的 timescale $printtimescale(tb); // $printtimescale($root); end endmodule 

可见即使模块前未放置 timescale 指令,仿真器仍应用了 1ns/1ns 的默认值。

仿真日志

 xcelium> run Time scale of (tb) is 1ns / 1ns xmsim: *W,RNQUIE: Simulation is complete. 

标准 timescale 作用域

默认情况下,文件中的 timescale 指令应用于其后所有模块,直到遇到下一个 timescale 指令。

 `timescale 1ns/1ps module tb; des m_des(); alu m_alu(); initial begin $printtimescale(tb); $printtimescale(tb.m_alu); $printtimescale(tb.m_des); end endmodule module alu; endmodule `timescale 1ns/10ps module des; endmodule 

上例中,tb 和 alu 最终 timescale 为 1ns/1ps,而 des 因指令在其定义前被替换,得到 1ns/10ps。

仿真日志

 xcelium> run Time scale of (tb) is 1ns / 1ps Time scale of (tb.m_alu) is 1ns / 1ps Time scale of (tb.m_des) is 1ns / 10ps xmsim: *W,RNQUIE: Simulation is complete. 

跨 Verilog 文件的作用域

其他文件可通过 `include 指令包含进当前文件,这是预处理指令,使编译器在编译前将被包含文件内容放入主文件。相当于直接将整个文件内容粘贴至此。

 // main.v `timescale 1ns/1ps module tb; des m_des(); alu m_alu(); initial begin $printtimescale(tb); $printtimescale(tb.m_alu); $printtimescale(tb.m_des); end endmodule `include "file_alu.v" `include "file_des.v" // file_alu.v module alu; endmodule // file_des.v `timescale 1ns/10ps module des; endmodule 

结果与前例完全相同。alu 得到 1ns/1ps,因它是在找到 alu 定义前最后有效的指令,尽管放在不同文件。des 得到 1ns/10ps,因指令在其定义前被替换。

仿真日志

 xcelium> run Time scale of (tb) is 1ns / 1ps Time scale of (tb.m_alu) is 1ns / 1ps Time scale of (tb.m_des) is 1ns / 10ps xmsim: *W,RNQUIE: Simulation is complete. 

交换文件顺序可改变 timescale

文件包含顺序对 timescale 指令重定义起关键作用,下例即证。

 // main.v `timescale 1ns/1ps module tb; des m_des(); alu m_alu(); initial begin $printtimescale(tb); $printtimescale(tb.m_alu); $printtimescale(tb.m_des); end endmodule // 注意!交换包含顺序 `include "file_des.v" `include "file_alu.v" // file_alu.v module alu; endmodule // file_des.v `timescale 1ns/10ps module des; endmodule 

现在模块 alu 得到 timescale 1ns/10ps。

仿真日志

 xcelium> run Time scale of (tb) is 1ns / 1ps Time scale of (tb.m_alu) is 1ns / 10ps Time scale of (tb.m_des) is 1ns / 10ps xmsim: *W,RNQUIE: Simulation is complete. 

这是在文件顶部放置 timescale 指令的原因之一,使该文件所有模块无论文件包含顺序如何都假设正确 timescale。

但这可能导致难以在不修改每个文件的情况下用不同精度编译。许多编译器和仿真器也提供覆盖默认 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 文件 I/O:打开、读取、写入和关闭
  12. Verilog 入门指南:Hello World 示例