记录 - VHDL 示例
VHDL 中的 Record 构造可用于简化您的代码。记录类似于 C 中的结构 .记录最常用于定义新的 VHDL 类型。这种新类型包含用户想要的任何一组信号。大多数情况下,这用于简化接口。这对于具有大量始终相同的信号的接口非常方便。例如,与片外存储器的接口可能很大,并且在整个设计中一遍又一遍地具有相同的信号。记录可用于缩小代码大小并减少实体中需要维护的信号。设计者只需在单个包文件中定义记录类型,然后将包文件用于任何使用该记录类型的实体。
下面的示例在包文件 (example_record_pkg.vhd) 中创建两种记录类型。这些类型在 example_record.vhd 中用于简化 FIFO 接口。为所有 FIFO 的输入创建一个 t_FROM_FIFO 类型的信号 并且为所有 FIFO 的输出创建一个类型为 t_TO_FIFO 的单独信号 .
总结:
- 记录用于简化 VHDL 中的实体和端口映射。
- 记录可能包含不同类型的元素。 (std_logic、整数等)
- 记录类似于 C 中的结构。
- 跨多个文件使用的记录应保存在一个包文件中。
- 定义为记录的信号可以被初始化。
- 可以创建记录数组。
library ieee;
use ieee.std_logic_1164.all;
package example_record_pkg is
-- Outputs from the FIFO.
type t_FROM_FIFO is record
wr_full : std_logic; -- FIFO Full Flag
rd_empty : std_logic; -- FIFO Empty Flag
rd_dv : std_logic;
rd_data : std_logic_vector(7 downto 0);
end record t_FROM_FIFO;
-- Inputs to the 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; -- USING PACKAGE HERE!
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 -- intialize output record
);
end example_record;
architecture behave of example_record is
signal r_WR_DATA : unsigned(7 downto 0) := (others => '0');
begin
-- Handles writes to the 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;
-- Handles reads from the 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