善用VHDL记录类型构建整洁可复用FIFO接口
VHDL中记录提供强大方式精简设计,堪比C语言结构体。将相关信号分组为单一类型,能大幅减少代码重复、提升可读性、简化跨多实体的端口映射。
记录在处理大型反复接口时大放异彩——如连接片外存储器或FIFO——同组控制与数据信号反复出现。在包中一次定义记录类型并到处复用,保持实体接口整洁,便于后续修改。
以下示例演示如何在专用包(example_record_pkg.vhd)创建两记录类型,建模FIFO输入输出信号。单一记录信号代表FIFO所有输入,另一代表所有输出。
核心收获:
- 记录简化VHDL实体声明与端口映射。
- 可容纳不同类型元素——
std_logic、integer、std_logic_vector等。 - 记录类比C结构体,为许多工程师提供熟悉范式。
- 共享记录类型应置于单一包以保一致性。
- 记录信号可用常量初始化获可预测上电值。
- 记录数组支持结构化数据处理,如FIFO配置表。
下方为包定义,后接使用记录类型管理FIFO读写逻辑的示例实体。代码全符合IEEE 1076-2008标准。
library ieee; use ieee.std_logic_1164.all; package example_record_pkg is -- FIFO输出。 type t_FROM_FIFO is record wr_full : std_logic; -- FIFO满标志 rd_empty : std_logic; -- FIFO空标志 rd_dv : std_logic; rd_data : std_logic_vector(7 downto 0); end record t_FROM_FIFO; -- FIFO输入。 type t_TO_FIFO is record wr_en : std_logic; wr_data : std_logic_vector(7 downto 0); rd_en : std_logic; end record t_TO_FIFO; constant c_FROM_FIFO_INIT : t_FROM_FIFO := (wr_full => '0', rd_empty => '1', rd_dv => '0', rd_data => (others => '0')); constant c_TO_FIFO_INIT : t_TO_FIFO := (wr_en => '0', wr_data => (others => '0'), rd_en => '0'); end package example_record_pkg;
library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; use work.example_record_pkg.all; -- 使用包! entity example_record is port ( i_clk : in std_logic; i_fifo : in t_FROM_FIFO; o_fifo : out t_TO_FIFO := c_TO_FIFO_INIT -- 初始化输出记录 ); end example_record; architecture behave of example_record is signal r_WR_DATA : unsigned(7 downto 0) := (others => '0'); begin -- 处理FIFO写入 p_FIFO_WR : process (i_clk) is begin if rising_edge(i_clk) then if i_fifo.wr_full = '0' then o_fifo.wr_en <= '1'; o_fifo.wr_data <= std_logic_vector(r_WR_DATA + 1); end if; end if; end process p_FIFO_WR; -- 处理FIFO读取 p_FIFO_RD : process (i_clk) is begin if rising_edge(i_clk) then if i_fifo.rd_empty = '0' then o_fifo.rd_en <= '1'; end if; end if; end process p_FIFO_RD; end behave;
VHDL