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 2

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

Line No. Rev Author Line
1 2 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(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(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(Valid tpl_2(temp));
156
            tempPic <= shortTermPicListPrev(tempPic);
157
            tempCount <= tempCount+1;
158
         end
159
      else
160
         begin
161
            returnList.enq(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(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,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: infifo.deq();
718
               endcase
719
            end
720
         tagged DFBLuma .indata :
721
            begin
722
               infifo.deq();
723
               //$display( "TRACE Buffer Control: input Luma %0d %h %h", indata.mb, indata.pixel, indata.data);
724
               Bit#(TAdd#(PicAreaSz,6)) addr = {(zeroExtend(indata.ver)*zeroExtend(picWidth)),2'b00}+zeroExtend(indata.hor);
725
               storeReqQ.enq(FBStoreReq {addr:inAddrBase+zeroExtend(addr),data:indata.data});
726
            end
727
         tagged DFBChroma .indata :
728
            begin
729
               infifo.deq();
730
               Bit#(TAdd#(PicAreaSz,4)) addr = {(zeroExtend(indata.ver)*zeroExtend(picWidth)),1'b0}+zeroExtend(indata.hor);
731
               Bit#(TAdd#(PicAreaSz,6)) chromaOffset = {frameinmb,6'b000000};
732
               Bit#(TAdd#(PicAreaSz,4)) vOffset = 0;
733
               if(indata.uv == 1)
734
                  vOffset = {frameinmb,4'b0000};
735
               storeReqQ.enq(FBStoreReq {addr:(inAddrBase+zeroExtend(chromaOffset)+zeroExtend(vOffset)+zeroExtend(addr)),data:indata.data});
736
               //$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)));
737
            end
738
         tagged EndOfFrame :
739
            begin
740
               infifo.deq();
741
               $display( "INFO Buffer Control: EndOfFrame reached");
742
               inputframedone <= True;
743
               newInputFrame <= True;
744
               refPicListDone <= False;
745
            end
746
         default: infifo.deq();
747
      endcase
748
   endrule
749
 
750
 
751
   rule initingRefPicList ( initRefPicList );
752
      if(shortTermPicList.resultSlot() matches tagged Valid .xdata)
753
         begin
754
            shortTermPicList.deq();
755
            refPicList.upd(refPicListCount,xdata);
756
            refPicListCount <= refPicListCount+1;
757
            $display( "Trace BufferControl: initingRefPicList shortTermPicList %h", xdata);
758
         end
759
      else if(longTermPicList.resultSlot() matches tagged Valid .xdata)
760
         begin
761
            longTermPicList.deq();
762
            refPicList.upd(refPicListCount,xdata);
763
            refPicListCount <= refPicListCount+1;
764
            $display( "Trace BufferControl: initingRefPicList longTermPicList %h", xdata);
765
         end
766
      else
767
         begin
768
            shortTermPicList.deq();
769
            longTermPicList.deq();
770
            initRefPicList <= False;
771
            refPicListCount <= 0;
772
            $display( "Trace BufferControl: initingRefPicList end");
773
         end
774
   endrule
775
 
776
 
777
   rule reorderingRefPicList ( reorderRefPicList );
778
      $display( "Trace BufferControl: reorderingRefPicList");
779
      if(shortTermPicList.resultSlot() matches tagged Valid .xdata)//////////////////////////////////////////////////////////////////////////////////////////
780
         begin
781
            shortTermPicList.deq();
782
            tempSlot <= refPicList.sub(refIdx);
783
            refPicList.upd(refIdx,xdata);
784
            refPicListCount <= refIdx+1;
785
            tempSlot2 <= xdata;
786
         end
787
      else if(longTermPicList.resultSlot() matches tagged Valid .xdata)/////////////////////////////////////////////////////////////////////////////////////may get stuck?
788
         begin
789
            longTermPicList.deq();
790
            tempSlot <= refPicList.sub(refIdx);
791
            refPicList.upd(refIdx,xdata);
792
            refPicListCount <= refIdx+1;
793
            tempSlot2 <= xdata;
794
         end
795
      else
796
         begin
797
            if(refPicListCount
798
               begin
799
                  tempSlot <= refPicList.sub(refPicListCount);
800
                  refPicList.upd(refPicListCount,tempSlot);
801
                  refPicListCount <= refPicListCount+1;
802
               end
803
            else
804
               begin
805
                  reorderRefPicList <= False;
806
                  refPicListCount <= 0;
807
                  refIdx <= refIdx+1;
808
               end
809
         end
810
   endrule
811
 
812
 
813
   rule adjustingFreeSlots ( adjustFreeSlots != 0 );
814
      if(adjustFreeSlots == 1)
815
         begin
816
            shortTermPicList.listAll();
817
            longTermPicList.listAll();
818
            freeSlots.init();
819
            adjustFreeSlots <= 2;
820
            $display( "Trace BufferControl: adjustingFreeSlots begin");
821
         end
822
      else
823
         begin
824
            if(shortTermPicList.resultSlot() matches tagged Valid .xdata)
825
               begin
826
                  shortTermPicList.deq();
827
                  freeSlots.remove(xdata);
828
                  $display( "Trace BufferControl: adjustingFreeSlots shortTermPicList %h", xdata);
829
               end
830
            else if(longTermPicList.resultSlot() matches tagged Valid .xdata)
831
               begin
832
                  longTermPicList.deq();
833
                  freeSlots.remove(xdata);
834
                  $display( "Trace BufferControl: adjustingFreeSlots longTermPicList %h", xdata);
835
               end
836
            else
837
               begin
838
                  shortTermPicList.deq();
839
                  longTermPicList.deq();
840
                  adjustFreeSlots <= 0;
841
                  $display( "Trace BufferControl: adjustingFreeSlots end");
842
               end
843
         end
844
   endrule
845
 
846
 
847
   rule outputingReq ( outprocess != Idle );
848
      if(outprocess==Y)
849
         begin
850
            loadReqQ1.enq(FBLoadReq (outAddrBase+zeroExtend(outReqCount)));
851
            if(outReqCount == {1'b0,frameinmb,6'b000000}-1)
852
               outprocess <= U;
853
            outReqCount <= outReqCount+1;
854
         end
855
      else if(outprocess==U)
856
         begin
857
            loadReqQ1.enq(FBLoadReq (outAddrBase+zeroExtend(outReqCount)));
858
            if(outReqCount == {1'b0,frameinmb,6'b000000}+{3'b000,frameinmb,4'b0000}-1)
859
               outprocess <= V;
860
            outReqCount <= outReqCount+1;
861
         end
862
      else
863
         begin
864
            //$display( "TRACE BufferControl: outputingReq V %h %h %h", outAddrBase, outReqCount, (outAddrBase+zeroExtend(outReqCount)));
865
            loadReqQ1.enq(FBLoadReq (outAddrBase+zeroExtend(outReqCount)));
866
            if(outReqCount == {1'b0,frameinmb,6'b000000}+{2'b00,frameinmb,5'b00000}-1)
867
               outprocess <= Idle;
868
            outReqCount <= outReqCount+1;
869
         end
870
   endrule
871
 
872
 
873
   rule outputingResp ( !outputframedone );
874
      if(loadRespQ1.first() matches tagged FBLoadResp .xdata)
875
         begin
876
            loadRespQ1.deq();
877
            outfifo.enq(YUV xdata);
878
            if(outRespCount == {1'b0,frameinmb,6'b000000}+{2'b00,frameinmb,5'b00000}-1)
879
               outputframedone <= True;
880
            outRespCount <= outRespCount+1;
881
         end
882
   endrule
883
 
884
 
885
   rule goToNextFrame ( outputframedone && inputframedone && inLoadReqQ.first()==IPLoadEndFrame );
886
      inputframedone <= False;
887
      outprocess <= Y;
888
      outputframedone <= False;
889
      outSlot <= inSlot;
890
      outAddrBase <= inAddrBase;
891
      outReqCount <= 0;
892
      outRespCount <= 0;
893
      loadReqQ1.enq(FBEndFrameSync);
894
      loadReqQ2.enq(FBEndFrameSync);
895
      storeReqQ.enq(FBEndFrameSync);
896
      inLoadReqQ.deq();
897
      lockInterLoads <= True;
898
   endrule
899
 
900
 
901
   rule unlockInterLoads ( lockInterLoads && refPicListDone );
902
      lockInterLoads <= False;
903
   endrule
904
 
905
 
906
   rule theEndOfFile ( outputframedone && noMoreInput );
907
      outfifo.enq(EndOfFile);
908
   endrule
909
 
910
 
911
   rule interLumaReq ( inLoadReqQ.first() matches tagged IPLoadLuma .reqdata &&& !lockInterLoads );
912
      inLoadReqQ.deq();
913
      Bit#(5) slot = refPicList.sub(zeroExtend(reqdata.refIdx));
914
      Bit#(FrameBufferSz) addrBase = (zeroExtend(slot)*zeroExtend(frameinmb)*3)<<5;
915
      Bit#(TAdd#(PicAreaSz,6)) addr = {(zeroExtend(reqdata.ver)*zeroExtend(picWidth)),2'b00}+zeroExtend(reqdata.hor);
916
      inLoadOutOfBounds.enq({reqdata.horOutOfBounds,(reqdata.hor==0 ? 0 : 1)});
917
      loadReqQ2.enq(FBLoadReq (addrBase+zeroExtend(addr)));
918
      //$display( "Trace BufferControl: interLumaReq %h %h %h %h %h", reqdata.refIdx, slot, addrBase, addr, addrBase+zeroExtend(addr));
919
   endrule
920
 
921
 
922
   rule interChromaReq ( inLoadReqQ.first() matches tagged IPLoadChroma .reqdata &&& !lockInterLoads );
923
      inLoadReqQ.deq();
924
      Bit#(5) slot = refPicList.sub(zeroExtend(reqdata.refIdx));
925
      Bit#(FrameBufferSz) addrBase = (zeroExtend(slot)*zeroExtend(frameinmb)*3)<<5;
926
      Bit#(TAdd#(PicAreaSz,6)) chromaOffset = {frameinmb,6'b000000};
927
      Bit#(TAdd#(PicAreaSz,4)) vOffset = 0;
928
      if(reqdata.uv == 1)
929
         vOffset = {frameinmb,4'b0000};
930
      Bit#(TAdd#(PicAreaSz,6)) addr = {(zeroExtend(reqdata.ver)*zeroExtend(picWidth)),1'b0}+zeroExtend(reqdata.hor);
931
      inLoadOutOfBounds.enq({reqdata.horOutOfBounds,(reqdata.hor==0 ? 0 : 1)});
932
      loadReqQ2.enq(FBLoadReq (addrBase+zeroExtend(chromaOffset)+zeroExtend(vOffset)+zeroExtend(addr)));
933
      //$display( "Trace BufferControl: interChromaReq %h %h %h %h %h", reqdata.refIdx, slot, addrBase, addr, addrBase+zeroExtend(chromaOffset)+zeroExtend(vOffset)+zeroExtend(addr));
934
   endrule
935
 
936
 
937
   rule interResp ( loadRespQ2.first() matches tagged FBLoadResp .data );
938
      loadRespQ2.deq();
939
      if(inLoadOutOfBounds.first() == 2'b10)
940
         inLoadRespQ.enq(IPLoadResp ({data[7:0],data[7:0],data[7:0],data[7:0]}));
941
      else if(inLoadOutOfBounds.first() == 2'b11)
942
         inLoadRespQ.enq(IPLoadResp ({data[31:24],data[31:24],data[31:24],data[31:24]}));
943
      else
944
         inLoadRespQ.enq(IPLoadResp data);
945
      inLoadOutOfBounds.deq();
946
      //$display( "Trace BufferControl: interResp %h %h", inLoadOutOfBounds.first(), data);
947
   endrule
948
 
949
 
950
 
951
   interface Put ioin  = fifoToPut(infifo);
952
   interface Get ioout = fifoToGet(outfifo);
953
   interface Client buffer_client_load1;
954
      interface Get request   = fifoToGet(loadReqQ1);
955
      interface Put response  = fifoToPut(loadRespQ1);
956
   endinterface
957
   interface Client buffer_client_load2;
958
      interface Get request   = fifoToGet(loadReqQ2);
959
      interface Put response  = fifoToPut(loadRespQ2);
960
   endinterface
961
   interface Get buffer_client_store = fifoToGet(storeReqQ);
962
   interface Server inter_server;
963
      interface Put request   = fifoToPut(inLoadReqQ);
964
      interface Get response  = fifoToGet(inLoadRespQ);
965
   endinterface
966
 
967
 
968
endmodule
969
 
970
endpackage

powered by: WebSVN 2.1.0

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