• VHDL菜鸟入门到精通之激励文件编写


    目录

    一、概览

    二、激励文件结构

    三、样例

    3.1 组合逻辑

    3.2 时序逻辑

    四、常用编写

    4.1 时钟信号

    4.2 延时

    4.3 循环

    4.4 进程


    一、概览

     

    二、激励文件结构

        VHDL激励文件结构和设计文件较为类似,下面以3-8译码器的激励文件对结构进行说明。

    激励文件主要包括:

    1)库的声明与使用

    2)实体的申明

    3)结构体的申明

    4)元件的声明

    5)设计文件实体例化

    6)信号生成

    1. library IEEE; --申明库IEEE
    2. use IEEE.STD_LOGIC_1164.ALL; --使用库文件IEEE中包STD_LOGIC_1164中的所有内容
    3. use IEEE.STD_LOGIC_ARITH.ALL;
    4. use IEEE.STD_LOGIC_UNSIGNED.ALL;
    5. entity decoder_tb is --定义一个实体decoder_tb
    6. GENERIC (n:integer := 7); --Generic语句声明一个参数n,值为7,作用范围为全局,该语句非必须的,根据实际情况使用
    7. end decoder_tb;
    8. architecture Behavioral of decoder_tb is --为实体decoder_tb申明一个结构体Behavioral
    9. component decoder is --实体内进行元件component申明,主要用于定义端口
    10. PORT(en:IN STD_LOGIC; --port内对端口进行申明
    11. sel:IN INTEGER RANGE 0 TO n;
    12. x:out std_logic_vector(7 downto 0));
    13. end component decoder;
    14. signal en:STD_LOGIC; --信号申明,测试中使用的信号
    15. signal sel: INTEGER RANGE 0 TO n;
    16. signal x:std_logic_vector(7 downto 0);
    17. begin
    18. dut:decoder port map(en=>en,sel=>sel,x=>x); --对于元件decoder进行例化,同时和测试文件中的信号进行映射
    19. process --激励信号生成
    20. begin
    21. en<='0';
    22. sel<=0;
    23. wait for 10ns;
    24. en<='1';
    25. wait for 20ns;
    26. sel<=1;
    27. wait ;
    28. end process;
    29. end Behavioral; --结构体结束语句

    三、样例

    通常设计根据输入与输出的时间关系分为组合逻辑和时序逻辑,样例也针对2种场景提供。

    3.1 组合逻辑

    以一个3-8译码器为例,输入的真值表逻辑见下图,真值表逻辑是根据输入的数字X,输出Y中下标为值X的为1,其余为0,将X用二进制表示即为sel[2:0]的3比特。

     

    设计文件代码

    1. library IEEE;
    2. use IEEE.STD_LOGIC_1164.ALL;
    3. entity decoder is
    4. GENERIC (n:integer := 8);
    5.  Port (en:IN STD_LOGIC;
    6.  sel:IN INTEGER RANGE 0 TO n-1 ;
    7.  x:OUT STD_LOGIC_VECTOR (7 DOWNTO 0) );
    8. end decoder;
    9. architecture Behavioral of decoder is
    10. begin
    11. PROCESS(en,sel)
    12. variable temp1:STD_LOGIC_VECTOR(x'high DOWNTO 0);
    13. begin
    14. temp1:=(others=>'1');
    15. if(en='1') then
    16. temp1(sel):='0';
    17. end if;
    18. x<=temp1;
    19. end process;
    20. end Behavioral;

    测试文件代码

    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. entity decoder_tb is                
    6. GENERIC (n:integer := 7);
    7. end decoder_tb;
    8. architecture Behavioral of decoder_tb is  
    9. component decoder is  
    10. PORT(en:IN STD_LOGIC;                
    11. sel:IN INTEGER RANGE 0 TO n;
    12. x:out std_logic_vector(7 downto 0));
    13. end component decoder;
    14. signal en:STD_LOGIC;                      
    15. signal sel: INTEGER RANGE 0 TO n;
    16. signal x:std_logic_vector(7 downto 0);
    17. begin
    18. dut:decoder port map(en=>en,sel=>sel,x=>x);  
    19. process                                    
    20. begin
    21. en<='0';
    22. sel<=0;
    23. wait for 10ns;
    24. en<='1';
    25. wait for 20ns;
    26. sel<=1;
    27. wait for 20ns;
    28. sel<=2;
    29. wait for 20ns;
    30. sel<=3;
    31. wait for 20ns;
    32. sel<=4;
    33. wait for 20ns;
    34. sel<=5;
    35. wait for 20ns;
    36. sel<=6;
    37. wait for 20ns;
    38. sel<=7;
    39. wait ;
    40. end process;
    41. end Behavioral;

    综合结果,选择输入sel连接到8个LUT4,每个LUT4对应译码输出X中的一位,无时序逻辑单元触发器。

     

    仿真结果,输出信号Y中对应索引值为sel的为0,符合预期

     

    3.2 时序逻辑

    时序逻辑选用触发器的设计进行示例,设计文件代码。

    1. library IEEE;
    2. use IEEE.STD_LOGIC_1164.ALL;
    3. entity FF is
    4. Port (en,clk,d:IN STD_LOGIC;
    5.   q:OUT STD_LOGIC );
    6. end FF;
    7. architecture Behavioral of FF is
    8. begin
    9. PROCESS(en,clk)
    10. begin
    11. if(en='0') then
    12. q<='0';
    13. else
    14. if clk'event and clk='1' then
    15. q<=d;
    16. end if;
    17. end if;
    18. end process;
    19. end Behavioral;

    测试文件代码

    1. entity decoder_tb is
    2. end decoder_tb;
    3. architecture Behavioral of decoder_tb is
    4. component FF is
    5.  Port ( en,clk,d:IN STD_LOGIC;
    6.   q:out STD_LOGIC);
    7. end component FF;
    8. signal en:STD_LOGIC:='0';
    9. signal clk:STD_LOGIC:='0';
    10. signal d:STD_LOGIC:='0';
    11. signal q:STD_LOGIC;
    12. begin
    13. dut:FF port map(en=>en,clk=>clk,d=>d,q=>q);
    14. process
    15. constant period:Time:=10ns;
    16. begin
    17. clk<='1';
    18. wait for period/2;
    19. clk<='0';
    20. wait for period/2;
    21. end process;
    22. process
    23. begin
    24. en<='0';
    25. d<='1';
    26. wait for 30ns;
    27. en<='1';
    28. wait for 70ns;
    29. d<='0';
    30. wait for 30ns;
    31. d<='1';
    32. wait;
    33. end process;
    34. end Behavioral;

    综合结果:综合出一个FDCE

     

    仿真结果:

     

    四、常用编写

    下面将介绍激励编写中常用到的描述

    4.1 时钟信号

    a)占空比为50%

    1. constant PERIOD : time := <value>;  --定义时钟周期
    2. --使用after语句
    3. CLK <= not CLK after PERIOD/2;
    4. --使用wait语句
    5. constant PERIOD : time := <value>;
    6. CLK <= '0';
    7. wait for PERIOD/2;
    8. CLK <= '1';
    9. wait for PERIOD/2;

    b)非50%占空比

    1. constant DUTY_CYCLE : real := <value_0.01_to_0.99>; --定义占空比系数
    2. constant PERIOD : time := <value>;  --定义时钟周期
    3. --使用after语句
    4. CLK <= '1' after (PERIOD - (PERIOD * DUTY_CYCLE)) when CLK = '0'
    5.  else '0' after (PERIOD * DUTY_CYCLE);
    6. --使用wait语句
    7. CLK <= '0';
    8. wait for (PERIOD - (PERIOD * DUTY_CYCLE));
    9. CLK <= '1';
    10. wait for (PERIOD * DUTY_CYCLE);

    c)差分端口占空比为50%

    1. constant PERIOD : time := <value>;   --设置时钟周期
    2. --使用after语句
    3. CLK_P <= not CLK_P after PERIOD/2;
    4. CLK_N <= not CLK_N after PERIOD/2;
    5. --使用wait语句
    6. CLK_P <= '0';
    7. CLK_N <= '1';
    8. wait for PERIOD/2;
    9. CLK_P <= '1';
    10. CLK_N <= '0';
    11. wait for PERIOD/2;

    4.2 延时

    a) 指定延时时间

    1. constant SIM_TIME : time := 10 ms; --设置仿真时间SIM_TIME, 时间为10ms
    2. <signal_name> <= <signal_value> after SIM_TIME; --信号在SIM_TIME后进行赋值
    3. wait on <signal_name>; --延时到信号有变化
    4. wait until falling_edge(<signal_name>); --延时到信号的下降沿到来
    5. wait until rising_edge(<signal_name>); --延时到信号的上升沿到来
    6. wait until <signal_name> = <value>; --延时到信号变化到指定值

    4.3 循环

    a) loop语句

    1. loop
    2.   CLK <= not CLK;
    3.   wait for PERIOD/2;
    4.   if <signal_name = <value> then
    5.      exit;
    6.   end if;
    7. end loop;

    b) for语句

    1. for <variable_name> in <lower_limit> to <upper_limit> loop
    2. <statement>;
    3. <statement>;
    4. end loop;

    c)while语句

    1. while <condition> loop
    2.   <statement>;
    3.   <statement>;
    4. end loop;

    4.4 进程

    a) 组合逻辑

    1. process (所有的输入信号,信号间用逗号隔开)
    2. begin
    3.   <statements>;
    4. end process;

    b)时序逻辑

    时钟下降沿触发,异步复位

    1. process (<clock>,<async_reset>)
    2. begin
    3. if <async_reset> = '1' then
    4. <statements>;
    5. elsif (<clock>'event and = '0') then
    6. if = '1' then
    7. ;
    8. else
    9. ;
    10. end if;
    11. end if;
    12. end process;

    时钟下降沿触发,同步置位

    1. process (<clock>)
    2. begin
    3. if (<clock>'event and = '0'>) then
    4. if = '1' then
    5. ;
    6. else
    7. ;
    8. end if;
    9. end if;
    10. end process;

    时钟上升沿触发,异步复位

    1. process (<clock>,<async_reset>)
    2. begin  
    3.   if <async_reset> = '1' then
    4.      <statements>;
    5.   elsif (<clock>'event and = '1') then
    6.      if = '1' then
    7.         ;
    8.      else
    9.         ;
    10.      end if;
    11.   end if;
    12. end process;

    时钟上升沿触发,同步复位

    1. process (<clock>)
    2. begin  
    3.   if (<clock>'event and = '1'>) then
    4.      if = '1' then
    5.         ;
    6.      else
    7.         ;
    8.      end if;
    9.   end if;
    10. end process;
  • 相关阅读:
    SpringCloud环境
    计算机毕业设计之java+springboot基于vue的地方美食分享网站
    shell 查看当前日期时间
    使用命令行创建uniapp+TS项目,使用vscode编辑器
    16-云原生监控体系-rabbitmq_exporter监控 RabbitMQ-[部署&Dashborad&告警规则实战]
    商业智能BI业务分析思维:供应链分析 - 什么是牛鞭效应(一)
    网上商城建设:微信小程序直播申请开通流程及开通方法
    Autosar MCAL-ADC详解(二)-基于Tc27x的cfg软件
    抽丝剥茧C语言(高阶)指针进阶练习
    Java Reflect 反射
  • 原文地址:https://blog.csdn.net/zyp626/article/details/132944508