首页 > 其他分享 >IC验证 -- 1. Verilog Testbench

IC验证 -- 1. Verilog Testbench

时间:2024-03-31 19:12:33浏览次数:13  
标签:-- module design Verilog Testbench input IC rstn DUT

What is a Verilog Testbench ?

A Verilog Testbench is a simulation environment used to verify the functionality and correctness of a digital design described in the Verilog hardware description language (HDL).

The purpose of a testbench is to provide a way to simulate the behavior of the degign under various conditions, inputs and scenarios before actually fabricating the physical hardware. It allows designers to catch bugs, validate functionality, and optimize designs without the cost and time associated with physical prototyping.

Verilog Testbench Components

DUT or Design Under Test is the Verilog module or design that you want to test. It could be a simple component like an adder or a more complex design like a microprocessor.

The testbench itself is implemented as a separate top-level Verilog module. This module is responsible for generating input stimuli for the DUT, capturing its output, and comparing it with expected outputs.

The testbench generates different input patterns and sequences to test different scenarios and edge cases of the design and can be coded using functions and tasks and forms the test stimulus. Some examples are the different input patterns, clock signals, reset signals, and other control signals to test various aspects of the DUT's behavior.

Testbench signals are connected to the ports of the DUT instantiation, and are monitored by different tasks to check design functionality.

Verilog Testbench Example

1. functionality of a latch.

module d_latch (
                input d,
                input en,
                input rstn,
                output reg q
               );

  always @ (en or rstn or d)begin
    if (!rstn) begin
      q <= 0;
    end else begin
      if (en) begin
        q <= d;
      end
    end
  end
endmodule

A Verilog Testbench can be written by the following steps:

1. Declare top-level testbench module

module tb_latch;

    // All testbench code goes inside this module

endmodule

2. Declare signals for DUT connection

The latch design contains 3 inputs and 1 output. Inputs are declared of type reg so that it can be driven from a procedural block such as initial. Outputs are declared as type wire so that it is visible in the testbench module and can be monitored to check design behavior.

reg  d;         // To drive input "d" of the DUT
reg  en;        // To drive input "en" of the DUT
reg  rstn;      // To drive input "rstn" of the DUT
reg  prev_q;    // To ensure q has not changed when en=0

wire q;        // To tap output "q" from DUT

3. Instantiate DUT

Create a module instantiation of the DUT verilog module and connect testbench signals to the DUT ports.

dut    u0(
          .d(d),
          .clk(clk),
          .rstn(rstn),
          .q(q)
         );

4. Initialize testbench variables

Note that all reg variables have an uninitialized value of X and can be initialized to some value inside an initial block.

initial begin
  d    <= 0;
  en   <= 0;
  rstn <= 0;
end 

It can also be written inside a function which can be called inside the initial block. Note that functions cannot have simulation delays using # operator.

function void init();
  d    <= 0;
  en   <= 0;
  rstn <= 0;
endfunction

initial begin
  init();

  #10;  // Wait for 10 time units
end

5. Write test stimulus.

For our case, we have to release reset and drive some random combination of inputs to see what values the design provides on its output port q for each change in input.

task reset_release();
  // 2. Release reset
  #10 rstn <= 1;
endtask

task test_1();
  // 3. Randomly change d and enable
  for (i = 0; i < 5; i = i+1) begin
    delay  = $random;
    delay2 = $random;
    #(delay2) en <= ~en;
    #(delay)  d  <= i;

    // Check output value for given inputs
    checker(d, en, rstn, q);
    prev_q <= q;
  end
endtask

initial begin
  // As shown in step 4
  init();
  reset_release();
  test_1();
end

6. Write checker code

The checker, depending on the complexity of the design can be written in multiple functions and tasks and called at different points in a simulation. For our purposes of a simple design such as a latch in this example, it can be coded entirely in a single function and called just after driving inputs to the design as shown in step 5.

function checker (input d, en, rstn, q);
  #1;
  if (!rstn) begin
    if (q != 0)
        $srror("Q is not 0 during resetn!");
  end else begin
    if (en) begin
        if (q != d)
            $srror("Q does not follow D when EN is high!");
    end else begin
        if (q != prev_q)
            $srror("Q does not get latched!");
    end
  end
endfunction

Hence by running simulations using the testbench, designers can design flaws, validate the functionality of the DUT, and refine the design before moving on to the physical implementation stage. Testbenches are a crucial part of the digital design and verification process, ensuring that the resulting hardware behaves as intended.

See other examples like 4-bit counter, Full Adder, Single Port RAM!

标签:--,module,design,Verilog,Testbench,input,IC,rstn,DUT
From: https://www.cnblogs.com/sys-123456/p/18107100

相关文章

  • Base64编码的全面介绍
    1.Base64的定义和作用Base64是一种用64个字符表示二进制数据的编码方式,通常用于在网络传输中将二进制数据转换为可打印字符的形式。Base64编码后的数据由大小写字母、数字和特殊字符组成,可以安全地在文本协议中传输,同时保留数据的完整性。Base64编码解码|一个覆盖广泛主......
  • 回顾大学本科三年
    现在已经大三,距离毕业也仅仅只有一年多的时间了,相比于我刚刚进入学校时对计算机这门专业的期望来说,其实我不算是真正喜欢计算机这门专业,只是找一门相对好就业的热门专业,我个人认为我比较倾向于的领域是机械制造相关的领域。大学三年,我在计算机专业上学习到的实际上关于生产的知识......
  • 使用sddm出现无法登录的原因及解决方法
    配置文件介绍SDDM的默认配置文件为/usr/lib/sddm/sddm.conf.d/default.conf配置加载配置目录中的所有文件,然后按下面列出的顺序加载配置文件,后者具有最高优先级。应该对本地配置进行更改。/usr/lib/sddm/sddm.conf.d系统配置目录/etc/sddm.conf.d本地配置目录/e......
  • 思科 VRF-Lite
          ......
  • 多态
    多态:静态多态,动态多态静态多态:函数重载、运算符重载动态多态:派生类和虚函数实现运行时多态静态多态与动态多态的区别:静态多态的函数地址早绑定-编译阶段确定函数地址动态多态的函数地址晚绑定-运行阶段确定函数地址多态满足条件:有继承关系子类重写父类的虚函数多态使用条......
  • 南京理工最新突破!基于梯度和频率域的深度超分辨率新方法
    论文一作:Zhengxue Wang(授权) |编辑:3DCV 添加微信:dddvision,备注:立体视觉,拉你入群。文末附行业细分群由南京理工PCALab开发的深度图超分辨率方法SGNet(SGNet:StructureGuidedNetworkviaGradient-FrequencyAwarenessforDepthMapSuper-Resolution),针对仅通过空间域......
  • C生万物之循环结构全面学习<四>
    万水千山总是情,点点关注行不行。一声朋友一生情,点点关注才能行!文章目录1.三种循环结构1_1while循环if和while对比while执行流程实战示例1_2for循环for循环执行流程for循环实战while循环和for循环对比1_3do-while循环do-while循环执行流程2_1break和continue语句wh......
  • 爬虫工作量由小到大的思维转变---<第六十四章 > Scrapy利用Bloom过滤器增强爬虫的页面去
    前言:        网络爬虫系统是信息时代获取和管理网络数据的重要工具,广泛应用于搜索引擎索引、数据聚合、在线研究等领域。随着网络信息的海量增长,爬虫系统不可避免地会面临重复内容的爬取问题。这不仅浪费了计算资源和网络带宽,而且还会降低数据处理的效率,并可能引起网......
  • 第四章页面组件操作题
    1,使用canvas组件实现“奥运五环”的绘制。代码如下wxml:<canvascanvas-id="wuhuan"></canvas>js:Page({onLoad:function(options){constctx=wx.createCanvasContext('wuhuan');//设置五环颜色constcolors=["blue",&quo......
  • Python数据分析九
    一、Python之列表操作方法remove和pop在Python中,列表还提供了其他一些常用的操作方法,例如删除指定元素和弹出(移除并返回)指定位置的元素。其中,remove()方法用于删除列表中第一个匹配的元素,而pop()方法用于弹出指定位置的元素。以下是详细的代码示例:使用remove()方法删除列表中......