-
Notifications
You must be signed in to change notification settings - Fork 383
/
preview_fifo.sv
291 lines (251 loc) · 8.14 KB
/
preview_fifo.sv
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//------------------------------------------------------------------------------
// preview_fifo.sv
// Konstantin Pavlov, pavlovconst@gmail.com
//------------------------------------------------------------------------------
// INFO ------------------------------------------------------------------------
// Preview FIFO, v2
//
// The module performs just like an ordinany FIFO in show-ahead mode.
// The diffenence is that 0, 1 or 2 words may be written at once.
// Also, 0, 1 or 2 words may be requested at once.
// This gives an opportunity for the reader to "preview" up to 2 future
// fifo words without actually fetching it yet.
//
/* --- INSTANTIATION TEMPLATE BEGIN ---
preview_fifo #(
.WIDTH( 16 ),
.DEPTH( 16 ) // must be at least 8
) pf (
.clk( clk ),
.nrst( nrst ),
// input port
.wrreq( ), // 3 bit one-hot
.id0( ), // first word
.id1( ), // secong word
// output port
.rdreq( ), // 3 bit one-hot
.od0( ), // first word
.od1( ), // second word
.empty( [1:0] ), // 2'b00, 2'b10 or 2'b11
.full( [1:0] ), // 2'b11, 2'b01 or 2'b00
.usedw( [USED_W:0] ) // attention to the width!
);
--- INSTANTIATION TEMPLATE END ---*/
module preview_fifo #( parameter
WIDTH = 32,
DEPTH = 16, // must be at least 8
USED_W = $clog2(DEPTH)
)(
input clk,
input nrst,
// input port
input [2:0] wrreq, // 3'b001 - no write
// 3'b010 - write one word
// 3'b100 - write two words at once
input [WIDTH-1:0] id0, // preceding data word
input [WIDTH-1:0] id1, // subsequent data word
// both data words are written simultaneously
// output port
input [2:0] rdreq, // 3'b001 - no read
// 3'b010 - read one word
// 3'b100 - read two words at once
output logic [WIDTH-1:0] od0, // first data word (preceding)
output logic [WIDTH-1:0] od1, // seconfd dadta word (subsequent)
output [1:0] empty, // when FIFO has just one word -
// any of thse bits will be active
// when FIFO has no words -
// both of these flags will be active
output [1:0] full, // "full" flags, logic is similar to "empty"
output logic[USED_W:0] usedw // word count, attention to the additional
// MSB for holding word count when full
);
// *_ptr=0 means that next read or write shoul happen with FIFO0
// *_ptr=1 means that next read or write shoul happen with FIFO1
logic wr_ptr = 1'b0;
logic rd_ptr = 1'b0;
logic [1:0] f_wrreq;
logic [1:0][WIDTH-1:0] f_wrdata;
logic [1:0] f_rdreq;
logic [1:0][WIDTH-1:0] f_rddata;
// underflow and owerflow protection flags
logic w0_valid, w1_valid, w2_valid;
logic r0_valid, r1_valid, r2_valid;
always_comb begin
w0_valid <= ~full[0];
w1_valid <= ~full[1];
w2_valid <= ~|full[1:0]; // write two words is valid
r0_valid <= ~empty[0];
r1_valid <= ~empty[1];
r2_valid <= ~|empty[1:0]; // read two words is valid
end
// writing internal FIFOs ======================================================
always_comb begin
case( wrreq[2:0] )
3'b010: begin // writing one word
if( wr_ptr ) begin
f_wrreq[0] <= 1'b0;
if( w1_valid ) begin // protecting from overflow
f_wrreq[1] <= 1'b1;
end else begin
f_wrreq[1] <= 1'b0;
end
end else begin
if( w0_valid ) begin // protecting from overflow
f_wrreq[0] <= 1'b1;
end else begin
f_wrreq[0] <= 1'b0;
end
f_wrreq[1] <= 1'b0;
end // wr_ptr
end
3'b100: begin // writing two words
if( w2_valid ) begin // protecting from overflow
f_wrreq[0] <= 1'b1;
f_wrreq[1] <= 1'b1;
end else begin
f_wrreq[0] <= 1'b0;
f_wrreq[1] <= 1'b0;
end
end
default: begin // supports valid "3'b001" case and all invalid cases
f_wrreq[0] <= 1'b0;
f_wrreq[1] <= 1'b0;
end
endcase
if( wr_ptr ) begin
f_wrdata[0][WIDTH-1:0] <= id1[WIDTH-1:0];
f_wrdata[1][WIDTH-1:0] <= id0[WIDTH-1:0];
end else begin
f_wrdata[0][WIDTH-1:0] <= id0[WIDTH-1:0];
f_wrdata[1][WIDTH-1:0] <= id1[WIDTH-1:0];
end
end
always_ff @(posedge clk) begin
if( ~nrst ) begin
wr_ptr = 1'b0;
end else begin
if( wr_ptr ) begin
if( wrreq[2:0] == 3'b010 && w1_valid ) begin
wr_ptr = ~wr_ptr;
end
end else begin
if( wrreq[2:0] == 3'b010 && w0_valid ) begin
wr_ptr = ~wr_ptr;
end
end
end // nrst
end
// reading internal FIFOs ======================================================
always_comb begin
case( rdreq[2:0] )
3'b010: begin
if( rd_ptr ) begin
f_rdreq[0] <= 1'b0;
if( r1_valid ) begin // protecting from underflow
f_rdreq[1] <= 1'b1;
end else begin
f_rdreq[1] <= 1'b0;
end
end else begin
if( r0_valid ) begin // protecting from underflow
f_rdreq[0] <= 1'b1;
end else begin
f_rdreq[0] <= 1'b0;
end
f_rdreq[1] <= 1'b0;
end
end
3'b100: begin
if( r2_valid ) begin // protecting from underflow
f_rdreq[0] <= 1'b1;
f_rdreq[1] <= 1'b1;
end else begin
f_rdreq[0] <= 1'b0;
f_rdreq[1] <= 1'b0;
end
end
default: begin
f_rdreq[0] <= 1'b0;
f_rdreq[1] <= 1'b0;
end
endcase
if( rd_ptr ) begin
od0[WIDTH-1:0] <= f_rddata[1][WIDTH-1:0];
od1[WIDTH-1:0] <= f_rddata[0][WIDTH-1:0];
end else begin
od0[WIDTH-1:0] <= f_rddata[0][WIDTH-1:0];
od1[WIDTH-1:0] <= f_rddata[1][WIDTH-1:0];
end
end
always_ff @(posedge clk) begin
if( ~nrst ) begin
rd_ptr = 1'b0;
end else begin
if( rd_ptr ) begin
if( rdreq[2:0] == 3'b010 && r1_valid ) begin
rd_ptr = ~rd_ptr;
end
end else begin
if( rdreq[2:0] == 3'b010 && r0_valid ) begin
rd_ptr = ~rd_ptr;
end
end
end // nrst
end
// internal FIFOs itself =======================================================
logic [1:0][USED_W-2:0] f_usedw_i;
logic [1:0][USED_W-1:0] f_usedw;
scfifo #(
.LPM_WIDTH( WIDTH ),
.LPM_NUMWORDS( DEPTH/2 ), // must be at least 4
.LPM_WIDTHU( USED_W-1 ),
.LPM_SHOWAHEAD( "ON" ),
.UNDERFLOW_CHECKING( "ON" ),
.OVERFLOW_CHECKING( "ON" ),
.ENABLE_ECC( "FALSE" ),
.ALLOW_RWCYCLE_WHEN_FULL( "ON" ),
.USE_EAB( "ON" )
) internal_fifo0 (
.clock( clk ),
.aclr( 1'b0 ),
.sclr( ~nrst ),
.data( f_wrdata[0][WIDTH-1:0] ),
.wrreq( f_wrreq[0] ),
.rdreq( f_rdreq[0] ),
.q( f_rddata[0][WIDTH-1:0] ),
.empty( empty[0] ),
.full( full[0] ),
.usedw( f_usedw_i[0][USED_W-2:0] )
);
scfifo #(
.LPM_WIDTH( WIDTH ),
.LPM_NUMWORDS( DEPTH/2 ), // must be at least 4
.LPM_WIDTHU( USED_W-1 ),
.LPM_SHOWAHEAD( "ON" ),
.UNDERFLOW_CHECKING( "ON" ),
.OVERFLOW_CHECKING( "ON" ),
.ENABLE_ECC( "FALSE" ),
.ALLOW_RWCYCLE_WHEN_FULL( "ON" ),
.USE_EAB( "ON" )
) internal_fifo1 (
.clock( clk ),
.aclr( 1'b0 ),
.sclr( ~nrst ),
.data( f_wrdata[1][WIDTH-1:0] ),
.wrreq( f_wrreq[1] ),
.rdreq( f_rdreq[1] ),
.q( f_rddata[1][WIDTH-1:0] ),
.empty( empty[1] ),
.full( full[1] ),
.usedw( f_usedw_i[1][USED_W-2:0] )
);
always_comb begin
f_usedw[0][USED_W-1:0] = ( full[0] )?
( 1<<(USED_W-1) ):
( {1'b0,f_usedw_i[0][USED_W-2:0]} );
f_usedw[1][USED_W-1:0] = ( full[1] )?
( 1<<(USED_W-1) ):
( {1'b0,f_usedw_i[1][USED_W-2:0]} );
usedw[USED_W:0] = f_usedw[0][USED_W-1:0] + f_usedw[1][USED_W-1:0];
end
endmodule