(实例解读Testbench编写方法.docxVIP

  • 24
  • 0
  • 约2.7千字
  • 约 5页
  • 2017-02-01 发布于北京
  • 举报
(实例解读Testbench编写方法

实例解读Testbench编写方法前言:在进行quartusII设计时,不少人刚开始觉得仿真极其不方便,还需要编写测试文件,浪费时间精力。不过小编想告诉大家,其实testbench编写很容易学,不外乎wait for 语句的堆叠就可以了,至于高深的用法,对一般程序都涉及不到,没必要弄得那么复杂。下面看看实例吧,将详细说明testbench的编写:我们可以通过Quartus自动生成一个Testbench的模板,选择Processing - Start - Start Test Bench Template Writer,等待完成后打开刚才生成的Testbench,默认是保存在simulation\modelsim文件夹下的.vt格式文件。?打开vt文件后可以看到Quartus已经为我们完成了一些基本工作,包括端口部分的代码和接口变量的声明,我们要做的就是在这个做好的模具里添加我们需要的测试代码。一个最基本的Testbench包含三个部分,信号定义、模块接口和功能代码。‘timescale 1ns/ 1ps表示仿真的单位时间为1ns,精度为1ps。想要进行仿真首先要规定时间单位,而且最好在Testbench里面统一规定时间单位,而不要在工程代码里定义,因为不同的模块如果时间单位不同可能会为仿真带来一些问题,而timescale本身对综合也就是实际电路没有影响。其实Testbench本身可以看做一个模块或者设备(本例中的模块名为add_vlg_tst),和你自己编写的模块进行通信。通过Testbench模块向待测模块输出信号作为激励,同时接收从待测模块输出的信号来查看结果。因此,在待测模块中的reg型信号在Testbench中就变成了wire,待测模块中的wire型信号在Testbench中则对应为reg型。那么inout怎么办呢,inout型信号也要设成wire,同时要用一个reg型信号作为输出寄存器,同时设置一个三态门,由一个使能信号控制,如:assign?inout_sig = out_en ? out_reg : 1’bz;处理完接口和声明之后,需要自己设置一些激励信号,激励信号的内容就是肯能会输入到待测模块中的波形。下面我们就来写一个简单的测试程序。实例:60进制计数器源代码:library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;use ieee.std_logic_unsigned.all;entity cnt60 is port( clk,reset:in std_logic; p:out std_logic_vector(3 downto 0); q:out std_logic_vector(3 downto 0) );end cnt60;architecture arch of cnt60 is signal cp:std_logic;signal m:std_logic_vector(3 downto 0);signal n:std_logic_vector(3 downto 0);begin p_1:process(reset,clk)begin if (reset=1) then m=0000; else if (clkevent and clk=1) then if (m=1001) then m=0000; else m=m+0001; end if; end if; end if; end process p_1;cp=m(3) and m(0);p_2:process(clk,reset)begin if (reset=1) then n=0000;else if(clkevent and clk=1) then if (cp=1) then if (n=0101) then n=0000; else n=n+0001; end if;end if; end if;end if;end process p_2;p=m(3)m(2)m(1)m(0);q=n(3)n(2)n(1)n(0);end arch;Testbench编写:LIBRARY ieee;USE ieee.std_logic_1164.all;ENTITY cnt60_vhd_tst ISEND cnt60_vhd_tst;ARCHITECTURE cnt60_arch OF cnt60_vhd_tst IS-- constants-- signalsSIGNAL clk : STD_LOGIC;SIGNAL p : STD_LOGIC_VECTOR(3 DOWNTO 0);SIGNAL q : STD_LOGIC_VECTOR(

文档评论(0)

1亿VIP精品文档

相关文档