fifo vhdl

复制代码
 1 library ieee;
 2 use ieee.std_logic_1164.all;
 3 use ieee.std_logic_unsigned.all;
 4 
 5 entity fifo is
 6   port (
 7     reset, clk, r, w : in  std_logic;
 8     empty, full      : out std_logic;
 9     d                : in  std_logic_vector(7 downto 0);
10     q                : out std_logic_vector(7 downto 0));
11 end fifo;
12 
13 architecture rtl of fifo is
14   constant m            : integer := 8;
15   constant n            : integer := 8;
16   signal   rcntr, wcntr : std_logic_vector(2 downto 0);
17   subtype  wrdtype is std_logic_vector(n - 1 downto 0);
18   type     regtype is array(0 to m - 1) of wrdtype;
19   signal   reg                 : regtype;
20   signal   rw                  : std_logic_vector(1 downto 0);
21   signal   full_buf, empty_buf : std_logic;
22 begin
23   rw <= r & w;
24   seq : process(reset, clk)
25   begin
26     if reset = '1' then
27       rcntr     <= (others => '0');
28       wcntr     <= (others => '0');
29       empty_buf <= '1';
30       full_buf  <= '0';
31       for j in 0 to m - 1 loop
32         reg(j) <= (others => '0');
33       end loop;
34     elsif falling_edge(clk) then
35       case rw is
36         when "11" =>
37           -- read and write at the same time
38           rcntr                    <= rcntr + 1;
39           wcntr                    <= wcntr + 1;
40           reg(conv_integer(wcntr)) <= d;
41         when "10" =>
42           -- only read
43           if empty_buf = '0' then
44             -- not empty
45             if (rcntr + 1) = wcntr then
46               empty_buf <= '1';
47             end if;
48             rcntr <= rcntr + 1;
49           end if;
50           full_buf <= '0';
51         when "01" =>
52           -- only write
53           empty_buf <= '0';
54           if full_buf = '0' then
55             -- not full
56             reg(conv_integer(wcntr)) <= d;
57             if (wcntr + 1) = rcntr then
58               full_buf <= '1';
59             end if;
60             wcntr <= wcntr + 1;
61           end if;
62         when others =>
63           null;
64       end case;
65     end if;
66   end process;
67 
68   q     <= reg(conv_integer(rcntr));
69   full  <= full_buf;
70   empty <= empty_buf;
71 end rtl;
复制代码
posted @   IAmAProgrammer  阅读(429)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示