VHDL与VerilogHDL的Testbench模板
一般而言,一个testbench需要包含的部分如下:
(1)VHDL:entity 和 architecture的声明;Verilog:module declaration
(2)信号声明
(3)实例化待测试文件
(4)提供仿真激励
其中第(4)步是关键所在,需要完成产生时钟信号,以及提供激励信号两个任务。
VHDL Testbench中产生时钟信号的两种方法
首先要在信号声明部分,定义一个constant如下:
constant clk_period:TIME:=10ns;
方法一
clk<= not clk after clk_period/2;
方法二
process
begin
wait for clk_period/2;
clk<='1';
wait for clk_period/2;
clk<='0';
end process;
其次激励信号生成语法也利用wait for语句产生即可
process
begin
rst_n<='0';
en<='0';
wait for(clk_period*30);
rst_n<='1';
wait for(clk_period*30);
en<='0';
wait;
end process;
Testbench模板
首先写了一个简单二分频电路作为待测试文件如下:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity two_Divider is
port(
clk : in std_logic;
rst_n : in std_logic;
clkout : out std_logic
);
end entity two_Divider;
architecture behav of two_Divider is
signal clkout_temp : std_logic:='0';
begin
process(clk,rst_n)
begin
if(rst_n='0')then
clkout_temp<='0';
elsif(clk'event and clk='1')then
clkout_temp<=not clkout_temp;
end if ;
clkout<=clkout_temp;
end process;
end behav;
这里为了验证之前两种产生时钟的方法,特生成了两个时钟,编写testbench如下:
library ieee;
use ieee.std_logic_1164.all;
entity two_Divider_tb is
end entity;
architecture behav of two_Divider_tb is
component two_Divider
port(
clk : in std_logic;
rst_n : in std_logic;
clkout : out std_logic
);
end component;
signal clk : std_logic:='0'; --初始化,否则仿真结果中可能出现高阻态
signal rst_n : std_logic;
signal clkout : std_logic;
signal clk1 : std_logic:='0';
constant clk_p1:TIME:=10ns;
constant clk_p2:TIME:=10ns;
begin
--实例化待测试文件
instant:two_Divider port map(clk=>clk ,rst_n=>rst_n,clkout=>clkout);
--方法一产生时钟
clk1<= not clk1 after clk_p2/2;
--方法二产生时钟
process
begin
wait for clk_p1/2;
clk<='1';
wait for clk_p1/2;
clk<='0';
end process;
--提供激励信号
process
begin
rst_n<='0';
wait for(clk_p1*30);
rst_n<='1';
wait;
end process;
end behav;
仿真结果如下:
与理论分析一致
Verilog编写testbench与VHDL大同小异,附一个testbench如下:
`timescale 1ns/1pstestbench
module my_tb;
reg datain,clk,rst,clk1; //对DUT而言,输入定义为reg型
wire q; //对DUT而言,输出定义为wire型
parameter clock_period=10;
parameter clock_period1=20;
DUT u1(.datain(datain),.rst(rst),.clk(clk), .q(q));//调用待测试程序
initial //产生时钟方法一
begin
clk1 = 1'b1;
forever clk1=#(clock_period1/2) ~clk1;
end
initial clk = 1'b1; //产生时钟方法一
always #(clock_period/2)clk=~clk;
//初始化,激励信号产生
initial
begin
datain = 1'b0;
rst = 1'b0;
#(clock_period*10) rst = 1'b1;
#(clock_period*10) datain = 1'b1;
end
endmodule
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/134029.html