-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipeline2.bsv
More file actions
348 lines (301 loc) · 10.9 KB
/
Pipeline2.bsv
File metadata and controls
348 lines (301 loc) · 10.9 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//----------------------------------------------------------------------//
// The MIT License
//
// Copyright (c) 2007 Alfred Man Cheuk Ng, mcn02@mit.edu
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//----------------------------------------------------------------------//
import Connectable::*;
import FIFO::*;
import GetPut::*;
import List::*;
import RWire::*;
import Vector::*;
import EHRReg::*;
//import Debug::*;
`ifndef DEBUG_PIPELINE
Bool debugPipeline=False;
`else
Bool debugPipeline=`DEBUG_PIPELINE;
`endif
interface Pipeline2#(type a);
interface Put#(a) in;
interface Get#(a) out;
endinterface
// bypass pipestage with rwire, need to guarantee get can always fire
module mkPipeStage_RWire#(function ActionValue#(a) f(a mesg))
(Pipeline2#(a)) provisos (Bits#(a,asz));
RWire#(a) canGet <- mkRWire;
interface Put in;
method Action put(a mesg);
let a_res <- f(mesg);
canGet.wset(a_res);
endmethod
endinterface
interface Get out;
method ActionValue#(a) get() if (isValid(canGet.wget));
return fromMaybe(?, canGet.wget);
endmethod
endinterface
endmodule
// normal pipeStage with FIFO
module mkPipeStage_FIFO#(function ActionValue#(a) f(a mesg))
(Pipeline2#(a)) provisos (Bits#(a,asz));
FIFO#(a) outQ <- mkSizedFIFO(2);
interface Put in;
method Action put(a mesg);
let a_res <- f(mesg);
outQ.enq(a_res);
endmethod
endinterface
interface out = fifoToGet(outQ);
endmodule
// normal pipeline
module [m] mkPipeline2_Norm#(Bit#(idx_sz) maxStage,
function m#(Pipeline2#(a)) mkP)
(Pipeline2#(a)) provisos (Bits#(a, asz),
IsModule#(m, mMod));
// state elements
Vector#(TExp#(idx_sz), Pipeline2#(a)) stageFUs = newVector;
FIFO#(a) outQ <- mkSizedFIFO(2);
stageFUs[0] <- mkP;
for (Bit#(idx_sz) i = 1; i <= maxStage; i = i + 1)
begin
stageFUs[i] <- mkP;
mkConnection(stageFUs[i-1].out, stageFUs[i].in);
end
mkConnection(stageFUs[maxStage].out, fifoToPut(outQ));
// methods
interface in = stageFUs[0].in;
interface out = fifoToGet(outQ);
endmodule // mkP
// circular pipeline, assume stageFU.out < stageFU.in or no conflict
module [m] mkPipeline2_Circ#(Bit#(idx_sz) maxStage,
function m#(Pipeline2#(a)) mkP)
(Pipeline2#(a)) provisos (Bits#(a, asz),IsModule#(m,mMod));
// instantiate sharable functional unit
//FIFO#(a) outQ <- mkSizedFIFO(2);
RWire#(a) outWire <- mkRWire;
Pipeline2#(a) stageFU <- mkP;
EHRReg#(2,Maybe#(Bit#(idx_sz))) stage <- mkEHRReg(Invalid);
RWire#(a) passData <- mkRWire;
// constants
Reg#(Maybe#(Bit#(idx_sz))) stage0 = (stage[0]);
Reg#(Maybe#(Bit#(idx_sz))) stage1 = (stage[1]);
rule getStageRes(isValid(stage0));
let curStage = fromMaybe(?,stage0);
let res <- stageFU.out.get;
if (curStage == maxStage) // finish
begin
outWire.wset(res);
stage0 <= tagged Invalid;
end
else
begin
passData.wset(res);
stage0 <= tagged Valid (curStage + 1);
end
// $display("circ.getStageRes: stage: %d",curStage);
endrule
rule execNextStage(isValid(stage1) && isValid(passData.wget));
let mesg = fromMaybe(?,passData.wget);
stageFU.in.put(mesg);
// $display("cir.execNextStage");
endrule
// rule printCheck(True);
// $display("maxStage = %d",maxStage);
// $display("isValid(stage[0]) = %d",isValid(stage0));
// $display("stage[0] = %d",fromMaybe(?,stage0));
// $display("isValid(stage[1]) = %d",isValid(stage1));
// $display("stage[1] = %d",fromMaybe(?,stage1));
// $display("isValid(passData.wget) = %d",isValid(passData.wget));
// // $display("passData.wget = %d",fromMaybe(?,passData.wget));
// endrule
interface Put in;
method Action put(a mesg) if (!isValid(stage1));
stageFU.in.put(mesg);
stage1 <= tagged Valid 0;
endmethod
endinterface
interface Get out;
method ActionValue#(a) get() if(outWire.wget matches tagged Valid .data);
return data;
endmethod
endinterface
endmodule // mkP
// time multiplex pipline
module [m] mkPipeline2_Time#(function m#(Pipeline2#(Vector#(psz,a))) mkP)
(Pipeline2#(Vector#(sz,a)))
provisos (Bits#(a, asz),
Div#(sz,psz,noStages), // div now change to return ceiling
Log#(noStages,stage_idx),
Mul#(noStages,psz,total_sz),
Add#(sz,ext_sz,total_sz),
Bits#(Vector#(total_sz,a),xxA),
Bits#(Vector#(noStages,Vector#(psz,a)),xxA),
IsModule#(m,mMod));
// constants
Integer maxStageInt = valueOf(noStages)-1;
Bit#(stage_idx) maxStage = fromInteger(maxStageInt);
Integer pSzInt = valueOf(psz);
// state element
Pipeline2#(Vector#(psz,a)) stageFU <- mkP;
Reg#(Bit#(stage_idx)) putStage <- mkReg(0);
Reg#(Bit#(stage_idx)) getStage <- mkReg(0);
Vector#(noStages,FIFO#(Vector#(psz,a))) inBuffers = newVector;
for (Integer i = 1; i <= maxStageInt; i = i + 1)
inBuffers[i] <- mkLFIFO;
Vector#(noStages,FIFO#(Vector#(psz,a))) outBuffers = newVector;
outBuffers <- replicateM(mkLFIFO);
rule startExec(putStage > 0);
begin
let mesg = inBuffers[putStage].first;
inBuffers[putStage].deq;
stageFU.in.put(mesg);
putStage <= (putStage == maxStage) ? 0 : putStage + 1;
if(debugPipeline)
begin
$display("time.startExec: putStage: %d",putStage);
end
end
endrule
rule finishExec(True);
begin
let mesg <- stageFU.out.get;
outBuffers[getStage].enq(mesg);
getStage <= (getStage == maxStage) ? 0 : getStage + 1;
if(debugPipeline)
begin
$display("time.finishExec: getStage: %d",getStage);
end
end
endrule
interface Put in;
method Action put(Vector#(sz,a) mesg) if (putStage == 0);
Vector#(ext_sz, a) extVec = newVector;
Vector#(total_sz, a) appendVec = append(mesg, extVec);
Vector#(noStages, Vector#(psz, a)) resVecs = unpack(pack(appendVec));
for (Integer i = 1; i <= maxStageInt; i = i + 1)
inBuffers[i].enq(resVecs[i]);
stageFU.in.put(resVecs[0]);
putStage <= (maxStageInt == 0) ? 0 : 1;
endmethod
endinterface
interface Get out;
method ActionValue#(Vector#(sz,a)) get();
Vector#(noStages, Vector#(psz, a)) outVecs = newVector;
for (Integer i = 0; i <= maxStageInt; i = i + 1)
begin
outVecs[i] = outBuffers[i].first;
outBuffers[i].deq;
end
Vector#(total_sz, a) appendVec = unpack(pack(outVecs));
return take(appendVec);
endmethod
endinterface
endmodule // mkP
// time multiplex pipline, with constant control information passed through.
// This is module subsumes the above module, and should eventually replace
// it.
// This is where the vertical pipeline folding occurs.
// This pipeline is synchronous, and may drop data.
module [m] mkPipeline2_TimeControl#(function m#(Pipeline2#(Tuple3#(ctrl_t,Bit#(stage_idx),Vector#(psz,a)))) mkP)
(Pipeline2#(Tuple2#(ctrl_t,Vector#(sz,a))))
provisos (Bits#(a, asz),
Bits#(ctrl_t,ctrl_t_sz),
Div#(sz,psz,noStages), // div now change to return ceiling
Log#(noStages,stage_idx),
Mul#(noStages,psz,total_sz),
Add#(sz,ext_sz,total_sz),
Add#(noStagesMinusOne, 1, noStages),
Bits#(Vector#(total_sz,a),xxA),
Bits#(Vector#(noStages,Vector#(psz,a)),xxA),
IsModule#(m,mMod));
// constants
Integer maxStageInt = valueOf(noStages)-1;
Bit#(stage_idx) maxStage = fromInteger(maxStageInt);
Integer pSzInt = valueOf(psz);
// perhaps odd stuff here?
FIFO#(Tuple2#(ctrl_t,Vector#(noStagesMinusOne,Vector#(psz,a)))) inBuffer <- mkLFIFO;
// state element
Pipeline2#(Tuple3#(ctrl_t,Bit#(stage_idx),Vector#(psz,a))) stageFU <- mkP;
Reg#(Bit#(stage_idx)) putStage <- mkReg(0);
Reg#(Bit#(stage_idx)) getStage <- mkReg(0);
Vector#(noStages,Reg#(Vector#(psz,a))) outRegs <-replicateM(mkReg(?));
Reg#(ctrl_t) ctrlReg <- mkReg(?);
Vector#(noStages,FIFO#(Bit#(0))) tokenFIFOs <- replicateM(mkLFIFO);
rule startExec(putStage > 0);
begin
match {.ctrl,.data} = inBuffer.first;
stageFU.in.put(tuple3(ctrl,putStage,data[putStage-1]));
putStage <= (putStage == maxStage) ? 0 : putStage + 1;
if(putStage == maxStage)
begin
if(debugPipeline)
begin
$display("Calling in buffer deq");
end
inBuffer.deq;
end
if(debugPipeline)
begin
$display("time.startExec: putStage: %d",putStage);
end
end
endrule
//Rules probably conflict...
rule finishExec;
begin
let mesg <- stageFU.out.get;
match {.ctrl,.num,.dataOut} = mesg; //I think num == maxStage... Maybe not?
ctrlReg <= ctrl;
tokenFIFOs[getStage].enq(0);
outRegs[getStage] <= dataOut;
getStage <= (getStage == maxStage) ? 0 : getStage + 1;
if(debugPipeline)
begin
$display("time.finishExec: getStage: %d ctrl: %d num: %d",getStage, ctrl, num);
end
end
endrule
interface Put in;
method Action put(Tuple2#(ctrl_t,Vector#(sz,a)) mesg) if (putStage == 0);
match {.ctrl, .data} = mesg;
Vector#(ext_sz, a) extVec = newVector;
Vector#(total_sz, a) appendVec = append(data, extVec);
Vector#(noStages, Vector#(psz, a)) resVecs = unpack(pack(appendVec));
if(maxStageInt > 0)
inBuffer.enq(tuple2(ctrl,takeTail(resVecs)));
stageFU.in.put(tuple3(ctrl,0,resVecs[0]));
putStage <= (maxStageInt == 0) ? 0 : 1;
endmethod
endinterface
interface Get out;
method ActionValue#(Tuple2#(ctrl_t,Vector#(sz,a))) get();
Vector#(total_sz, a) appendVec = unpack(pack(readVReg(outRegs)));
for (Integer i = 0; i <= maxStageInt; i = i + 1)
begin
tokenFIFOs[i].deq;
end
return tuple2(ctrlReg,take(appendVec));
endmethod
endinterface
endmodule // mkP