1 |
2 |
daniel.kho |
/*
|
2 |
|
|
This file is part of the Memories project:
|
3 |
|
|
http://opencores.org/project,wb_fifo
|
4 |
|
|
|
5 |
|
|
Description
|
6 |
|
|
FIFO memory model.
|
7 |
|
|
|
8 |
|
|
To Do:
|
9 |
|
|
|
10 |
|
|
Author(s):
|
11 |
|
|
- Daniel C.K. Kho, daniel.kho@opencores.org | daniel.kho@tauhop.com
|
12 |
|
|
|
13 |
|
|
Copyright (C) 2012-2013 Authors and OPENCORES.ORG.
|
14 |
|
|
|
15 |
|
|
This source file may be used and distributed without
|
16 |
|
|
restriction provided that this copyright statement is not
|
17 |
|
|
removed from the file and that any derivative work contains
|
18 |
|
|
the original copyright notice and the associated disclaimer.
|
19 |
|
|
|
20 |
|
|
This source file is free software; you can redistribute it
|
21 |
|
|
and/or modify it under the terms of the GNU Lesser General
|
22 |
|
|
Public License as published by the Free Software Foundation;
|
23 |
|
|
either version 2.1 of the License, or (at your option) any
|
24 |
|
|
later version.
|
25 |
|
|
|
26 |
|
|
This source is distributed in the hope that it will be
|
27 |
|
|
useful, but WITHOUT ANY WARRANTY; without even the implied
|
28 |
|
|
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
29 |
|
|
PURPOSE. See the GNU Lesser General Public License for more
|
30 |
|
|
details.
|
31 |
|
|
|
32 |
|
|
You should have received a copy of the GNU Lesser General
|
33 |
|
|
Public License along with this source; if not, download it
|
34 |
|
|
from http://www.opencores.org/lgpl.shtml.
|
35 |
|
|
*/
|
36 |
|
|
library ieee; use ieee.std_logic_1164.all, ieee.numeric_std.all;
|
37 |
|
|
library tauhop;
|
38 |
|
|
--package fifoTypes is new tauhop.types generic map(t_data=>unsigned(31 downto 0));
|
39 |
|
|
use tauhop.fifoTransactor.all;
|
40 |
|
|
|
41 |
|
|
--library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all;
|
42 |
|
|
--library tauhop; use tauhop.fifoTypes.all;
|
43 |
|
|
entity fifo is
|
44 |
|
|
generic(memoryDepth:positive);
|
45 |
|
|
port(clk,reset:in std_ulogic;
|
46 |
|
|
fifoInterface:inout t_fifoTransactor
|
47 |
|
|
);
|
48 |
|
|
end entity fifo;
|
49 |
|
|
|
50 |
|
|
architecture rtl of fifo is
|
51 |
|
|
type t_memory is array(memoryDepth-1 downto 0) of i_transactor.t_msg;
|
52 |
|
|
signal memory:t_memory;
|
53 |
|
|
signal ptr:natural range 0 to memoryDepth-1;
|
54 |
|
|
|
55 |
4 |
daniel.kho |
/* FIFO control signalling. */
|
56 |
|
|
signal fifoCtrl:t_fifo;
|
57 |
|
|
|
58 |
3 |
daniel.kho |
/*
|
59 |
|
|
writeRequest and readRequest are inputs. This indicate that a block is requesting to write to or
|
60 |
|
|
read from the FIFO.
|
61 |
|
|
For write requests, the external block requests to write some data into the FIFO. The data
|
62 |
|
|
is attached as part of the write request (writeRequest.message).
|
63 |
|
|
For read requests, the external block requests to read some data from the FIFO. The data will
|
64 |
|
|
later be attached in the read response (readResponse.message).
|
65 |
|
|
|
66 |
|
|
There is no such concept as messages attached to a write response (no writeResponse.message) or
|
67 |
|
|
read request (no readRequest.message).
|
68 |
|
|
|
69 |
|
|
To generate a write response, the FIFO can assert an acknowledge signal, which could be part of
|
70 |
|
|
the response (writeResponse.trigger). The acknowledge is generated only when the FIFO is not
|
71 |
|
|
full. The requester can check this flag so that it will not continue requesting a write when
|
72 |
|
|
the FIFO is full.
|
73 |
|
|
When generating a read response, the FIFO can assert an acknowledge signal as part of the
|
74 |
|
|
response (readResponse.trigger), while at the same time, sending data back to the external
|
75 |
|
|
requester (readResponse.message). The acknowledge signal is generated only when the FIFO is
|
76 |
|
|
not empty. The requester can check this flag so that it will not continue requesting a read
|
77 |
|
|
when the FIFO is empty.
|
78 |
|
|
*/
|
79 |
2 |
daniel.kho |
signal i_writeRequest,i_readRequest:i_transactor.t_bfm;
|
80 |
3 |
daniel.kho |
signal i_full,i_empty:boolean;
|
81 |
2 |
daniel.kho |
signal write,read:boolean;
|
82 |
7 |
daniel.kho |
|
83 |
|
|
signal writeRequested, readRequested: boolean;
|
84 |
2 |
daniel.kho |
begin
|
85 |
7 |
daniel.kho |
/* Registers and pipelines. */
|
86 |
|
|
/* TODO recheck pipelining. */
|
87 |
|
|
process(clk) is begin
|
88 |
|
|
if falling_edge(clk) then
|
89 |
|
|
/* TODO add buffers for pipelined request signals,
|
90 |
|
|
i.e., add a flip-flop and a buffer.
|
91 |
|
|
*/
|
92 |
|
|
i_writeRequest <= fifoInterface.writeRequest after 1 ps;
|
93 |
|
|
i_readRequest <= fifoInterface.readRequest after 1 ps;
|
94 |
|
|
|
95 |
|
|
i_full <= fifoCtrl.full;
|
96 |
|
|
i_empty <= fifoCtrl.empty;
|
97 |
|
|
end if;
|
98 |
|
|
end process;
|
99 |
|
|
|
100 |
|
|
/* Synchronous FIFO. */
|
101 |
2 |
daniel.kho |
controller: process(reset,clk) is begin
|
102 |
7 |
daniel.kho |
--if reset then
|
103 |
|
|
-- fifoInterface.readResponse.message<=(others=>'Z');
|
104 |
|
|
-- fifoInterface.readResponse.trigger<=false;
|
105 |
|
|
if falling_edge(clk) then
|
106 |
3 |
daniel.kho |
/* Default assignments. */
|
107 |
|
|
fifoInterface.readResponse.trigger<=false;
|
108 |
|
|
fifoInterface.writeResponse.trigger<=false;
|
109 |
|
|
|
110 |
|
|
/* Write request.
|
111 |
|
|
Safety control: allow writing only when FIFO is not full.
|
112 |
|
|
*/
|
113 |
|
|
--if i_pctFilled<d"100" and (fifoInterface.writeRequest.trigger xor i_writeRequest.trigger) then
|
114 |
7 |
daniel.kho |
--if not i_full and (fifoInterface.writeRequest.trigger xor i_writeRequest.trigger) then -- TODO change to write
|
115 |
|
|
if not i_full and write then
|
116 |
3 |
daniel.kho |
fifoInterface.writeResponse.trigger<=true;
|
117 |
2 |
daniel.kho |
memory(ptr)<=fifoInterface.writeRequest.message;
|
118 |
|
|
end if;
|
119 |
|
|
|
120 |
3 |
daniel.kho |
/* Read request.
|
121 |
|
|
Safety control: allow reading only when FIFO is not empty.
|
122 |
|
|
*/
|
123 |
7 |
daniel.kho |
--if not i_empty and (fifoInterface.readRequest.trigger xor i_readRequest.trigger) then -- TODO change to read
|
124 |
|
|
if not i_empty and read then
|
125 |
3 |
daniel.kho |
fifoInterface.readResponse.trigger<=true;
|
126 |
2 |
daniel.kho |
fifoInterface.readResponse.message<=memory(ptr);
|
127 |
|
|
end if;
|
128 |
7 |
daniel.kho |
|
129 |
|
|
/* Synchronous reset. */
|
130 |
|
|
if reset then
|
131 |
|
|
fifoInterface.readResponse.message<=(others=>'Z');
|
132 |
|
|
fifoInterface.readResponse.trigger<=false;
|
133 |
|
|
end if;
|
134 |
2 |
daniel.kho |
end if;
|
135 |
|
|
end process controller;
|
136 |
|
|
|
137 |
7 |
daniel.kho |
write <= fifoInterface.writeRequest.trigger xor i_writeRequest.trigger;
|
138 |
|
|
read <= fifoInterface.readRequest.trigger xor i_readRequest.trigger;
|
139 |
2 |
daniel.kho |
|
140 |
7 |
daniel.kho |
/* Request indicator. Derived from fifoInterface.writeRequest.trigger
|
141 |
|
|
and fifoInterface.readRequest.trigger.
|
142 |
|
|
Asserts when there are incoming requests.
|
143 |
|
|
*/
|
144 |
|
|
process(clk) is begin
|
145 |
|
|
if falling_edge(clk) then
|
146 |
|
|
writeRequested <= fifoInterface.writeRequest.trigger xor i_writeRequest.trigger;
|
147 |
|
|
readRequested <= fifoInterface.readRequest.trigger xor i_readRequest.trigger;
|
148 |
|
|
end if;
|
149 |
|
|
end process;
|
150 |
|
|
|
151 |
2 |
daniel.kho |
addrPointer: process(reset,clk) is begin
|
152 |
|
|
if reset then ptr<=0;
|
153 |
|
|
elsif falling_edge(clk) then
|
154 |
|
|
/* Increment or decrement the address pointer only when write or read is HIGH;
|
155 |
|
|
do nothing when both are HIGH or when both are LOW.
|
156 |
|
|
*/
|
157 |
|
|
if write xor read then
|
158 |
|
|
if write then
|
159 |
|
|
if ptr<memoryDepth-1 then ptr<=ptr+1; end if;
|
160 |
|
|
end if;
|
161 |
|
|
if read then
|
162 |
|
|
if ptr>0 then ptr<=ptr-1; end if;
|
163 |
|
|
end if;
|
164 |
|
|
end if;
|
165 |
|
|
end if;
|
166 |
|
|
end process addrPointer;
|
167 |
|
|
|
168 |
4 |
daniel.kho |
fifoCtrl.pctFilled<=to_unsigned(ptr*100/(memoryDepth-1), fifoCtrl.pctFilled'length);
|
169 |
2 |
daniel.kho |
|
170 |
|
|
process(clk) is begin
|
171 |
|
|
if rising_edge(clk) then
|
172 |
4 |
daniel.kho |
fifoCtrl.nearFull<=true when fifoCtrl.pctFilled>=d"75" and fifoCtrl.pctFilled<d"100" else false;
|
173 |
|
|
fifoCtrl.full<=true when fifoCtrl.pctFilled=d"100" else false;
|
174 |
|
|
fifoCtrl.nearEmpty<=true when fifoCtrl.pctFilled<=d"25" and fifoCtrl.pctFilled>d"0" else false;
|
175 |
|
|
fifoCtrl.empty<=true when fifoCtrl.pctFilled=d"0" else false;
|
176 |
2 |
daniel.kho |
end if;
|
177 |
|
|
end process;
|
178 |
|
|
|
179 |
|
|
process(clk) is begin
|
180 |
|
|
if falling_edge(clk) then
|
181 |
4 |
daniel.kho |
fifoCtrl.overflow<=fifoCtrl.full and write;
|
182 |
|
|
fifoCtrl.underflow<=fifoCtrl.empty and read;
|
183 |
2 |
daniel.kho |
end if;
|
184 |
|
|
end process;
|
185 |
|
|
end architecture rtl;
|