Skip to content

Commit 2d2041a

Browse files
committed
Fixed usedw[] calculations in preview_fifo. Other minor fixes
1 parent eb69a3a commit 2d2041a

File tree

4 files changed

+46
-32
lines changed

4 files changed

+46
-32
lines changed

dynamic_delay.sv

+9-7
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ dynamic_delay #(
3434

3535

3636
module dynamic_delay #( parameter
37-
LENGTH = 63, // maximum delay chain length
38-
WIDTH = 4, // data width
37+
LENGTH = 63, // maximum delay chain length
38+
WIDTH = 4, // data width
3939

40-
SEL_W = $clog2( (LENGTH+1)*WIDTH ) // output selector width
41-
// plus one is for zero delay element
40+
SEL_W = $clog2( (LENGTH+1)*WIDTH ) // output selector width
41+
// plus one is for zero delay element
4242
)(
4343
input clk,
4444
input nrst,
4545
input ena,
46-
input [WIDTH-1:0] in, // input data
47-
input [SEL_W-1:0] sel, // output selector
48-
output logic [WIDTH-1:0] out // output data
46+
input [WIDTH-1:0] in, // input data
47+
// bit in[0] is the "oldest" one
48+
// bit in[WIDTH] is considered the most recent
49+
input [SEL_W-1:0] sel, // output selector
50+
output logic [WIDTH-1:0] out // output data
4951
);
5052

5153

preview_fifo.sv

+32-20
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,25 @@
1818
1919
preview_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;
8187
logic [1:0] f_rdreq;
8288
logic [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
8991
logic w0_valid, w1_valid, w2_valid;
9092
logic 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
@@ -221,6 +223,9 @@ end
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
@@ -231,7 +236,7 @@ end
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 ),
@@ -243,7 +248,7 @@ end
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 #(
@@ -256,7 +261,7 @@ end
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

277289
endmodule
278290

preview_fifo_tb.sv

+3-3
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ end
126126

127127
logic [1:0] empty;
128128
logic [1:0] full;
129-
logic [4:0] usedw;
129+
logic [5:0] usedw;
130130

131131
logic [7:0] od0;
132132
logic [7:0] od1;
133133

134134
logic [2:0] rdreq;
135135
always_ff @(posedge clk200) begin
136136
`ifdef R_ENA
137-
if( (usedw[4:0] >= 4) ) begin //&& dis_writes ) begin
137+
if( (usedw[5:0] >= 4) ) begin //&& dis_writes ) begin
138138
if( RandomNumber1[14:13] == 2'b11 ) begin
139139
rdreq[2:0] <= 3'b010;
140140
//$display("RD 1 %h",od0[7:0]);
@@ -197,7 +197,7 @@ preview_fifo #(
197197

198198
.empty( empty[1:0] ),
199199
.full( full[1:0] ),
200-
.usedw( usedw[4:0] )
200+
.usedw( usedw[5:0] )
201201
);
202202

203203

pulse_stretch.sv

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
/* --- INSTANTIATION TEMPLATE BEGIN ---
1414
1515
pulse_stretch #(
16-
.WIDTH( 8 )
17-
.USE_COUNTER(0)
16+
.WIDTH( 8 ),
17+
.USE_CNTR( 0 )
1818
) ps1 (
1919
.clk( clk ),
2020
.nrst( nrst ),

0 commit comments

Comments
 (0)