本人是刚接触FPGA的小白,这也是我第一次写文章。如果文中有错误和不恰当的地方,还望大家提出建议和指导。以下就是我对testbench的理解和总结。
对于testbench文件,我是这样理解的:testbench文件实际上也是一个vhdl文件(后缀为.vhd)。我们将设计好的电路模块封装为一个元件,然后在testbench文件例化它,再通过映射的端口产生激励信号。这些激励信号模拟成待测试的电路模块所需要的信号,以此验证我们设计的电路是否符合要求。
testbench的基本格式如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30library ieee; --也可以加入其它的库; use ieee.std_logic_1164.all; entity testbench --testbench是测试文件的名字; end testbench; architecture arch_test of testbench is signal clk : std_logic := '0'; signal rst : std_logic := '1'; ......; signal qout : std_logic_vector(3 downto 0); ......; constant clk_period : time := 20 ns; ......; component sample_name --待测试电路模块的名字; port( clk : in std_logic; rst : in std_logic; ......; qout : out std_logic_vector( 3 downto 0); ......); end component sample_name; begin UUT : sample_name port map(clk,rst,...,qout,...); clk <= not clk after clk_period/2; --生成占空比为50%的时钟; process --产生激励源的模块; begin end process; end arch_test;
(1) testbench文件中对实体的描述:
在testbench文件的实体中,可以省略有关端口的描述。testbench文件名常以待测试的电路模块名加上“tb”命名,例如计数器(文件名为counter)的testbench名可以写为“counter_tb”。
(2) 待测试电路模块的例化:
对于待测试电路模块的例化和VHDL语法中例化的格式是一样的。但是testbench中,最好将映射的端口以信号量(signal)的形式定义,方便我们产生激励信号。另外,我们可以定义一个时间常量(如上面代码中的clk_period)来进行时钟和激励信号的生成。
(3) 用VHDL产生激励信号:
产生占空比为50%的时钟信号:
clk <= not clk after clk_period/2; --以这种形式产生时钟信号,需要在定义clk的时候用“:=”赋初值。
产生占空比为n/(n+m)的时钟信号:
process
begin
clk <= ‘1’;
wait for clk_period/n;
clk <= ‘0’;
wait for clk_period/m;
end process;
产生激励信号:
激励信号可以在一个进程中,用“wait”关键字生成。在testbench中,可综合和不可综合的关键字(语法)都可以使用。没有什么限制,只要产生我们需要的激励信号即可。这里没有什么固定的格式要求。
下面是一个测试文件的简单示例:
模为10的计数器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity cnt_10 is port( clk : in std_logic; reset : in std_logic; dout : out std_logic_vector(3 downto 0)); end cnt_10; architecture mybehave of cnt_10 is signal temp : std_logic_vector(3 downto 0); begin process(clk) begin if reset = '1' then temp <= (others =>'0'); elsif clk'event and clk='1' then if temp ="1001" then temp <= (others =>'0'); else temp <= temp +1; end if; end if; end process; dout <= temp; end architecture mybehave;
计数器的testbench文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity cnt_10_tb is end cnt_10_tb; architecture mybehave of cnt_10_tb is signal clk,reset : std_logic := '0'; signal dout : std_logic_vector(3 downto 0):= "0000"; constant clk_period : time := 20 ns; component CNT_10 port( clk: in std_logic; reset : in std_logic; dout : out std_logic_vector(3 downto 0)); end component CNT_10; begin UUT:CNT_10 port map(clk,reset,dout); clk <= not clk after clk_period/2; process begin wait for clk_period; reset <= '1'; wait for clk_period; reset <= '0'; wait; end process; end architecture mybehave;
最后
以上就是乐观嚓茶最近收集整理的关于VHDL的TestBench仿真的全部内容,更多相关VHDL内容请搜索靠谱客的其他文章。
发表评论 取消回复