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

Subversion Repositories bluespec-h264

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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