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 15

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

powered by: WebSVN 2.1.0

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