如何写VHDL的test bench文件

来源:互联网 发布:微霸软件效果 编辑:程序博客网 时间:2024/05/21 07:51

      最近项目上要用到FPGA,之前用的一直是verilog,后面换成了VHDL。对ISE一窍不通啊,研究了一些testbench文件的编写,record一下。

       借用一下博文http://hi.baidu.com/lovelink/item/ff34ce9b12f45988581461ac的话。

       首先对TESTBENCH作一个形象一些的比喻吧,它就象是一个面包板(做过电路实验吧),他对外没有任何接口,但它要向要插在他上面的器件提供接口,这样才能正确的插入,还有它必须对插在它上面的器件提供正常的信号。当然在它上面还必须要有这个器件。这时就完成了一个TESTBENCH。应该大概明白了其中的意思了吧。

       好了,根据上面的比喻我们可以非常明确的知道一个TESTBENCH要写一些什么东西,首先它对外无接口,所以它的实体部分是空的。在它上面要有相应的器件,所以在它的结构体中要申明我们要测试的器件,也就是component的申明。还有就是它要对器件提供接口,所以它的结构体应该提供一些信号,并且要对这些信号进行正确的测试赋值。当然还要进行一些插入工作,就是信号的对应工作。这样一个TESTBENCH就完成了。原理很简单的,应该很容易明白。不过在真正的测试中可能不会用太多的这种方式吧,应该会选用测试向量吧,这个的准确性更高一些。不过怎么样写测试向量,这到是一个有大学问的东西,因为当我们的管脚很多的时候,测试的向量数目是要心指数增长的,当然不可能把所有的情况都测试完成了,只有是测试其中的一部分,这儿怎么样写出有代表性的一组测试向量是很有学问的,应该说是研究的热点吧。

      几个testbench要用到的重要语句:

    (1)wait:无限等待,表示永远挂起,对于汉语wait语句的进程来说,进程在一开始执行一次后面就不执行了;

    (2)wait on 信号表:敏感信号等待语句,等待敏感信号表中的信号发生变化才执行;

    (3)wait until 表达式:条件等待语句,当条件表达式中所含的信号发生了变化,并为true时,进程才脱离等待状态;

    (4)wait for 时间表达式:此语句中声明了一个时间段,从从执行到当前的wait语句开始,只要这个时间段内,进程处于等待状态,超过这段时间,进程自动恢复执行该等待语句的下一条语句。wait for 5ns

下面贴一下我的代码

 
 
 
--自己写的加法器LIBRARY ieee;USE ieee.std_logic_1164.ALL;  library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity adder isport(a,b:in std_logic;  co,so:outstd_logic);end adder;architecture Behavioral of adder isbeginco<=a and b;so<=a xor b;end Behavioral;
 
 
--为了练习例化语句,我又外包了一层框架,代码如下:library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity test isport(ain,bin:IN std_logic;sum,cout:out std_logic);end test;architecture Behavioral of test is   component adderport(a,b:in std_logic;  co,so:outstd_logic);end component;begin   u1: adder port map(a=>ain,b=>bin,so=>sum,co=>cout);end Behavioral;
 
 
 
 
 --下面是系统生成的testbench文件,当然我改了一下

LIBRARY ieee;USE ieee.std_logic_1164.ALL; -- Uncomment the following library declaration if using-- arithmetic functions with Signed or Unsigned values--USE ieee.numeric_std.ALL; ENTITY adder_test ISEND adder_test; ARCHITECTURE behavior OF adder_test IS      -- Component Declaration for the Unit Under Test (UUT)     COMPONENT test    PORT(         ain : IN  std_logic;         bin : IN  std_logic;         sum : OUT  std_logic;         cout : OUT  std_logic        );    END COMPONENT;   

   --Inputs   signal ain : std_logic := '0';   signal bin : std_logic := '0';

  --Outputs   signal sum : std_logic;   signal cout : std_logic;   -- No clocks detected in port list. Replace <clock> below with    -- appropriate port name  --  -- constant clk_period  : time := 10 ns; BEGIN  -- Instantiate the Unit Under Test (UUT)   uut: test PORT MAP (          ain => ain,          bin => bin,          sum => sum,          cout => cout        );

   -- Clock process definitions

 

   -- Stimulus process ain_gen:process     begin        ain<='0';        wait for 30 ns;        ain<='1';    wait for 100 ns;    ain<='0';       wait;     end process;      bin_gen:process     begin        bin<='0';        wait for 100 ns;        bin<='1';       wait for 200 ns;       bin<='0';          wait;     end process;     

END;

 

 

 

 

 


	
				
		
原创粉丝点击