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;
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值