vhdl testbench简单例子

QUARTUS10以后altera竟然把自己的simulator去掉了。现在必须用modelsim进行simulation。

查了蛮多资料,这位仁兄写的文章像是人能看懂的。

转过来和大家分享。

==================================================================================================

弄了好长时间vhdl,一直对testbench很迷惑。前几天静下心来好好看了下资料,终于会写简单的testbench了。

 

六进制计数器的代码

[c-sharp]  view plain copy
  1. library ieee;  
  2. use ieee.std_logic_1164.all;  
  3. use ieee.std_logic_arith.all;  
  4. --use ieee.std_logic_unsigned.all;  
  5.   
  6. entity cnt6 is  
  7.   port  
  8.   (clr,en,clk :in std_logic;  
  9.   q  :out  std_logic_vector(2 downto 0)  
  10.   );  
  11. end entity;  
  12.   
  13. architecture rtl of cnt6 is  
  14. signal tmp  :std_logic_vector(2 downto 0);  
  15. begin  
  16.   process(clk)  
  17. --    variable q6:integer;  
  18.     begin  
  19.       if(clk'event and clk='1') then  
  20.         if(clr='0')then  
  21.           tmp<="000";  
  22.         elsif(en='1') then  
  23.           if(tmp="101")then  
  24.             tmp<="000";  
  25.           else  
  26.             tmp<=unsigned(tmp)+'1';  
  27.           end if;  
  28.         end if;  
  29.       end if;  
  30.       q<=tmp;  
  31. --      qa<=q(0);  
  32.  --     qb<=q(1);  
  33.  --     qc<=q(2);  
  34.   end process;  
  35. end rtl;  

 

六进制计数器testbench的代码

[c-sharp]  view plain copy
  1. library ieee;  
  2. use ieee.std_logic_1164.all;  
  3.   
  4. entity cnt6_tb is    
  5. end cnt6_tb;  
  6.   
  7. architecture rtl of cnt6_tb is  
  8.   component cnt6  
  9.     port(  
  10.       clr,en,clk :in std_logic;  
  11.       q  :out  std_logic_vector(2 downto 0)  
  12.       );  
  13.   end component;  
  14.   
  15.   signal clr  :std_logic:='0';  
  16.   signal en   :std_logic:='0';  
  17.   signal clk  :std_logic:='0';  
  18.   signal  q   :std_logic_vector(2 downto 0);  
  19.     
  20.   constant clk_period :time :=20 ns;    
  21.   begin  
  22.     instant:cnt6 port map  
  23.     (  
  24.       clk=>clk,en=>en,clr=>clr,q=>q  
  25.       );  
  26.   clk_gen:process  
  27.   begin      
  28.     wait for clk_period/2;  
  29.     clk<='1';    
  30.     wait for clk_period/2;  
  31.     clk<='0';  
  32.   end process;  
  33.     
  34.   clr_gen:process  
  35.   begin  
  36.     clr<='0';  
  37.     wait for 30 ns;  
  38.     clr<='1';  
  39.     wait;  
  40.   end process;  
  41.       
  42.   en_gen:process  
  43.   begin  
  44.     en<='0';  
  45.     wait for 50ns;  
  46.     en<='1';  
  47.     wait;  
  48.   end process;  
  49. end rtl;  

 

其实testbench也有自己固定的一套格式,总结如下:

[c-sharp]  view plain copy
  1. --测试平台文件(testbench)的基本结构  
  2. library ieee;  
  3. use ieee.std_logic_1164.all;  
  4.   
  5. entity test_bench is      --测试平台文件的空实体(不需要端口定义)  
  6.   
  7. end test_bench;  
  8.   
  9. architecture tb_behavior of test_bench is  
  10.     component entity_under_test         --被测试元件的声明  
  11.         port(  
  12.         list-of-ports-theri-types-and-modes  
  13.         );  
  14.     end component;  
  15.       
  16. begin  
  17.     instantiation:entity_under_test port map  
  18.     (  
  19.         port-associations  
  20.     );  
  21.       
  22.     process()       --产生时钟信号  
  23.     ……  
  24.     end process;  
  25.       
  26.     process()       --产生激励源  
  27.     ……  
  28.     end process;  
  29. end tb_behavior;  
  30.   
  31. -------------------------------------------------------------------  
  32. --简单计数程序源码  
  33. library ieee;  
  34. use ieee.std_logic_1164.all;  
  35. use ieee.std_logic_unsigned.all;  
  36. use ieee.std_logic_unsigned.all;  
  37.   
  38. entity sim_counter is  
  39.     port(  
  40.         clk     :in     std_logic;  
  41.         reset   :in     std_logic;  
  42.         count   :out    std_logic_vector(3 downto 0)  
  43.         );  
  44. end entity;  
  45.   
  46. architecture behavioral of sim_counter is  
  47.   
  48. signal temp :std_logic_vector(3 downto 0);  
  49.   
  50. begin  
  51.     process(clk,reset)  
  52.     begin  
  53.         if reset='1' then  
  54.             temp<="0000";  
  55.         elsif clk'event and clk='1' then  
  56.             temp<=temp+1;  
  57.         end if;  
  58.     end process;  
  59.     count<=temp;  
  60. end behavioral;  
  61.   
  62. -------------------------------------------------------------------  
  63. --简单计数程序,测试文件代码(testbench)  
  64. library ieee;  
  65. use ieee.std_logic_1164.all;  
  66. use ieee.std_logic_unsigned.all;  
  67. use ieee.numeric_std.all;  
  68.   
  69. entity counter_tb_vhd is            --测试平台实体  
  70. end counter_tb_vhd;  
  71.   
  72. architecture behavior of counter_tb_vhd is  
  73.     --被测试元件(DUT)的声明  
  74.     component sim_counter  
  75.     port(  
  76.         clk :in std_logic;  
  77.         reset   :in std_logic;  
  78.         count   :out std_logic_vector(3 downto 0)  
  79.         );  
  80.     end component;  
  81.     --输入信号  
  82.     signal clk:std_logic:='0';  
  83.     signal reset :std_logic:='0';  
  84.     --输出信号  
  85.     signal count    :std_logic_vector(3 downto 0);  
  86.       
  87.     constant clk_period :time   :=20 ns;        --时钟周期的定义  
  88.   
  89. begin  
  90.     dut:sim_counter port map(  
  91.         clk=>clk,reset=>reset,counter=>counter  
  92.         );  
  93.     clk_gen:process  
  94.     begin  
  95.         clk='1';  
  96.         wait for clk_period/2;  
  97.         clk='0';  
  98.         wait for clk_period/2;  
  99.     end process;  
  100.       
  101.     tb:process      --激励信号  
  102.     begin  
  103.         wait for 20 ns;  
  104.         reset<='1';  
  105.         wait for 20 ns;  
  106.         reset<='0';  
  107.         wait for 200 ns;  
  108.         wait;       --will wait forever;  
  109.     end process;  
  110. end;  
  111.   
  112.   
  113. --激励信号的产生方式  
  114. --1.以一定的离散时间间隔产生激励信号的波形  
  115. --2.基于实体的状态产生激励信号,也就是说基于实体的输出响应产生激励信号  
  116.   
  117. --两种常用的复位信号  
  118. --1.周期性的激励信号,如时钟  
  119. --2.时序变化的激励型号,如复位  
  120.   
  121. --eg.产生不对称时钟信号  
  122.     w_clk<='0' after period/4 when w_clk='1' else  
  123.            '1' after 3*period/4 when w_clk='0' else  
  124.            '0';   
  125.              
  126. --eg.产生堆成时钟信号,process语句  
  127. clk_gen1:process  
  128. constan clk_period  := 40 ns;  
  129. begin  
  130.     clk='1';  
  131.     wait for clk_period/2;  
  132.     clk='0';  
  133.     wait for clk_period/2;  
  134. end process;      

 

如果自己不想写这些testbench的这些固定格式,可以在quartus里自动生成testbench文件的模板,然后往里面写信号就行了

步骤:processing->start->start test bench template write

这里需要注意的是要在仿真选项里选择一个仿真工具,然后才会生成testbench

 

自动生成的testbench模板格式如下:

[c-sharp]  view plain copy
  1. -- Copyright (C) 1991-2008 Altera Corporation  
  2. -- Your use of Altera Corporation's design tools, logic functions   
  3. -- and other software and tools, and its AMPP partner logic   
  4. -- functions, and any output files from any of the foregoing   
  5. -- (including device programming or simulation files), and any   
  6. -- associated documentation or information are expressly subject   
  7. -- to the terms and conditions of the Altera Program License   
  8. -- Subscription Agreement, Altera MegaCore Function License   
  9. -- Agreement, or other applicable license agreement, including,   
  10. -- without limitation, that your use is for the sole purpose of   
  11. -- programming logic devices manufactured by Altera and sold by   
  12. -- Altera or its authorized distributors.  Please refer to the   
  13. -- applicable agreement for further details.  
  14.   
  15. -- ***************************************************************************  
  16. -- This file contains a Vhdl test bench template that is freely editable to     
  17. -- suit user's needs .Comments are provided in each section to help the user    
  18. -- fill out necessary details.                                                  
  19. -- ***************************************************************************  
  20. -- Generated on "03/13/2011 20:05:04"  
  21.                                                               
  22. -- Vhdl Test Bench template for design  :  cnt6  
  23. --   
  24. -- Simulation tool : ModelSim (VHDL)  
  25. --   
  26.   
  27. LIBRARY ieee;                                                 
  28. USE ieee.std_logic_1164.all;                                  
  29.   
  30. ENTITY cnt6_vhd_tst IS  
  31. END cnt6_vhd_tst;  
  32. ARCHITECTURE cnt6_arch OF cnt6_vhd_tst IS  
  33. -- constants                                                   
  34. -- signals                                                     
  35. SIGNAL clk : STD_LOGIC;  
  36. SIGNAL clr : STD_LOGIC;  
  37. SIGNAL en : STD_LOGIC;  
  38. SIGNAL q : STD_LOGIC_VECTOR(2 DOWNTO 0);  
  39. COMPONENT cnt6  
  40.     PORT (  
  41.     clk : IN STD_LOGIC;  
  42.     clr : IN STD_LOGIC;  
  43.     en : IN STD_LOGIC;  
  44.     q : OUT STD_LOGIC_VECTOR(2 DOWNTO 0)  
  45.     );  
  46. END COMPONENT;  
  47. BEGIN  
  48.     i1 : cnt6  
  49.     PORT MAP (  
  50. -- list connections between master ports and signals  
  51.     clk => clk,  
  52.     clr => clr,  
  53.     en => en,  
  54.     q => q  
  55.     );  
  56. init : PROCESS                                                 
  57. -- variable declarations                                       
  58. BEGIN                                                          
  59.         -- code that executes only once                        
  60. WAIT;                                                         
  61. END PROCESS init;                                             
  62. always : PROCESS                                                
  63. -- optional sensitivity list                                    
  64. -- (        )                                                   
  65. -- variable declarations                                        
  66. BEGIN                                                           
  67.         -- code executes for every event on sensitivity list    
  68. WAIT;                                                          
  69. END PROCESS always;                                            
  70. END cnt6_arch;  


  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值