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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [release/] [mkCalc_nC.bsv] - Blame information for rev 85

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

Line No. Rev Author Line
1 85 jamey.hick
// The MIT License
2
 
3
// Copyright (c) 2006-2007 Massachusetts Institute of Technology
4
 
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
 
12
// The above copyright notice and this permission notice shall be included in
13
// all copies or substantial portions of the Software.
14
 
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
// THE SOFTWARE.
22 84 jamey.hick
//**********************************************************************
23
// nC Calculator implementation
24
//----------------------------------------------------------------------
25
//
26
//
27
 
28
package mkCalc_nC;
29
 
30
import H264Types::*;
31
import ICalc_nC::*;
32
import FIFO::*;
33
 
34
import Connectable::*;
35
import GetPut::*;
36
import ClientServer::*;
37
 
38
 
39
(* synthesize *)
40
module mkCalc_nC( Calc_nC );
41
 
42
   Reg#(Bit#(PicWidthSz)) picWidth       <- mkReg(maxPicWidthInMB);
43
   Reg#(Bit#(PicAreaSz))  firstMb        <- mkReg(0);
44
   Reg#(Bit#(PicAreaSz))  currMb         <- mkReg(0);
45
   Reg#(Bit#(PicAreaSz))  currMbHor      <- mkReg(0);//horizontal position of currMb
46
   Reg#(Bit#(1))          waiting        <- mkReg(0);
47
   Reg#(Bit#(1))          reqCount       <- mkReg(0);
48
   Reg#(Bit#(2))          respCount      <- mkReg(0);
49
   Reg#(Bit#(1))          ipcmCount      <- mkReg(0);
50
   Reg#(Bit#(PicAreaSz))  pskipCount     <- mkReg(0);
51
   Reg#(Bit#(20))         leftVal        <- mkReg(0);
52
   Reg#(Bit#(20))         topVal         <- mkReg(0);
53
   Reg#(Bit#(10))         leftValChroma0 <- mkReg(0);
54
   Reg#(Bit#(10))         topValChroma0  <- mkReg(0);
55
   Reg#(Bit#(10))         leftValChroma1 <- mkReg(0);
56
   Reg#(Bit#(10))         topValChroma1  <- mkReg(0);
57
   FIFO#(MemReq#(TAdd#(PicWidthSz,1),20)) memReqQ  <- mkFIFO;
58
   FIFO#(MemResp#(20))                    memRespQ <- mkFIFO;
59
   Bit#(1) bit1 = 1;
60
   Bit#(1) bit0 = 0;
61
 
62
   rule currMbHorUpdate( !(currMbHor
63
      Bit#(PicAreaSz) temp = zeroExtend(picWidth);
64
      if((currMbHor >> 3) >= temp)
65
         currMbHor <= currMbHor - (temp << 3);
66
      else
67
         currMbHor <= currMbHor - temp;
68
   endrule
69
 
70
   rule sendReq ( waiting == 1 && reqCount > 0 );
71
      Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
72
      Bit#(TAdd#(PicWidthSz,1)) temp  = {bit1,temp2};
73
      memReqQ.enq(tagged LoadReq temp );
74
      reqCount <= reqCount-1;
75
   endrule
76
 
77
   rule receiveResp ( waiting == 1 &&& respCount > 0 &&& memRespQ.first() matches tagged LoadResp .data );
78
      if( respCount == 2 )
79
         topVal <= data;
80
      else
81
         begin
82
            topValChroma0 <= data[9:0];
83
            topValChroma1 <= data[19:10];
84
            waiting <= 0;
85
         end
86
      memRespQ.deq();
87
      respCount <= respCount - 1;
88
   endrule
89
 
90
   rule ipcmReq ( waiting == 1 && ipcmCount > 0 );
91
      currMb <= currMb+1;
92
      currMbHor <= currMbHor+1;
93
      Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
94
      Bit#(TAdd#(PicWidthSz,1)) temp  = {bit1,temp2};
95
      memReqQ.enq(tagged StoreReq {addr:temp,data:20'b10000100001000010000} );
96
      ipcmCount <= 0;
97
      waiting <= 0;
98
   endrule
99
 
100
   rule pskipReq ( waiting == 1 && pskipCount > 0 && currMbHor
101
      if(pskipCount[0] == 1)
102
         begin
103
            currMb <= currMb+1;
104
            currMbHor <= currMbHor+1;
105
            Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
106
            Bit#(TAdd#(PicWidthSz,1)) temp  = {bit1,temp2};
107
            memReqQ.enq(StoreReq {addr:temp,data:20'b00000000000000000000} );
108
            if(pskipCount == 1)
109
               waiting <= 0;
110
         end
111
      else
112
         begin
113
            Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
114
            Bit#(TAdd#(PicWidthSz,1)) temp  = {bit0,temp2};
115
            memReqQ.enq(StoreReq {addr:temp,data:20'b00000000000000000000} );
116
         end
117
      pskipCount <= pskipCount - 1;
118
   endrule
119
 
120
   method Action initialize_picWidth( Bit#(PicWidthSz) picWidthInMb ) if( waiting == 0 && currMbHor
121
      picWidth  <= picWidthInMb;
122
   endmethod
123
 
124
   method Action initialize( Bit#(PicAreaSz) firstMbAddr ) if( waiting == 0 && currMbHor
125
      firstMb   <= firstMbAddr;
126
      currMb    <= firstMbAddr;
127
      currMbHor <= firstMbAddr;
128
      leftVal   <= 0;
129
      leftValChroma0 <= 0;
130
      leftValChroma1 <= 0;
131
   endmethod
132
 
133
   method Action loadMb( Bit#(PicAreaSz) mbAddr ) if( waiting == 0 && currMbHor
134
      if( mbAddr != currMb )
135
         $display( "ERROR EntropyDec: mkCalc_nC loadMb wrong mbAddr" );
136
      else
137
         begin
138
            if( currMbHor == 0 || currMb == firstMb)
139
               begin
140
                  leftVal <= 20'b11111111111111111111;
141
                  leftValChroma0 <= 10'b1111111111;
142
                  leftValChroma1 <= 10'b1111111111;
143
               end
144
            if( currMb-firstMb < zeroExtend(picWidth) )
145
               begin
146
                  topVal <= 20'b11111111111111111111;
147
                  topValChroma0 <= 10'b1111111111;
148
                  topValChroma1 <= 10'b1111111111;
149
               end
150
            else
151
               begin
152
                  waiting <= 1;
153
                  reqCount <= 1;
154
                  respCount <= 2;
155
                  Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
156
                  Bit#(TAdd#(PicWidthSz,1)) temp  = {bit0,temp2};
157
                  memReqQ.enq(tagged LoadReq temp );
158
                  //$display( "ERROR EntropyDec: mkCalc_nC loadMb incomplete" );
159
               end
160
         end
161
   endmethod
162
 
163
   method Bit#(5) nCcalc_luma( Bit#(4) microBlockNum ) if( waiting == 0 && currMbHor
164
      Bit#(6) templeft = 0;
165
      Bit#(6) temptop  = 0;
166
      if(microBlockNum[3]==0 && microBlockNum[1]==0)
167
         templeft = zeroExtend(leftVal[4:0]);
168
      else if(microBlockNum[3]==0 && microBlockNum[1]==1)
169
         templeft = zeroExtend(leftVal[9:5]);
170
      else if(microBlockNum[3]==1 && microBlockNum[1]==0)
171
         templeft = zeroExtend(leftVal[14:10]);
172
      else
173
         templeft = zeroExtend(leftVal[19:15]);
174
      if(microBlockNum[2]==0 && microBlockNum[0]==0)
175
         temptop = zeroExtend(topVal[4:0]);
176
      else if(microBlockNum[2]==0 && microBlockNum[0]==1)
177
         temptop = zeroExtend(topVal[9:5]);
178
      else if(microBlockNum[2]==1 && microBlockNum[0]==0)
179
         temptop = zeroExtend(topVal[14:10]);
180
      else
181
         temptop = zeroExtend(topVal[19:15]);
182
      if(temptop!=6'b011111 && templeft!=6'b011111)
183
         return truncate((temptop+templeft+1) >> 1);
184
      else if(templeft!=6'b011111)
185
         return truncate(templeft);
186
      else if(temptop!=6'b011111)
187
         return truncate(temptop);
188
      else
189
         return 0;
190
   endmethod
191
 
192
   method Bit#(5) nCcalc_chroma( Bit#(3) microBlockNum ) if( waiting == 0 && currMbHor
193
      Bit#(6) templeft = 0;
194
      Bit#(6) temptop  = 0;
195
      if(microBlockNum[2]==0)
196
         begin
197
            if(microBlockNum[1]==0)
198
               templeft = zeroExtend(leftValChroma0[4:0]);
199
            else
200
               templeft = zeroExtend(leftValChroma0[9:5]);
201
            if(microBlockNum[0]==0)
202
               temptop = zeroExtend(topValChroma0[4:0]);
203
            else
204
               temptop = zeroExtend(topValChroma0[9:5]);
205
         end
206
      else
207
         begin
208
            if(microBlockNum[1]==0)
209
               templeft = zeroExtend(leftValChroma1[4:0]);
210
            else
211
               templeft = zeroExtend(leftValChroma1[9:5]);
212
            if(microBlockNum[0]==0)
213
               temptop = zeroExtend(topValChroma1[4:0]);
214
            else
215
               temptop = zeroExtend(topValChroma1[9:5]);
216
         end
217
      if(temptop!=6'b011111 && templeft!=6'b011111)
218
         return truncate((temptop+templeft+1) >> 1);
219
      else if(templeft!=6'b011111)
220
         return truncate(templeft);
221
      else if(temptop!=6'b011111)
222
         return truncate(temptop);
223
      else
224
         return 0;
225
   endmethod
226
 
227
   method Action  nNupdate_luma( Bit#(4) microBlockNum, Bit#(5) totalCoeff ) if( waiting == 0 && currMbHor
228
      Bit#(20) topValTemp = topVal;
229
      if(microBlockNum[3]==0 && microBlockNum[1]==0)
230
         leftVal <= {leftVal[19:5] , totalCoeff};
231
      else if(microBlockNum[3]==0 && microBlockNum[1]==1)
232
         leftVal <= {{leftVal[19:10] , totalCoeff} , leftVal[4:0]};
233
      else if(microBlockNum[3]==1 && microBlockNum[1]==0)
234
         leftVal <= {{leftVal[19:15] , totalCoeff} , leftVal[9:0]};
235
      else
236
         leftVal <= {totalCoeff , leftVal[14:0]};
237
      if(microBlockNum[2]==0 && microBlockNum[0]==0)
238
         topValTemp = {topVal[19:5] , totalCoeff};
239
      else if(microBlockNum[2]==0 && microBlockNum[0]==1)
240
         topValTemp = {{topVal[19:10] , totalCoeff} , topVal[4:0]};
241
      else if(microBlockNum[2]==1 && microBlockNum[0]==0)
242
         topValTemp = {{topVal[19:15] , totalCoeff} , topVal[9:0]};
243
      else
244
         topValTemp = {totalCoeff , topVal[14:0]};
245
      topVal <= topValTemp;
246
      if(microBlockNum == 15)
247
         begin
248
            Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
249
            Bit#(TAdd#(PicWidthSz,1)) temp  = {bit0,temp2};
250
            memReqQ.enq(StoreReq {addr:temp,data:topValTemp} );
251
         end
252
      //$display( "TRACE nNupdate_luma old leftVal %b", leftVal );
253
      //$display( "TRACE nNupdate_luma old topVal %b", topVal );
254
      //$display( "TRACE nNupdate_luma microBlockNum %0d", microBlockNum );
255
      //$display( "TRACE nNupdate_luma totalCoeff %0d", totalCoeff );
256
   endmethod
257
 
258
   method Action  nNupdate_chroma( Bit#(3) microBlockNum, Bit#(5) totalCoeff ) if( waiting == 0 && currMbHor
259
      Bit#(10) topValChroma0Temp = topValChroma0;
260
      Bit#(10) topValChroma1Temp = topValChroma1;
261
      if(microBlockNum[2]==0)
262
         begin
263
            if(microBlockNum[1]==0)
264
               leftValChroma0 <= {leftValChroma0[9:5] , totalCoeff};
265
            else
266
               leftValChroma0 <= {totalCoeff , leftValChroma0[4:0]};
267
            if(microBlockNum[0]==0)
268
               topValChroma0Temp = {topValChroma0[9:5] , totalCoeff};
269
            else
270
               topValChroma0Temp = {totalCoeff , topValChroma0[4:0]};
271
         end
272
      else
273
         begin
274
            if(microBlockNum[1]==0)
275
               leftValChroma1 <= {leftValChroma1[9:5] , totalCoeff};
276
            else
277
               leftValChroma1 <= {totalCoeff , leftValChroma1[4:0]};
278
            if(microBlockNum[0]==0)
279
               topValChroma1Temp = {topValChroma1[9:5] , totalCoeff};
280
            else
281
               topValChroma1Temp = {totalCoeff , topValChroma1[4:0]};
282
         end
283
      topValChroma0 <= topValChroma0Temp;
284
      topValChroma1 <= topValChroma1Temp;
285
      if(microBlockNum == 7)
286
         begin
287
            currMb <= currMb+1;
288
            currMbHor <= currMbHor+1;
289
            Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
290
            Bit#(TAdd#(PicWidthSz,1)) temp  = {bit1,temp2};
291
            memReqQ.enq(StoreReq {addr:temp,data:{topValChroma1Temp,topValChroma0Temp}} );
292
         end
293
   endmethod
294
 
295
   method Action  nNupdate_pskip( Bit#(PicAreaSz) inmb_skip_run ) if( waiting == 0 && currMbHor
296
      //$display( "TRACE nNupdate_pskip mb_skip_run = %0d", inmb_skip_run );
297
 
298
      if(inmb_skip_run > 0)
299
         begin
300
            waiting <= 1;
301
            pskipCount <= (inmb_skip_run << 1)-1;
302
            Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
303
            Bit#(TAdd#(PicWidthSz,1)) temp  = {bit0,temp2};
304
            memReqQ.enq(StoreReq {addr:temp,data:20'b00000000000000000000} );
305
            leftVal <= 0;
306
            leftValChroma0 <= 10'b0000000000;
307
            leftValChroma1 <= 10'b0000000000;
308
         end
309
   endmethod
310
 
311
   method Action  nNupdate_ipcm() if( waiting == 0 && currMbHor
312
      leftVal <= 20'b10000100001000010000;
313
      leftValChroma0 <= 10'b1000010000;
314
      leftValChroma1 <= 10'b1000010000;
315
      //$display( "TRACE nNupdate_ipcm");
316
 
317
      waiting <= 1;
318
      ipcmCount <= 1;
319
      Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
320
      Bit#(TAdd#(PicWidthSz,1)) temp  = {bit0,temp2};
321
      memReqQ.enq(StoreReq {addr:temp,data:20'b10000100001000010000} );
322
   endmethod
323
 
324
   interface Client mem_client;
325
      interface Get request  = fifoToGet(memReqQ);
326
      interface Put response = fifoToPut(memRespQ);
327
   endinterface
328
 
329
 
330
endmodule
331
 
332
 
333
 
334
endpackage

powered by: WebSVN 2.1.0

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