zoukankan      html  css  js  c++  java
  • 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;

  • 相关阅读:
    线段树区间最大子段和
    NTT数论变换
    cdq分治·三维偏序问题
    线段树区间开方
    怎么联系$zcy$呢?
    题解 CF375D 【Tree and Queries】
    点分治模板
    Good Bye 2018题解
    Hello 2019题解
    Codeforces Round #525 (Div. 2)题解
  • 原文地址:https://www.cnblogs.com/shangdawei/p/2503737.html
Copyright © 2011-2022 走看看