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

Subversion Repositories bluespec-h264

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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