GeMRTOS
STD_FIFO.vhd
Go to the documentation of this file.
1 library IEEE;
2 USE IEEE.STD_LOGIC_1164.ALL;
3 USE IEEE.NUMERIC_STD.ALL;
4 
5 entity STD_FIFO is
6  Generic (
7  constant DATA_WIDTH : positive := 8;
8  constant FIFO_DEPTH : positive := 256 -- greater than or equal to 4 if Almost_Full is wanted
9  );
10  Port (
11  CLK : in STD_LOGIC;
12  RST : in STD_LOGIC;
13  WriteEn : in STD_LOGIC;
14  DataIn : in STD_LOGIC_VECTOR (DATA_WIDTH - 1 downto 0);
15  ReadEn : in STD_LOGIC;
16  DataOut : out STD_LOGIC_VECTOR (DATA_WIDTH - 1 downto 0);
17  Empty : out STD_LOGIC;
18  Almost_Full : out STD_LOGIC;
19  Full : out STD_LOGIC
20  );
21 end STD_FIFO;
22 
23 architecture Behavioral of STD_FIFO is
24 
25 type FIFO_Memory is array (0 to FIFO_DEPTH - 1) of STD_LOGIC_VECTOR (DATA_WIDTH - 1 downto 0);
27 
28 signal Head : natural range 0 to FIFO_DEPTH - 1;
29 signal Tail : natural range 0 to FIFO_DEPTH - 1;
30 signal Next_Head : natural range 0 to FIFO_DEPTH - 1;
31 signal Next_Tail : natural range 0 to FIFO_DEPTH - 1;
32 signal Next_Next_Head : natural range 0 to FIFO_DEPTH - 1;
33 
34 signal Looped : boolean;
35 
36 
37 signal fifo_control : std_logic_vector(1 downto 0);
38 
39 
40 begin
41 
43 
44 Next_Tail <= 0 when (Tail = FIFO_DEPTH - 1) else
45  Tail + 1;
46 
47 Next_Head <= 0 when (Head = FIFO_DEPTH - 1) else
48  Head + 1;
49 
50 Next_Next_Head <= 0 when (Next_Head = FIFO_DEPTH - 1) else
51  Next_Head + 1;
52 
53 DataOut <= Memory(Tail) when Head /= Tail else
54  (others => '1');
55 
56 Full <= '1' when Next_Head = Tail else
57  '0';
58 
59 Almost_Full <= '1' when Next_Next_Head = Tail else
60  '0';
61 
62 Empty <= '1' when Head = Tail else
63  '0';
64 
65 -- Memory Pointer Process
66 fifo_proc : process (CLK)
67  begin
68  if rising_edge(CLK) then
69  if RST = '1' then
70  Head <= 0;
71  Tail <= 0;
72  else
73  case fifo_control is -- ReadEn & WriteEn
74  when "00" => -- not read, not write
75  -- do nothing
76  when "01" => -- not read, WRITE
77  if Next_Head /= Tail then -- it is not full
78  Memory(Head) <= DataIn;
79  Head <= Next_Head;
80  end if;
81  when "10" => -- READ, not write
82  if Head /= Tail then -- it is not empty
83  Tail <= Next_Tail;
84  end if;
85  when "11" => -- READ, WRITE
86  if Head = Tail then -- just write because it is empty
87  Memory(Head) <= DataIn;
88  Head <= Next_Head;
89  else -- it is full or half full, write and read
90  Memory(Head) <= DataIn;
91  Head <= Next_Head;
92  Tail <= Next_Tail;
93  end if;
94  when others =>
95  end case;
96  end if;
97  end if;
98  end process;
99 
100 end Behavioral;
STD_FIFO.Behavioral.Head
natural range 0 to FIFO_DEPTH- 1 Head
Definition: STD_FIFO.vhd:28
STD_FIFO.FIFO_DEPTH
FIFO_DEPTHpositive := 256
Definition: STD_FIFO.vhd:9
STD_FIFO
Definition: STD_FIFO.vhd:5
STD_FIFO.WriteEn
in WriteEnSTD_LOGIC
Definition: STD_FIFO.vhd:13
STD_FIFO.Behavioral.Next_Head
natural range 0 to FIFO_DEPTH- 1 Next_Head
Definition: STD_FIFO.vhd:30
STD_FIFO.RST
in RSTSTD_LOGIC
Definition: STD_FIFO.vhd:12
STD_FIFO.Empty
out EmptySTD_LOGIC
Definition: STD_FIFO.vhd:17
STD_FIFO.DataIn
in DataInSTD_LOGIC_VECTOR( DATA_WIDTH- 1 downto 0)
Definition: STD_FIFO.vhd:14
STD_FIFO.DATA_WIDTH
DATA_WIDTHpositive := 8
Definition: STD_FIFO.vhd:7
STD_FIFO.Behavioral
Definition: STD_FIFO.vhd:23
STD_FIFO.Full
out FullSTD_LOGIC
Definition: STD_FIFO.vhd:20
STD_FIFO.Behavioral.Memory
FIFO_Memory Memory
Definition: STD_FIFO.vhd:26
STD_FIFO.Behavioral.fifo_control
std_logic_vector( 1 downto 0) fifo_control
Definition: STD_FIFO.vhd:37
STD_FIFO.CLK
in CLKSTD_LOGIC
Definition: STD_FIFO.vhd:11
STD_FIFO.Behavioral.Next_Next_Head
natural range 0 to FIFO_DEPTH- 1 Next_Next_Head
Definition: STD_FIFO.vhd:32
STD_FIFO.DataOut
out DataOutSTD_LOGIC_VECTOR( DATA_WIDTH- 1 downto 0)
Definition: STD_FIFO.vhd:16
STD_FIFO.Behavioral.Looped
boolean Looped
Definition: STD_FIFO.vhd:34
STD_FIFO.Almost_Full
out Almost_FullSTD_LOGIC
Definition: STD_FIFO.vhd:18
STD_FIFO.Behavioral.Tail
natural range 0 to FIFO_DEPTH- 1 Tail
Definition: STD_FIFO.vhd:29
STD_FIFO.Behavioral.FIFO_Memory
( 0 to FIFO_DEPTH- 1) STD_LOGIC_VECTOR( DATA_WIDTH- 1 downto 0) FIFO_Memory
Definition: STD_FIFO.vhd:25
STD_FIFO.ReadEn
in ReadEnSTD_LOGIC
Definition: STD_FIFO.vhd:15
STD_FIFO.Behavioral.Next_Tail
natural range 0 to FIFO_DEPTH- 1 Next_Tail
Definition: STD_FIFO.vhd:31