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 84

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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