VHDL学习-FIFO

FIFO( First Input First Output)简单说就是指先进先出。FIFO一般用于不同时钟域之间的数据传输。FIFO设计的难点在于空/满状态的判断。为了保证数据正确的写入或读出,而不发生溢出或读空的状态出现,必须保证FIFO在满的情况下,不能进行写操作。在空的状态下不能进行读操作。怎样判断FIFO的满/空就成了FIFO设计的核心问题。FIFO分为同步和异步两种,一般常用的为异步,即输入时钟与输出时钟不相同。FIFO的重要参数有宽度和深度,即FIFO一次读写操作的数据位和存储多少个位的数据。
之前从油管上视频链接中下载的FIFO程序,代码如下

-------------------------------------------------------------------------------
-- File Downloaded from http://www.nandland.com
--
-- Description: Creates a Synchronous FIFO made out of registers.
--              Generic: g_WIDTH sets the width of the FIFO created.
--              Generic: g_DEPTH sets the depth of the FIFO created.
--
--              Total FIFO register usage will be width * depth
--              Note that this fifo should not be used to cross clock domains.
--              (Read and write clocks NEED TO BE the same clock domain)
--
--              FIFO Full Flag will assert as soon as last word is written.
--              FIFO Empty Flag will assert as soon as last word is read.
--
--              FIFO is 100% synthesizable.  It uses assert statements which do
--              not synthesize, but will cause your simulation to crash if you
--              are doing something you shouldn't be doing (reading from an
--              empty FIFO or writing to a full FIFO).
--
--              No Flags = No Almost Full (AF)/Almost Empty (AE) Flags
--              There is a separate module that has programmable AF/AE flags.
-------------------------------------------------------------------------------
 
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
 
entity module_fifo_regs_no_flags is
  generic (
    g_WIDTH : natural := 8;
    g_DEPTH : integer := 32
    );
  port (
    i_rst_sync : in std_logic;
    i_clk      : in std_logic;
 
    -- FIFO Write Interface
    i_wr_en   : in  std_logic;
    i_wr_data : in  std_logic_vector(g_WIDTH-1 downto 0);
    o_full    : out std_logic;
 
    -- FIFO Read Interface
    i_rd_en   : in  std_logic;
    o_rd_data : out std_logic_vector(g_WIDTH-1 downto 0);
    o_empty   : out std_logic
    );
end module_fifo_regs_no_flags;
 
architecture rtl of module_fifo_regs_no_flags is
 
  type t_FIFO_DATA is array (0 to g_DEPTH-1) of std_logic_vector(g_WIDTH-1 downto 0);
  signal r_FIFO_DATA : t_FIFO_DATA := (others => (others => '0'));
 
  signal r_WR_INDEX   : integer range 0 to g_DEPTH-1 := 0;
  signal r_RD_INDEX   : integer range 0 to g_DEPTH-1 := 0;
 
  -- # Words in FIFO, has extra range to allow for assert conditions
  signal r_FIFO_COUNT : integer range -1 to g_DEPTH+1 := 0;
 
  signal w_FULL  : std_logic;
  signal w_EMPTY : std_logic;
   
begin
 
  p_CONTROL : process (i_clk) is
  begin
    if rising_edge(i_clk) then
      if i_rst_sync = '1' then
        r_FIFO_COUNT <= 0;
        r_WR_INDEX   <= 0;
        r_RD_INDEX   <= 0;
      else
 
        -- Keeps track of the total number of words in the FIFO
        if (i_wr_en = '1' and i_rd_en = '0') then
          r_FIFO_COUNT <= r_FIFO_COUNT + 1;
        elsif (i_wr_en = '0' and i_rd_en = '1') then
          r_FIFO_COUNT <= r_FIFO_COUNT - 1;
        end if;
 
        -- Keeps track of the write index (and controls roll-over)
        if (i_wr_en = '1' and w_FULL = '0') then
          if r_WR_INDEX = g_DEPTH-1 then
            r_WR_INDEX <= 0;
          else
            r_WR_INDEX <= r_WR_INDEX + 1;
          end if;
        end if;
 
        -- Keeps track of the read index (and controls roll-over)        
        if (i_rd_en = '1' and w_EMPTY = '0') then
          if r_RD_INDEX = g_DEPTH-1 then
            r_RD_INDEX <= 0;
          else
            r_RD_INDEX <= r_RD_INDEX + 1;
          end if;
        end if;
 
        -- Registers the input data when there is a write
        if i_wr_en = '1' then
          r_FIFO_DATA(r_WR_INDEX) <= i_wr_data;
        end if;
         
      end if;                           -- sync reset
    end if;                             -- rising_edge(i_clk)
  end process p_CONTROL;
   
  o_rd_data <= r_FIFO_DATA(r_RD_INDEX);
 
  w_FULL  <= '1' when r_FIFO_COUNT = g_DEPTH else '0';
  w_EMPTY <= '1' when r_FIFO_COUNT = 0       else '0';
 
  o_full  <= w_FULL;
  o_empty <= w_EMPTY;
   
  -- ASSERTION LOGIC - Not synthesized
  -- synthesis translate_off
 
  p_ASSERT : process (i_clk) is
  begin
    if rising_edge(i_clk) then
      if i_wr_en = '1' and w_FULL = '1' then
        report "ASSERT FAILURE - MODULE_REGISTER_FIFO: FIFO IS FULL AND BEING WRITTEN " severity failure;
      end if;
 
      if i_rd_en = '1' and w_EMPTY = '1' then
        report "ASSERT FAILURE - MODULE_REGISTER_FIFO: FIFO IS EMPTY AND BEING READ " severity failure;
      end if;
    end if;
  end process p_ASSERT;
 
  -- synthesis translate_on
end rtl;
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的AXI4-Stream FIFO写数据的VHDL实现,可以用于仿真或实际硬件实现: ```vhdl library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.NUMERIC_STD.ALL; entity axi4_stream_fifo is generic ( DATA_WIDTH : integer := 32; -- 数据宽度 ADDR_WIDTH : integer := 6; -- 地址宽度 MAX_DEPTH : integer := 64 -- 最大深度 ); port ( -- AXI4-Stream接口 s_axis_tdata : in std_logic_vector(DATA_WIDTH-1 downto 0); s_axis_tvalid : in std_logic; s_axis_tready : out std_logic; -- AXI接口 s_axi_awaddr : in std_logic_vector(ADDR_WIDTH-1 downto 0); s_axi_awvalid : in std_logic; s_axi_awready : out std_logic; s_axi_wdata : in std_logic_vector(DATA_WIDTH-1 downto 0); s_axi_wvalid : in std_logic; s_axi_wready : out std_logic; s_axi_bresp : out std_logic_vector(1 downto 0); s_axi_bvalid : out std_logic; s_axi_bready : in std_logic ); end entity; architecture rtl of axi4_stream_fifo is -- FIFO深度计数器 signal count : integer range 0 to MAX_DEPTH-1 := 0; -- FIFO存储器 type fifo_mem_t is array (0 to MAX_DEPTH-1) of std_logic_vector(DATA_WIDTH-1 downto 0); signal fifo_mem : fifo_mem_t := (others => (others => '0')); begin -- AXI4-Stream接口写数据 write_data: process (s_axis_tdata, s_axis_tvalid, s_axis_tready) is begin if (s_axis_tvalid = '1' and s_axis_tready = '1') then -- 数据写入FIFO fifo_mem(count) <= s_axis_tdata; count <= count + 1; end if; end process; -- AXI接口响应写请求 respond_write: process (s_axi_awaddr, s_axi_awvalid, s_axi_awready, s_axi_wdata, s_axi_wvalid, s_axi_wready) is begin if (s_axi_awvalid = '1' and s_axi_awready = '1' and s_axi_wvalid = '1' and s_axi_wready = '1') then -- 写入FIFO的地址为当前深度 s_axi_awaddr <= std_logic_vector(to_unsigned(count-1, ADDR_WIDTH)); -- 写入数据 fifo_mem(count-1) <= s_axi_wdata; -- 计数器加1 count <= count + 1; -- 响应写请求 s_axi_bresp <= "00"; s_axi_bvalid <= '1'; end if; end process; -- AXI接口读请求 read_request: process (s_axi_awaddr, s_axi_awvalid, s_axi_awready) is begin if (s_axi_awvalid = '1' and s_axi_awready = '1') then -- 读请求的地址为0 s_axi_awaddr <= (others => '0'); -- 响应读请求 s_axi_bresp <= "00"; s_axi_bvalid <= '1'; end if; end process; -- AXI接口读数据 read_data: process (s_axi_araddr, s_axi_arvalid, s_axi_arready) is begin if (s_axi_arvalid = '1' and s_axi_arready = '1') then -- 读取FIFO的第一个数据 s_axi_rdata <= fifo_mem(0); -- 读取后计数器减1 count <= count - 1; -- 响应读请求 s_axi_rvalid <= '1'; end if; end process; end architecture; ``` 请注意,这只是一个简单的实现,没有考虑流控制和错误处理。在实际应用中,您可能需要更多的功能和保护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值