OpenCores
URL https://opencores.org/ocsvn/bluespec-h264/bluespec-h264/trunk

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [src/] [mkBufferControl.bsv] - Blame information for rev 100

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 83 jamey.hick
 
2
// The MIT License
3
 
4
// Copyright (c) 2006-2007 Massachusetts Institute of Technology
5
 
6
// Permission is hereby granted, free of charge, to any person obtaining a copy
7
// of this software and associated documentation files (the "Software"), to deal
8
// in the Software without restriction, including without limitation the rights
9
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
// copies of the Software, and to permit persons to whom the Software is
11
// furnished to do so, subject to the following conditions:
12
 
13
// The above copyright notice and this permission notice shall be included in
14
// all copies or substantial portions of the Software.
15
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
// THE SOFTWARE.
23
 
24 2 jamey.hick
//**********************************************************************
25
// Buffer Controller
26
//----------------------------------------------------------------------
27
//
28
//
29
 
30
package mkBufferControl;
31
 
32
import H264Types::*;
33
 
34
import IBufferControl::*;
35
import FIFO::*;
36
import Vector::*;
37
 
38
import Connectable::*;
39
import GetPut::*;
40
import ClientServer::*;
41
 
42
 
43
 
44
//-----------------------------------------------------------
45
// Local Datatypes
46
//-----------------------------------------------------------
47
 
48
typedef union tagged
49
{
50
 void     Idle;          //not working on anything in particular
51
 void     Y;
52
 void     U;
53
 void     V;
54
}
55
Outprocess deriving(Eq,Bits);
56
 
57
 
58
//-----------------------------------------------------------
59
// Short term pic list submodule
60
//-----------------------------------------------------------
61
 
62
typedef union tagged
63
{
64
 void     Idle;          //not working on anything in particular
65
 void     Remove;
66
 void     RemoveOutput;
67
 void     RemoveFound;
68
 void     InsertGap;
69
 void     Search;
70
 void     ListAll;
71
}
72
ShortTermPicListState deriving(Eq,Bits);
73
 
74
interface ShortTermPicList;
75
   method Action clear();
76
   method Action insert( Bit#(16) frameNum, Bit#(5) slot, Bit#(5) maxAllowed );
77
   method Action insert_gap( Bit#(16) frameNum, Bit#(5) slot, Bit#(5) maxAllowed, Bit#(16) gap, Bit#(5) log2_max_frame_num );
78
   method Action remove( Bit#(16) frameNum, Bool removeOutputFlag );
79
   method Action search( Bit#(16) frameNum );
80
   method Action listAll();
81
   method Action deq();
82
   method Maybe#(Bit#(5)) resultSlot();
83
   method Bit#(5) numPics();
84
endinterface
85
 
86
module mkShortTermPicList( ShortTermPicList );
87
   function Bit#(5) shortTermPicListNext( Bit#(5) addrFunc );
88
      if(addrFunc
89
         return addrFunc+1;
90
      else
91
         return 0;
92
   endfunction
93
   function Bit#(5) shortTermPicListPrev( Bit#(5) addrFunc );
94
      if(addrFunc==0)
95
         return maxRefFrames-1;
96
      else
97
         return addrFunc-1;
98
   endfunction
99
 
100
   RFile1#(Bit#(5),Tuple2#(Bit#(16),Bit#(5))) rfile <- mkRFile1(0,maxRefFrames-1);
101
   Reg#(ShortTermPicListState) state <- mkReg(Idle);
102
   Reg#(Bit#(5))  log2_mfn  <- mkReg(0);
103
   Reg#(Bit#(5))  nextPic   <- mkReg(0);
104
   Reg#(Bit#(5))  picCount  <- mkReg(0);
105
   Reg#(Bit#(5))  tempPic   <- mkReg(0);
106
   Reg#(Bit#(5))  tempCount <- mkReg(0);
107
   Reg#(Bit#(16)) tempNum   <- mkReg(0);
108
   FIFO#(Maybe#(Bit#(5))) returnList <- mkFIFO();
109
 
110
   rule removing ( state==Remove || state==RemoveOutput || state==RemoveFound );
111
      if(state!=RemoveFound)
112
         begin
113 19 jamey.hick
            Tuple2#(Bit#(16),Bit#(5)) temp = rfile.sub(tempPic);
114 2 jamey.hick
            if(tpl_1(temp)==tempNum)
115
               begin
116
                  state <= RemoveFound;
117
                  if(state==RemoveOutput)
118 62 jamey.hick
                     returnList.enq(tagged Valid tpl_2(temp));
119 2 jamey.hick
               end
120
            if(tempCount>=picCount)
121
               $display( "ERROR BufferControl: ShortTermPicList removing not found");
122
         end
123
      else
124
         begin
125
            Bit#(5) tempPrev = shortTermPicListPrev(tempPic);
126 19 jamey.hick
            rfile.upd(tempPrev,rfile.sub(tempPic));
127 2 jamey.hick
            if(tempCount==picCount)
128
               begin
129
                  picCount <= picCount-1;
130
                  nextPic <= tempPrev;
131
                  state <= Idle;
132
               end
133
         end
134
      tempCount <= tempCount+1;
135
      tempPic <= shortTermPicListNext(tempPic);
136
   endrule
137
 
138
   rule insertingGap ( state matches tagged InsertGap );
139
      if(tempCount>0)
140
         begin
141
            if(tempCount>1)
142
               rfile.upd(nextPic,tuple2(tempNum,31));
143
            else
144
               rfile.upd(nextPic,tuple2(tempNum,tempPic));
145
            nextPic <= shortTermPicListNext(nextPic);
146
         end
147
      else
148
         state <= Idle;
149
      Bit#(17) tempOne = 1;
150
      Bit#(17) maxPicNum = tempOne << log2_mfn;
151
      if(zeroExtend(tempNum) == maxPicNum-1)
152
         tempNum <= 0;
153
      else
154
         tempNum <= tempNum+1;
155
      tempCount <= tempCount-1;
156
   endrule
157
 
158
   rule searching ( state matches tagged Search );
159
      if(tempCount
160
         begin
161 19 jamey.hick
            Tuple2#(Bit#(16),Bit#(5)) temp = rfile.sub(tempPic);
162 2 jamey.hick
            if(tpl_1(temp)==tempNum)
163
               begin
164 62 jamey.hick
                  returnList.enq(tagged Valid tpl_2(temp));
165 2 jamey.hick
                  state <= Idle;
166
               end
167
            tempPic <= shortTermPicListPrev(tempPic);
168
            tempCount <= tempCount+1;
169
         end
170
      else
171
         $display( "ERROR BufferControl: ShortTermPicList searching not found");
172
   endrule
173
 
174
   rule listingAll ( state matches tagged ListAll );
175
      if(tempCount
176
         begin
177 19 jamey.hick
            Tuple2#(Bit#(16),Bit#(5)) temp = rfile.sub(tempPic);
178 62 jamey.hick
            returnList.enq(tagged Valid tpl_2(temp));
179 2 jamey.hick
            tempPic <= shortTermPicListPrev(tempPic);
180
            tempCount <= tempCount+1;
181
         end
182
      else
183
         begin
184 62 jamey.hick
            returnList.enq(tagged Invalid);
185 2 jamey.hick
            state <= Idle;
186
         end
187
   endrule
188
 
189
   method Action clear() if(state matches tagged Idle);
190
      picCount <= 0;
191
      nextPic <= 0;
192
   endmethod
193
 
194
   method Action insert( Bit#(16) frameNum, Bit#(5) slot, Bit#(5) maxAllowed ) if(state matches tagged Idle);
195
      rfile.upd(nextPic,tuple2(frameNum,slot));
196
      nextPic <= shortTermPicListNext(nextPic);
197
      if(maxAllowed>picCount)
198
         picCount <= picCount+1;
199
   endmethod
200
 
201
   method Action insert_gap( Bit#(16) frameNum, Bit#(5) slot, Bit#(5) maxAllowed, Bit#(16) gap, Bit#(5) log2_max_frame_num ) if(state matches tagged Idle);
202
      state <= InsertGap;
203
      log2_mfn <= log2_max_frame_num;
204
      if(zeroExtend(picCount)+gap+1 >= zeroExtend(maxAllowed))
205
         picCount <= maxAllowed;
206
      else
207
         picCount <= truncate(zeroExtend(picCount)+gap+1);
208
      Bit#(5) temp;
209
      if(gap+1 >= zeroExtend(maxAllowed))
210
         temp = maxAllowed;
211
      else
212
         temp = truncate(gap+1);
213
      tempCount <= temp;
214
      Bit#(17) tempOne = 1;
215
      Bit#(17) maxPicNum = tempOne << log2_max_frame_num;
216
      Bit#(17) tempFrameNum = zeroExtend(frameNum);
217
      if(tempFrameNum+1 > zeroExtend(temp))
218
         tempNum <= truncate(tempFrameNum+1-zeroExtend(temp));
219
      else
220
         tempNum <= truncate(maxPicNum+tempFrameNum+1-zeroExtend(temp));
221
      tempPic <= slot;
222
   endmethod
223
 
224
   method Action remove( Bit#(16) frameNum, Bool removeOutputFlag ) if(state matches tagged Idle);
225
      if(removeOutputFlag)
226
         state <= RemoveOutput;
227
      else
228
         state <= Remove;
229
      tempCount <= 0;
230
      Bit#(5) temp = (maxRefFrames-picCount)+nextPic;
231
      if(temp>maxRefFrames-1)
232
         tempPic <= temp-maxRefFrames;
233
      else
234
         tempPic <= temp;
235
      tempNum <= frameNum;
236
   endmethod
237
 
238
   method Action search( Bit#(16) frameNum ) if(state matches tagged Idle);
239
      state <= Search;
240
      tempCount <= 0;
241
      tempPic <= shortTermPicListPrev(nextPic);
242
      tempNum <= frameNum;
243
   endmethod
244
 
245
   method Action listAll() if(state matches tagged Idle);
246
      state <= ListAll;
247
      tempCount <= 0;
248
      tempPic <= shortTermPicListPrev(nextPic);
249
   endmethod
250
 
251
   method Action deq();
252
      returnList.deq();
253
   endmethod
254
 
255
   method Maybe#(Bit#(5)) resultSlot();
256
      return returnList.first();
257
   endmethod
258
 
259
   method Bit#(5) numPics() if(state matches tagged Idle);
260
      return picCount;
261
   endmethod
262
endmodule
263
 
264
//-----------------------------------------------------------
265
// Long term pic list submodule
266
//-----------------------------------------------------------
267
 
268
typedef union tagged
269
{
270
 void     Idle;          //not working on anything in particular
271
 void     Clear;
272
 void     ListAll;
273
}
274
LongTermPicListState deriving(Eq,Bits);
275
 
276
interface LongTermPicList;
277
   method Action clear();
278
   method Action insert( Bit#(5) frameNum, Bit#(5) slot );
279
   method Action remove( Bit#(5) frameNum );
280
   method Action maxIndexPlus1( Bit#(5) maxAllowed );
281
   method Action search( Bit#(5) frameNum );
282
   method Action listAll();
283
   method Action deq();
284
   method Maybe#(Bit#(5)) resultSlot();
285
   method Bit#(5) numPics();
286
endinterface
287
 
288
module mkLongTermPicList( LongTermPicList );
289
//   RegFile#(Bit#(5),Maybe#(Bit#(5))) rfile <- mkRegFile(0,maxRefFrames-1);
290
   RFile1#(Bit#(5),Maybe#(Bit#(5))) rfile <- mkRFile1Full();
291
   Reg#(LongTermPicListState) state <- mkReg(Idle);
292
   Reg#(Bit#(5)) picCount <- mkReg(0);
293
   Reg#(Bit#(5)) tempPic  <- mkReg(0);
294
   FIFO#(Maybe#(Bit#(5))) returnList <- mkFIFO();
295
 
296
   rule clearing ( state matches tagged Clear );
297
      if(tempPic
298
         begin
299 19 jamey.hick
            if(rfile.sub(tempPic) matches tagged Valid .data &&& picCount!=0)
300 2 jamey.hick
               picCount <= picCount-1;
301
            rfile.upd(tempPic,Invalid);
302
            tempPic <= tempPic+1;
303
         end
304
      else
305
         state <= Idle;
306
      //$display( "TRACE BufferControl: LongTermPicList clearing %h %h", picCount, tempPic);
307
   endrule
308
 
309
   rule listingAll ( state matches tagged ListAll );
310
      if(tempPic
311
         begin
312 19 jamey.hick
            Maybe#(Bit#(5)) temp = rfile.sub(tempPic);
313 2 jamey.hick
            if(temp matches tagged Valid .data)
314 62 jamey.hick
               returnList.enq(tagged Valid data);
315 2 jamey.hick
            tempPic <= tempPic+1;
316
         end
317
      else
318
         begin
319 62 jamey.hick
            returnList.enq(tagged Invalid);
320 2 jamey.hick
            state <= Idle;
321
         end
322
      //$display( "TRACE BufferControl: LongTermPicList listingAll %h %h", picCount, tempPic);
323
   endrule
324
 
325
   method Action clear() if(state matches tagged Idle);
326
      state <= Clear;
327
      tempPic <= 0;
328
      //$display( "TRACE BufferControl: LongTermPicList clear %h", picCount);
329
   endmethod
330
 
331
   method Action insert( Bit#(5) frameNum, Bit#(5) slot ) if(state matches tagged Idle);
332 19 jamey.hick
      if(rfile.sub(frameNum) matches tagged Invalid)
333 2 jamey.hick
         picCount <= picCount+1;
334 62 jamey.hick
      rfile.upd(frameNum,tagged Valid slot);
335 2 jamey.hick
      //$display( "TRACE BufferControl: LongTermPicList insert %h %h %h", picCount, frameNum, slot);
336
   endmethod
337
 
338
   method Action remove( Bit#(5) frameNum ) if(state matches tagged Idle);
339 19 jamey.hick
      if(rfile.sub(frameNum) matches tagged Invalid)
340 2 jamey.hick
         $display( "ERROR BufferControl: LongTermPicList removing not found");
341
      else
342
         picCount <= picCount-1;
343
      rfile.upd(frameNum,Invalid);
344
      //$display( "TRACE BufferControl: LongTermPicList remove %h %h", picCount, frameNum);
345
   endmethod
346
 
347
   method Action maxIndexPlus1( Bit#(5) index ) if(state matches tagged Idle);
348
      state <= Clear;
349
      tempPic <= index;
350
      //$display( "TRACE BufferControl: LongTermPicList maxIndexPlus1 %h %h", picCount, index);
351
   endmethod
352
 
353
   method Action search( Bit#(5) frameNum ) if(state matches tagged Idle);
354 19 jamey.hick
      returnList.enq(rfile.sub(frameNum));
355 2 jamey.hick
      //$display( "TRACE BufferControl: LongTermPicList search %h %h", picCount, frameNum);
356
   endmethod
357
 
358
   method Action listAll() if(state matches tagged Idle);
359
      state <= ListAll;
360
      tempPic <= 0;
361
      //$display( "TRACE BufferControl: LongTermPicList listAll %h", picCount);
362
   endmethod
363
 
364
   method Action deq();
365
      returnList.deq();
366
      //$display( "TRACE BufferControl: LongTermPicList deq %h", picCount);
367
   endmethod
368
 
369
   method Maybe#(Bit#(5)) resultSlot();
370
      return returnList.first();
371
   endmethod
372
 
373
   method Bit#(5) numPics() if(state matches tagged Idle);
374
      return picCount;
375
   endmethod
376
endmodule
377
 
378
 
379
//-----------------------------------------------------------
380
// Free slot module
381
//-----------------------------------------------------------
382
 
383
interface FreeSlots;
384
   method Action  init();
385
   method Action  add( Bit#(5) slot );
386
   method Action  remove( Bit#(5) slot );
387
   method Bit#(5) first( Bit#(5) exception );
388
endinterface
389
 
390
module mkFreeSlots( FreeSlots );
391
   Reg#(Vector#(18,Bit#(1))) slots <- mkRegU();
392
 
393
   method Action  init();
394
      Vector#(18,Bit#(1)) tempSlots = replicate(0);
395
      slots <= tempSlots;
396
   endmethod
397
 
398
   method Action add( Bit#(5) slot );
399
      Vector#(18,Bit#(1)) tempSlots = slots;
400
      tempSlots[slot] = 0;
401
      slots <= tempSlots;
402
      if(slot >= maxRefFrames+2)
403
         $display( "ERROR BufferControl: FreeSlots add out of bounds");
404
   endmethod
405
 
406
   method Action remove( Bit#(5) slot );
407
      Vector#(18,Bit#(1)) tempSlots = slots;
408
      if(slot != 31)
409
         begin
410
            tempSlots[slot] = 1;
411
            slots <= tempSlots;
412
            if(slot >= maxRefFrames+2)
413
               $display( "ERROR BufferControl: FreeSlots remove out of bounds");
414
         end
415
   endmethod
416
 
417
   method Bit#(5) first( Bit#(5) exception );
418
      Bit#(5) tempout = 31;
419
      for(Integer ii=17; ii>=0; ii=ii-1)
420
         begin
421
            if(slots[fromInteger(ii)]==1'b0 && fromInteger(ii)!=exception)
422
               tempout = fromInteger(ii);
423
         end
424
      return tempout;
425
   endmethod
426
 
427
endmodule
428
 
429
 
430
//-----------------------------------------------------------
431
// Helper functions
432
 
433
 
434
 
435
//-----------------------------------------------------------
436
// Buffer Controller  Module
437
//-----------------------------------------------------------
438
 
439
 
440
(* synthesize *)
441
module mkBufferControl( IBufferControl );
442
 
443
   FIFO#(DeblockFilterOT) infifo  <- mkSizedFIFO(bufferControl_infifo_size);
444
   FIFO#(BufferControlOT) outfifo <- mkFIFO();
445
 
446
   FIFO#(FrameBufferLoadReq)  loadReqQ1  <- mkFIFO();
447
   FIFO#(FrameBufferLoadResp) loadRespQ1 <- mkFIFO();
448
   FIFO#(FrameBufferLoadReq)  loadReqQ2  <- mkFIFO();
449
   FIFO#(FrameBufferLoadResp) loadRespQ2 <- mkFIFO();
450
   FIFO#(FrameBufferStoreReq) storeReqQ  <- mkFIFO();
451
 
452
   FIFO#(InterpolatorLoadReq)  inLoadReqQ  <- mkFIFO();
453
   FIFO#(InterpolatorLoadResp) inLoadRespQ <- mkFIFO();
454
   FIFO#(Bit#(2)) inLoadOutOfBounds <- mkSizedFIFO(64);
455
 
456
   Reg#(Bit#(5)) log2_max_frame_num <- mkReg(0);
457
   Reg#(Bit#(5)) num_ref_frames <- mkReg(0);
458
   Reg#(Bit#(1)) gaps_in_frame_num_allowed_flag <- mkReg(0);
459
   Reg#(Bit#(PicWidthSz))  picWidth  <- mkReg(maxPicWidthInMB);
460
   Reg#(Bit#(PicHeightSz)) picHeight <- mkReg(0);
461
   Reg#(Bit#(PicAreaSz))   frameinmb <- mkReg(0);
462
 
463
   Reg#(Bit#(5))  ppsnum_ref_idx_l0_active <- mkReg(0);
464
   Reg#(Bit#(16)) frame_num <- mkReg(0);
465
   Reg#(Bit#(16)) prevRefFrameNum <- mkReg(0);
466
   Reg#(Bit#(5))  num_ref_idx_l0_active <- mkReg(0);
467
   Reg#(Bit#(2))  reordering_of_pic_nums_idc <- mkReg(0);
468
   Reg#(Bit#(16)) picNumLXPred <- mkReg(0);
469
   Reg#(Bit#(3))  memory_management_control_operation <- mkReg(0);
470
 
471
   Reg#(Bool) newInputFrame    <- mkReg(True);
472
   Reg#(Bool) noMoreInput      <- mkReg(False);
473
   Reg#(Bool) inputframedone   <- mkReg(False);
474
   Reg#(Outprocess) outprocess <- mkReg(Idle);
475
   Reg#(Bool) outputframedone  <- mkReg(True);
476
 
477
   Reg#(Bit#(5)) inSlot <- mkReg(0);
478
   Reg#(Bit#(FrameBufferSz)) inAddrBase <- mkReg(0);
479
   Reg#(Bit#(5)) outSlot <- mkReg(31);
480
   Reg#(Bit#(FrameBufferSz)) outAddrBase <- mkReg(0);
481
   Reg#(Bit#(TAdd#(PicAreaSz,7))) outReqCount <- mkReg(0);
482
   Reg#(Bit#(TAdd#(PicAreaSz,7))) outRespCount <- mkReg(0);
483
 
484
   FreeSlots freeSlots <- mkFreeSlots();//may include outSlot (have to make sure it's not used)
485
   ShortTermPicList shortTermPicList <- mkShortTermPicList();
486
   LongTermPicList  longTermPicList  <- mkLongTermPicList();
487
   RFile1#(Bit#(5),Bit#(5)) refPicList <- mkRFile1(0,maxRefFrames-1);
488
   Reg#(Bit#(5)) refPicListCount <- mkReg(0);
489
   Reg#(Bool) initRefPicList <- mkReg(False);
490
   Reg#(Bool) reorderRefPicList <- mkReg(False);
491
   Reg#(Bit#(5)) refIdx <- mkReg(0);
492
   Reg#(Bit#(5)) tempSlot <- mkReg(0);
493
   Reg#(Bit#(5)) tempSlot2 <- mkReg(0);
494
   Reg#(Bit#(2)) adjustFreeSlots <- mkReg(0);
495
 
496
   Reg#(Bool) refPicListDone <- mkReg(False);
497
   Reg#(Bool) lockInterLoads <- mkReg(True);
498
   DoNotFire donotfire <- mkDoNotFire();
499
 
500
 
501
   //-----------------------------------------------------------
502
   // Rules
503
 
504
   rule inputing ( !noMoreInput && !inputframedone );
505
      //$display( "Trace Buffer Control: passing infifo packed %h", pack(infifo.first()));
506
      case (infifo.first()) matches
507
         tagged EDOT .indata :
508
            begin
509
               case (indata) matches
510
                  tagged SPSlog2_max_frame_num .xdata :
511
                     begin
512
                        if(adjustFreeSlots == 0)
513
                           begin
514
                              infifo.deq();
515
                              log2_max_frame_num <= xdata;
516
                              freeSlots.init();
517
                              shortTermPicList.clear();
518
                              longTermPicList.clear();
519
                           end
520
                        else
521
                           donotfire.doNotFire();
522
                     end
523
                  tagged SPSnum_ref_frames .xdata :
524
                     begin
525
                        infifo.deq();
526
                        num_ref_frames <= xdata;
527
                     end
528
                  tagged SPSgaps_in_frame_num_allowed_flag .xdata :
529
                     begin
530
                        infifo.deq();
531
                        gaps_in_frame_num_allowed_flag <= xdata;
532
                     end
533
                  tagged SPSpic_width_in_mbs .xdata :
534
                     begin
535
                        infifo.deq();
536
                        picWidth <= xdata;
537
                     end
538
                  tagged SPSpic_height_in_map_units .xdata :
539
                     begin
540
                        infifo.deq();
541
                        picHeight <= xdata;
542
                        frameinmb <= zeroExtend(picWidth)*zeroExtend(xdata);
543
                     end
544
                  tagged PPSnum_ref_idx_l0_active .xdata :
545
                     begin
546
                        infifo.deq();
547
                        ppsnum_ref_idx_l0_active <= xdata;
548
                     end
549
                  tagged SHfirst_mb_in_slice .xdata :
550
                     begin
551
                        if(adjustFreeSlots == 0)
552
                           begin
553
                              infifo.deq();
554
                              newInputFrame <= False;
555
                              shortTermPicList.listAll();
556
                              longTermPicList.listAll();
557
                              initRefPicList <= True;
558
                              refPicListCount <= 0;
559
                              if(newInputFrame)
560
                                 begin
561
                                    inSlot <= freeSlots.first(outSlot);
562
                                    inAddrBase <= (zeroExtend(freeSlots.first(outSlot))*zeroExtend(frameinmb)*3)<<5;
563
                                 end
564
                              $display( "Trace BufferControl: passing SHfirst_mb_in_slice %h %h %0d", freeSlots.first(outSlot), outSlot, (newInputFrame ? 1 : 0));
565
                           end
566
                        else
567
                           donotfire.doNotFire();
568
                     end
569
                  tagged SHframe_num .xdata :
570
                     begin
571
                        infifo.deq();
572
                        frame_num <= xdata;
573
                        picNumLXPred <= frame_num;
574
                     end
575
                  tagged SHnum_ref_idx_active_override_flag .xdata :
576
                     begin
577
                        infifo.deq();
578
                        num_ref_idx_l0_active <= ppsnum_ref_idx_l0_active;
579
                     end
580
                  tagged SHnum_ref_idx_l0_active .xdata :
581
                     begin
582
                        infifo.deq();
583
                        num_ref_idx_l0_active <= xdata;
584
                     end
585
                  tagged SHRref_pic_list_reordering_flag_l0 .xdata :
586
                     begin
587
                        if(!initRefPicList)
588
                           begin
589
                              infifo.deq();
590
                              if(xdata==0)
591
                                 refPicListDone <= True;
592
                           end
593
                        else
594
                           donotfire.doNotFire();
595
                        refIdx <= 0;
596
                     end
597
                  tagged SHRreordering_of_pic_nums_idc .xdata :
598
                     begin
599
                        if(!reorderRefPicList)
600
                           begin
601
                              infifo.deq();
602
                              reordering_of_pic_nums_idc <= xdata;
603
                              if(xdata==3)
604
                                 refPicListDone <= True;
605
                           end
606
                        else
607
                           donotfire.doNotFire();
608
                     end
609
                  tagged SHRabs_diff_pic_num .xdata :
610
                     begin
611
                        if(!reorderRefPicList)
612
                           begin
613
                              infifo.deq();
614
                              Bit#(16) picNumLXNoWrap;
615
                              Bit#(17) tempOne = 1;
616
                              Bit#(17) maxPicNum = tempOne << log2_max_frame_num;
617
                              if(reordering_of_pic_nums_idc==0)
618
                                 begin
619
                                    if(picNumLXPred < truncate(xdata))
620
                                       picNumLXNoWrap = truncate(zeroExtend(picNumLXPred)-xdata+maxPicNum);
621
                                    else
622
                                       picNumLXNoWrap = truncate(zeroExtend(picNumLXPred)-xdata);
623
                                 end
624
                              else
625
                                 begin
626
                                    if(zeroExtend(picNumLXPred)+xdata >= maxPicNum)
627
                                       picNumLXNoWrap = truncate(zeroExtend(picNumLXPred)+xdata-maxPicNum);
628
                                    else
629
                                       picNumLXNoWrap = truncate(zeroExtend(picNumLXPred)+xdata);
630
                                 end
631
                              picNumLXPred <= picNumLXNoWrap;
632
                              shortTermPicList.search(picNumLXNoWrap);
633
                              reorderRefPicList <= True;
634
                              refPicListCount <= 0;
635
                           end
636
                        else
637
                           donotfire.doNotFire();
638
                     end
639
                  tagged SHRlong_term_pic_num .xdata :
640
                     begin
641
                        if(!reorderRefPicList)
642
                           begin
643
                              infifo.deq();
644
                              longTermPicList.search(xdata);
645
                              reorderRefPicList <= True;
646
                              refPicListCount <= 0;
647
                           end
648
                        else
649
                           donotfire.doNotFire();
650
                     end
651
                  tagged SHDlong_term_reference_flag .xdata :
652
                     begin
653
                        infifo.deq();
654
                        if(xdata==0)
655
                           shortTermPicList.insert(frame_num,inSlot,num_ref_frames);
656
                        else
657
                           longTermPicList.insert(0,inSlot);
658
                        adjustFreeSlots <= 1;
659
                     end
660
                  tagged SHDadaptive_ref_pic_marking_mode_flag .xdata :
661
                     begin
662
                        infifo.deq();
663
                        Bit#(17) tempFrameNum = zeroExtend(frame_num);
664
                        Bit#(17) tempOne = 1;
665
                        Bit#(17) maxPicNum = tempOne << log2_max_frame_num;
666
                        Bit#(16) tempGap = 0;
667
                        if(frame_num < prevRefFrameNum)
668
                           tempFrameNum = tempFrameNum + maxPicNum;
669
                        if(tempFrameNum-zeroExtend(prevRefFrameNum) > 1)
670
                           tempGap = truncate(tempFrameNum-zeroExtend(prevRefFrameNum)-1);
671
                        if(xdata==0)
672
                           begin
673
                              if(tempGap==0)
674
                                 shortTermPicList.insert(frame_num,inSlot,(num_ref_frames-longTermPicList.numPics()));
675
                              else
676
                                 shortTermPicList.insert_gap(frame_num,inSlot,(num_ref_frames-longTermPicList.numPics()),tempGap,log2_max_frame_num);
677
                              adjustFreeSlots <= 1;
678
                           end
679
                        prevRefFrameNum <= frame_num;
680
                     end
681
                  tagged SHDmemory_management_control_operation .xdata :
682
                     begin
683
                        infifo.deq();
684
                        memory_management_control_operation <= xdata;
685
                        if(xdata==0)
686
                           adjustFreeSlots <= 1;
687
                        else if(xdata==5)
688
                           begin
689
                              shortTermPicList.clear();
690
                              longTermPicList.clear();
691
                           end
692
                     end
693
                  tagged SHDdifference_of_pic_nums .xdata :
694
                     begin
695
                        infifo.deq();
696
                        Bit#(16) picNumXNoWrap;
697
                        Bit#(17) tempOne = 1;
698
                        Bit#(17) maxPicNum = tempOne << log2_max_frame_num;
699
                        if(frame_num < truncate(xdata))
700
                           picNumXNoWrap = truncate(zeroExtend(frame_num)-xdata+maxPicNum);
701
                        else
702
                           picNumXNoWrap = truncate(zeroExtend(frame_num)-xdata);
703
                        if(memory_management_control_operation == 1)
704
                           shortTermPicList.remove(picNumXNoWrap,False);
705
                        else
706
                           shortTermPicList.remove(picNumXNoWrap,True);
707
                     end
708
                  tagged SHDlong_term_pic_num .xdata :
709
                     begin
710
                        infifo.deq();
711
                        longTermPicList.remove(xdata);
712
                     end
713
                  tagged SHDlong_term_frame_idx .xdata :
714
                     begin
715
                        infifo.deq();
716
                        if(memory_management_control_operation == 3)
717
                           begin
718
                              if(shortTermPicList.resultSlot() matches tagged Valid .validdata)
719
                                 longTermPicList.insert(xdata,validdata);
720
                              else
721
                                 $display( "ERROR BufferControl: SHDlong_term_frame_idx Invalid output from shortTermPicList");
722
                              shortTermPicList.deq();
723
                           end
724
                        else
725
                           longTermPicList.insert(xdata,inSlot);
726
                     end
727
                  tagged SHDmax_long_term_frame_idx_plus1 .xdata :
728
                     begin
729
                        infifo.deq();
730
                        longTermPicList.maxIndexPlus1(xdata);
731
                     end
732
                  tagged EndOfFile :
733
                     begin
734
                        infifo.deq();
735
                        $display( "INFO Buffer Control: EndOfFile reached");
736
                        noMoreInput <= True;
737
                        //$finish(0);
738
                        //outfifo.enq(EndOfFile);
739
                     end
740 46 jamey.hick
                  default:
741
                     begin
742
                       $display("WARNING: Why are we in this clause");
743
                       infifo.deq();
744
                     end
745 2 jamey.hick
               endcase
746
            end
747
         tagged DFBLuma .indata :
748
            begin
749
               infifo.deq();
750
               //$display( "TRACE Buffer Control: input Luma %0d %h %h", indata.mb, indata.pixel, indata.data);
751
               Bit#(TAdd#(PicAreaSz,6)) addr = {(zeroExtend(indata.ver)*zeroExtend(picWidth)),2'b00}+zeroExtend(indata.hor);
752
               storeReqQ.enq(FBStoreReq {addr:inAddrBase+zeroExtend(addr),data:indata.data});
753
            end
754
         tagged DFBChroma .indata :
755
            begin
756
               infifo.deq();
757
               Bit#(TAdd#(PicAreaSz,4)) addr = {(zeroExtend(indata.ver)*zeroExtend(picWidth)),1'b0}+zeroExtend(indata.hor);
758
               Bit#(TAdd#(PicAreaSz,6)) chromaOffset = {frameinmb,6'b000000};
759
               Bit#(TAdd#(PicAreaSz,4)) vOffset = 0;
760
               if(indata.uv == 1)
761
                  vOffset = {frameinmb,4'b0000};
762
               storeReqQ.enq(FBStoreReq {addr:(inAddrBase+zeroExtend(chromaOffset)+zeroExtend(vOffset)+zeroExtend(addr)),data:indata.data});
763
               //$display( "TRACE Buffer Control: input Chroma %0d %0h %h %h %h %h", indata.uv, indata.ver, indata.hor, indata.data, addr, (inAddrBase+zeroExtend(chromaOffset)+zeroExtend(vOffset)+zeroExtend(addr)));
764
            end
765
         tagged EndOfFrame :
766
            begin
767
               infifo.deq();
768
               $display( "INFO Buffer Control: EndOfFrame reached");
769
               inputframedone <= True;
770
               newInputFrame <= True;
771
               refPicListDone <= False;
772
            end
773
         default: infifo.deq();
774
      endcase
775
   endrule
776
 
777
 
778
   rule initingRefPicList ( initRefPicList );
779
      if(shortTermPicList.resultSlot() matches tagged Valid .xdata)
780
         begin
781
            shortTermPicList.deq();
782
            refPicList.upd(refPicListCount,xdata);
783
            refPicListCount <= refPicListCount+1;
784
            $display( "Trace BufferControl: initingRefPicList shortTermPicList %h", xdata);
785
         end
786
      else if(longTermPicList.resultSlot() matches tagged Valid .xdata)
787
         begin
788
            longTermPicList.deq();
789
            refPicList.upd(refPicListCount,xdata);
790
            refPicListCount <= refPicListCount+1;
791
            $display( "Trace BufferControl: initingRefPicList longTermPicList %h", xdata);
792
         end
793
      else
794
         begin
795
            shortTermPicList.deq();
796
            longTermPicList.deq();
797
            initRefPicList <= False;
798
            refPicListCount <= 0;
799
            $display( "Trace BufferControl: initingRefPicList end");
800
         end
801
   endrule
802
 
803
 
804
   rule reorderingRefPicList ( reorderRefPicList );
805
      $display( "Trace BufferControl: reorderingRefPicList");
806
      if(shortTermPicList.resultSlot() matches tagged Valid .xdata)//////////////////////////////////////////////////////////////////////////////////////////
807
         begin
808
            shortTermPicList.deq();
809 19 jamey.hick
            tempSlot <= refPicList.sub(refIdx);
810 2 jamey.hick
            refPicList.upd(refIdx,xdata);
811
            refPicListCount <= refIdx+1;
812
            tempSlot2 <= xdata;
813
         end
814
      else if(longTermPicList.resultSlot() matches tagged Valid .xdata)/////////////////////////////////////////////////////////////////////////////////////may get stuck?
815
         begin
816
            longTermPicList.deq();
817 19 jamey.hick
            tempSlot <= refPicList.sub(refIdx);
818 2 jamey.hick
            refPicList.upd(refIdx,xdata);
819
            refPicListCount <= refIdx+1;
820
            tempSlot2 <= xdata;
821
         end
822
      else
823
         begin
824
            if(refPicListCount
825
               begin
826 19 jamey.hick
                  tempSlot <= refPicList.sub(refPicListCount);
827 2 jamey.hick
                  refPicList.upd(refPicListCount,tempSlot);
828
                  refPicListCount <= refPicListCount+1;
829
               end
830
            else
831
               begin
832
                  reorderRefPicList <= False;
833
                  refPicListCount <= 0;
834
                  refIdx <= refIdx+1;
835
               end
836
         end
837
   endrule
838
 
839
 
840
   rule adjustingFreeSlots ( adjustFreeSlots != 0 );
841
      if(adjustFreeSlots == 1)
842
         begin
843
            shortTermPicList.listAll();
844
            longTermPicList.listAll();
845
            freeSlots.init();
846
            adjustFreeSlots <= 2;
847
            $display( "Trace BufferControl: adjustingFreeSlots begin");
848
         end
849
      else
850
         begin
851
            if(shortTermPicList.resultSlot() matches tagged Valid .xdata)
852
               begin
853
                  shortTermPicList.deq();
854
                  freeSlots.remove(xdata);
855
                  $display( "Trace BufferControl: adjustingFreeSlots shortTermPicList %h", xdata);
856
               end
857
            else if(longTermPicList.resultSlot() matches tagged Valid .xdata)
858
               begin
859
                  longTermPicList.deq();
860
                  freeSlots.remove(xdata);
861
                  $display( "Trace BufferControl: adjustingFreeSlots longTermPicList %h", xdata);
862
               end
863
            else
864
               begin
865
                  shortTermPicList.deq();
866
                  longTermPicList.deq();
867
                  adjustFreeSlots <= 0;
868
                  $display( "Trace BufferControl: adjustingFreeSlots end");
869
               end
870
         end
871
   endrule
872
 
873
 
874
   rule outputingReq ( outprocess != Idle );
875
      if(outprocess==Y)
876
         begin
877
            loadReqQ1.enq(FBLoadReq (outAddrBase+zeroExtend(outReqCount)));
878
            if(outReqCount == {1'b0,frameinmb,6'b000000}-1)
879
               outprocess <= U;
880
            outReqCount <= outReqCount+1;
881
         end
882
      else if(outprocess==U)
883
         begin
884
            loadReqQ1.enq(FBLoadReq (outAddrBase+zeroExtend(outReqCount)));
885
            if(outReqCount == {1'b0,frameinmb,6'b000000}+{3'b000,frameinmb,4'b0000}-1)
886
               outprocess <= V;
887
            outReqCount <= outReqCount+1;
888
         end
889
      else
890
         begin
891
            //$display( "TRACE BufferControl: outputingReq V %h %h %h", outAddrBase, outReqCount, (outAddrBase+zeroExtend(outReqCount)));
892
            loadReqQ1.enq(FBLoadReq (outAddrBase+zeroExtend(outReqCount)));
893
            if(outReqCount == {1'b0,frameinmb,6'b000000}+{2'b00,frameinmb,5'b00000}-1)
894
               outprocess <= Idle;
895
            outReqCount <= outReqCount+1;
896
         end
897
   endrule
898
 
899
 
900
   rule outputingResp ( !outputframedone );
901
      if(loadRespQ1.first() matches tagged FBLoadResp .xdata)
902
         begin
903
            loadRespQ1.deq();
904 62 jamey.hick
            outfifo.enq(tagged YUV xdata);
905 2 jamey.hick
            if(outRespCount == {1'b0,frameinmb,6'b000000}+{2'b00,frameinmb,5'b00000}-1)
906
               outputframedone <= True;
907
            outRespCount <= outRespCount+1;
908
         end
909
   endrule
910
 
911
 
912
   rule goToNextFrame ( outputframedone && inputframedone && inLoadReqQ.first()==IPLoadEndFrame );
913
      inputframedone <= False;
914
      outprocess <= Y;
915
      outputframedone <= False;
916
      outSlot <= inSlot;
917
      outAddrBase <= inAddrBase;
918
      outReqCount <= 0;
919
      outRespCount <= 0;
920
      loadReqQ1.enq(FBEndFrameSync);
921
      loadReqQ2.enq(FBEndFrameSync);
922
      storeReqQ.enq(FBEndFrameSync);
923
      inLoadReqQ.deq();
924
      lockInterLoads <= True;
925
   endrule
926
 
927
 
928
   rule unlockInterLoads ( lockInterLoads && refPicListDone );
929
      lockInterLoads <= False;
930
   endrule
931
 
932
 
933
   rule theEndOfFile ( outputframedone && noMoreInput );
934
      outfifo.enq(EndOfFile);
935
   endrule
936
 
937
 
938
   rule interLumaReq ( inLoadReqQ.first() matches tagged IPLoadLuma .reqdata &&& !lockInterLoads );
939
      inLoadReqQ.deq();
940 19 jamey.hick
      Bit#(5) slot = refPicList.sub(zeroExtend(reqdata.refIdx));
941 2 jamey.hick
      Bit#(FrameBufferSz) addrBase = (zeroExtend(slot)*zeroExtend(frameinmb)*3)<<5;
942
      Bit#(TAdd#(PicAreaSz,6)) addr = {(zeroExtend(reqdata.ver)*zeroExtend(picWidth)),2'b00}+zeroExtend(reqdata.hor);
943
      inLoadOutOfBounds.enq({reqdata.horOutOfBounds,(reqdata.hor==0 ? 0 : 1)});
944
      loadReqQ2.enq(FBLoadReq (addrBase+zeroExtend(addr)));
945
      //$display( "Trace BufferControl: interLumaReq %h %h %h %h %h", reqdata.refIdx, slot, addrBase, addr, addrBase+zeroExtend(addr));
946
   endrule
947
 
948
 
949
   rule interChromaReq ( inLoadReqQ.first() matches tagged IPLoadChroma .reqdata &&& !lockInterLoads );
950 19 jamey.hick
      inLoadReqQ.deq();
951
      Bit#(5) slot = refPicList.sub(zeroExtend(reqdata.refIdx));
952 2 jamey.hick
      Bit#(FrameBufferSz) addrBase = (zeroExtend(slot)*zeroExtend(frameinmb)*3)<<5;
953
      Bit#(TAdd#(PicAreaSz,6)) chromaOffset = {frameinmb,6'b000000};
954
      Bit#(TAdd#(PicAreaSz,4)) vOffset = 0;
955
      if(reqdata.uv == 1)
956
         vOffset = {frameinmb,4'b0000};
957
      Bit#(TAdd#(PicAreaSz,6)) addr = {(zeroExtend(reqdata.ver)*zeroExtend(picWidth)),1'b0}+zeroExtend(reqdata.hor);
958
      inLoadOutOfBounds.enq({reqdata.horOutOfBounds,(reqdata.hor==0 ? 0 : 1)});
959
      loadReqQ2.enq(FBLoadReq (addrBase+zeroExtend(chromaOffset)+zeroExtend(vOffset)+zeroExtend(addr)));
960
      //$display( "Trace BufferControl: interChromaReq %h %h %h %h %h", reqdata.refIdx, slot, addrBase, addr, addrBase+zeroExtend(chromaOffset)+zeroExtend(vOffset)+zeroExtend(addr));
961
   endrule
962
 
963
 
964
   rule interResp ( loadRespQ2.first() matches tagged FBLoadResp .data );
965
      loadRespQ2.deq();
966
      if(inLoadOutOfBounds.first() == 2'b10)
967 62 jamey.hick
         inLoadRespQ.enq(tagged IPLoadResp ({data[7:0],data[7:0],data[7:0],data[7:0]}));
968 2 jamey.hick
      else if(inLoadOutOfBounds.first() == 2'b11)
969 62 jamey.hick
         inLoadRespQ.enq(tagged IPLoadResp ({data[31:24],data[31:24],data[31:24],data[31:24]}));
970 2 jamey.hick
      else
971 62 jamey.hick
         inLoadRespQ.enq(tagged IPLoadResp data);
972 2 jamey.hick
      inLoadOutOfBounds.deq();
973
      //$display( "Trace BufferControl: interResp %h %h", inLoadOutOfBounds.first(), data);
974
   endrule
975
 
976
 
977
 
978
   interface Put ioin  = fifoToPut(infifo);
979
   interface Get ioout = fifoToGet(outfifo);
980
   interface Client buffer_client_load1;
981
      interface Get request   = fifoToGet(loadReqQ1);
982
      interface Put response  = fifoToPut(loadRespQ1);
983
   endinterface
984
   interface Client buffer_client_load2;
985
      interface Get request   = fifoToGet(loadReqQ2);
986
      interface Put response  = fifoToPut(loadRespQ2);
987
   endinterface
988
   interface Get buffer_client_store = fifoToGet(storeReqQ);
989
   interface Server inter_server;
990
      interface Put request   = fifoToPut(inLoadReqQ);
991
      interface Get response  = fifoToGet(inLoadRespQ);
992
   endinterface
993
 
994
 
995
endmodule
996
 
997
endpackage

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.