亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> VHDL

在VHDL中使用过程:用可复用代码简化设计

在VHDL中,过程是让设计师避免重复代码块的子程序。当同一操作在多处需要时,创建完整模块可能过度。过程提供轻量级可复用方案。

过程可在任何声明区——架构、包或进程——声明,其作用域限于该区域。调用时,过程体执行如同其代码原地书写,给你模块化好处而无单独组件开销。

与函数不同,过程不直接返回值。但可在参数表声明out或inout信号传回值,实际上让过程像带输入输出的小模块。

本文属基础VHDL教程系列。

定义过程的规范语法如下:

 procedure <procedure_name> (signal|variable|constant <name1> : in|out|inout <type>; signal|variable|constant <name2> : in|out|inout <type>; ... ) is <declarations_for_use_within_the_procedure> begin <code_performed_by_the_procedure_here> end procedure; 

参数表定义过程的输入输出——像迷你模块。参数可以是信号、变量或常量。过程内可声明额外对象(常量、变量、类型、子类型、别名)仅在该块有效;不能局部声明信号。

因过程可含wait语句,特别适合测试平台——如简单BFM驱动器或检查DUT输出。

练习

前一教程用深度嵌套if-then-else语句构建计时器模块。每层嵌套增加复杂度降低可读性,尽管对不同信号集执行相同操作。更好方法是将重复逻辑封装进过程。

以下视频教程演示创建VHDL过程:

示例:基于过程的计时器

测试平台代码除增加调用过程驱动DUT外保持不变:

 library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity T19_ProcedureTb is end entity; architecture sim of T19_ProcedureTb is constant ClockFrequencyHz : integer := 10; -- 10 Hz constant ClockPeriod : time := 1000 ms / ClockFrequencyHz; signal Clk : std_logic := '1'; signal nRst : std_logic := '0'; signal Seconds : integer; signal Minutes : integer; signal Hours : integer; begin i_Timer : entity work.T19_Timer(rtl) generic map(ClockFrequencyHz => ClockFrequencyHz) port map ( Clk => Clk, nRst => nRst, Seconds => Seconds, Minutes => Minutes, Hours => Hours); Clk <= not Clk after ClockPeriod / 2; process is begin wait until rising_edge(Clk); wait until rising_edge(Clk); nRst <= '1'; wait; end process; end architecture; 

计时器模块现利用过程进行计数器增量:

 library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity T19_Timer is generic(ClockFrequencyHz : integer); port( Clk : in std_logic; nRst : in std_logic; -- Negative reset Seconds : inout integer; Minutes : inout integer; Hours : inout integer); end entity; architecture rtl of T19_Timer is signal Ticks : integer; procedure IncrementWrap( signal Counter : inout integer; constant WrapValue : in integer; constant Enable : in boolean; variable Wrapped : out boolean) is begin Wrapped := false; if Enable then if Counter = WrapValue - 1 then Wrapped := true; Counter <= 0; else Counter <= Counter + 1; end if; end if; end procedure; begin process(Clk) is variable Wrap : boolean; begin if rising_edge(Clk) then if nRst = '0' then Ticks <= 0; Seconds <= 0; Minutes <= 0; Hours <= 0; else IncrementWrap(Ticks, ClockFrequencyHz, true, Wrap); IncrementWrap(Seconds, 60, Wrap, Wrap); IncrementWrap(Minutes, 60, Wrap, Wrap); IncrementWrap(Hours, 24, Wrap, Wrap); end if; end if; end process; end architecture; 

下方波形确认计时器到达60分钟时分钟计数器正确回绕:

在VHDL中使用过程:用可复用代码简化设计

分析

波形显示回绕功能与原始嵌套if实现完全一致。变化纯粹是结构性的——底层逻辑无变化。

第一参数Counter为inout信号,允许过程读取并更新其值。第二、三参数WrapValue和Enable为常量;在过程内视为编译期常量。

最后参数Wrapped为声明为out的variable。它作为伪返回值,指示该调用中计数器是否回绕。

主进程中四次连续调用IncrementWrap级联计数器。上次调用的Wrap变量喂给下次调用的Enable输入。此处用变量至关重要,因信号更新仅在进程挂起后可见;变量提供即时过程数据流。

在VHDL中使用过程:用可复用代码简化设计

要点

进入下一教程 »

VHDL

  1. 刀具磨床实战技法指南:从入门到精通
  2. 钼如何塑造现代工业:用途、优势与供应动态
  3. VHDL过程示例:标准逻辑向量自增
  4. VHDL 字符串列表创建:最佳实践与示例
  5. 优雅结束 VHDL 测试平台仿真
  6. VHDL 实现 PWM 控制器:设计、仿真与 FPGA 演示
  7. VHDL 生成随机数:从均匀分布到 OSVVM
  8. 利用进程内过程简化VHDL状态机设计
  9. 使用VHDL不纯函数提升FSM可读性与可维护性
  10. 掌握VHDL函数:高效设计实战指南
  11. 用VHDL设计有限状态机:红绿灯实战范例
  12. 构建可靠VHDL计时器:从时钟周期计数到小时