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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [LumaChromaParallel/] [mkPrediction.bsv] - Blame information for rev 90

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

Line No. Rev Author Line
1 86 jamey.hick
// The MIT License
2
 
3
// Copyright (c) 2006-2007 Massachusetts Institute of Technology
4
 
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
 
12
// The above copyright notice and this permission notice shall be included in
13
// all copies or substantial portions of the Software.
14
 
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
// THE SOFTWARE.
22
//**********************************************************************
23
// Prediction
24
//----------------------------------------------------------------------
25
//
26
//
27
 
28
package mkPrediction;
29
 
30
import H264Types::*;
31
 
32
import IPrediction::*;
33
import IInterpolator::*;
34
import mkInterpolator::*;
35
import FIFO::*;
36
import FIFOF::*;
37
import Vector::*;
38
 
39
import Connectable::*;
40
import GetPut::*;
41
import ClientServer::*;
42
 
43
 
44
//-----------------------------------------------------------
45
// Local Datatypes
46
//-----------------------------------------------------------
47
 
48
typedef union tagged
49
{
50
 void     Intra;            //Intra non-4x4
51
 void     Intra4x4;
52
 void     Inter;
53
}
54
OutState deriving(Eq,Bits);
55
 
56
typedef union tagged
57
{
58
 void     Start;            //not working on anything in particular
59
 void     Intra16x16;
60
 void     Intra4x4;
61
 void     IntraPCM;
62
}
63
IntraState deriving(Eq,Bits);
64
 
65
typedef union tagged
66
{
67
 void     Start;            //not working on anything in particular
68
 void     InterP16x16;
69
 void     InterP16x8;
70
 void     InterP8x16;
71
 void     InterP8x8;
72
 void     InterP8x8ref0;
73
 void     InterPskip;
74
}
75
InterState deriving(Eq,Bits);
76
 
77
typedef union tagged
78
{
79
 Bit#(1) NotInter;//0 for not available, 1 for intra-coded
80
 struct {Bit#(4) refIdx; Bit#(14) mvhor; Bit#(12) mvver; Bit#(1) nonZeroTransCoeff;} BlockMv;
81
}
82
InterBlockMv deriving(Eq,Bits);
83
 
84
typedef union tagged
85
{
86
 void SkipMB;
87
 void NonSkipMB;
88
 void Intra4x4;
89
 void Intra4x4PlusChroma;
90
}
91
NextOutput deriving(Eq,Bits);
92
 
93
 
94
 
95
//-----------------------------------------------------------
96
// Helper functions
97
 
98
function Bit#(8) intra4x4SelectTop( Bit#(72) valVector, Bit#(4) idx );
99
   case(idx)
100
      0: return valVector[15:8];
101
      1: return valVector[23:16];
102
      2: return valVector[31:24];
103
      3: return valVector[39:32];
104
      4: return valVector[47:40];
105
      5: return valVector[55:48];
106
      6: return valVector[63:56];
107
      7: return valVector[71:64];
108
      default: return valVector[7:0];
109
   endcase
110
endfunction
111
 
112
function Bit#(8) intra4x4SelectLeft( Bit#(40) valVector, Bit#(3) idx );
113
   case(idx)
114
      0: return valVector[15:8];
115
      1: return valVector[23:16];
116
      2: return valVector[31:24];
117
      3: return valVector[39:32];
118
      default: return valVector[7:0];
119
   endcase
120
endfunction
121
 
122
function Bit#(8) select32to8( Bit#(32) valVector, Bit#(2) idx );
123
   case(idx)
124
      0: return valVector[7:0];
125
      1: return valVector[15:8];
126
      2: return valVector[23:16];
127
      3: return valVector[31:24];
128
   endcase
129
endfunction
130
 
131
function Bit#(8) select16to8( Bit#(16) valVector, Bit#(1) idx );
132
   case(idx)
133
      0: return valVector[7:0];
134
      1: return valVector[15:8];
135
   endcase
136
endfunction
137
 
138
function Bool absDiffGEFour14( Bit#(14) val1, Bit#(14) val2 );
139
   Int#(15) int1 = unpack(signExtend(val1));
140
   Int#(15) int2 = unpack(signExtend(val2));
141
   if(int1>=int2)
142
      return (int1 >= (int2+4));
143
   else
144
      return (int2 >= (int1+4));
145
endfunction
146
 
147
function Bool absDiffGEFour12( Bit#(12) val1, Bit#(12) val2 );
148
   Int#(13) int1 = unpack(signExtend(val1));
149
   Int#(13) int2 = unpack(signExtend(val2));
150
   if(int1>=int2)
151
      return (int1 >= (int2+4));
152
   else
153
      return (int2 >= (int1+4));
154
endfunction
155
 
156
 
157
//-----------------------------------------------------------
158
// Prediction Module
159
//-----------------------------------------------------------
160
 
161
 
162
(* synthesize *)
163
module mkPrediction( IPrediction );
164
 
165
   //Common state
166
   FIFO#(EntropyDecOT)   infifo     <- mkSizedFIFO(prediction_infifo_size);
167
   FIFO#(InverseTransOT) infifo_ITB <- mkSizedFIFO(prediction_infifo_ITB_size);
168 90 jamey.hick
   FIFO#(EntropyDecOT)   outfifochroma  <- mkFIFO;
169
   FIFO#(EntropyDecOT)   outfifoluma    <- mkFIFO;
170 86 jamey.hick
   Reg#(Bool)            passFlag   <- mkReg(True);
171
   Reg#(Bit#(4))         blockNum   <- mkReg(0);
172
   Reg#(Bit#(4))         pixelNum   <- mkReg(0);
173
 
174
   Reg#(Bit#(PicWidthSz))  picWidth  <- mkReg(maxPicWidthInMB);
175
   Reg#(Bit#(PicHeightSz)) picHeight <- mkReg(0);
176
   Reg#(Bit#(PicAreaSz))   firstMb   <- mkReg(0);
177
   Reg#(Bit#(PicAreaSz))   currMb    <- mkReg(0);
178
   Reg#(Bit#(PicAreaSz))   currMbHor <- mkReg(0);//horizontal position of currMb
179
   Reg#(Bit#(PicHeightSz)) currMbVer <- mkReg(0);//vertical position of currMb
180
 
181
   FIFOF#(OutState)   outstatefifo   <- mkFIFOF;
182
   FIFOF#(NextOutput) nextoutputfifo <- mkFIFOF;
183
   Reg#(Bit#(4))   outBlockNum    <- mkReg(0);
184
   Reg#(Bit#(4))   outPixelNum    <- mkReg(0);
185
   FIFO#(Vector#(4,Bit#(8))) predictedfifo  <- mkSizedFIFO(prediction_predictedfifo_size);
186
   Reg#(ChromaFlag)   outChromaFlag  <- mkReg(Luma);
187
   Reg#(Bool)      outFirstQPFlag <- mkReg(False);
188
 
189
   DoNotFire donotfire <- mkDoNotFire();
190
 
191
   //Reg#(Vector#(16,Bit#(8))) workVector       <- mkRegU();
192
 
193
   //Inter state
194
   Interpolator interpolator <- mkInterpolator();
195
   Reg#(InterState) interstate <- mkReg(Start);
196
   Reg#(Bit#(PicAreaSz)) interPskipCount <- mkReg(0);
197
   Reg#(Vector#(5,InterBlockMv)) interTopVal <- mkRegU();
198
   Reg#(Vector#(4,InterBlockMv)) interLeftVal <- mkRegU();
199
   Reg#(Vector#(4,InterBlockMv)) interTopLeftVal <- mkRegU();
200
   FIFO#(MemReq#(TAdd#(PicWidthSz,2),32)) interMemReqQ <- mkFIFO;
201 90 jamey.hick
 
202 86 jamey.hick
   Reg#(MemReq#(TAdd#(PicWidthSz,2),32)) interMemReqQdelay <- mkRegU();
203
   FIFO#(MemResp#(32))  interMemRespQ <- mkFIFO;
204
   Reg#(Bit#(3)) interReqCount <- mkReg(0);
205
   Reg#(Bit#(3)) interRespCount <- mkReg(0);
206
 
207
   Reg#(Bit#(2)) interStepCount <- mkReg(0);
208
   Reg#(Bit#(2)) interMbPartNum <- mkReg(0);
209
   Reg#(Bit#(2)) interSubMbPartNum <- mkReg(0);
210
   Reg#(Bit#(2)) interPassingCount <- mkReg(0);
211
   Reg#(Vector#(4,Bit#(4))) interRefIdxVector <- mkRegU();
212
   Reg#(Vector#(4,Bit#(2))) interSubMbTypeVector <- mkRegU();
213
   RFile1#(Bit#(4),Tuple2#(Bit#(14),Bit#(12))) interMvFile <- mkRFile1Full();
214
   Reg#(Bit#(15)) interMvDiffTemp <- mkReg(0);
215
   FIFO#(Tuple2#(Bit#(15),Bit#(13))) interMvDiff <- mkFIFO;
216
   Reg#(Bit#(5)) interNewestMv <- mkReg(0);
217 90 jamey.hick
 
218
 
219 86 jamey.hick
   // Registers for pipelining the interStage rule
220
 
221
   Reg#(Bit#(3)) partWidthR <- mkRegU();
222
   Reg#(Bit#(3)) partHeightR <- mkRegU();
223
   Reg#(Bit#(3)) numPartR <- mkRegU();
224
   Reg#(Bit#(3)) numSubPartR <- mkRegU();
225
   Reg#(Bit#(2)) subMbTypeR <- mkRegU();
226
   Reg#(Bool) calcmvR <- mkRegU();
227
   Reg#(Bool) leftmvR <- mkRegU();
228
   Reg#(Bit#(4)) refIndexR <- mkRegU();
229
   Reg#(Vector#(3,InterBlockMv)) blockABCR <- mkRegU();
230
   Reg#(Bit#(14)) mvhorfinalR <- mkRegU();
231
   Reg#(Bit#(12)) mvverfinalR <- mkRegU();
232
   Reg#(Bit#(5)) interNewestMvNextR <- mkRegU();
233
 
234
 
235
   Reg#(Bit#(2)) interIPStepCount <- mkReg(0);
236
   Reg#(Bit#(2)) interIPMbPartNum <- mkReg(0);
237
   Reg#(Bit#(2)) interIPSubMbPartNum <- mkReg(0);
238
 
239
   Reg#(Bit#(PicWidthSz)) interCurrMbDiff <- mkReg(0);
240
 
241
   Reg#(Vector#(4,Bool)) interTopNonZeroTransCoeff <- mkRegU();
242
   Reg#(Vector#(4,Bool)) interLeftNonZeroTransCoeff <- mkRegU();
243
   FIFO#(Tuple2#(Bit#(2),Bit#(2))) interBSfifo <- mkSizedFIFO(32);
244
   Reg#(Bool) interBSoutput <- mkReg(True);
245
   FIFO#(InterBlockMv) interOutBlockMvfifo <- mkSizedFIFO(8);
246 90 jamey.hick
   FIFO#(ChromaFlag) memReqTypeFIFO <- mkSizedFIFO(32);
247 86 jamey.hick
 
248
 
249
   //Intra state
250
   Reg#(IntraState)     intrastate      <- mkReg(Start);
251
   Reg#(Bit#(1))        intraChromaFlag <- mkReg(0);
252
   FIFO#(MemReq#(TAdd#(PicWidthSz,2),68)) intraMemReqQ  <- mkFIFO;
253
   Reg#(MemReq#(TAdd#(PicWidthSz,2),68)) intraMemReqQdelay <- mkRegU;
254
   FIFO#(MemResp#(68))  intraMemRespQ <- mkFIFO;
255
   Reg#(Vector#(4,Bit#(4))) intra4x4typeLeft <- mkRegU();//15=unavailable, 14=inter-MB, 13=intra-non-4x4
256
   Reg#(Vector#(4,Bit#(4))) intra4x4typeTop  <- mkRegU();//15=unavailable, 14=inter-MB, 13=intra-non-4x4
257
   Reg#(Bit#(1)) ppsconstrained_intra_pred_flag <- mkReg(0);
258
   Reg#(Vector#(4,Bit#(40))) intraLeftVal <- mkRegU();
259
   Reg#(Vector#(9,Bit#(8))) intraLeftValChroma0 <- mkRegU();
260
   Reg#(Vector#(9,Bit#(8))) intraLeftValChroma1 <- mkRegU();
261
   Reg#(Vector#(5,Bit#(32))) intraTopVal <- mkRegU();
262
   Reg#(Vector#(4,Bit#(16))) intraTopValChroma0 <- mkRegU();
263
   Reg#(Vector#(4,Bit#(16))) intraTopValChroma1 <- mkRegU();
264
   Reg#(Bit#(32)) intraLeftValNext <- mkReg(0);
265
   Reg#(Bit#(2)) intra16x16_pred_mode <- mkReg(0);
266
   FIFO#(Bit#(4)) rem_intra4x4_pred_mode <- mkSizedFIFO(16);
267
   FIFO#(Bit#(2)) intra_chroma_pred_mode <- mkFIFO;
268
   Reg#(Bit#(4)) cur_intra4x4_pred_mode <- mkReg(0);
269
   Reg#(Bit#(1)) intraChromaTopAvailable <- mkReg(0);
270
   Reg#(Bit#(1)) intraChromaLeftAvailable <- mkReg(0);
271
 
272
   Reg#(Bit#(3)) intraReqCount <- mkReg(0);
273
   Reg#(Bit#(3)) intraRespCount <- mkReg(0);
274
   Reg#(Bit#(4)) intraStepCount <- mkReg(0);
275
   Reg#(Bit#(13)) intraSumA <-  mkReg(0);
276
   Reg#(Bit#(15)) intraSumB <-  mkReg(0);
277
   Reg#(Bit#(15)) intraSumC <-  mkReg(0);
278
 
279
   Reg#(Vector#(4,Bit#(8))) intraPredVector <- mkRegU();
280
 
281
   //-----------------------------------------------------------
282
   // Rules
283
 
284
   //////////////////////////////////////////////////////////////////////////////
285
//   rule stateMonitor ( True );
286
//      if(predictedfifo.notEmpty())
287
//       $display( "TRACE Prediction: stateMonitor predictedfifo.first() %0d", predictedfifo.first());////////////////////
288
//      if(infifo.first() matches tagged ITBresidual .xdata)
289
//       $display( "TRACE Prediction: stateMonitor infifo.first() %0d", xdata);////////////////////
290
//      if(infifo.first() matches tagged ITBresidual .xdata)
291
//       $display( "TRACE Prediction: stateMonitor outBlockNum outPixelNum outChromaFlag %0d %0d", outBlockNum, outPixelNum, outChromaFlag);////////////////////
292
//   endrule
293
   //////////////////////////////////////////////////////////////////////////////
294
 
295
   rule checkFIFO ( True );
296
      $display( "Trace Prediction: checkFIFO %h", infifo.first() );
297
   endrule
298
   rule checkFIFO_ITB ( True );
299
      $display( "Trace Prediction: checkFIFO_ITB %h", infifo_ITB.first() );
300
   endrule
301
   rule checkFIFO_predicted ( True );
302
      $display( "Trace Prediction: checkFIFO_predicted %h", predictedfifo.first() );
303
   endrule
304
 
305
 
306
   rule passing ( passFlag && !outstatefifo.notEmpty() && currMbHor
307
      $display( "Trace Prediction: passing infifo packed %h", pack(infifo.first()));
308
      case (infifo.first()) matches
309
         tagged NewUnit . xdata :
310
            begin
311
               infifo.deq();
312 90 jamey.hick
               outfifoluma.enq(infifo.first());
313
               outfifochroma.enq(infifo.first());
314 86 jamey.hick
               $display("ccl4newunit");
315
               $display("ccl4rbspbyte %h", xdata);
316
            end
317
         tagged SPSpic_width_in_mbs .xdata :
318
            begin
319
               infifo.deq();
320 90 jamey.hick
               outfifoluma.enq(infifo.first());
321
               outfifochroma.enq(infifo.first());
322 86 jamey.hick
               picWidth <= xdata;
323
               interpolator.setPicWidth(xdata);
324
            end
325
         tagged SPSpic_height_in_map_units .xdata :
326
            begin
327
               infifo.deq();
328 90 jamey.hick
               outfifoluma.enq(infifo.first());
329
               outfifochroma.enq(infifo.first());
330 86 jamey.hick
               picHeight <= xdata;
331
               interpolator.setPicHeight(xdata);
332
            end
333
         tagged PPSconstrained_intra_pred_flag .xdata :
334
            begin
335
               infifo.deq();
336
               ppsconstrained_intra_pred_flag <= xdata;
337
            end
338
         tagged SHfirst_mb_in_slice .xdata :
339
            begin
340
               infifo.deq();
341 90 jamey.hick
               outfifoluma.enq(infifo.first());
342
               outfifochroma.enq(infifo.first());
343 86 jamey.hick
               firstMb   <= xdata;
344
               currMb    <= xdata;
345
               currMbHor <= xdata;
346
               currMbVer <= 0;
347
               intra4x4typeLeft <= replicate(15);
348
               interTopLeftVal <= replicate(tagged NotInter 0);
349
               if(xdata==0)
350
                  interLeftVal <= replicate(tagged NotInter 0);
351
               outFirstQPFlag <= True;
352
            end
353
         tagged SDmb_skip_run .xdata : passFlag <= False;
354
         tagged SDMmbtype .xdata : passFlag <= False;
355
         tagged EndOfFile :
356
            begin
357
               infifo.deq();
358 90 jamey.hick
               outfifochroma.enq(infifo.first());
359
               outfifoluma.enq(infifo.first());
360 86 jamey.hick
               $display( "INFO Prediction: EndOfFile reached" );
361
               //$finish(0);////////////////////////////////
362
            end
363
         default:
364
            begin
365
               infifo.deq();
366 90 jamey.hick
               outfifoluma.enq(infifo.first());
367
               outfifochroma.enq(infifo.first());
368 86 jamey.hick
            end
369
      endcase
370
   endrule
371
 
372
 
373
   rule inputing ( !passFlag );
374
      $display( "Trace Prediction: inputing infifo packed %h", pack(infifo.first()));
375
      case (infifo.first()) matches
376
         tagged SDmb_skip_run .xdata :
377
            begin
378
               if(interstate==Start && intrastate==Start)
379
                  begin
380
                     if(interPskipCount < xdata)
381
                        begin
382
                           if(!outstatefifo.notEmpty() || interCurrMbDiff
383
                              begin
384
                                 $display( "Trace Prediction: passing SDmb_skip_run %0d", xdata);
385
                                 outstatefifo.enq(Inter);
386
                                 interstate <= InterPskip;
387
                                 interReqCount <= 1;
388
                                 interRespCount <= 1;
389
                                 intra4x4typeLeft <= replicate(14);
390
                                 intra4x4typeTop <= replicate(14);
391
                                 interTopLeftVal <= update(interTopLeftVal , 0, (tagged NotInter 0));
392
                                 interTopVal <= replicate(tagged NotInter 0);
393
                                 interPskipCount <= interPskipCount+1;
394
                                 interNewestMv <= 0;
395
                                 interRefIdxVector <= replicate(0);
396
                                 interCurrMbDiff <= interCurrMbDiff+1;
397
                                 nextoutputfifo.enq(SkipMB);
398
                              end
399
                           else
400
                              donotfire.doNotFire();
401
                        end
402
                     else
403
                        begin
404
                           $display( "Trace Prediction: passing no SDmb_skip_run");
405
                           interPskipCount <= 0;
406
                           infifo.deq();
407
                        end
408
                  end
409
               else
410
                  donotfire.doNotFire();
411
            end
412
         tagged SDMmbtype .xdata :
413
            begin
414
               if(interstate==Start && intrastate==Start)//not necessary (just need to keep inter from feeding predictedfifo or change intra state until intrastate==Start)
415
                  begin
416
                     infifo.deq();
417
                     $display( "INFO Prediction: SDMmbtype %0d", xdata);
418
                     if(mbPartPredMode(xdata,0)==Intra_16x16)
419
                        begin
420
                           if(!outstatefifo.notEmpty())
421
                              begin
422
                                 outstatefifo.enq(Intra);
423
                                 intrastate <= Intra16x16;
424
                                 if(xdata matches tagged I_16x16 {intra16x16PredMode:.tempv1, codedBlockPatternChroma:.tempv2, codedBlockPatternLuma:.tempv3})
425
                                    intra16x16_pred_mode <= tempv1;
426
                                 else
427
                                    $display( "ERROR Prediction: MacroblockLayer 5 sdmmbtype not I_16x16" );
428
                                 intraReqCount <= 1;
429
                                 intraRespCount <= 1;
430
                                 interTopLeftVal <= replicate(tagged NotInter 1);
431
                                 interLeftVal <= replicate(tagged NotInter 1);
432
                                 interTopVal <= replicate(tagged NotInter 1);
433
                              end
434
                           else
435
                              donotfire.doNotFire();
436
                        end
437
                     else if(xdata==I_NxN)
438
                        begin
439
                           if(!outstatefifo.notEmpty())
440
                              begin
441
                                 outstatefifo.enq(Intra4x4);
442
                                 intrastate <= Intra4x4;
443
                                 intraReqCount <= 1;
444
                                 intraRespCount <= 1;
445
                                 interTopLeftVal <= replicate(tagged NotInter 1);
446
                                 interLeftVal <= replicate(tagged NotInter 1);
447
                                 interTopVal <= replicate(tagged NotInter 1);
448
                              end
449
                           else
450
                              donotfire.doNotFire();
451
                        end
452
                     else if(xdata==I_PCM)
453
                        begin
454
                           $display( "ERROR Prediction: I_PCM not implemented yet");
455
                           $finish;////////////////////////////////////////////////////////////////////////////////////////
456
                           intra4x4typeLeft <= replicate(13);
457
                           intra4x4typeTop <= replicate(13);
458
                           interTopLeftVal <= replicate(tagged NotInter 1);
459
                           interLeftVal <= replicate(tagged NotInter 1);
460
                           interTopVal <= replicate(tagged NotInter 1);
461
                        end
462
                     else
463
                        begin
464
                           if(!outstatefifo.notEmpty() || interCurrMbDiff
465
                              begin
466
                                 outstatefifo.enq(Inter);
467
                                 case(xdata)
468
                                    P_L0_16x16: interstate <= InterP16x16;
469
                                    P_L0_L0_16x8: interstate <= InterP16x8;
470
                                    P_L0_L0_8x16: interstate <= InterP8x16;
471
                                    P_8x8: interstate <= InterP8x8;
472
                                    P_8x8ref0: interstate <= InterP8x8ref0;
473
                                    default: $display( "ERROR Prediction: passing SDMmbtype inter prediction unknown mbtype");
474
                                 endcase
475
                                 interReqCount <= 1;
476
                                 interRespCount <= 1;
477
                                 intra4x4typeLeft <= replicate(14);/////////////////////////////////////////////////////////////////////////////
478
                                 intra4x4typeTop <= replicate(14);
479
                                 interTopLeftVal <= update(interTopLeftVal , 0, (tagged NotInter 0));
480
                                 interTopVal <= replicate(tagged NotInter 0);
481
                                 interNewestMv <= 0;
482
                                 interRefIdxVector <= replicate(0);
483
                                 nextoutputfifo.enq(NonSkipMB);
484
                              end
485
                           else
486
                              donotfire.doNotFire();
487
                        end
488
                     interCurrMbDiff <= interCurrMbDiff+1;
489
                  end
490
               else
491
                  donotfire.doNotFire();
492
            end
493
         tagged SDMMrem_intra4x4_pred_mode .xdata :
494
            begin
495
               infifo.deq();
496
               ////outfifo.enq(infifo.first());
497
               rem_intra4x4_pred_mode.enq(xdata);
498
            end
499
         tagged SDMMintra_chroma_pred_mode .xdata :
500
            begin
501
               infifo.deq();
502
               ////outfifo.enq(infifo.first());
503
               intra_chroma_pred_mode.enq(xdata);
504
            end
505
         tagged SDMMref_idx_l0 .xdata :
506
            begin
507
               infifo.deq();
508
               ////outfifo.enq(infifo.first());
509
               interRefIdxVector <= update(interRefIdxVector,interPassingCount,xdata[3:0]);
510
               if(interstate==InterP16x16 || interPassingCount==1)
511
                  interPassingCount <= 0;
512
               else
513
                  interPassingCount <= interPassingCount+1;
514
            end
515
         tagged SDMMmvd_l0 .xdata :
516
            begin
517
               infifo.deq();
518
               ////outfifo.enq(infifo.first());
519
               if(interPassingCount==1)
520
                  begin
521
                     Bit#(13) interMvDiffTemp2 = truncate(xdata);
522
                     interMvDiff.enq(tuple2(interMvDiffTemp,interMvDiffTemp2));
523
                     interPassingCount <= 0;
524
                  end
525
               else
526
                  begin
527
                     interMvDiffTemp <= truncate(xdata);
528
                     interPassingCount <= interPassingCount+1;
529
                  end
530
            end
531
         tagged SDMSsub_mb_type .xdata :
532
            begin
533
               infifo.deq();
534
               ////outfifo.enq(infifo.first());
535
               interSubMbTypeVector <= update(interSubMbTypeVector,interPassingCount,xdata);
536
               interPassingCount <= interPassingCount+1;
537
            end
538
         tagged SDMSref_idx_l0 .xdata :
539
            begin
540
               infifo.deq();
541
               ////outfifo.enq(infifo.first());
542
               interRefIdxVector <= update(interRefIdxVector,interPassingCount,xdata[3:0]);
543
               interPassingCount <= interPassingCount+1;
544
            end
545
         tagged SDMSmvd_l0 .xdata :
546
            begin
547
               infifo.deq();
548
               ////outfifo.enq(infifo.first());
549
               if(interPassingCount==1)
550
                  begin
551
                     Bit#(13) interMvDiffTemp2 = truncate(xdata);
552
                     interMvDiff.enq(tuple2(interMvDiffTemp,interMvDiffTemp2));
553
                     interPassingCount <= 0;
554
                  end
555
               else
556
                  begin
557
                     interMvDiffTemp <= truncate(xdata);
558
                     interPassingCount <= interPassingCount+1;
559
                  end
560
            end
561
         default: passFlag <= True;
562
      endcase
563
   endrule
564
 
565
 
566
   rule outputing ( currMbHor
567
      Bit#(1) outputFlag = 0;
568
      Vector#(4,Bit#(8)) outputVector = replicate(0);
569
      Bit#(2) blockHor = {outBlockNum[2],outBlockNum[0]};
570
      Bit#(2) blockVer = {outBlockNum[3],outBlockNum[1]};
571
      Bit#(2) pixelVer = {outPixelNum[3],outPixelNum[2]};
572
      Bit#(4) totalVer = {blockVer,pixelVer};
573 87 jamey.hick
      $display( "bsFIFO Trace Prediction: outputing (%d,%d)", blockVer,blockHor );
574 86 jamey.hick
      if(outFirstQPFlag)
575
         begin
576
            if(infifo_ITB.first() matches tagged IBTmb_qp .xdata)
577
               begin
578
                  infifo_ITB.deq();
579 90 jamey.hick
                  outfifoluma.enq(IBTmb_qp {qpy:xdata.qpy,qpc:xdata.qpc});
580
                  outfifochroma.enq(IBTmb_qp {qpy:xdata.qpy,qpc:xdata.qpc});
581 86 jamey.hick
                  outFirstQPFlag <= False;
582
                  $display( "Trace Prediction: outputing outFirstQP %h %h %h", outBlockNum, outPixelNum, xdata);
583
               end
584
            else
585
               $display( "ERROR Prediction: outputing unexpected infifo_ITB.first()");
586
         end
587
      else if(nextoutputfifo.first() == SkipMB)
588
         begin
589
            if(interBSoutput && outChromaFlag==Luma && outPixelNum==0)
590
               begin
591
                  interBSoutput <= False;
592
                  interBSfifo.deq();
593
                  Bit#(2) tempHorBS = tpl_1(interBSfifo.first());
594
                  Bit#(2) tempVerBS = tpl_2(interBSfifo.first());
595
                  Bit#(3) horBS = (tempHorBS==3 ? 4 : (interLeftNonZeroTransCoeff[blockVer] ? 2 : zeroExtend(tempHorBS)));
596
                  Bit#(3) verBS = (tempVerBS==3 ? 4 : (interTopNonZeroTransCoeff[blockHor]&&blockVer!=0 ? 2 : zeroExtend(tempVerBS)));
597 90 jamey.hick
                  outfifoluma.enq(PBbS {bShor:horBS,bSver:verBS,blockNum: outBlockNum});
598
                  outfifochroma.enq(PBbS {bShor:horBS,bSver:verBS,blockNum: outBlockNum});
599 86 jamey.hick
                  interLeftNonZeroTransCoeff <= update(interLeftNonZeroTransCoeff, blockVer, False);
600
                  interTopNonZeroTransCoeff <= update(interTopNonZeroTransCoeff, blockHor, False);
601
                  $display( "Trace Prediction: outputing SkipMB bS %h %h %h %h", outBlockNum, outPixelNum, currMbHor, currMbVer);
602
               end
603
            else
604
               begin
605
                  interBSoutput <= True;
606
                  outputVector = predictedfifo.first();
607 90 jamey.hick
                  outfifoluma.enq(tagged PBoutput tuple2(outChromaFlag,outputVector));
608
                  outfifochroma.enq(tagged PBoutput tuple2(outChromaFlag,outputVector));
609 86 jamey.hick
                  outputFlag = 1;
610
                  predictedfifo.deq();
611
                  $display( "Trace Prediction: outputing SkipMB out %h %h %h", outBlockNum, outPixelNum, outputVector);
612
               end
613
         end
614
      else
615
         begin
616
            case ( infifo_ITB.first() ) matches
617
               tagged IBTmb_qp .xdata :
618
                  begin
619
                     infifo_ITB.deq();
620 90 jamey.hick
                     outfifoluma.enq(tagged IBTmb_qp {qpy:xdata.qpy,qpc:xdata.qpc});
621
                     outfifochroma.enq(tagged IBTmb_qp {qpy:xdata.qpy,qpc:xdata.qpc});
622 86 jamey.hick
                     outFirstQPFlag <= False;
623
                     $display( "Trace Prediction: outputing ITBmb_qp %h %h %h", outBlockNum, outPixelNum, xdata);
624
                  end
625
               tagged ITBresidual .xdata :
626
                  begin
627
                     if(interBSoutput && outChromaFlag==Luma && outPixelNum==0)
628
                        begin
629
                           interBSoutput <= False;
630
                           if(outstatefifo.first() != Inter)
631 90 jamey.hick
                              begin
632
                                 outfifoluma.enq(tagged PBbS {bShor:(blockHor==0 ? 4 : 3),bSver:(blockVer==0 ? 4 : 3),blockNum: outBlockNum});
633
                                 outfifochroma.enq(tagged PBbS {bShor:(blockHor==0 ? 4 : 3),bSver:(blockVer==0 ? 4 : 3),blockNum: outBlockNum});
634
                              end
635 86 jamey.hick
                           else
636
                              begin
637
                                 interBSfifo.deq();
638
                                 Bit#(2) tempHorBS = tpl_1(interBSfifo.first());
639
                                 Bit#(2) tempVerBS = tpl_2(interBSfifo.first());
640
                                 Bit#(3) horBS = (tempHorBS==3 ? 4 : 2);
641
                                 Bit#(3) verBS = (tempVerBS==3 ? 4 : 2);
642 90 jamey.hick
                                 outfifoluma.enq(tagged PBbS {bShor:horBS,bSver:verBS,blockNum: outBlockNum});
643
                                 outfifochroma.enq(tagged PBbS {bShor:horBS,bSver:verBS,blockNum: outBlockNum});
644 86 jamey.hick
                              end
645
                           interLeftNonZeroTransCoeff <= update(interLeftNonZeroTransCoeff, blockVer, True);
646
                           interTopNonZeroTransCoeff <= update(interTopNonZeroTransCoeff, blockHor, True);
647
                           $display( "Trace Prediction: outputing ITBresidual bS %h %h %h %h %h", outChromaFlag, outBlockNum, outPixelNum, currMbHor, currMbVer);
648
                        end
649
                     else
650
                        begin
651
                           interBSoutput <= True;
652
                           Bit#(11) tempOutputValue = 0;
653
                           for(Integer ii=0; ii<4; ii=ii+1)
654
                              begin
655
                                 tempOutputValue = signExtend(xdata[ii]) + zeroExtend((predictedfifo.first())[ii]);
656
                                 if(tempOutputValue[10]==1)
657
                                    outputVector[ii] = 0;
658
                                 else if(tempOutputValue[9:0] > 255)
659
                                    outputVector[ii] = 255;
660
                                 else
661
                                    outputVector[ii] = tempOutputValue[7:0];
662
                              end
663 90 jamey.hick
                           outfifoluma.enq(tagged PBoutput tuple2(outChromaFlag,outputVector));
664
                           outfifochroma.enq(tagged PBoutput tuple2(outChromaFlag,outputVector));
665 86 jamey.hick
                           infifo_ITB.deq();
666
                           predictedfifo.deq();
667
                           outputFlag = 1;
668
                           $display( "Trace Prediction: outputing ITBresidual out %h %h %h %h %h %h", outChromaFlag, outBlockNum, outPixelNum, predictedfifo.first(), xdata, outputVector);
669
                        end
670
                  end
671
               tagged ITBcoeffLevelZeros :
672
                  begin
673
                     if(interBSoutput && outChromaFlag==Luma && outPixelNum==0)
674
                        begin
675
                           interBSoutput <= False;
676
                           if(outstatefifo.first() != Inter)
677 90 jamey.hick
                              begin
678
                                 outfifoluma.enq(tagged PBbS {bShor:(blockHor==0 ? 4 : 3),bSver:(blockVer==0 ? 4 : 3),blockNum: outBlockNum});
679
                                 outfifochroma.enq(tagged PBbS {bShor:(blockHor==0 ? 4 : 3),bSver:(blockVer==0 ? 4 : 3),blockNum: outBlockNum});
680
                              end
681 86 jamey.hick
                           else
682
                              begin
683
                                 interBSfifo.deq();
684
                                 Bit#(2) tempHorBS = tpl_1(interBSfifo.first());
685
                                 Bit#(2) tempVerBS = tpl_2(interBSfifo.first());
686
                                 Bit#(3) horBS = (tempHorBS==3 ? 4 : (interLeftNonZeroTransCoeff[blockVer] ? 2 : zeroExtend(tempHorBS)));
687
                                 Bit#(3) verBS = (tempVerBS==3 ? 4 : (interTopNonZeroTransCoeff[blockHor]&&blockVer!=0 ? 2 : zeroExtend(tempVerBS)));
688 90 jamey.hick
                                 outfifoluma.enq(tagged PBbS {bShor:horBS,bSver:verBS,blockNum: outBlockNum});
689
                                 outfifochroma.enq(tagged PBbS {bShor:horBS,bSver:verBS,blockNum: outBlockNum});
690 86 jamey.hick
                              end
691
                           interLeftNonZeroTransCoeff <= update(interLeftNonZeroTransCoeff, blockVer, False);
692
                           interTopNonZeroTransCoeff <= update(interTopNonZeroTransCoeff, blockHor, False);
693
                           $display( "Trace Prediction: outputing ITBcoeffLevelZeros bS %h %h %h %h %h", outChromaFlag, outBlockNum, outPixelNum, currMbHor, currMbVer);
694
                        end
695
                     else
696
                        begin
697
                           interBSoutput <= True;
698
                           if(outPixelNum == 12)
699
                              infifo_ITB.deq();
700
                           outputVector = predictedfifo.first();
701 90 jamey.hick
                           outfifoluma.enq(tagged PBoutput tuple2(outChromaFlag,outputVector));
702
                           outfifochroma.enq(tagged PBoutput tuple2(outChromaFlag,outputVector));
703 86 jamey.hick
                           outputFlag = 1;
704
                           predictedfifo.deq();
705
                           $display( "Trace Prediction: outputing ITBcoeffLevelZeros out %h %h %h %h %h", outChromaFlag, outBlockNum, outPixelNum, predictedfifo.first(), outputVector);
706
                        end
707
                  end
708
               default: $display( "ERROR Prediction: outputing unknown infifo_ITB input" );
709
            endcase
710
         end
711
 
712
      if(outputFlag == 1)
713
         begin
714
            $display("ccl4PBoutput %0d", outputVector[0]);
715
            $display("ccl4PBoutput %0d", outputVector[1]);
716
            $display("ccl4PBoutput %0d", outputVector[2]);
717
            $display("ccl4PBoutput %0d", outputVector[3]);
718
 
719
            if(outBlockNum==0 && pixelVer==0 && outChromaFlag==Luma && currMb!=firstMb && picWidth>1)
720
               begin
721
                  intraMemReqQ.enq(intraMemReqQdelay);
722
                  interMemReqQ.enq(interMemReqQdelay);
723
                  //$display( "TRACE Prediction: passing storing addr data");//////////////////
724
               end
725
 
726
            if(blockHor==3 || (blockHor[0]==1 && outChromaFlag==Chroma) || (outstatefifo.first()==Intra4x4 && outChromaFlag==Luma))
727
               begin
728
                  if(outChromaFlag==Luma)
729
                     begin
730
                        Bit#(32) intraLeftValNextTemp = intraLeftValNext;
731
                        if(totalVer==0 || (outstatefifo.first()==Intra4x4 && pixelVer==0))
732
                           begin
733
                              Bit#(32) tempValSet = select(intraTopVal,zeroExtend(blockHor));
734
                              intraLeftValNextTemp = zeroExtend(tempValSet[31:24]);
735
                           end
736
                        case(pixelVer)
737
                           0:intraLeftValNext <= {intraLeftValNextTemp[31:16],outputVector[3],intraLeftValNextTemp[7:0]};
738
                           1:intraLeftValNext <= {intraLeftValNextTemp[31:24],outputVector[3],intraLeftValNextTemp[15:0]};
739
                           2:intraLeftValNext <= {outputVector[3],intraLeftValNextTemp[23:0]};
740
                           3:
741
                           begin
742
                              intraLeftVal <= update(intraLeftVal,blockVer,{outputVector[3],intraLeftValNextTemp});
743
                              intraLeftValNext <= zeroExtend(outputVector[3]);
744
                              if(outstatefifo.first()==Intra4x4)
745
                                 intra4x4typeLeft <= update(intra4x4typeLeft,blockVer,cur_intra4x4_pred_mode);
746
                              else if(outstatefifo.first()==Intra)
747
                                 intra4x4typeLeft <= update(intra4x4typeLeft,blockVer,13);
748
                              else
749
                                 intra4x4typeLeft <= update(intra4x4typeLeft,blockVer,14);
750
                           end
751
                        endcase
752
                     end
753
                  else
754
                     begin
755
                        if(outBlockNum[2]==0)
756
                           intraLeftValChroma0 <= update(intraLeftValChroma0,totalVer+1,outputVector[3]);
757
                        else
758
                           intraLeftValChroma1 <= update(intraLeftValChroma1,totalVer+1,outputVector[3]);
759
                     end
760
               end
761
 
762
            if(pixelVer==3 && (blockVer==3 || (blockVer[0]==1 && outChromaFlag==Chroma) || (outstatefifo.first()==Intra4x4 && outChromaFlag==Luma)))
763
               begin
764
                  if(outChromaFlag==Luma)
765
                     begin
766
                        intraTopVal <= update(intraTopVal,zeroExtend(blockHor),{outputVector[3],outputVector[2],outputVector[1],outputVector[0]});
767
                        if(outstatefifo.first()==Intra4x4)
768
                           intra4x4typeTop <= update(intra4x4typeTop,blockHor,cur_intra4x4_pred_mode);
769
                        else if(outstatefifo.first()==Intra)
770
                           intra4x4typeTop <= update(intra4x4typeTop,blockHor,13);
771
                        else
772
                           intra4x4typeTop <= update(intra4x4typeTop,blockHor,14);
773
                     end
774
                  else
775
                     begin
776
                        if(outBlockNum[2]==0)
777
                           begin
778
                              Vector#(4,Bit#(16)) intraTopValChroma0Next = intraTopValChroma0;
779
                              intraTopValChroma0Next[{blockHor[0],1'b0}] = {outputVector[1],outputVector[0]};
780
                              intraTopValChroma0Next[{blockHor[0],1'b1}] = {outputVector[3],outputVector[2]};
781
                              intraTopValChroma0 <= intraTopValChroma0Next;
782
                           end
783
                        else
784
                           begin
785
                              Vector#(4,Bit#(16)) intraTopValChroma1Next = intraTopValChroma1;
786
                              intraTopValChroma1Next[{blockHor[0],1'b0}] = {outputVector[1],outputVector[0]};
787
                              intraTopValChroma1Next[{blockHor[0],1'b1}] = {outputVector[3],outputVector[2]};
788
                              intraTopValChroma1 <= intraTopValChroma1Next;
789
                           end
790
                     end
791
               end
792
 
793
            if(outChromaFlag==Chroma && outBlockNum==7)
794
               begin
795
                  Bit#(PicWidthSz) tempStoreAddr = truncate(currMbHor);
796
                  InterBlockMv outBlockMv = interOutBlockMvfifo.first();
797
                  if(outBlockMv matches tagged BlockMv .bdata)
798
                     begin
799
                        outBlockMv = (BlockMv {refIdx:bdata.refIdx,mvhor:bdata.mvhor,mvver:bdata.mvver,nonZeroTransCoeff:(interTopNonZeroTransCoeff[pixelVer]?1:0)});
800
                        interOutBlockMvfifo.deq();
801
                     end
802
                  else if(pixelVer==3)
803
                     interOutBlockMvfifo.deq();
804
                  if(pixelVer==3 && picWidth>1)
805
                     interMemReqQdelay <= StoreReq {addr:{tempStoreAddr,pixelVer},data:pack(outBlockMv)};
806
                  else
807
                     interMemReqQ.enq(tagged StoreReq {addr:{tempStoreAddr,pixelVer},data:pack(outBlockMv)});
808
                  if(pixelVer>0)
809
                     begin
810
                        Bit#(4)  intra4x4typeTopStore = ((outstatefifo.first()==Inter) ? 14 : ((outstatefifo.first()!=Intra4x4) ? 13: intra4x4typeTop[(pixelVer-1)]));
811
                        Bit#(32) intraTopValStore = intraTopVal[(pixelVer-1)];
812
                        Bit#(16) intraTopValChroma0Store = intraTopValChroma0[(pixelVer-1)];
813
                        Bit#(16) intraTopValChroma1Store = (pixelVer<3 ? intraTopValChroma1[(pixelVer-1)] : {outputVector[1],outputVector[0]});
814
                        Bit#(68) intraStore = {intra4x4typeTopStore,intraTopValChroma1Store,intraTopValChroma0Store,intraTopValStore};
815
                        intraMemReqQ.enq(tagged StoreReq {addr:{tempStoreAddr,(pixelVer-1)},data:intraStore});
816
                        if(pixelVer==3)
817
                           begin
818
                              intra4x4typeTopStore = ((outstatefifo.first()==Inter) ? 14 : ((outstatefifo.first()!=Intra4x4) ? 13: intra4x4typeTop[3]));
819
                              intraTopValStore = intraTopVal[3];
820
                              intraTopValChroma0Store = intraTopValChroma0[3];
821
                              intraTopValChroma1Store = {outputVector[3],outputVector[2]};
822
                              intraStore = {intra4x4typeTopStore,intraTopValChroma1Store,intraTopValChroma0Store,intraTopValStore};
823
                              intraMemReqQdelay <= StoreReq {addr:{tempStoreAddr,2'b11},data:intraStore};
824
                           end
825
                     end
826
               end
827
            outPixelNum <= outPixelNum+4;
828
            if(outPixelNum == 12)
829
               begin
830
                  if(outChromaFlag==Luma)
831
                     begin
832
                        outBlockNum <= outBlockNum+1;
833
                        if(outBlockNum == 15)
834
                           outChromaFlag <= Chroma;
835
                        if(nextoutputfifo.first() == Intra4x4)
836
                           nextoutputfifo.deq();
837
                     end
838
                  else
839
                     begin
840
                        if(outBlockNum == 7)
841
                           begin
842
                              outBlockNum <= 0;
843
                              outChromaFlag <= Luma;
844
                              currMb <= currMb+1;
845
                              currMbHor <= currMbHor+1;
846
                              interCurrMbDiff <= interCurrMbDiff-1;
847
                              outstatefifo.deq;
848
                              intrastate <= Start;
849
                              if(truncate(currMbHor)==picWidth-1 && currMbVer==picHeight-1)
850
                                 interpolator.endOfFrame();
851
                              nextoutputfifo.deq();
852
                           end
853
                        else
854
                           outBlockNum <= outBlockNum+1;
855
                     end
856
               end
857
         end
858
   endrule
859
 
860
 
861
   rule currMbHorUpdate( !(currMbHor
862
      Bit#(PicAreaSz) temp = zeroExtend(picWidth);
863
      if((currMbHor >> 3) >= temp)
864
         begin
865
            currMbHor <= currMbHor - (temp << 3);
866
            currMbVer <= currMbVer + 8;
867
         end
868
      else
869
         begin
870
            currMbHor <= currMbHor - temp;
871
            currMbVer <= currMbVer + 1;
872
         end
873
      //$display( "Trace Prediction: currMbHorUpdate %h %h", currMbHor, currMbVer);
874
   endrule
875
 
876
 
877
   // inter prediction rules
878
 
879
   rule interSendReq ( interReqCount>0 && currMbHor
880
      Bit#(PicAreaSz) currMbHorTemp = currMbHor+zeroExtend(interCurrMbDiff)-1;
881
      Bit#(PicAreaSz) currMbTemp = currMb+zeroExtend(interCurrMbDiff)-1;
882
      if( currMbHorTemp >= zeroExtend(picWidth) )
883
         currMbHorTemp = currMbHorTemp-zeroExtend(picWidth);
884
      Bit#(PicWidthSz) temp2 = truncate(currMbHorTemp);
885
      Bit#(TAdd#(PicWidthSz,2)) temp = 0;
886
      Bool noMoreReq = False;
887
      if( currMbTemp < zeroExtend(picWidth) )
888
         noMoreReq = True;
889
      else
890
         begin
891
            if(interReqCount<5)
892
               begin
893
                  Bit#(2) temp3 = truncate(interReqCount-1);
894
                  temp = {temp2,temp3};
895
               end
896
            else if(interReqCount==5)
897
               begin
898
                  if((currMbHorTemp+1)
899
                     temp = {(temp2+1),2'b00};
900
                  else if(currMbHorTemp>0 && currMbTemp-firstMb>zeroExtend(picWidth))
901
                     temp = {(temp2-1),2'b11};
902
                  else
903
                     noMoreReq = True;
904
               end
905
            else if(interReqCount==6)
906
               begin
907
                  if((currMbHorTemp+1)0 && currMbTemp-firstMb>zeroExtend(picWidth))
908
                     temp = {(temp2-1),2'b11};
909
                  else
910
                     noMoreReq = True;
911
               end
912
            else
913
               noMoreReq = True;
914
         end
915
      if(!noMoreReq)
916
         begin
917
            interMemReqQ.enq(tagged LoadReq temp);
918
            interReqCount <= interReqCount+1;
919
            //$display( "TRACE Prediction: interSendReq addr %0d",temp);///////////////////////
920
         end
921
      else
922
         interReqCount <= 0;
923
      $display( "Trace Prediction: interSendReq %h %h %h", interstate, interReqCount, temp);
924
   endrule
925
 
926
 
927
   rule interReceiveNoResp ( interRespCount>0 && currMbHor
928
      Bit#(PicAreaSz) currMbHorTemp = currMbHor+zeroExtend(interCurrMbDiff)-1;
929
      if( currMbHorTemp >= zeroExtend(picWidth) )
930
         currMbHorTemp = currMbHorTemp-zeroExtend(picWidth);
931
      interRespCount <= 0;
932
      interStepCount <= 1;
933
      interIPStepCount <= 1;
934
      if(currMbHorTemp == 0)
935
         begin
936
            interLeftVal <= replicate(tagged NotInter 0);
937
            interTopLeftVal <= replicate(tagged NotInter 0);
938
         end
939
      $display( "Trace Prediction: interReceiveNoResp %h %h", interstate, interRespCount);
940
   endrule
941
 
942
 
943
   rule interReceiveResp ( interRespCount>0 && interRespCount<7 && currMbHor
944
      Bit#(PicAreaSz) currMbHorTemp = currMbHor+zeroExtend(interCurrMbDiff)-1;
945
      Bit#(PicAreaSz) currMbTemp = currMb+zeroExtend(interCurrMbDiff)-1;
946
      if( currMbHorTemp >= zeroExtend(picWidth) )
947
         currMbHorTemp = currMbHorTemp-zeroExtend(picWidth);
948
      Bool noMoreResp = False;
949
      Bit#(2) temp2bit = 0;
950
      InterBlockMv unpackedData = unpack(data);
951
      Vector#(5,InterBlockMv) interTopValNext = interTopVal;
952
      Vector#(4,InterBlockMv) interTopLeftValNext = interTopLeftVal;
953
      if(interRespCount<5)
954
         begin
955
            temp2bit = truncate(interRespCount-1);
956
            interTopValNext[temp2bit] = unpackedData;
957
            if((interRespCount==4 || (interRespCount==1 && (interstate==InterPskip || interstate==InterP16x16 || interstate==InterP16x8)))
958
               && (!((currMbHorTemp+1)0 && currMbTemp-firstMb>zeroExtend(picWidth))))
959
               noMoreResp = True;
960
         end
961
      else if(interRespCount==5)
962
         begin
963
            if((currMbHorTemp+1)
964
               begin
965
                  interTopValNext[4] = unpackedData;
966
                  if(!(currMbHorTemp>0 && currMbTemp-firstMb>zeroExtend(picWidth)))
967
                     noMoreResp = True;
968
               end
969
            else
970
               begin
971
                  interTopLeftValNext[0] = unpackedData;
972
                  noMoreResp = True;
973
               end
974
         end
975
      else
976
         begin
977
            interTopLeftValNext[0] = unpackedData;
978
            noMoreResp = True;
979
         end
980
      interMemRespQ.deq();
981
      //$display( "TRACE Prediction: interReceiveResp data %h",data);///////////////////////
982
      if(!noMoreResp)
983
         interRespCount <= interRespCount+1;
984
      else
985
         begin
986
            interRespCount <= 0;
987
            interStepCount <= 1;
988
            interIPStepCount <= 1;
989
            if(currMbHorTemp == 0)
990
               begin
991
                  interLeftVal <= replicate(tagged NotInter 0);
992
                  interTopLeftValNext = replicate(tagged NotInter 0);
993
               end
994
         end
995
      interTopVal <= interTopValNext;
996
      interTopLeftVal <= interTopLeftValNext;
997
      $display( "Trace Prediction: interReceiveResp %h %h %h", interstate, interRespCount, data);
998
   endrule
999
 
1000
 
1001
   rule interProcessStep ( interStepCount>0 && currMbHor
1002
      Bit#(PicAreaSz) currMbTemp = currMb+zeroExtend(interCurrMbDiff)-1;
1003
      Bit#(2) blockHor = {interMbPartNum[0],interSubMbPartNum[0]};
1004
      Bit#(2) blockVer = {interMbPartNum[1],interSubMbPartNum[1]};
1005
 
1006
      if(interStepCount == 1)
1007
         begin
1008
           Bit#(3) partWidth = 0;
1009
           Bit#(3) partHeight = 0;
1010
           Bit#(3) numPart = 1;
1011
           Bit#(3) numSubPart = 1;
1012
           Bit#(2) subMbType = 0;
1013
           Bool noBlockC = False;
1014
           Bool calcmv = False;
1015
           Bool leftmv = False;
1016
           if(interstate==InterPskip || interstate==InterP16x16)
1017
              begin
1018
                 partWidth = 4;
1019
                 partHeight = 4;
1020
                 numPart = 1;
1021
                 calcmv = (interMbPartNum==0 && interSubMbPartNum==0);
1022
                 leftmv = (blockHor>0);
1023
              end
1024
           else if(interstate==InterP16x8)
1025
              begin
1026
                 partWidth = 4;
1027
                 partHeight = 2;
1028
                 numPart = 2;
1029
                 if(interMbPartNum==2)
1030
                    noBlockC = True;
1031
                 calcmv = (interMbPartNum[0]==0 && interSubMbPartNum==0);
1032
                 leftmv = (blockHor>0);
1033
              end
1034
           else if(interstate==InterP8x16)
1035
              begin
1036
                 partWidth = 2;
1037
                 partHeight = 4;
1038
                 numPart = 2;
1039
                 calcmv = (interMbPartNum[1]==0 && interSubMbPartNum==0);
1040
                 leftmv = !(blockVer>0);
1041
              end
1042
           else if(interstate==InterP8x8 || interstate==InterP8x8ref0)
1043
              begin
1044
                 numPart = 4;
1045
                 subMbType = interSubMbTypeVector[interMbPartNum];
1046
                 numSubPart = numSubMbPart(subMbType);
1047
                 case(subMbType)
1048
                    0:
1049
                    begin
1050
                       partWidth = 2;
1051
                       partHeight = 2;
1052
                       if(interMbPartNum==3)
1053
                          noBlockC = True;
1054
                       calcmv = (interSubMbPartNum==0);
1055
                       leftmv = (blockHor[0]>0);
1056
                    end
1057
                    1:
1058
                    begin
1059
                       partWidth = 2;
1060
                       partHeight = 1;
1061
                       if(interSubMbPartNum==2)
1062
                          noBlockC = True;
1063
                       calcmv = (interSubMbPartNum[0]==0);
1064
                       leftmv = True;
1065
                    end
1066
                    2:
1067
                    begin
1068
                       partWidth = 1;
1069
                       partHeight = 2;
1070
                       calcmv = (interSubMbPartNum[1]==0);
1071
                       leftmv = False;
1072
                    end
1073
                    3:
1074
                    begin
1075
                      partWidth = 1;
1076
                      partHeight = 1;
1077
                      if(interSubMbPartNum==3)
1078
                         noBlockC = True;
1079
                      calcmv = True;
1080
                    end
1081
                 endcase
1082
             end
1083
          else
1084
            $display( "ERROR Prediction: interProcessStep unexpected interstate");
1085
 
1086
          Bit#(4) refIndex = ((interstate==InterPskip||interstate==InterP8x8ref0) ? 0 : interRefIdxVector[interMbPartNum]);
1087
          Vector#(3,InterBlockMv) blockABC = replicate(tagged NotInter 0);
1088
          if( currMbTemp-firstMb==0 && blockHor==0 )
1089
             blockABC[0] = (tagged NotInter 0);
1090
          else
1091
             blockABC[0] = interLeftVal[blockVer];
1092
          if( currMbTemp-firstMb
1093
             blockABC[1] = (tagged NotInter 0);
1094
          else
1095
             blockABC[1] = interTopVal[blockHor];
1096
          blockABC[2] = interTopVal[{1'b0,blockHor}+partWidth];
1097
          if(noBlockC || blockABC[2]==(tagged NotInter 0))
1098
             blockABC[2] = interTopLeftVal[blockVer];
1099
          partWidthR <= partWidth;
1100
          partHeightR <= partHeight;
1101
          numPartR <= numPart;
1102
          numSubPartR <= numSubPart;
1103
          subMbTypeR <= subMbType;
1104
          calcmvR <= calcmv;
1105
          leftmvR <= leftmv;
1106
          refIndexR <= refIndex;
1107
          blockABCR <= blockABC;
1108
          interStepCount <= 2;
1109
      end
1110
   else if(interStepCount==2)
1111
      begin
1112
         Bit#(3) partWidth = partWidthR;
1113
         Bit#(3) partHeight = partHeightR;
1114
         Bit#(3) numPart = numPartR;
1115
         Bit#(3) numSubPart = numSubPartR;
1116
         Bit#(2) subMbType = subMbTypeR;
1117
         Bool calcmv = calcmvR;
1118
         Bool leftmv = leftmvR;
1119
         Bit#(4) refIndex = refIndexR;
1120
         Vector#(3,InterBlockMv) blockABC = blockABCR;
1121
         Bit#(14) mvhorfinal = 0;
1122
         Bit#(12) mvverfinal = 0;
1123
         Bit#(5) interNewestMvNext = 0;
1124
         if(calcmv)//motion vector caculation
1125
            begin
1126
               Vector#(3,Int#(14)) mvhorABC = replicate(0);
1127
               Vector#(3,Int#(12)) mvverABC = replicate(0);
1128
               Bit#(2) validCount = 0;
1129
               Bit#(14) mvhorPred = 0;
1130
               Bit#(12) mvverPred = 0;
1131
               for(Integer ii=0; ii<3; ii=ii+1)
1132
                  begin
1133
                     if(blockABC[ii] matches tagged BlockMv .xdata)
1134
                        begin
1135
                           mvhorABC[ii] = unpack(xdata.mvhor);
1136
                           mvverABC[ii] = unpack(xdata.mvver);
1137
                           if(xdata.refIdx == refIndex)
1138
                              begin
1139
                                 validCount = validCount+1;
1140
                                 mvhorPred = xdata.mvhor;
1141
                                 mvverPred = xdata.mvver;
1142
                              end
1143
                        end
1144
                     else
1145
                       begin
1146
                          mvhorABC[ii] = 0;
1147
                          mvverABC[ii] = 0;
1148
                       end
1149
                  end
1150
               if(validCount != 1)//median
1151
                  begin
1152
                     if(mvhorABC[0]>mvhorABC[1] && mvhorABC[0]>mvhorABC[2])
1153
                        mvhorPred = pack((mvhorABC[1]>mvhorABC[2]) ? mvhorABC[1] : mvhorABC[2]);
1154
                     else if(mvhorABC[0]
1155
                        mvhorPred = pack((mvhorABC[1]
1156
                     else
1157
                        mvhorPred = pack(mvhorABC[0]);
1158
                     if(mvverABC[0]>mvverABC[1] && mvverABC[0]>mvverABC[2])
1159
                        mvverPred = pack((mvverABC[1]>mvverABC[2]) ? mvverABC[1] : mvverABC[2]);
1160
                     else if(mvverABC[0]
1161
                        mvverPred = pack((mvverABC[1]
1162
                     else
1163
                        mvverPred = pack(mvverABC[0]);
1164
                  end
1165
               if(interstate==InterPskip)
1166
                  begin
1167
                     for(Integer ii=0; ii<2; ii=ii+1)
1168
                        begin
1169
                           if(blockABC[ii] matches tagged BlockMv .xdata)
1170
                              begin
1171
                                 if(xdata.refIdx==0 && xdata.mvhor==0 && xdata.mvver==0)
1172
                                    begin
1173
                                       mvhorPred = 0;
1174
                                       mvverPred = 0;
1175
                                    end
1176
                              end
1177
                           else if(blockABC[ii] matches tagged NotInter 0)
1178
                              begin
1179
                                 mvhorPred = 0;
1180
                                 mvverPred = 0;
1181
                              end
1182
                        end
1183
                  end
1184
               else if(interstate==InterP16x8 || interstate==InterP8x16)
1185
                  begin
1186
                     InterBlockMv blockCheck;
1187
                     if(interstate==InterP16x8)
1188
                        begin
1189
                           if(interMbPartNum==0)
1190
                              blockCheck = blockABC[1];
1191
                           else
1192
                              blockCheck = blockABC[0];
1193
                        end
1194
                     else
1195
                        begin
1196
                           if(interMbPartNum==0)
1197
                              blockCheck = blockABC[0];
1198
                           else
1199
                              blockCheck = blockABC[2];
1200
                        end
1201
                     if(blockCheck matches tagged BlockMv .xdata &&& xdata.refIdx==refIndex)
1202
                        begin
1203
                           mvhorPred = xdata.mvhor;
1204
                           mvverPred = xdata.mvver;
1205
                        end
1206
                  end
1207
               mvhorfinal = mvhorPred;
1208
               mvverfinal = mvverPred;
1209
               if(interstate!=InterPskip)
1210
                  begin
1211
                     mvhorfinal = truncate(tpl_1(interMvDiff.first()) + signExtend(mvhorPred));
1212
                     mvverfinal = truncate(tpl_2(interMvDiff.first()) + signExtend(mvverPred));
1213
                     interMvDiff.deq();
1214
                  end
1215
               interMvFile.upd({interMbPartNum,interSubMbPartNum},tuple2(mvhorfinal,mvverfinal));
1216
               interNewestMvNext = zeroExtend({interMbPartNum,interSubMbPartNum})+1;
1217
               $display( "Trace Prediction: interProcessStep %h %h %h %h %h %h %h %h %h", interstate, interStepCount, interMbPartNum, interSubMbPartNum, pack(blockABC[0]), pack(blockABC[1]), pack(blockABC[2]), mvhorPred, mvverPred);
1218
            end
1219
         else
1220
            begin
1221
               if(leftmv)
1222
                  begin
1223
                     if(blockABC[0] matches tagged BlockMv .xdata)
1224
                        begin
1225
                           mvhorfinal = unpack(xdata.mvhor);
1226
                           mvverfinal = unpack(xdata.mvver);
1227
                        end
1228
                     else
1229
                        $display( "ERROR Prediction: interProcessStep unexpected blockABC[0]");
1230
                  end
1231
               else
1232
                  begin
1233
                     if(blockABC[1] matches tagged BlockMv .xdata)
1234
                        begin
1235
                           mvhorfinal = unpack(xdata.mvhor);
1236
                           mvverfinal = unpack(xdata.mvver);
1237
                        end
1238
                     else
1239
                        $display( "ERROR Prediction: interProcessStep unexpected blockABC[1]");
1240
                  end
1241
            end
1242
 
1243
            mvhorfinalR <= mvhorfinal;
1244
            mvverfinalR <= mvverfinal;
1245
            interNewestMvNextR <= interNewestMvNext;
1246
            interStepCount <= 3;
1247
 
1248
         end
1249
      else // stepCount == 3
1250
         begin
1251
            Bit#(2) tempBShor = 0;//bS calculation
1252
            Bit#(2) tempBSver = 0;
1253
            Bool allDone = False;
1254
            Bit#(4) refIndex = refIndexR;
1255
            Bit#(14) mvhorfinal = mvhorfinalR;
1256
            Bit#(12) mvverfinal = mvverfinalR;
1257
            Bit#(5) interNewestMvNext = interNewestMvNextR;
1258
 
1259
            if(interLeftVal[blockVer] matches tagged BlockMv .xdata)
1260
               begin
1261
                  if(xdata.nonZeroTransCoeff == 1)
1262
                     tempBShor = 2;
1263
                  else
1264
                     begin
1265
                       if(xdata.refIdx!=refIndex || absDiffGEFour14(mvhorfinal,xdata.mvhor) || absDiffGEFour12(mvverfinal,xdata.mvver))
1266
                           tempBShor = 1;
1267
                       else
1268
                           tempBShor = 0;
1269
                     end
1270
               end
1271
            else
1272
            tempBShor = 3;
1273
            if(interTopVal[blockHor] matches tagged BlockMv .xdata)
1274
               begin
1275
                  if(xdata.nonZeroTransCoeff == 1)
1276
                     tempBSver = 2;
1277
                  else
1278
                     begin
1279
                        if(xdata.refIdx!=refIndex || absDiffGEFour14(mvhorfinal,xdata.mvhor) || absDiffGEFour12(mvverfinal,xdata.mvver))
1280
                           tempBSver = 1;
1281
                        else
1282
                           tempBSver = 0;
1283
                     end
1284
               end
1285
            else
1286
               tempBSver = 3;
1287
            interBSfifo.enq(tuple2(tempBShor,tempBSver));
1288
            Vector#(5,InterBlockMv) interTopValNext = interTopVal;//update inter*Val
1289
            Vector#(4,InterBlockMv) interLeftValNext = interLeftVal;
1290
            Vector#(4,InterBlockMv) interTopLeftValNext = interTopLeftVal;
1291
            interLeftValNext[blockVer] = (BlockMv {refIdx:refIndex,mvhor:mvhorfinal,mvver:mvverfinal,nonZeroTransCoeff:0});
1292
            interTopValNext[blockHor] = (BlockMv {refIdx:refIndex,mvhor:mvhorfinal,mvver:mvverfinal,nonZeroTransCoeff:0});
1293
            interTopLeftValNext[blockVer] = interTopVal[blockHor];
1294
            interTopVal <= interTopValNext;
1295
            interLeftVal <= interLeftValNext;
1296
            interTopLeftVal <= interTopLeftValNext;
1297
            if(blockVer == 3)
1298
               interOutBlockMvfifo.enq(tagged BlockMv {refIdx:refIndex,mvhor:mvhorfinal,mvver:mvverfinal,nonZeroTransCoeff:0});
1299
            if(interSubMbPartNum == 3)//next step
1300
               begin
1301
                  interSubMbPartNum <= 0;
1302
                  if(interMbPartNum == 3)
1303
                     begin
1304
                        interMbPartNum <= 0;
1305
                        allDone = True;
1306
                        interNewestMvNext = 16;
1307
                     end
1308
                  else
1309
                     interMbPartNum <= interMbPartNum+1;
1310
               end
1311
            else
1312
               interSubMbPartNum <= interSubMbPartNum+1;
1313
            if(interNewestMvNext > 0)
1314
               interNewestMv <= interNewestMvNext;
1315
 
1316
            // Check to see if we are done.
1317
 
1318
            if(allDone)
1319
               interStepCount <= 0;
1320
            else
1321
               interStepCount <= 1;
1322
 
1323
            $display( "Trace Prediction: interProcessStep final %h %h %h %h %h %h %h",interstate,interStepCount,interMbPartNum,interSubMbPartNum,mvhorfinal,mvverfinal,interNewestMvNext);
1324
 
1325
       end
1326
   endrule
1327
 
1328
 
1329
   rule interIPProcessStep ( interIPStepCount>0 && currMbHorzeroExtend({interIPMbPartNum,interIPSubMbPartNum}) );
1330
      Bit#(PicAreaSz) currMbHorTemp = currMbHor+zeroExtend(interCurrMbDiff)-1;
1331
      Bit#(PicHeightSz) currMbVerTemp = currMbVer;
1332
      if( currMbHorTemp >= zeroExtend(picWidth) )
1333
         begin
1334
            currMbHorTemp = currMbHorTemp-zeroExtend(picWidth);
1335
            currMbVerTemp = currMbVerTemp+1;
1336
         end
1337
      Bit#(2) blockHor = {interIPMbPartNum[0],interIPSubMbPartNum[0]};
1338
      Bit#(2) blockVer = {interIPMbPartNum[1],interIPSubMbPartNum[1]};
1339
      Bit#(3) numPart = 1;
1340
      Bit#(3) numSubPart = 1;
1341
      Bit#(2) subMbType = 0;
1342
      if(interstate==InterPskip || interstate==InterP16x16)
1343
         numPart = 1;
1344
      else if(interstate==InterP16x8)
1345
         numPart = 2;
1346
      else if(interstate==InterP8x16)
1347
         numPart = 2;
1348
      else if(interstate==InterP8x8 || interstate==InterP8x8ref0)
1349
         begin
1350
            numPart = 4;
1351
            subMbType = interSubMbTypeVector[interIPMbPartNum];
1352
            numSubPart = numSubMbPart(subMbType);
1353
         end
1354
      else
1355
         $display( "ERROR Prediction: interIPProcessStep unexpected interstate");
1356
      Bit#(4) refIndex = ((interstate==InterPskip||interstate==InterP8x8ref0) ? 0 : interRefIdxVector[interIPMbPartNum]);
1357
      Bit#(PicWidthSz) currMbHorT = truncate(currMbHorTemp);
1358
      Bit#(TAdd#(PicWidthSz,2)) horTemp = {currMbHorT,blockHor};
1359
      Bit#(TAdd#(PicHeightSz,4)) verTemp = {currMbVerTemp,blockVer,2'b00};
1360
      IPBlockType btTemp = IP16x16;
1361
      if(interstate==InterPskip || interstate==InterP16x16)
1362
         btTemp = IP16x16;
1363
      else if(interstate==InterP16x8)
1364
         btTemp = IP16x8;
1365
      else if(interstate==InterP8x16)
1366
         btTemp = IP8x16;
1367
      else
1368
         begin
1369
            case(subMbType)
1370
               0: btTemp = IP8x8;
1371
               1: btTemp = IP8x4;
1372
               2: btTemp = IP4x8;
1373
               3: btTemp = IP4x4;
1374
            endcase
1375
         end
1376
      Bit#(14) mvhorTemp = tpl_1(interMvFile.sub({interIPMbPartNum,interIPSubMbPartNum}));
1377
      Bit#(12) mvverTemp = tpl_2(interMvFile.sub({interIPMbPartNum,interIPSubMbPartNum}));
1378
      if(interIPStepCount == 1)
1379
         begin
1380
            if(!(interstate==InterP8x8 || interstate==InterP8x8ref0))
1381
               begin
1382
                  numPart = 4;
1383
                  Bit#(2) interIPMbPartNumTemp = interIPMbPartNum;
1384
                  if(btTemp==IP16x16)
1385
                     interIPMbPartNumTemp = 0;
1386
                  else if(btTemp==IP16x8 && interIPMbPartNumTemp[0]==1)
1387
                     interIPMbPartNumTemp = interIPMbPartNumTemp-1;
1388
                  else if(btTemp==IP8x16 && interIPMbPartNumTemp[1]==1)
1389
                     interIPMbPartNumTemp = interIPMbPartNumTemp-2;
1390
                  refIndex = ((interstate==InterPskip||interstate==InterP8x8ref0) ? 0 : interRefIdxVector[interIPMbPartNumTemp]);
1391
                  btTemp = IP8x8;
1392
                  mvhorTemp = tpl_1(interMvFile.sub({interIPMbPartNumTemp,2'b00}));
1393
                  mvverTemp = tpl_2(interMvFile.sub({interIPMbPartNumTemp,2'b00}));
1394 90 jamey.hick
                  $display("PARDEBLOCK issuing luma");
1395 86 jamey.hick
                  interpolator.request(IPLuma {refIdx:refIndex,hor:horTemp,ver:verTemp,mvhor:mvhorTemp,mvver:mvverTemp,bt:btTemp});
1396
               end
1397
            else
1398 90 jamey.hick
               begin
1399
                  $display("PARDEBLOCK issuing luma");
1400
                  interpolator.request(IPLuma {refIdx:refIndex,hor:horTemp,ver:verTemp,mvhor:mvhorTemp,mvver:mvverTemp,bt:btTemp});
1401
               end
1402 86 jamey.hick
         end
1403
      else
1404 90 jamey.hick
        begin
1405
           $display("PARDEBLOCK issuing Chroma");
1406
           interpolator.request(IPChroma {refIdx:refIndex,uv:interIPStepCount[0],hor:horTemp,ver:truncate(verTemp>>1),mvhor:mvhorTemp,mvver:mvverTemp,bt:btTemp});
1407
        end
1408 86 jamey.hick
      if(interIPSubMbPartNum >= truncate(numSubPart-1))
1409
         begin
1410
            interIPSubMbPartNum <= 0;
1411
            if(interIPMbPartNum >= truncate(numPart-1))
1412
               begin
1413
                  interIPMbPartNum <= 0;
1414
                  interIPStepCount <= interIPStepCount+1;
1415
               end
1416
            else
1417
               begin
1418
                  if(btTemp == IP16x8)
1419
                     interIPMbPartNum <= 2;
1420
                  else
1421
                     interIPMbPartNum <= interIPMbPartNum+1;
1422
               end
1423
         end
1424
      else
1425
         begin
1426
            if(subMbType == 1)
1427
               interIPSubMbPartNum <= 2;
1428
            else
1429
               interIPSubMbPartNum <= interIPSubMbPartNum+1;
1430
         end
1431 90 jamey.hick
      $display( "PARDEBLOCKTrace Prediction: interIPProcessStep %h %h %h %h %h %h %h %h %h %h", interstate, interIPStepCount, interIPMbPartNum, interIPSubMbPartNum, refIndex, horTemp, verTemp, mvhorTemp, mvverTemp, pack(btTemp));
1432 86 jamey.hick
   endrule
1433
 
1434
 
1435
   rule interDone ( interstate!=Start && interReqCount==0 && interRespCount==0 && interStepCount==0 && interIPStepCount==0 );
1436
      interstate <= Start;
1437
      //$display( "Trace Prediction: interOutputTransfer %h %h", interstate, interOutputCount);
1438
   endrule
1439
 
1440
 
1441
   rule interOutputTransfer ( True );
1442
      predictedfifo.enq(interpolator.first());
1443
      interpolator.deq();
1444
      //$display( "Trace Prediction: interOutputTransfer %h %h", interstate, interOutputCount);
1445
   endrule
1446
 
1447
 
1448
 
1449
   // intra prediction rules
1450
 
1451
   rule intraSendReq ( intraReqCount>0 && currMbHor
1452
      Bit#(PicWidthSz) temp2 = truncate(currMbHor);
1453
      Bit#(TAdd#(PicWidthSz,2)) temp = 0;
1454
      Bit#(1) noMoreReq = 0;
1455
      if( currMb-firstMb < zeroExtend(picWidth) )
1456
         noMoreReq = 1;
1457
      else
1458
         begin
1459
            if(intraReqCount<5)
1460
               begin
1461
                  Bit#(2) temp3 = truncate(intraReqCount-1);
1462
                  temp = {temp2,temp3};
1463
               end
1464
            else if(intraReqCount==5)
1465
               begin
1466
                  if((currMbHor+1)
1467
                     temp = {(temp2+1),2'b00};
1468
                  else if(currMbHor>0 && currMb-firstMb>zeroExtend(picWidth))
1469
                     temp = {(temp2-1),2'b11};
1470
                  else
1471
                     noMoreReq = 1;
1472
               end
1473
            else if(intraReqCount==6)
1474
               begin
1475
                  if((currMbHor+1)0 && currMb-firstMb>zeroExtend(picWidth))
1476
                     temp = {(temp2-1),2'b11};
1477
                  else
1478
                     noMoreReq = 1;
1479
               end
1480
            else
1481
               noMoreReq = 1;
1482
         end
1483
      if(noMoreReq == 0)
1484
         begin
1485
            intraMemReqQ.enq(tagged LoadReq temp);
1486
            intraReqCount <= intraReqCount+1;
1487
            //$display( "TRACE Prediction: intraSendReq addr %0d",temp);///////////////////////
1488
         end
1489
      else
1490
         intraReqCount <= 0;
1491
      $display( "Trace Prediction: intraSendReq");
1492
   endrule
1493
 
1494
 
1495
   rule intraReceiveNoResp ( intraRespCount>0 && currMbHor
1496
      intra4x4typeTop <= replicate(15);
1497
      intraRespCount <= 0;
1498
      intraStepCount <= 1;
1499
      blockNum <= 0;
1500
      pixelNum <= 0;
1501
      interOutBlockMvfifo.enq(tagged NotInter 1);
1502
      $display( "Trace Prediction: intraReceiveNoResp");
1503
   endrule
1504
 
1505
 
1506
   rule intraReceiveResp ( intraRespCount>0 && intraRespCount<7 && currMbHor
1507
      Bit#(1) noMoreResp = 0;
1508
      Bit#(2) temp2bit = 0;
1509
      if(intraRespCount<5)
1510
         begin
1511
            temp2bit = truncate(intraRespCount-1);
1512
            intra4x4typeTop <= update(intra4x4typeTop, temp2bit, data[67:64]);
1513
            if(intraRespCount==4)
1514
               begin
1515
                  Vector#(5,Bit#(32)) intraTopValTemp = intraTopVal;
1516
                  intraTopValTemp[3] = data[31:0];
1517
                  intraTopValTemp[4] = {data[31:24],data[31:24],data[31:24],data[31:24]};
1518
                  intraTopVal <= intraTopValTemp;
1519
                  if(!((currMbHor+1)0 && currMb-firstMb>zeroExtend(picWidth)))
1520
                     noMoreResp = 1;
1521
               end
1522
            else
1523
               intraTopVal <= update(intraTopVal, intraRespCount-1, data[31:0]);
1524
            intraTopValChroma0 <= update(intraTopValChroma0, temp2bit, data[47:32]);
1525
            intraTopValChroma1 <= update(intraTopValChroma1, temp2bit, data[63:48]);
1526
         end
1527
      else if(intraRespCount==5)
1528
         begin
1529
            if((currMbHor+1)
1530
               begin
1531
                  if(!(data[67:64]==15 || (data[67:64]==14 && ppsconstrained_intra_pred_flag==1)))
1532
                     intraTopVal <= update(intraTopVal, 4, data[31:0]);
1533
                  if(!(currMbHor>0 && currMb-firstMb>zeroExtend(picWidth)))
1534
                     noMoreResp = 1;
1535
               end
1536
            else
1537
               begin
1538
                  Bit#(40) temp2 = intraLeftVal[0];
1539
                  intraLeftVal <= update(intraLeftVal, 0, {temp2[39:8],data[31:24]});
1540
                  intraLeftValChroma0 <= update(intraLeftValChroma0, 0, data[47:40]);
1541
                  intraLeftValChroma1 <= update(intraLeftValChroma1, 0, data[63:56]);
1542
                  noMoreResp = 1;
1543
               end
1544
         end
1545
      else
1546
         begin
1547
            Bit#(40) temp2 = intraLeftVal[0];
1548
            intraLeftVal <= update(intraLeftVal, 0, {temp2[39:8],data[31:24]});
1549
            intraLeftValChroma0 <= update(intraLeftValChroma0, 0, data[47:40]);
1550
            intraLeftValChroma1 <= update(intraLeftValChroma1, 0, data[63:56]);
1551
            noMoreResp = 1;
1552
         end
1553
      intraMemRespQ.deq();
1554
      //$display( "TRACE Prediction: intraReceiveResp data %h",data);///////////////////////
1555
      if(noMoreResp == 0)
1556
         intraRespCount <= intraRespCount+1;
1557
      else
1558
         begin
1559
            intraRespCount <= 0;
1560
            intraStepCount <= 1;
1561
            blockNum <= 0;
1562
            pixelNum <= 0;
1563
            interOutBlockMvfifo.enq(tagged NotInter 1);
1564
         end
1565
      $display( "Trace Prediction: intraReceiveResp");
1566
   endrule
1567
 
1568
 
1569
   rule intraPredTypeStep ( intraStepCount==1 && !nextoutputfifo.notEmpty());
1570
      Bit#(2) blockHor = {blockNum[2],blockNum[0]};
1571
      Bit#(2) blockVer = {blockNum[3],blockNum[1]};
1572
      Bit#(4) topType = select(intra4x4typeTop, blockHor);
1573
      Bit#(4) leftType;
1574
      if(currMbHor!=0 || blockNum!=0)
1575
         leftType = select(intra4x4typeLeft, blockVer);
1576
      else
1577
         begin
1578
            leftType = 15;
1579
            intra4x4typeLeft <= replicate(15);
1580
         end
1581
      if(intrastate!=Intra4x4)
1582
         begin
1583
            intraStepCount <= intraStepCount+1;
1584
            nextoutputfifo.enq(NonSkipMB);
1585
         end
1586
      else
1587
         begin
1588
            Bit#(1) topAvailable;
1589
            Bit#(1) leftAvailable;
1590
            if(topType==15 || (topType==14 && ppsconstrained_intra_pred_flag==1))
1591
               topAvailable = 0;
1592
            else
1593
               topAvailable = 1;
1594
            if(leftType==15 || (leftType==14 && ppsconstrained_intra_pred_flag==1))
1595
               leftAvailable = 0;
1596
            else
1597
               leftAvailable = 1;
1598
            Bit#(4) predType = 0;
1599
            Bit#(4) remType = rem_intra4x4_pred_mode.first();
1600
            Bit#(4) curType = 0;
1601
            rem_intra4x4_pred_mode.deq();
1602
            if(topAvailable==0 || leftAvailable==0)
1603
               predType = 2;
1604
            else
1605
               begin
1606
                  Bit#(4) topType2 = topType;
1607
                  Bit#(4) leftType2 = leftType;
1608
                  if(topType>8)
1609
                     topType2 = 2;
1610
                  if(leftType>8)
1611
                     leftType2 = 2;
1612
                  if(topType2 > leftType2)
1613
                     predType = leftType2;
1614
                  else
1615
                     predType = topType2;
1616
               end
1617
            if(remType[3] == 1)
1618
               curType = predType;
1619
            else if(remType < predType)
1620
               curType = remType;
1621
            else
1622
               curType = remType+1;
1623
            cur_intra4x4_pred_mode <= curType;
1624
            intraStepCount <= intraStepCount+1;
1625
            if(blockNum == 15)
1626
               nextoutputfifo.enq(tagged Intra4x4PlusChroma);
1627
            else
1628
               nextoutputfifo.enq(tagged Intra4x4);
1629
            $display( "TRACE Prediction: intraPredTypeStep currMbHor currMbVer blockNum topType leftType predType remType curType %0d %0d %0d %0d %0d %0d %0d %0d",currMbHor,currMbVer,blockNum,topType,leftType,predType,remType,curType);//////////////////
1630
         end
1631
      //$display( "Trace Prediction: intraPredTypeStep");
1632
   endrule
1633
 
1634
 
1635
   rule intraProcessStep ( intraStepCount>1 );
1636
      $display( "TRACE Prediction: intraProcessStep %0d %0d", blockNum, pixelNum);////////////////////
1637
      //$display( "TRACE Prediction: intraProcessStep intraTopVal %h %h %h %h %h",intraTopVal[4],intraTopVal[3],intraTopVal[2],intraTopVal[1],intraTopVal[0]);/////////////////
1638
      Bit#(1) outFlag  = 0;
1639
      Bit#(4) nextIntraStepCount = intraStepCount+1;
1640
      Bit#(2) blockHor = {blockNum[2],blockNum[0]};
1641
      Bit#(2) blockVer = {blockNum[3],blockNum[1]};
1642
      Bit#(2) pixelVer = {pixelNum[3],pixelNum[2]};
1643
      Vector#(4,Bit#(8)) predVector = replicate(0);
1644
 
1645
      Bit#(4) topType = select(intra4x4typeTop, blockHor);
1646
      Bit#(4) leftType = select(intra4x4typeLeft, blockVer);
1647
      Bit#(1) topAvailable;
1648
      Bit#(1) leftAvailable;
1649
      if(topType==15 || (topType==14 && ppsconstrained_intra_pred_flag==1))
1650
         topAvailable = 0;
1651
      else
1652
         topAvailable = 1;
1653
      if(leftType==15 || (leftType==14 && ppsconstrained_intra_pred_flag==1))
1654
         leftAvailable = 0;
1655
      else
1656
         leftAvailable = 1;
1657
      if(blockNum==0 && pixelNum==0 && intraChromaFlag==0)
1658
         begin
1659
            intraChromaTopAvailable <= topAvailable;
1660
            intraChromaLeftAvailable <= leftAvailable;
1661
         end
1662
      if(intrastate==Intra4x4 && intraChromaFlag==0)
1663
         begin
1664
            if(intraStepCount==2)
1665
               begin
1666
                  outFlag = 1;
1667
                  Bit#(40) leftValSet = select(intraLeftVal,blockVer);
1668
                  Bit#(32) topMidValSet = select(intraTopVal,zeroExtend(blockHor));
1669
                  Bit#(32) topRightValSet = select(intraTopVal,{1'b0,blockHor}+1);
1670
                  Bit#(72) topValSet;
1671
                  if((blockNum[3:2]==3 && blockNum[0]==1) || blockNum[1:0]==3)
1672
                     topValSet = {topMidValSet[31:24],topMidValSet[31:24],topMidValSet[31:24],topMidValSet[31:24],topMidValSet,leftValSet[7:0]};
1673
                  else
1674
                     topValSet = {topRightValSet,topMidValSet,leftValSet[7:0]};
1675
                  $display( "TRACE Prediction: intraProcessStep intra4x4 %0d %0d %h %h", cur_intra4x4_pred_mode, blockNum, leftValSet, topValSet);////////////////////
1676
                  Bit#(4) topSelect1 = 0;
1677
                  Bit#(4) topSelect2 = 0;
1678
                  Bit#(4) topSelect3 = 0;
1679
                  Bit#(3) leftSelect1 = 0;
1680
                  Bit#(3) leftSelect2 = 0;
1681
                  Bit#(3) leftSelect3 = 0;
1682
                  Bit#(10) tempVal1 = 0;
1683
                  Bit#(10) tempVal2 = 0;
1684
                  Bit#(10) tempVal3 = 0;
1685
                  case(cur_intra4x4_pred_mode)
1686
                     0://vertical
1687
                     begin
1688
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1689
                           begin
1690
                              topSelect1 = fromInteger(pixelHor);
1691
                              Bit#(8) topVal = intra4x4SelectTop(topValSet,topSelect1);
1692
                              predVector[pixelHor] = topVal;
1693
                           end
1694
                     end
1695
                     1://horizontal
1696
                     begin
1697
                        leftSelect1 = zeroExtend(pixelVer);
1698
                        Bit#(8) leftVal = intra4x4SelectLeft(leftValSet,leftSelect1);
1699
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1700
                           predVector[pixelHor] = leftVal;
1701
                     end
1702
                     2://dc
1703
                     begin
1704
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1705
                           begin
1706
                              Bit#(10) tempTopSum = zeroExtend(topValSet[15:8])+zeroExtend(topValSet[23:16])+zeroExtend(topValSet[31:24])+zeroExtend(topValSet[39:32]) + 2;
1707
                              Bit#(10) tempLeftSum = zeroExtend(leftValSet[15:8])+zeroExtend(leftValSet[23:16])+zeroExtend(leftValSet[31:24])+zeroExtend(leftValSet[39:32]) + 2;
1708
                              Bit#(11) tempTotalSum = zeroExtend(tempTopSum)+zeroExtend(tempLeftSum);
1709
                              Bit#(8) topSum = tempTopSum[9:2];
1710
                              Bit#(8) leftSum = tempLeftSum[9:2];
1711
                              Bit#(8) totalSum = tempTotalSum[10:3];
1712
                              if(topAvailable==1 && leftAvailable==1)
1713
                                 predVector[pixelHor] = totalSum;
1714
                              else if(topAvailable==1)
1715
                                 predVector[pixelHor] = topSum;
1716
                              else if(leftAvailable==1)
1717
                                 predVector[pixelHor] = leftSum;
1718
                              else
1719
                                 predVector[pixelHor] = 8'b10000000;
1720
                           end
1721
                     end
1722
                     3://diagonal down left
1723
                     begin
1724
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1725
                           begin
1726
                              Bit#(4) selectNum = fromInteger(pixelHor)+zeroExtend(pixelVer);
1727
                              if(pixelHor==3 && pixelVer==3)
1728
                                 begin
1729
                                    topSelect1 = 6;
1730
                                    topSelect2 = 7;
1731
                                    topSelect3 = 7;
1732
                                 end
1733
                              else
1734
                                 begin
1735
                                    topSelect1 = selectNum;
1736
                                    topSelect2 = selectNum+1;
1737
                                    topSelect3 = selectNum+2;
1738
                                 end
1739
                              tempVal1 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1740
                              tempVal2 = zeroExtend(intra4x4SelectTop(topValSet,topSelect2));
1741
                              tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect3));
1742
                              Bit#(10) predVal = tempVal1 + (tempVal2<<1) + tempVal3 + 2;
1743
                              predVector[pixelHor] = predVal[9:2];
1744
                           end
1745
                     end
1746
                     4://diagonal down right
1747
                     begin
1748
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1749
                           begin
1750
                              if(fromInteger(pixelHor) > pixelVer)
1751
                                 begin
1752
                                    topSelect3 = fromInteger(pixelHor)-zeroExtend(pixelVer);
1753
                                    topSelect2 = topSelect3-1;
1754
                                    topSelect1 = topSelect3-2;
1755
                                    tempVal1 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1756
                                    tempVal2 = zeroExtend(intra4x4SelectTop(topValSet,topSelect2));
1757
                                    tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect3));
1758
                                 end
1759
                              else if(fromInteger(pixelHor) < pixelVer)
1760
                                 begin
1761
                                    leftSelect3 = zeroExtend(pixelVer)-fromInteger(pixelHor);
1762
                                    leftSelect2 = leftSelect3-1;
1763
                                    leftSelect1 = leftSelect3-2;
1764
                                    tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1765
                                    tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1766
                                    tempVal3 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect3));
1767
                                 end
1768
                              else
1769
                                 begin
1770
                                    leftSelect1 = 0;
1771
                                    leftSelect2 = -1;
1772
                                    topSelect1 = 0;
1773
                                    tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1774
                                    tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1775
                                    tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1776
                                 end
1777
                              Bit#(10) predVal = tempVal1 + (tempVal2<<1) + tempVal3 + 2;
1778
                              predVector[pixelHor] = predVal[9:2];
1779
                           end
1780
                     end
1781
                     5://vertical right
1782
                     begin
1783
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1784
                           begin
1785
                              Bit#(4) tempPixelHor = fromInteger(pixelHor);
1786
                              Bit#(4) zVR = (tempPixelHor<<1)-zeroExtend(pixelVer);
1787
                              if(zVR<=6 && zVR>=0)
1788
                                 begin
1789
                                    topSelect3 = fromInteger(pixelHor)-zeroExtend(pixelVer>>1);
1790
                                    topSelect2 = topSelect3-1;
1791
                                    if(zVR==1 || zVR==3 || zVR==5)
1792
                                       topSelect1 = topSelect3-2;
1793
                                    else
1794
                                       topSelect1 = topSelect3;
1795
                                    tempVal1 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1796
                                    tempVal2 = zeroExtend(intra4x4SelectTop(topValSet,topSelect2));
1797
                                    tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect3));
1798
                                 end
1799
                              else if(zVR==-1)
1800
                                 begin
1801
                                    leftSelect1 = 0;
1802
                                    leftSelect2 = -1;
1803
                                    topSelect1 = 0;
1804
                                    tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1805
                                    tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1806
                                    tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1807
                                 end
1808
                              else
1809
                                 begin
1810
                                    leftSelect1 = zeroExtend(pixelVer)-1;
1811
                                    leftSelect2 = leftSelect1-1;
1812
                                    leftSelect3 = leftSelect1-2;
1813
                                    tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1814
                                    tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1815
                                    tempVal3 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect3));
1816
                                 end
1817
                              Bit#(10) predVal = tempVal1 + (tempVal2<<1) + tempVal3 + 2;
1818
                              predVector[pixelHor] = predVal[9:2];
1819
                           end
1820
                     end
1821
                     6://horizontal down
1822
                     begin
1823
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1824
                           begin
1825
                              Bit#(4) tempPixelVer = zeroExtend(pixelVer);
1826
                              Bit#(4) zHD = (tempPixelVer<<1)-fromInteger(pixelHor);
1827
                              if(zHD<=6 && zHD>=0)
1828
                                 begin
1829
                                    leftSelect3 = zeroExtend(pixelVer)-fromInteger(pixelHor/2);
1830
                                    leftSelect2 = leftSelect3-1;
1831
                                    if(zHD==1 || zHD==3 || zHD==5)
1832
                                       leftSelect1 = leftSelect3-2;
1833
                                    else
1834
                                       leftSelect1 = leftSelect3;
1835
                                    tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1836
                                    tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1837
                                    tempVal3 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect3));
1838
                                 end
1839
                              else if(zHD==-1)
1840
                                 begin
1841
                                    leftSelect1 = 0;
1842
                                    leftSelect2 = -1;
1843
                                    topSelect1 = 0;
1844
                                    tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1845
                                    tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1846
                                    tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1847
                                 end
1848
                              else
1849
                                 begin
1850
                                    topSelect1 = fromInteger(pixelHor)-1;
1851
                                    topSelect2 = topSelect1-1;
1852
                                    topSelect3 = topSelect1-2;
1853
                                    tempVal1 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1854
                                    tempVal2 = zeroExtend(intra4x4SelectTop(topValSet,topSelect2));
1855
                                    tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect3));
1856
                                 end
1857
                              Bit#(10) predVal = tempVal1 + (tempVal2<<1) + tempVal3 + 2;
1858
                              predVector[pixelHor] = predVal[9:2];
1859
                           end
1860
                     end
1861
                     7://vertical left
1862
                     begin
1863
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1864
                           begin
1865
                              topSelect1 = fromInteger(pixelHor)+zeroExtend(pixelVer>>1);
1866
                              topSelect2 = topSelect1+1;
1867
                              if(pixelVer==1 || pixelVer==3)
1868
                                 topSelect3 = topSelect1+2;
1869
                              else
1870
                                 topSelect3 = topSelect1;
1871
                              tempVal1 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1872
                              tempVal2 = zeroExtend(intra4x4SelectTop(topValSet,topSelect2));
1873
                              tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect3));
1874
                              Bit#(10) predVal = tempVal1 + (tempVal2<<1) + tempVal3 + 2;
1875
                              predVector[pixelHor] = predVal[9:2];
1876
                           end
1877
                     end
1878
                     8://horizontal up
1879
                     begin
1880
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1881
                           begin
1882
                              Bit#(4) tempPixelVer = zeroExtend(pixelVer);
1883
                              Bit#(4) zHU = (tempPixelVer<<1)+fromInteger(pixelHor);
1884
                              if(zHU<=4)
1885
                                 begin
1886
                                    leftSelect1 = zeroExtend(pixelVer)+fromInteger(pixelHor/2);
1887
                                    leftSelect2 = leftSelect1+1;
1888
                                    if(zHU==1 || zHU==3)
1889
                                       leftSelect3 = leftSelect1+2;
1890
                                    else
1891
                                       leftSelect3 = leftSelect1;
1892
                                 end
1893
                              else
1894
                                 begin
1895
                                    if(zHU==5)
1896
                                       leftSelect1 = 2;
1897
                                    else
1898
                                       leftSelect1 = 3;
1899
                                    leftSelect2 = 3;
1900
                                    leftSelect3 = 3;
1901
                                 end
1902
                              tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1903
                              tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1904
                              tempVal3 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect3));
1905
                              Bit#(10) predVal = tempVal1 + (tempVal2<<1) + tempVal3 + 2;
1906
                              predVector[pixelHor] = predVal[9:2];
1907
                           end
1908
                     end
1909
                     default: $display( "ERROR Prediction: intraProcessStep intra4x4 unknown cur_intra4x4_pred_mode");
1910
                  endcase
1911
               end
1912
            else
1913
               $display( "ERROR Prediction: intraProcessStep intra4x4 unknown intraStepCount");
1914
         end
1915
      else if(intrastate==Intra16x16  && intraChromaFlag==0)
1916
         begin
1917
            //$display( "TRACE Prediction: intraProcessStep intra16x16 %0d %0d %0d %h", intra16x16_pred_mode, currMb, blockNum, select(intraTopVal,blockHor));/////////////////
1918
            case(intra16x16_pred_mode)
1919
               0://vertical
1920
               begin
1921
                  for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1922
                     begin
1923
                        Bit#(32) topValSet = select(intraTopVal,blockHor);
1924
                        Bit#(8) topVal = select32to8(topValSet,fromInteger(pixelHor));
1925
                        predVector[pixelHor] = topVal;
1926
                     end
1927
                  outFlag = 1;
1928
               end
1929
               1://horizontal
1930
               begin
1931
                  Bit#(40) leftValSet = select(intraLeftVal,blockVer);
1932
                  Bit#(8) leftVal = intra4x4SelectLeft(leftValSet,zeroExtend(pixelVer));
1933
                  for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1934
                     predVector[pixelHor] = leftVal;
1935
                  outFlag = 1;
1936
               end
1937
               2://dc
1938
               begin
1939
                  case(intraStepCount)
1940
                     2:
1941
                     begin
1942
                        if(topAvailable == 1)
1943
                           begin
1944
                              Bit#(32) topValSet = select(intraTopVal,0);
1945
                              intraSumA <= zeroExtend(topValSet[7:0])+zeroExtend(topValSet[15:8])+zeroExtend(topValSet[23:16])+zeroExtend(topValSet[31:24]);
1946
                           end
1947
                        else
1948
                           begin
1949
                              intraSumA <= 0;
1950
                              nextIntraStepCount = 6;
1951
                           end
1952
                     end
1953
                     3:
1954
                     begin
1955
                        Bit#(32) topValSet = select(intraTopVal,1);
1956
                        intraSumA <= intraSumA+zeroExtend(topValSet[7:0])+zeroExtend(topValSet[15:8])+zeroExtend(topValSet[23:16])+zeroExtend(topValSet[31:24]);
1957
                     end
1958
                     4:
1959
                     begin
1960
                        Bit#(32) topValSet = select(intraTopVal,2);
1961
                        intraSumA <= intraSumA+zeroExtend(topValSet[7:0])+zeroExtend(topValSet[15:8])+zeroExtend(topValSet[23:16])+zeroExtend(topValSet[31:24]);
1962
                     end
1963
                     5:
1964
                     begin
1965
                        Bit#(32) topValSet = select(intraTopVal,3);
1966
                        intraSumA <= intraSumA+zeroExtend(topValSet[7:0])+zeroExtend(topValSet[15:8])+zeroExtend(topValSet[23:16])+zeroExtend(topValSet[31:24])+8;
1967
                     end
1968
                     6:
1969
                     begin
1970
                        if(leftAvailable == 1)
1971
                           begin
1972
                              Bit#(40) leftValSet = select(intraLeftVal,0);
1973
                              intraSumA <= intraSumA+zeroExtend(leftValSet[15:8])+zeroExtend(leftValSet[23:16])+zeroExtend(leftValSet[31:24])+zeroExtend(leftValSet[39:32]);
1974
                           end
1975
                        else
1976
                           nextIntraStepCount = 10;
1977
                     end
1978
                     7:
1979
                     begin
1980
                        Bit#(40) leftValSet = select(intraLeftVal,1);
1981
                        intraSumA <= intraSumA+zeroExtend(leftValSet[15:8])+zeroExtend(leftValSet[23:16])+zeroExtend(leftValSet[31:24])+zeroExtend(leftValSet[39:32]);
1982
                     end
1983
                     8:
1984
                     begin
1985
                        Bit#(40) leftValSet = select(intraLeftVal,2);
1986
                        intraSumA <= intraSumA+zeroExtend(leftValSet[15:8])+zeroExtend(leftValSet[23:16])+zeroExtend(leftValSet[31:24])+zeroExtend(leftValSet[39:32]);
1987
                     end
1988
                     9:
1989
                     begin
1990
                        Bit#(40) leftValSet = select(intraLeftVal,3);
1991
                        intraSumA <= intraSumA+zeroExtend(leftValSet[15:8])+zeroExtend(leftValSet[23:16])+zeroExtend(leftValSet[31:24])+zeroExtend(leftValSet[39:32])+8;
1992
                     end
1993
                     10:
1994
                     begin
1995
                        if(leftAvailable == 1 && topAvailable == 1)
1996
                           intraSumA <= intraSumA >> 5;
1997
                        else if(leftAvailable == 1 || topAvailable == 1)
1998
                           intraSumA <= intraSumA >> 4;
1999
                        else
2000
                           intraSumA <= 128;
2001
                     end
2002
                     11:
2003
                     begin
2004
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
2005
                           predVector[pixelHor] = intraSumA[7:0];
2006
                        outFlag = 1;
2007
                     end
2008
                     default: $display( "ERROR Prediction: intraProcessStep intra16x16 DC unknown intraStepCount");
2009
                  endcase
2010
               end
2011
               3://plane
2012
               begin
2013
                  if(intraStepCount == 2)
2014
                     begin
2015
                        Bit#(32) topValSet = select(intraTopVal,3);
2016
                        Bit#(8) topVal = select32to8(topValSet,3);
2017
                        Bit#(40) leftValSet = select(intraLeftVal,3);
2018
                        Bit#(8) leftVal = intra4x4SelectLeft(leftValSet,3);
2019
                        Bit#(13) tempVal = zeroExtend(topVal) + zeroExtend(leftVal);
2020
                        intraSumA <= tempVal << 4;
2021
                        intraSumB <= 0;
2022
                        intraSumC <= 0;
2023
                     end
2024
                  else if(intraStepCount < 11)
2025
                     begin
2026
                        Bit#(4) xyPlusOne = intraStepCount-2;
2027
                        Bit#(4) xyPlusEight = intraStepCount+5;
2028
                        Bit#(4) sixMinusXY = 9-intraStepCount;
2029
                        Bit#(32) topValSet1 = select(intraTopVal,xyPlusEight[3:2]);
2030
                        Bit#(8) topVal1 = select32to8(topValSet1,xyPlusEight[1:0]);
2031
                        Bit#(40) leftValSet1 = select(intraLeftVal,xyPlusEight[3:2]);
2032
                        Bit#(8) leftVal1 = intra4x4SelectLeft(leftValSet1,zeroExtend(xyPlusEight[1:0]));
2033
                        Bit#(32) topValSet2=0;
2034
                        Bit#(8) topVal2;
2035
                        Bit#(40) leftValSet2;
2036
                        Bit#(8) leftVal2;
2037
                        if(intraStepCount==10)
2038
                           begin
2039
                              leftValSet2 = select(intraLeftVal,0);
2040
                              leftVal2 = intra4x4SelectLeft(leftValSet2,-1);
2041
                              topVal2 = leftVal2;
2042
                           end
2043
                        else
2044
                           begin
2045
                              topValSet2 = select(intraTopVal,sixMinusXY[3:2]);
2046
                              topVal2 = select32to8(topValSet2,sixMinusXY[1:0]);
2047
                              leftValSet2 = select(intraLeftVal,sixMinusXY[3:2]);
2048
                              leftVal2 = intra4x4SelectLeft(leftValSet2,zeroExtend(sixMinusXY[1:0]));
2049
                           end
2050
                        Bit#(15) diffH = zeroExtend(topVal1) - zeroExtend(topVal2);
2051
                        Bit#(15) diffV = zeroExtend(leftVal1) - zeroExtend(leftVal2);
2052
                        intraSumB <= intraSumB + (zeroExtend(xyPlusOne) * diffH);
2053
                        intraSumC <= intraSumC + (zeroExtend(xyPlusOne) * diffV);
2054
                     end
2055
                  else if(intraStepCount == 11)
2056
                     begin
2057
                        Bit#(18) tempSumB = (5*signExtend(intraSumB)) + 32;
2058
                        Bit#(18) tempSumC = (5*signExtend(intraSumC)) + 32;
2059
                        intraSumB <= signExtend(tempSumB[17:6]);
2060
                        intraSumC <= signExtend(tempSumC[17:6]);
2061
                     end
2062
                  else if(intraStepCount == 12)
2063
                     begin
2064
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
2065
                           begin
2066
                              Bit#(5)  positionHor = {1'b0,blockHor,fromInteger(pixelHor)};
2067
                              Bit#(5)  positionVer = {1'b0,blockVer,pixelVer};
2068
                              Bit#(16) tempProductB = signExtend(intraSumB) * signExtend(positionHor-7);
2069
                              Bit#(16) tempProductC = signExtend(intraSumC) * signExtend(positionVer-7);
2070
                              Bit#(16) tempTotal = tempProductB + tempProductC + zeroExtend(intraSumA) + 16;
2071
                              if(tempTotal[15]==1)
2072
                                 predVector[pixelHor] = 0;
2073
                              else if(tempTotal[14:5] > 255)
2074
                                 predVector[pixelHor] = 255;
2075
                              else
2076
                                 predVector[pixelHor] = tempTotal[12:5];
2077
                           end
2078
                        outFlag = 1;
2079
                     end
2080
                  else
2081
                     $display( "ERROR Prediction: intraProcessStep intra16x16 plane unknown intraStepCount");
2082
               end
2083
            endcase
2084
         end
2085
      else if(intraChromaFlag==1)
2086
         begin
2087
            //$display( "TRACE Prediction: intraProcessStep intraChroma %0d %0d %0d %0d %0d %0d %h %h %h %h %h %h %h %h",intra_chroma_pred_mode.first(),intraChromaTopAvailable,intraChromaLeftAvailable,currMb,blockNum,pixelNum,pack(intraLeftValChroma0),pack(intraTopValChroma0),pack(intraLeftValChroma1),pack(intraTopValChroma1),intraLeftValChroma0[0],intraTopValChroma0[3][15:8],intraLeftValChroma1[0],intraTopValChroma1[3][15:8]);///////////////////
2088
            Vector#(9,Bit#(8)) tempLeftVec;
2089
            Vector#(4,Bit#(16)) tempTopVec;
2090
            if(blockNum[2] == 0)
2091
               begin
2092
                  tempLeftVec = intraLeftValChroma0;
2093
                  tempTopVec = intraTopValChroma0;
2094
               end
2095
            else
2096
               begin
2097
                  tempLeftVec = intraLeftValChroma1;
2098
                  tempTopVec = intraTopValChroma1;
2099
               end
2100
            case(intra_chroma_pred_mode.first())
2101
               0://dc
2102
               begin
2103
                  if(intraStepCount == 2)
2104
                     begin
2105
                        Bit#(1) useTop=0;
2106
                        Bit#(1) useLeft=0;
2107
                        if(blockNum[1:0] == 0 || blockNum[1:0] == 3)
2108
                           begin
2109
                              useTop = intraChromaTopAvailable;
2110
                              useLeft = intraChromaLeftAvailable;
2111
                           end
2112
                        else if(blockNum[1:0] == 1)
2113
                           begin
2114
                              if(intraChromaTopAvailable == 1)
2115
                                 useTop = 1;
2116
                              else if(intraChromaLeftAvailable == 1)
2117
                                 useLeft = 1;
2118
                           end
2119
                              else if(blockNum[1:0] == 2)
2120
                                 begin
2121
                                    if(intraChromaLeftAvailable == 1)
2122
                                       useLeft = 1;
2123
                                    else if(intraChromaTopAvailable == 1)
2124
                                       useTop = 1;
2125
                                 end
2126
                        else
2127
                           $display( "ERROR Prediction: intraProcessStep intraChroma dc unknown blockNum");
2128
                        Bit#(10) topSum;
2129
                        Bit#(10) leftSum;
2130
                        Bit#(11) totalSum;
2131
                        if(blockHor[0] == 0)
2132
                           topSum = zeroExtend(tempTopVec[0][15:8])+zeroExtend(tempTopVec[0][7:0])+zeroExtend(tempTopVec[1][15:8])+zeroExtend(tempTopVec[1][7:0])+2;
2133
                        else
2134
                           topSum = zeroExtend(tempTopVec[2][15:8])+zeroExtend(tempTopVec[2][7:0])+zeroExtend(tempTopVec[3][15:8])+zeroExtend(tempTopVec[3][7:0])+2;
2135
                        if(blockVer[0] == 0)
2136
                           leftSum = zeroExtend(tempLeftVec[1])+zeroExtend(tempLeftVec[2])+zeroExtend(tempLeftVec[3])+zeroExtend(tempLeftVec[4])+2;
2137
                        else
2138
                           leftSum = zeroExtend(tempLeftVec[5])+zeroExtend(tempLeftVec[6])+zeroExtend(tempLeftVec[7])+zeroExtend(tempLeftVec[8])+2;
2139
                        totalSum = zeroExtend(topSum) + zeroExtend(leftSum);
2140
                        if(useTop==1 && useLeft==1)
2141
                           intraSumA <= zeroExtend(totalSum[10:3]);
2142
                        else if(useTop==1)
2143
                           intraSumA <= zeroExtend(topSum[9:2]);
2144
                        else if(useLeft==1)
2145
                           intraSumA <= zeroExtend(leftSum[9:2]);
2146
                        else
2147
                           intraSumA <= zeroExtend(8'b10000000);
2148
                     end
2149
                  else if(intraStepCount == 3)
2150
                     begin
2151
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
2152
                           predVector[pixelHor] = intraSumA[7:0];
2153
                        outFlag = 1;
2154
                     end
2155
                  else
2156
                     $display( "ERROR Prediction: intraProcessStep intraChroma dc unknown intraStepCount");
2157
               end
2158
               1://horizontal
2159
               begin
2160
                  for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
2161
                     begin
2162
                        Bit#(4) tempLeftIdx = {1'b0,blockVer[0],pixelVer} + 1;
2163
                        predVector[pixelHor] = select(tempLeftVec,tempLeftIdx);
2164
                     end
2165
                  outFlag = 1;
2166
               end
2167
               2://vertical
2168
               begin
2169
                  for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
2170
                     begin
2171
                        Bit#(2) pixelHorTemp = fromInteger(pixelHor);
2172
                        Bit#(16) tempTopVal = select(tempTopVec,{blockHor[0],pixelHorTemp[1]});
2173
                        if(pixelHorTemp[0] == 0)
2174
                           predVector[pixelHor] = tempTopVal[7:0];
2175
                        else
2176
                           predVector[pixelHor] = tempTopVal[15:8];
2177
                     end
2178
                  outFlag = 1;
2179
               end
2180
               3://plane
2181
               begin
2182
                  if(intraStepCount == 2)
2183
                     begin
2184
                        Bit#(16) topValSet = tempTopVec[3];
2185
                        Bit#(8) topVal = topValSet[15:8];
2186
                        Bit#(8) leftVal = tempLeftVec[8];
2187
                        Bit#(13) tempVal = zeroExtend(topVal) + zeroExtend(leftVal);
2188
                        intraSumA <= tempVal << 4;
2189
                        intraSumB <= 0;
2190
                        intraSumC <= 0;
2191
                     end
2192
                  else if(intraStepCount < 7)
2193
                     begin
2194
                        Bit#(3) xyPlusOne = truncate(intraStepCount)-2;
2195
                        Bit#(3) xyPlusFour = truncate(intraStepCount)+1;
2196
                        Bit#(4) twoMinusXY = 5-intraStepCount;
2197
                        Bit#(16) topValSet1 = select(tempTopVec,xyPlusFour[2:1]);
2198
                        Bit#(8) topVal1 = select16to8(topValSet1,xyPlusFour[0]);
2199
                        Bit#(4) tempLeftIdx1 = {1'b0,xyPlusFour} + 1;
2200
                        Bit#(8) leftVal1 = select(tempLeftVec,tempLeftIdx1);
2201
 
2202
                        Bit#(16) topValSet2 = select(tempTopVec,twoMinusXY[2:1]);
2203
                        Bit#(8) topVal2;
2204
                        Bit#(8) leftVal2 = select(tempLeftVec,twoMinusXY+1);
2205
                        if(intraStepCount==6)
2206
                           topVal2 = leftVal2;
2207
                        else
2208
                           topVal2 = select16to8(topValSet2,twoMinusXY[0]);
2209
                        Bit#(15) diffH = zeroExtend(topVal1) - zeroExtend(topVal2);
2210
                        Bit#(15) diffV = zeroExtend(leftVal1) - zeroExtend(leftVal2);
2211
                        intraSumB <= intraSumB + (zeroExtend(xyPlusOne) * diffH);
2212
                        intraSumC <= intraSumC + (zeroExtend(xyPlusOne) * diffV);
2213
                        Int#(15) tempDisplayH = unpack(zeroExtend(xyPlusOne) * diffH);
2214
                        Int#(15) tempDisplayV = unpack(zeroExtend(xyPlusOne) * diffV);
2215
                        //$display( "TRACE Prediction: intraProcessStep intraChroma plane partH partV %0d %0d",tempDisplayH,tempDisplayV);////////////////////
2216
                     end
2217
                  else if(intraStepCount == 7)
2218
                     begin
2219
                        Int#(15) tempDisplayH = unpack(intraSumB);
2220
                        Int#(15) tempDisplayV = unpack(intraSumC);
2221
                        //$display( "TRACE Prediction: intraProcessStep intraChroma plane H V %0d %0d",tempDisplayH,tempDisplayV);////////////////////
2222
                        Bit#(19) tempSumB = (34*signExtend(intraSumB)) + 32;
2223
                        Bit#(19) tempSumC = (34*signExtend(intraSumC)) + 32;
2224
                        intraSumB <= signExtend(tempSumB[18:6]);
2225
                        intraSumC <= signExtend(tempSumC[18:6]);
2226
                     end
2227
                  else if(intraStepCount == 8)
2228
                     begin
2229
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
2230
                           begin
2231
                              Bit#(4)  positionHor = {1'b0,blockHor[0],fromInteger(pixelHor)};
2232
                              Bit#(4)  positionVer = {1'b0,blockVer[0],pixelVer};
2233
                              Bit#(17) tempProductB = signExtend(intraSumB) * signExtend(positionHor-3);
2234
                              Bit#(17) tempProductC = signExtend(intraSumC) * signExtend(positionVer-3);
2235
                              Bit#(17) tempTotal = tempProductB + tempProductC + zeroExtend(intraSumA) + 16;
2236
                              if(tempTotal[16]==1)
2237
                                 predVector[pixelHor] = 0;
2238
                              else if(tempTotal[15:5] > 255)
2239
                                 predVector[pixelHor] = 255;
2240
                              else
2241
                                 predVector[pixelHor] = tempTotal[12:5];
2242
                           end
2243
                        outFlag = 1;
2244
                     end
2245
                  else
2246
                     $display( "ERROR Prediction: intraProcessStep intraChroma plane unknown intraStepCount");
2247
               end
2248
            endcase
2249
         end
2250
      else
2251
         $display( "ERROR Prediction: intraProcessStep unknown intrastate");
2252
 
2253
      if(outFlag==1)
2254
         begin
2255
            predictedfifo.enq(predVector);
2256
            pixelNum <= pixelNum+4;
2257
            if(pixelNum == 12)
2258
               begin
2259
                  if(intraChromaFlag==0)
2260
                     begin
2261
                        blockNum <= blockNum+1;
2262
                        if(blockNum == 15)
2263
                           begin
2264
                              intraChromaFlag <= 1;
2265
                              intraStepCount <= 2;
2266
                           end
2267
                        else if(intrastate==Intra4x4)
2268
                           intraStepCount <= 1;
2269
                     end
2270
                  else
2271
                     begin
2272
                        if(blockNum == 7)
2273
                           begin
2274
                              blockNum <= 0;
2275
                              intraChromaFlag <= 0;
2276
                              intraStepCount <= 0;
2277
                              intra_chroma_pred_mode.deq();
2278
                           end
2279
                        else
2280
                           begin
2281
                              blockNum <= blockNum+1;
2282
                              if(intra_chroma_pred_mode.first()==0)
2283
                                 intraStepCount <= 2;
2284
                              else if(blockNum==3)
2285
                                 intraStepCount <= 2;
2286
                           end
2287
                     end
2288
               end
2289
         end
2290
      else
2291
         intraStepCount <= nextIntraStepCount;
2292
      //$display( "Trace Prediction: intraProcessStep");
2293
   endrule
2294
 
2295
 
2296
 
2297
   interface Client mem_client_intra;
2298
      interface Get request  = fifoToGet(intraMemReqQ);
2299
      interface Put response = fifoToPut(intraMemRespQ);
2300
   endinterface
2301
   interface Client mem_client_inter;
2302
      interface Get request  = fifoToGet(interMemReqQ);
2303
      interface Put response = fifoToPut(interMemRespQ);
2304
   endinterface
2305
 
2306 90 jamey.hick
   interface Client mem_client_buffer;
2307
      interface Get request;
2308
         method ActionValue#(InterpolatorLoadReq) get();
2309
            InterpolatorLoadReq req <- interpolator.mem_client.request.get();
2310
            if(req matches tagged IPLoadLuma .data)
2311
              begin
2312
                memReqTypeFIFO.enq(Luma);
2313
              end
2314
            else if(req matches tagged IPLoadChroma .data)
2315
              begin
2316
                 memReqTypeFIFO.enq(Chroma);
2317
              end
2318
            return req;
2319
         endmethod
2320
      endinterface
2321
 
2322
      interface Put response;
2323
         method Action put(InterpolatorLoadResp resp);
2324
            memReqTypeFIFO.deq();
2325
            interpolator.mem_client.response.put(resp);
2326
         endmethod
2327
      endinterface
2328
   endinterface
2329
 
2330 86 jamey.hick
   interface Put ioin  = fifoToPut(infifo);
2331
   interface Put ioin_InverseTrans  = fifoToPut(infifo_ITB);
2332 90 jamey.hick
   interface Get iooutchroma = fifoToGet(outfifoluma);
2333
   interface Get iooutluma = fifoToGet(outfifochroma);
2334 86 jamey.hick
 
2335
endmodule
2336
 
2337
endpackage

powered by: WebSVN 2.1.0

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