]> rtime.felk.cvut.cz Git - fpga/uart.git/blob - fifo.vhd
Baud_gen scale input width redefined as generic. Default value is 16.
[fpga/uart.git] / fifo.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.all;
4 use ieee.std_logic_unsigned.all;
5
6 entity fifo is
7   generic (
8     width : integer := 2
9   );
10   port (
11     clk      : in  std_logic;
12     reset    : in  std_logic;
13     we       : in  std_logic;
14     re       : in  std_logic;
15     d_in     : in  std_logic_vector (7 downto 0);
16     d_out    : out std_logic_vector (7 downto 0);
17     full     : out std_logic;
18     hfull    : out std_logic;
19     empty    : out std_logic;
20     overflow : out std_logic
21   );
22 end fifo;
23
24 --------------------------------------------------------------------------------
25
26 architecture behavioral of fifo is
27
28   subtype mem_addr_t is std_logic_vector (width-1 downto 0);
29   type mem_t is array (3 downto 0) of std_logic_vector (7 downto 0);
30
31   signal memory : mem_t;
32   
33   signal read_addr : mem_addr_t;
34   signal write_addr : mem_addr_t;
35
36   signal length : std_logic_vector (width downto 0);
37
38   signal full_s : std_logic;
39
40 --------------------------------------------------------------------------------
41
42 begin
43   
44   process (clk, reset)
45   begin
46     if (reset = '1') then
47       length <= (others => '0');
48       overflow <= '0';
49       
50     elsif (rising_edge(clk)) then
51       if ((re = '1') and (we = '0')) then
52         length <= length - 1;
53       elsif ((re = '0') and (we = '1')) then
54         if (full_s = '1') then
55           overflow <= '1';
56         else
57           length <= length + 1;
58         end if;
59       end if;
60     end if;
61   end process;
62
63
64   process (clk, reset)
65   begin
66     if (reset = '1') then
67       read_addr <= (others => '0');
68       write_addr <= (others => '0');
69     
70     elsif (rising_edge(clk)) then
71       if (re = '1') then
72         read_addr <= read_addr + 1;
73       end if;
74       
75       if (we = '1') then
76         write_addr <= write_addr + 1;
77         memory (conv_integer(write_addr)) <= d_in;
78         
79         if (full_s = '1') then
80           read_addr <= read_addr + 1;
81         end if;
82       end if;
83     end if;
84   end process;
85   
86 --------------------------------------------------------------------------------
87   
88   d_out <= memory (conv_integer(read_addr));
89   
90   full_s  <= '1' when (length >= 2**width) else '0';
91
92   full  <= full_s;
93   hfull <= '1' when (length >= 2**(width-1)) else '0';
94   empty <= '1' when (length = 0) else '0';
95   
96 end behavioral;
97