SystemVerilog-Clocking

在SystemVerilog中引入时钟块是为了解决在写testbench时对于特定时序和同步处理的要求而设计的。

时钟块是在一个特定的时钟上的一系列同步的信号,它基本上能够将testbench中与时序相关的结构、函数和过程块分开,能够帮助设计人员根据transaction 和 cycle完善testbench,时钟块只能在module、interface或program中声明。

First Example

这里有一个简单的示例,描述了SystemVerilog中的Clocking如何工作,这是一个可置位的二进制计数器

module COUNTER (input Clock, Reset, Enable, Load, UpDn,input [7:0] Data, output reg[7:0] Q);always @(posedge Clock or posedge Reset)if (Reset)Q <= 0;elseif (Enable)if (Load)Q <= Data;elseif (UpDn)Q <= Q + 1;elseQ <= Q - 1;
endmodule

未采用时钟块计数器的testbench如下:

module Test_Counter;timeunit 1ns;reg Clock = 0, Reset, Enable, Load, UpDn;reg [7:0] Data;wire [7:0] Q;reg OK;// Clock generatoralwaysbegin#5 Clock = 1;#5 Clock = 0;end// Test stimulusinitialbeginEnable = 0;Load = 0;UpDn = 1;Reset = 1;#10; // Should be resetReset = 0;#10; // Should do nothing - not enabledEnable = 1;    #20; // Should count up to 2UpDn = 0;#40; // Should count downto 254UpDn = 1;// etc. ...end// Instance the device-under-testCOUNTER G1 (Clock, Reset, Enable, Load, UpDn, Data, Q);// Check the resultsinitialbeginOK = 1;#9;if (Q !== 8'b00000000)OK = 0;#10;if (Q !== 8'b00000000)OK = 0;#20;if (Q !== 8'b00000010)OK = 0;#40;if (Q !== 8'b11111110)OK = 0;// etc. ...end
endmodule

采用了时钟块设计的testbench如下:

module Test_Counter_w_clocking;timeunit 1ns;reg Clock = 0, Reset, Enable, Load, UpDn;reg [7:0] Data;wire [7:0] Q;// Clock generatoralwaysbegin#5 Clock = 1;#5 Clock = 0;end// Test programprogram test_counter;// SystemVerilog "clocking block"// Clocking outputs are DUT inputs and vice versadefault clocking cb_counter @(posedge Clock);default input #1step output #4;output negedge Reset;output Enable, Load, UpDn, Data;input Q;endclocking// Apply the test stimulusinitial begin// Set all inputs at the beginning    Enable = 0;            Load = 0;UpDn = 1;Reset = 1;// Will be applied on negedge of clock!##1 cb_counter.Reset  <= 0;// Will be applied 4ns after the clock!##1 cb_counter.Enable <= 1;##2 cb_counter.UpDn   <= 0;##4 cb_counter.UpDn   <= 1;// etc. ...      end// Check the results - could combine with stimulus blockinitial begin##1   // Sampled 1ps (or whatever the precision is) before posedge clock##1 assert (cb_counter.Q == 8'b00000000);##1 assert (cb_counter.Q == 8'b00000000);##2 assert (cb_counter.Q == 8'b00000010);##4 assert (cb_counter.Q == 8'b11111110);// etc. ...      end// Simulation stops automatically when both initials have been completedendprogram// Instance the counterCOUNTER G1 (Clock, Reset, Enable, Load, UpDn, Data, Q);// Instance the test program - not required, because program will be// instanced implicitly.// test_COUNTER T1 ();
endmodule

值得注意的是,在这个testbench模块中,时钟块嵌套在程序中(这样设计testbench的好处能够在Program article这篇文章中完全找到),程序块在不采用module和interface时也能嵌套,因此在多人合作开发的程序中可以共享局部设计( This way multiple co-operating programs can share variables local to the scope? 需要重新理解),无端口的嵌套程序或者顶层设计不需要(显示)实例化(而是隐式实例化,它的模块名同设计中的模块名一样)。

时钟块即是声明同时也是对声明的实例化,需要注意的一点是,在testbench中的时钟块的信号方向与testbench中的方向是相对的,因此Q是计数器的输出,是时钟块的输入,还有一点是时钟块中不需要定义宽度,只标明方向。

cb_counter时钟块中的信号与Clock的上升沿同步,默认情况下输入有 #1step的偏斜(skew),输出有4ns的时钟偏斜,时钟偏斜决定了从时钟触发开始到某个信号被采样或驱动的时间长度,输入信号偏斜为负(通常指时钟采样前的一段时间),输出偏斜通常指比采样时钟后的那一段时间。

输入时钟偏斜为 #1step 表示时钟的有效沿读取的值始终是紧接相应时钟沿之前的信号的最后一个值,step表示的是时间精度。

##运算符在testbench中用于延迟特定时钟沿或时钟周期单位。

Clocking Block Drives

时钟块的输出口和双向口能够在特定的时钟事件触发时和能够以特定的时钟偏斜驱动到其相应的信号上,要注意的一点是,驱动器不会改变时钟块双向口的输入。这是因为读取输入始终会产生最后的采样值,而不是驱动值。

同步信号驱动被视为非阻塞赋值,如果在同一仿真时间将多个同步驱动信号连接到同一时钟块的输入或输出上,就会运行错误,4态口会设为X,双态口会设为0。

如下是一些用时钟块的驱动信号的实例:

cb.Data[2:0] <= 3'h2;    // Drive 3-bit slice of Data in current cycle
##1 cb.Data <= 8'hz;         // Wait 1 Clk cycle and then drive Data
##2 cb.Data[1] <= 1;         // Wait 2 cycles, then drive bit 1 of Data
cb.Data <= ##1 Int_Data; // Remember the value of Int_Data, and then drive Data 1 Clk cycle later
cb.Data[7:4] <= 4'b0101;
cb.Data[7:4] <= 4'b0011; // Error: driven value of Data[7:4] is 4’b0xx1

Clocking Blocks and Interfaces

以下是一个用interface实现的多个时钟块,一个时钟块能够用一个interface去减少testbench中用于表示连接的代码。

在下面的testbench中可以看到(如 modport TestR),interface 的信号的方向与时钟块中指定的方向相同,而从DUT(如 modort Ram)看,接口信号方向相反,testbench中时钟块的信号方向是现对于testbench的,当modport声明能够用于描述任意方向(如: testbench or DUT),为了说明这一点,我们将实现两条总线,他们具有不同时钟,并且testbench与顶层分开,testbench作为program实现。

// Interface definitions
interface DataBus (input Clock);logic [7:0] Addr, Data;modport TestR (inout Addr, inout Data);modport Ram (inout Addr, inout Data);
endinterfaceinterface CtrlBus (input Clock);logic RWn;// RWn is output, as it is in the clocking blockmodport TestR (output RWn);// RWn is input, reversed than in the clocking blockmodport Ram (input RWn);
endinterface// Testbench defined as a program, with two clocking blocks
program TestRAM (DataBus.TestR DataInt,CtrlBus.TestR CtrlInt);clocking cb1 @(posedge DataInt.Clock);inout #5ns DataInt.Data;inout #2ns DataInt.Addr;endclockingclocking cb2 @(posedge CtrlInt.Clock);   output #10;output RWn = CtrlInt.RWn;  //  Hierarchical expressionendclockinginitial begincb2.RWn = 0;cb1.DataInt.Data = 1;...end
endprogrammodule RAM (DataBus.Ram DataInt, CtrlBus.Ram CtrlInt);logic [7:0] mem[0:255];always @*if (CtrlInt.RWn)DataInt.Data = mem[DataInt.Addr];elsemem[DataInt.Addr] = DataInt.Data;
endmodulemodule Top;logic Clk1, Clk2;// Instance the interfacesDataBus TheDataBus(.Clock(Clk1));CtrlBus TheCtrlBus(.Clock(Clk2));RAM TheRAM (.DataBus.Ram(TheDataBus.Ram),.CtrlBus.Ram(TheCtrlBus.Ram)); // Connect themTestRAM TheTest (.DataBus.TestR(TheDataBus.TestR),.CtrlBus.TestR(TheCtrlBus.TestR));
endmodule

Clocking block events

可以通过时钟块名称直接访问时钟块的时钟事件,如 @(cb) 等于@(posedge clk).可以通过用 时钟块名字和 (.) 操作符俩访问时钟块的各个信号,所有的event都会同步到时钟块。

以下是同步语句的一些示例:

// Wait for the next change of Data signal from the cb clocking block
@(cb.Data);// Wait for positive edge of signal cb.Ack
@(posedge cb.Ack);// Wait for posedge of signal cb.Ack or negedge of cb.Req
@(posedge cb.Ack or negedge cb.Req);// Wait for the next change of bit 2 of cb.Data
@(cb.Data[2]);
// Wait for the next change of the specified slice
@(cb.Data[7:5]);

更多关于时钟块结构能够在 Accellera SystemVerilog LRM, section 15找到。

SystemVerilog-Clocking相关推荐

  1. SystemVerilog与功能验证

    目录 一.功能验证流程 二.验证手段.验证技术.验证方法学 三.数据类型与编程结构 四.并发进程与进程同步 五.面向对象编程 六.虚接口 七.随机测试 八.继承与多态 九.功能覆盖率 十.断言 一.功 ...

  2. SystemVerilog学习总结

    因为选择的offer是IC验证,以前学的是设计,所以只能再学systemverilog(SV)和UVM了.在学习SV和UVM之前,我认为需要学习以下几个部分:数字电路.c++和verilog.因为IC ...

  3. SystemVerilog LRM 学习笔记 -- SV Scheduler仿真调度

    1. 为什么要理解scheduler? SystemVerilog是HDVL语言,相较与Verilog,除了面向HW design应用,也为了提高verif的效率.所以其仿真调度算法在向下兼容Veri ...

  4. SystemVerilog调度机制与一些现象的思考

    文章内容主要来自于以下文档,然后对自己平时遇到的一些现象作出了思考. 1.IEEE systemverilog.std.1800-2012 2.SystemVerilog Event Regions ...

  5. Systemverilog的一个牛人总结

    Systemverilog 数据类型 l       合并数组和非合并数组 1)合并数组: 存储方式是连续的,中间没有闲置空间. 例如,32bit的寄存器,可以看成是4个8bit的数据,或者也可以看成 ...

  6. 转一篇Systemverilog的一个牛人总结

    Systemverilog 数据类型 l 合并数组和非合并数组 1)合并数组: 存储方式是连续的,中间没有闲置空间. 例如,32bit的寄存器,可以看成是4个8bit的数据,或者也可以看成是1个32b ...

  7. SystemVerilog: 仿真验证知识点点滴滴

    目录 1. 软件世界和硬件世界 2. 什么是package 3. import pkg::*是将pkg的所有都导入进来吗? 4. import vs include 5. 仿真时间和无限死循环 6. ...

  8. Verilog与SystemVerilog编程陷阱:怎样避免101个常犯的编码错误

    这篇是计算机类的优质预售推荐>>>><Verilog与SystemVerilog编程陷阱:怎样避免101个常犯的编码错误> 编辑推荐 纠错式学习,从"陷阱 ...

  9. 通过Clocking Wizard定制和生成一个IP核(MMCM)(Virtex7)(ISE版)

    目录 定制过程 准备进入定制页面 第一页 Clocking features 第二页 第三页 Selecting Optional Ports 第四页 第五页 第六页 定制过程 准备进入定制页面 首先 ...

最新文章

  1. Transformers2.0让你三行代码调用语言模型,兼容TF2.0和PyTorch
  2. 皮一皮:中国好男友!!!
  3. Python中[::-1]的意义
  4. 聊一聊mongodb中的 explain 和 hint
  5. Azure Services Platform
  6. redistemplate使用_SpringBoot 使用 Redis 缓存
  7. 中国送餐行业市场供需与战略研究报告
  8. 从零单排PAT1015,1016,1017,1018
  9. c语言 16进制编辑器,十六进制编辑器(010 Editor 32位)
  10. STM32官方标准固件库下载及介绍
  11. 怎么改java游戏_jar游戏按键修改,怎么修改jar游戏按键
  12. 如何发布一个BT种子文件
  13. python写出租车计费系统_用VHDL设计出租车计费系统
  14. UE4脸部捕捉关键函数使用
  15. 睡眠障碍,正在“杀死”3亿中国人
  16. 内核代码之SIP ALG分析
  17. sympy 求微分方程_Sympy常用函数总结
  18. 多元线性方程的几种解法
  19. C语言求解一元二次方程组的代码
  20. vue如何在一个工程里判断h5还是pc,(利用在一个页面显示不同router-view内容原理,本文只使用于单页面项目,多页面项目请查看我另一篇博文)

热门文章

  1. 3、《每天5分钟玩转Docker容器技术》学习-Docker架构
  2. 架构师带你实时解读微服务架构改造案例:天气预报系统的架构设计
  3. 支撑Java内存模型的基础原理 西安尚学堂
  4. ffmpeg视频模糊处理,降噪处理
  5. 千帆过尽,只想循着一颗心
  6. 程序化交易的本质是什么?
  7. 简单介绍JS与JSP的区别
  8. nserror 自定义错误_iOS NSError错误code对照表
  9. 数据结构之栈 篇四——栈应用实现进制转换
  10. 良心安利食物 美食免抠元素素材网站