aboutsummaryrefslogtreecommitdiffstats
path: root/testsuite/gna/bug010/FIFO.vhdl
blob: e68a04e55397872c4667516347c75258c841ce11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;

entity FIFO is

	generic(Depth : integer := 3);

	port(
		iClk	: in std_logic;
		iReset	: in std_logic;
		-- write port
		iWrEn		: in std_logic;
		iData		: in std_logic_vector(7 downto 0);
		oHasSpace	: out std_logic;
		-- read port
		iRdEn		: in std_logic;
		oData		: out std_logic_vector(7 downto 0);
		oHasData	: out std_logic
	);
end FIFO;

architecture behaviour of FIFO is

	constant DMSB	: integer := Depth - 1;
	constant Size	: integer := 2 ** DEPTH;

	type regArrayT is array(0 to Size-1) of std_logic_vector(7 downto 0);
	
	signal free					: unsigned(Depth downto 0) := (others => '0');
	signal rIdx, wIdx			: unsigned(DMSB downto 0) := (others => '0');
	signal regArray				: regArrayT;
	signal rdEn, wrEn			: std_logic;
	signal hasData, hasSpace	: std_logic;
	
begin

	oData <= regArray(to_integer(rIdx));
	hasData <= '0' when free = Size else '1';
	oHasData <= hasData;

	hasSpace <= '0' when free = to_unsigned(0, Depth) else '1';
	oHasSpace <= hasSpace;
	
	rdEn <= iRdEn and hasData;
	wrEn <= iWrEn and hasSpace;

	main: process(iClk) begin
		if iClk'event and iClk = '1' then
			if iReset = '1' then
				free <= to_unsigned(Size, Depth + 1);
				rIdx <= (others => '0');
				wIdx <= (others => '0');
			elsif wrEn = '1' and rdEn = '1' then
				rIdx <= rIdx + 1;
				regArray(to_integer(wIdx)) <= iData;
				wIdx <= wIdx + 1;
			elsif rdEn = '1' then
				rIdx <= rIdx + 1;
				free <= free + 1;
			elsif wrEn = '1' then
				regArray(to_integer(wIdx)) <= iData;
				wIdx <= wIdx + 1;
				free <= free - 1;
			end if;
		end if;
	end process;
	
end behaviour;