forked from pConst/basic_verilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preview_fifo.sv
207 lines (173 loc) · 5.04 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
//------------------------------------------------------------------------------
// preview_fifo.sv
// Konstantin Pavlov, [email protected]
//------------------------------------------------------------------------------
// INFO ------------------------------------------------------------------------
// module performs just like an ordinany FIFO in show-ahead mode
// the diffenence is that 0,1 or 2 words may be requested at once
// reader can also "preview" future fifo elements without actually fetching it
/* --- INSTANTIATION TEMPLATE BEGIN ---
preview_fifo #( parameter
.WIDTH( 16 ),
.DEPTH( 16 ),
) pf (
.clk( clk ),
.nrst( nrst ),
// input port
.wrreq( ),
.ena( ),
.id0( ),
.id1( ),
// output port
.valid( ),
.shift_req_oh( ),
.od( ),
.od( )
);
--- INSTANTIATION TEMPLATE END ---*/
module preview_fifo #( parameter
WIDTH = 32,
DEPTH = 16, // should be power of 2
USED_W = $clog2(DEPTH)
)(
input clk,
input nrst,
// input port
output wrreq, // request to make a write
input ena, // write data to fifo command
input [WIDTH-1:0] id0, // preceding data word
input [WIDTH-1:0] id1, // subsequent data word
// both data words are written simultaneously
// output port
output valid, // data outputs have valid data
input [2:0] shift_req_oh, // 3'b001 - no shift
// 3'b010 - shift one word
// 3'b100 - shift two words at once
output logic [WIDTH-1:0] od0, // first data word (preceding)
output logic [WIDTH-1:0] od1, // seconfd dadta word (subsequent)
// debug ports
output logic fail = 1'b0
);
// pointer to the first data word
logic ptr = 1'b0;
logic [WIDTH-1:0] fifo_d0;
logic [WIDTH-1:0] fifo_d1;
logic rdreq0 = 1'b0;
logic rdreq1 = 1'b0;
logic empty0;
logic empty1;
logic full0;
logic full1;
// usedw0[] == usedw1[] OR usedw0[] == (usedw1[]-1) combinations are possible
logic [USED_W-1:0] usedw0;
logic [USED_W-1:0] usedw1;
scfifo #(
.LPM_WIDTH( WIDTH ),
.LPM_NUMWORDS( DEPTH ),
.LPM_WIDTHU( USED_W ),
.LPM_SHOWAHEAD( "ON" ),
.UNDERFLOW_CHECKING( "ON" ),
.OVERFLOW_CHECKING( "ON" ),
.ALMOST_FULL_VALUE( DEPTH-4 ),
.ALMOST_EMPTY_VALUE( 0+4 ),
.ENABLE_ECC( "FALSE" ),
.ALLOW_RWCYCLE_WHEN_FULL( "ON" ),
.USE_EAB( "ON" ),
.MAXIMIZE_SPEED( 5 ),
.DEVICE_FAMILY( "Cyclone V" )
) internal_fifo0 (
.clock( clk ),
.aclr( 1'b0 ),
.sclr( ~nrst ),
.data( id0[WIDTH-1:0] ),
.wrreq( ena ),
.rdreq( rdreq0 ),
.q( fifo_d0[WIDTH-1:0] ),
.empty( empty0 ),
.full( full0 ),
.almost_full( ),
.almost_empty( ),
.usedw( usedw0[USED_W-1:0] ),
.eccstatus( ) // [1:0]
);
scfifo #(
.LPM_WIDTH( WIDTH ),
.LPM_NUMWORDS( DEPTH ),
.LPM_WIDTHU( USED_W ),
.LPM_SHOWAHEAD( "ON" ),
.UNDERFLOW_CHECKING( "ON" ),
.OVERFLOW_CHECKING( "ON" ),
.ALMOST_FULL_VALUE( DEPTH-4 ),
.ALMOST_EMPTY_VALUE( 0+4 ),
.ENABLE_ECC( "FALSE" ),
.ALLOW_RWCYCLE_WHEN_FULL( "ON" ),
.USE_EAB( "ON" ),
.MAXIMIZE_SPEED( 5 ),
.DEVICE_FAMILY( "Cyclone V" )
) internal_fifo1 (
.clock( clk ),
.aclr( 1'b0 ),
.sclr( ~nrst ),
.data( id1[WIDTH-1:0] ),
.wrreq( ena ),
.rdreq( rdreq1 ),
.q( fifo_d1[WIDTH-1:0] ),
.empty( empty1 ),
.full( full1 ),
.almost_full( ),
.almost_empty( ),
.usedw( usedw1[USED_W-1:0] ),
.eccstatus( ) // [1:0]
);
always_ff @(posedge clk) begin
if( ~nrst ) begin
ptr <= 1'b0;
fail <= 1'b0;
end else begin
if( ptr ) begin // d1 fifo has one extra unaligned word
case( shift_req_oh[2:0] )
3'b010: begin
ptr <= ~ptr;
od0[WIDTH-1:0] <= od1[WIDTH-1:0];
od1[WIDTH-1:0] <= fifo_d1[WIDTH-1:0];
rdreq0 = 1'b0;
rdreq1 = 1'b1;
end
3'b100: begin
//ptr <= ptr;
od0[WIDTH-1:0] <= fifo_d0[WIDTH-1:0];
od1[WIDTH-1:0] <= fifo_d1[WIDTH-1:0];
rdreq0 = 1'b1;
rdreq1 = 1'b1;
end
default: ; // do nothing
endcase
end else begin // aligned data
unique case( shift_req_oh[2:0] )
3'b010: begin
ptr <= ~ptr;
od0[WIDTH-1:0] <= od1[WIDTH-1:0];
od1[WIDTH-1:0] <= fifo_d0[WIDTH-1:0];
rdreq0 = 1'b1;
rdreq1 = 1'b0;
end
3'b100: begin
//ptr <= ptr;
od0[WIDTH-1:0] <= fifo_d1[WIDTH-1:0];
od1[WIDTH-1:0] <= fifo_d0[WIDTH-1:0];
rdreq0 = 1'b1;
rdreq1 = 1'b1;
end
default: ; // do nothing
endcase
end // if
if( |shift_req_oh[2:1] && empty0 ) begin
fail <= 1'b1;
end
end // ~nrst
end
// valid is when both output ports show valid data
assign valid = ~empty1;
// extra cycles for request and data propagation
assign wrreq = (usedw0[USED_W-1:0] < (DEPTH-4));
endmodule