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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [src/] [mkPrediction.bsv] - Blame information for rev 83

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

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

powered by: WebSVN 2.1.0

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