Advertisement
Guest User

Untitled

a guest
May 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 2.40 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer: Nabil Sayegh
  4. --
  5. -- Create Date:    12:11:03 08/07/2009
  6. -- Design Name:
  7. -- Module Name:    fifo - Behavioral
  8. -- Project Name:
  9. -- Target Devices:
  10. -- Tool versions:
  11. -- Description:
  12. --
  13. -- Dependencies:
  14. --
  15. -- Revision:
  16. -- Revision 0.01 - File Created
  17. -- Additional Comments:
  18. --
  19. ----------------------------------------------------------------------------------
  20. library ieee;
  21.   use ieee.std_logic_1164.all;
  22.   use ieee.numeric_std.all;
  23.  
  24. entity fifo_simple is
  25.     generic
  26.     (
  27.     addr_width : Integer := 8;
  28.     data_width : Integer := 8
  29.     );
  30.     port
  31.     (
  32.         clock              : IN  std_logic;
  33.         reset              : IN  std_logic;
  34.  
  35.         wren               : IN  std_logic;
  36.         rden               : IN  std_logic;
  37.         din                : IN  std_logic_vector(data_width-1 downto 0);
  38.         dout               : OUT std_logic_vector(data_width-1 downto 0);
  39.         empty              : OUT std_logic;
  40.         almost_full        : OUT std_logic
  41.     );
  42. end entity fifo_simple;
  43.  
  44. architecture Behavioral of fifo_simple is
  45.  
  46.     type mem_t is array (2**addr_width-1 downto 0) of std_logic_vector(data_width-1 downto 0);
  47.     signal mem : mem_t;
  48.     signal waddr : unsigned(addr_width-1 downto 0);
  49.     signal raddr : unsigned(addr_width-1 downto 0);
  50.     signal raddr_next : unsigned(addr_width-1 downto 0);
  51.     signal size : unsigned(addr_width-1 downto 0);
  52.  
  53.     constant high_water : Integer := 2**addr_width-32;
  54.  
  55. begin
  56.  
  57.     empty <= '1' when size = 0 else '0';
  58.     raddr_next <= raddr + unsigned'(0 => rden);
  59.     fifo : process(clock, reset)
  60.     begin
  61.         if rising_edge(clock) then
  62.             -- write
  63.             if wren = '1' then
  64.                 mem(to_integer(waddr)) <= din;
  65.                 waddr <= waddr + 1;                    
  66.             end if;
  67.  
  68.             -- read (read before write, because we read the old value)
  69.             dout <= mem(to_integer(raddr_next));
  70.             raddr <= raddr_next;
  71.  
  72.             -- size calculation
  73.             if wren = '1' and rden = '0' then
  74.                 size <= size + 1;
  75.                 empty <= '0';
  76.             elsif wren = '0' and rden = '1' then
  77.                 size <= size - 1;
  78.                 if size = 1 then
  79.                     empty <= '1';
  80.                 end if;
  81.             end if;
  82.  
  83.             if size >= high_water then
  84.                 almost_full <= '1';
  85.             else
  86.                 almost_full <= '0';
  87.             end if;
  88.  
  89.         end if;
  90.  
  91.         if reset = '1' then
  92.             waddr <= (others => '0');
  93.             raddr <= (others => '0');
  94.             size <= (others => '0');
  95.         else
  96.         end if;
  97.     end process;
  98.  
  99. end architecture Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement