一个简单的VHDL程序
- 格式:ppt
- 大小:147.00 KB
- 文档页数:31


use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;--use ieee.std_logic_unsigned.all;entity cnt6 isport(clr,en,clk :in std_logic;q :out std_logic_vector(2 downto 0) );end entity;architecture rtl of cnt6 issignal tmp :std_logic_vector(2 downto 0); beginprocess(clk)-- variable q6:integer;beginif(clk'event and clk='1') thenif(clr='0')thentmp<="000";elsif(en='1') thenif(tmp="101")thentmp<="000";elsetmp<=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的代码signal en :std_logic:='0';signal clk :std_logic:='0';signal q :std_logic_vector(2 downto 0);constant clk_period :time :=20 ns;begininstant:cnt6 port map(clk=>clk,en=>en,clr=>clr,q=>q);clk_gen:processbeginwait for clk_period/2;clk<='1';wait for clk_period/2;clk<='0';end process;clr_gen:processbeginclr<='0';wait for 30 ns;clr<='1';wait;end process;en_gen:processbeginen<='0';wait for 50ns;en<='1';wait;end process;end rtl;--测试平台文件(testbench)的基本结构library ieee;use ieee.std_logic_1164.all;entity test_bench is --测试平台文件的空实体(不需要端口定义) end test_bench;architecture tb_behavior of test_bench iscomponent entity_under_test --被测试元件的声明port(list-of-ports-theri-types-and-modes);end component;begininstantiation: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 isport(clk :in std_logic;reset :in std_logic;count :out std_logic_vector(3 downto 0));end entity;architecture behavioral of sim_counter issignal temp :std_logic_vector(3 downto 0);beginprocess(clk,reset)beginif reset='1' thentemp<="0000";elsif clk'event and clk='1' thentemp<=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_counterport(clk :in std_logic;reset :in std_logic;count :out 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; --时钟周期的定义begindut:sim_counter port map(clk=>clk,reset=>reset,counter=>counter);clk_gen:processbeginclk='1';wait for clk_period/2;clk='0';wait for clk_period/2;end process;tb:process --激励信号beginwait 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:processconstan clk_period := 40 ns;beginclk='1';wait for clk_period/2;clk='0';wait for clk_period/2;end process;四、如果自己不想写这些testbench的这些固定格式,可以在quartus 里自动生成testbench文件的模板,然后往里面写信号就行了步骤:processing->start->start test bench template write这里需要注意的是要在仿真选项里选择一个仿真工具,然后才会生成testbench自动生成的testbench模板格式如下:-- 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 ISEND cnt6_vhd_tst;ARCHITECTURE cnt6_arch OF cnt6_vhd_tst IS-- constants-- signalsSIGNAL clk : STD_LOGIC;SIGNAL clr : STD_LOGIC;SIGNAL en : STD_LOGIC;SIGNAL q : STD_LOGIC_VECTOR(2 DOWNTO 0);COMPONENT cnt6PORT (clk : IN STD_LOGIC;clr : IN STD_LOGIC;en : IN STD_LOGIC;q : OUT STD_LOGIC_VECTOR(2 DOWNTO 0));END COMPONENT;BEGINi1 : cnt6PORT MAP (-- list connections between master ports and signalsclk => clk,clr => clr,en => en,q => q);init : PROCESS-- variable declarationsBEGIN-- code that executes only onceWAIT;END PROCESS init;always : PROCESS-- optional sensitivity list-- ( )-- variable declarationsBEGIN-- code executes for every event on sensitivity list WAIT;END PROCESS always;END cnt6_arch;。
VHDL语言教程VHDL是一种硬件描述语言,用于描述数字电路和系统,并进行硬件的设计和仿真。
它被广泛应用于数字电路设计、嵌入式系统开发和可编程逻辑控制器等领域。
本教程将介绍VHDL语言的基本概念和语法,帮助您了解和学习这门强大的硬件描述语言。
一、VHDL概述VHDL是Very High Speed Integrated Circuit Hardware Description Language的缩写,意为高速集成电路硬件描述语言。
它是一种硬件描述语言,用于描述数字电路和系统。
与传统的电路设计方法相比,使用VHDL可以更加方便、高效地进行电路设计和测试。
1.VHDL的起源和发展VHDL最早由美国国防部为了解决数字电路设计复杂、效率低下的问题而研发。
后来,VHDL成为了一种IEEE标准(IEEE1076)并被广泛应用于数字电路设计和仿真。
2.VHDL的优点VHDL具有以下几个优点:-高级抽象:VHDL提供了一种高级描述电路的方法,使得设计者能够更加方便地表达复杂的电路结构和行为。
-可重用性:VHDL支持模块化设计,使得设计者可以将电路的不同部分进行抽象和封装,提高了电路的可重用性。
-高效仿真:VHDL可以进行高效的电路仿真和测试,有助于验证电路设计的正确性和可靠性。
-灵活性:VHDL可以应用于各种不同类型和规模的电路设计,从小规模的数字逻辑电路到大规模的系统级设计。
二、VHDL语法和基本概念1.VHDL的结构VHDL程序由程序单元(unit)组成,程序单元是VHDL描述的最小单元。
程序单元包括实体(entity)、结构(architecture)、过程(process)和包(package)等。
2. 实体(entity)实体是VHDL描述电路模块的一个部分,包括输入输出端口、信号声明和实体标识符等。
3. 结构(architecture)结构描述了实体的具体电路结构和行为,包括组件声明、信号赋值和行为描述等。
等精度频率计设计VHDL程序下面是一个基于VHDL的精度频率计设计程序:library IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.NUMERIC_STD.ALL;entity Frequency_Counter isgenericTOLERANCE : integer := 1; --容差范围REF_FREQ : natural := 100; --参考频率BIT_WIDTH : integer := 16 --输出频率计数器宽度portclk : in std_logic;reset : in std_logic;frequency : out std_logic_vector(BIT_WIDTH - 1 downto 0) --计数器输出值end entity Frequency_Counter;architecture Behavioral of Frequency_Counter issignal count : unsigned(BIT_WIDTH - 1 downto 0); --计数器signal count_enable : std_logic; --计数使能信号signal ref_counter : unsigned(ceil(log2(real(REF_FREQ * CLK_FREQ) / TOLERANCE)) - 1 downto 0); --参考计数器beginprocess(clk, reset)beginif reset = '1' thencount <= (others => '0'); --复位为0count_enable <= '0';ref_counter <= (others => '0');elsif rising_edge(clk) thencount <= count + 1; --计数信号递增end if;if ref_counter = REF_FREQ - 1 thenelseref_counter <= ref_counter + 1; --参考计数器递增end if;end if;end process;process(clk, reset)beginif reset = '1' thencount_enable <= '0';elsif rising_edge(clk) thencount_enable <= '1'; --参考频率达到后启用计数信号elsecount_enable <= '0';end if;end if;end process;frequency <= std_logic_vector(count);end architecture Behavioral;在此设计中,我们定义了一个Frequency_Counter实体,它有几个泛型参数,包括CLK_FREQ(输入时钟频率),TOLERANCE(容差范围),REF_FREQ(参考频率)和BIT_WIDTH(输出频率计数器的宽度)。