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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [src/] [mkCalc_nC.bsv] - Blame information for rev 100

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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