集成电路技术分享

 找回密码
 我要注册

QQ登录

只需一步,快速开始

搜索
查看: 1551|回复: 5

VHDL——如何写简单的testbench

[复制链接]
fpga_feixiang 发表于 2017-12-4 10:35:59 | 显示全部楼层 |阅读模式
六进制计数器的代码
[csharp] view plain copy
library ieee;   
use ieee.std_logic_1164.all;   
use ieee.std_logic_arith.all;   
--use ieee.std_logic_unsigned.all;   
   
entity cnt6 is   
  port   
  (clr,en,clk :in std_logic;   
  q  ut  std_logic_vector(2 downto 0)   
  );   
end entity;   
   
architecture rtl of cnt6 is   
signal tmp  :std_logic_vector(2 downto 0);   
begin   
  process(clk)   
--    variable q6:integer;   
    begin   
      if(clk'event and clk='1') then   
        if(clr='0')then   
          tmp<="000";   
        elsif(en='1') then   
          if(tmp="101")then   
            tmp<="000";   
          else   
            tmp<=unsigned(tmp)+'1';   
          end if;   
        end if;   
      end if;   
      q<=tmp;   
--      qa<=q(0);   
--     qb<=q(1);   
--     qc<=q(2);   
  end process;   
end rtl;   

六进制计数器testbench的代码
[csharp] view plain copy
library ieee;   
use ieee.std_logic_1164.all;   
   
entity cnt6_tb is      
end cnt6_tb;   
   
architecture rtl of cnt6_tb is   
  component cnt6   
    port(   
      clr,en,clk :in std_logic;   
      q  ut  std_logic_vector(2 downto 0)   
      );   
  end component;   
   
  signal clr  :std_logic:='0';   
  signal en   :std_logic:='0';   
  signal clk  :std_logic:='0';   
  signal  q   :std_logic_vector(2 downto 0);   
      
  constant clk_period :time :=20 ns;      
  begin   
    instant:cnt6 port map   
    (   
      clk=>clk,en=>en,clr=>clr,q=>q   
      );   
  clk_gen:process   
  begin        
    wait for clk_period/2;   
    clk<='1';      
    wait for clk_period/2;   
    clk<='0';   
  end process;   
      
  clr_gen:process   
  begin   
    clr<='0';   
    wait for 30 ns;   
    clr<='1';   
    wait;   
  end process;   
        
  en_gen:process   
  begin   
    en<='0';   
    wait for 50ns;   
    en<='1';   
    wait;   
  end process;   
end rtl;   

其实testbench也有自己固定的一套格式,总结如下:
[csharp] view plain copy
--测试平台文件(testbench)的基本结构   
library ieee;   
use ieee.std_logic_1164.all;   
   
entity test_bench is      --测试平台文件的空实体(不需要端口定义)   
   
end test_bench;   
   
architecture tb_behavior of test_bench is   
    component entity_under_test         --被测试元件的声明   
        port(   
        list-of-ports-theri-types-and-modes   
        );   
    end component;   
        
begin   
    instantiation:entity_under_test port map   
    (   
        port-associations   
    );   
        
    process()       --产生时钟信号   
    ……   
    end process;   
        
    process()       --产生激励源   
    ……   
    end process;   
end tb_behavior;   
   
-------------------------------------------------------------------   
--简单计数程序源码   
library ieee;   
use ieee.std_logic_1164.all;   
use ieee.std_logic_unsigned.all;   
use ieee.std_logic_unsigned.all;   
   
entity sim_counter is   
    port(   
        clk     :in     std_logic;   
        reset   :in     std_logic;   
        count   ut    std_logic_vector(3 downto 0)   
        );   
end entity;   
   
architecture behavioral of sim_counter is   
   
signal temp :std_logic_vector(3 downto 0);   
   
begin   
    process(clk,reset)   
    begin   
        if reset='1' then   
            temp<="0000";   
        elsif clk'event and clk='1' then   
            temp<=temp+1;   
        end if;   
    end process;   
    count<=temp;   
end behavioral;   
   
-------------------------------------------------------------------   
--简单计数程序,测试文件代码(testbench)   
library ieee;   
use ieee.std_logic_1164.all;   
use ieee.std_logic_unsigned.all;   
use ieee.numeric_std.all;   
   
entity counter_tb_vhd is            --测试平台实体   
end counter_tb_vhd;   
   
architecture behavior of counter_tb_vhd is   
    --被测试元件(DUT)的声明   
    component sim_counter   
    port(   
        clk :in std_logic;   
        reset   :in std_logic;   
        count   ut std_logic_vector(3 downto 0)   
        );   
    end component;   
    --输入信号   
    signal clk:std_logic:='0';   
    signal reset :std_logic:='0';   
    --输出信号   
    signal count    :std_logic_vector(3 downto 0);   
        
    constant clk_period :time   :=20 ns;        --时钟周期的定义   
   
begin   
    dut:sim_counter port map(   
        clk=>clk,reset=>reset,counter=>counter   
        );   
    clk_gen:process   
    begin   
        clk='1';   
        wait for clk_period/2;   
        clk='0';   
        wait for clk_period/2;   
    end process;   
        
    tb:process      --激励信号   
    begin   
        wait for 20 ns;   
        reset<='1';   
        wait for 20 ns;   
        reset<='0';   
        wait for 200 ns;   
        wait;       --will wait forever;   
    end process;   
end;   
   
   
--激励信号的产生方式   
--1.以一定的离散时间间隔产生激励信号的波形   
--2.基于实体的状态产生激励信号,也就是说基于实体的输出响应产生激励信号   
   
--两种常用的复位信号   
--1.周期性的激励信号,如时钟   
--2.时序变化的激励型号,如复位   
   
--eg.产生不对称时钟信号   
    w_clk<='0' after period/4 when w_clk='1' else   
           '1' after 3*period/4 when w_clk='0' else   
           '0';     
               
--eg.产生堆成时钟信号,process语句   
clk_gen1:process   
constan clk_period  := 40 ns;   
begin   
    clk='1';   
    wait for clk_period/2;   
    clk='0';   
    wait for clk_period/2;   
end process;      

如果自己不想写这些testbench的这些固定格式,可以在SIE里自动生成testbench文件的模板
步骤:New Surce -> VHDL Test Bench, 然后才会生成testbench

自动生成的testbench模板格式如下:
[csharp] view plain copy
-- Copyright (C) 1991-2008 Altera Corporation   
-- Your use of Altera Corporation's design tools, logic functions     
-- and other software and tools, and its AMPP partner logic     
-- functions, and any output files from any of the foregoing     
-- (including device programming or simulation files), and any     
-- associated documentation or information are expressly subject     
-- to the terms and conditions of the Altera Program License     
-- Subscription Agreement, Altera MegaCore Function License     
-- Agreement, or other applicable license agreement, including,     
-- without limitation, that your use is for the sole purpose of     
-- programming logic devices manufactured by Altera and sold by     
-- Altera or its authorized distributors.  Please refer to the     
-- applicable agreement for further details.   
   
-- ***************************************************************************   
-- This file contains a Vhdl test bench template that is freely editable to      
-- suit user's needs .Comments are provided in each section to help the user      
-- fill out necessary details.                                                   
-- ***************************************************************************   
-- Generated on "03/13/2011 20:05:04"   
                                                               
-- Vhdl Test Bench template for design  :  cnt6   
--     
-- Simulation tool : ModelSim (VHDL)   
--     
   
LIBRARY ieee;                                                   
USE ieee.std_logic_1164.all;                                    
   
ENTITY cnt6_vhd_tst IS   
END cnt6_vhd_tst;   
ARCHITECTURE cnt6_arch OF cnt6_vhd_tst IS   
-- constants                                                     
-- signals                                                      
SIGNAL clk : STD_LOGIC;   
SIGNAL clr : STD_LOGIC;   
SIGNAL en : STD_LOGIC;   
SIGNAL q : STD_LOGIC_VECTOR(2 DOWNTO 0);   
COMPONENT cnt6   
    PORT (   
    clk : IN STD_LOGIC;   
    clr : IN STD_LOGIC;   
    en : IN STD_LOGIC;   
    q : OUT STD_LOGIC_VECTOR(2 DOWNTO 0)   
    );   
END COMPONENT;   
BEGIN   
    i1 : cnt6   
    PORT MAP (   
-- list connections between master ports and signals   
    clk => clk,   
    clr => clr,   
    en => en,   
    q => q   
    );   
init : PROCESS                                                   
-- variable declarations                                         
BEGIN                                                            
        -- code that executes only once                          
WAIT;                                                           
END PROCESS init;                                               
always : PROCESS                                                  
-- optional sensitivity list                                      
-- (        )                                                     
-- variable declarations                                          
BEGIN                                                            
        -- code executes for every event on sensitivity list      
WAIT;                                                            
END PROCESS always;                                             
END cnt6_arch;   
zhiweiqiang33 发表于 2017-12-9 14:50:12 | 显示全部楼层
VHDL——如何写简单的testbench
zxopenhl 发表于 2017-12-10 09:49:47 | 显示全部楼层
这个可以借鉴借鉴!
奋斗的小孩 发表于 2017-12-23 19:54:13 | 显示全部楼层
不会写激励的,可以参考一下呦
小丽呀小丽 发表于 2017-12-23 20:05:05 | 显示全部楼层
仿真成功··············
zxopenljx 发表于 5 天前 | 显示全部楼层
VHDL——如何写简单的testbench
您需要登录后才可以回帖 登录 | 我要注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|小黑屋|手机版|Archiver|fpga论坛|fpga设计论坛 ( 京ICP备20003123号-1 )

GMT+8, 2025-2-24 19:57 , Processed in 0.060552 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表