1818
1919preview_fifo #(
2020 .WIDTH( 16 ),
21- .DEPTH( 16 )
21+ .DEPTH( 16 ) // must be at least 8
2222) pf (
2323 .clk( clk ),
2424 .nrst( nrst ),
2525
2626 // input port
27- .wrreq( ), // 3 bit one-hot
28- .id0( ), // first word
29- .id1( ), // secong word
27+ .wrreq( ), // 3 bit one-hot
28+ .id0( ), // first word
29+ .id1( ), // secong word
3030
3131 // output port
32- .rdreq( ), // 3 bit one-hot
33- .od0( ), // first word
34- .od1( ) // second word
32+ .rdreq( ), // 3 bit one-hot
33+ .od0( ), // first word
34+ .od1( ), // second word
35+
36+ .empty( [1:0] ), // 2'b00, 2'b10 or 2'b11
37+ .full( [1:0] ), // 2'b11, 2'b01 or 2'b00
38+ .usedw( [USED_W:0] ) // attention to the width!
39+
3540);
3641
3742--- INSTANTIATION TEMPLATE END ---*/
@@ -66,7 +71,8 @@ module preview_fifo #( parameter
6671 // when FIFO has no words -
6772 // both of these flags will be active
6873 output [1 : 0 ] full, // "full" flags, logic is similar to "empty"
69- output [USED_W - 1 : 0 ] usedw // word count
74+ output logic [USED_W : 0 ] usedw // word count, attention to the additional
75+ // MSB for holding word count when full
7076);
7177
7278
@@ -81,10 +87,6 @@ logic [1:0][WIDTH-1:0] f_wrdata;
8187logic [1 : 0 ] f_rdreq;
8288logic [1 : 0 ][WIDTH - 1 : 0 ] f_rddata;
8389
84- // usedw0[] == usedw1[] OR usedw0[] == (usedw1[]-1) combinations are possible
85- logic [1 : 0 ][USED_W - 2 : 0 ] f_usedw;
86-
87-
8890// underflow and owerflow protection flags
8991logic w0_valid, w1_valid, w2_valid;
9092logic r0_valid, r1_valid, r2_valid;
@@ -148,11 +150,11 @@ always_ff @(posedge clk) begin
148150 end else begin
149151 if ( wr_ptr ) begin
150152 if ( wrreq[2 : 0 ] == 3'b010 && w1_valid ) begin
151- wr_ptr = ~ wr_ptr; // no protection against full
153+ wr_ptr = ~ wr_ptr;
152154 end
153155 end else begin
154156 if ( wrreq[2 : 0 ] == 3'b010 && w0_valid ) begin
155- wr_ptr = ~ wr_ptr; // no protection against full
157+ wr_ptr = ~ wr_ptr;
156158 end
157159 end
158160 end // nrst
221223
222224// internal FIFOs itself =======================================================
223225
226+ logic [1 : 0 ][USED_W - 2 : 0 ] f_usedw_i;
227+ logic [1 : 0 ][USED_W - 1 : 0 ] f_usedw;
228+
224229 scfifo # (
225230 .LPM_WIDTH ( WIDTH ),
226231 .LPM_NUMWORDS ( DEPTH / 2 ), // must be at least 4
231236 .ENABLE_ECC ( " FALSE" ),
232237 .ALLOW_RWCYCLE_WHEN_FULL ( " ON" ),
233238 .USE_EAB ( " ON" )
234- ) fifo0 (
239+ ) internal_fifo0 (
235240 .clock ( clk ),
236241 .aclr ( 1'b0 ),
237242 .sclr ( ~ nrst ),
243248 .q ( f_rddata[0 ][WIDTH - 1 : 0 ] ),
244249 .empty ( empty[0 ] ),
245250 .full ( full[0 ] ),
246- .usedw ( f_usedw [0 ][USED_W - 2 : 0 ] )
251+ .usedw ( f_usedw_i [0 ][USED_W - 2 : 0 ] )
247252 );
248253
249254 scfifo # (
256261 .ENABLE_ECC ( " FALSE" ),
257262 .ALLOW_RWCYCLE_WHEN_FULL ( " ON" ),
258263 .USE_EAB ( " ON" )
259- ) fifo1 (
264+ ) internal_fifo1 (
260265 .clock ( clk ),
261266 .aclr ( 1'b0 ),
262267 .sclr ( ~ nrst ),
@@ -268,11 +273,18 @@ end
268273 .q ( f_rddata[1 ][WIDTH - 1 : 0 ] ),
269274 .empty ( empty[1 ] ),
270275 .full ( full[1 ] ),
271- .usedw ( f_usedw [1 ][USED_W - 2 : 0 ] )
276+ .usedw ( f_usedw_i [1 ][USED_W - 2 : 0 ] )
272277 );
273278
274- assign usedw[USED_W - 1 : 0 ] = f_usedw[0 ][USED_W - 2 : 0 ] + f_usedw[1 ][USED_W - 2 : 0 ];
275-
279+ always_comb begin
280+ f_usedw[0 ][USED_W - 1 : 0 ] = ( full[0 ] )?
281+ ( 1 << (USED_W - 1 ) ):
282+ ( { 1'b0 ,f_usedw_i[0 ][USED_W - 2 : 0 ]} );
283+ f_usedw[1 ][USED_W - 1 : 0 ] = ( full[1 ] )?
284+ ( 1 << (USED_W - 1 ) ):
285+ ( { 1'b0 ,f_usedw_i[1 ][USED_W - 2 : 0 ]} );
286+ usedw[USED_W : 0 ] = f_usedw[0 ][USED_W - 1 : 0 ] + f_usedw[1 ][USED_W - 1 : 0 ];
287+ end
276288
277289endmodule
278290
0 commit comments