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 84

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

Line No. Rev Author Line
1 84 jamey.hick
//**********************************************************************
2
// nC Calculator implementation
3
//----------------------------------------------------------------------
4
//
5
//
6
 
7
package mkCalc_nC;
8
 
9
import H264Types::*;
10
import ICalc_nC::*;
11
import FIFO::*;
12
 
13
import Connectable::*;
14
import GetPut::*;
15
import ClientServer::*;
16
 
17
 
18
(* synthesize *)
19
module mkCalc_nC( Calc_nC );
20
 
21
   Reg#(Bit#(PicWidthSz)) picWidth       <- mkReg(maxPicWidthInMB);
22
   Reg#(Bit#(PicAreaSz))  firstMb        <- mkReg(0);
23
   Reg#(Bit#(PicAreaSz))  currMb         <- mkReg(0);
24
   Reg#(Bit#(PicAreaSz))  currMbHor      <- mkReg(0);//horizontal position of currMb
25
   Reg#(Bit#(1))          waiting        <- mkReg(0);
26
   Reg#(Bit#(1))          reqCount       <- mkReg(0);
27
   Reg#(Bit#(2))          respCount      <- mkReg(0);
28
   Reg#(Bit#(1))          ipcmCount      <- mkReg(0);
29
   Reg#(Bit#(PicAreaSz))  pskipCount     <- mkReg(0);
30
   Reg#(Bit#(20))         leftVal        <- mkReg(0);
31
   Reg#(Bit#(20))         topVal         <- mkReg(0);
32
   Reg#(Bit#(10))         leftValChroma0 <- mkReg(0);
33
   Reg#(Bit#(10))         topValChroma0  <- mkReg(0);
34
   Reg#(Bit#(10))         leftValChroma1 <- mkReg(0);
35
   Reg#(Bit#(10))         topValChroma1  <- mkReg(0);
36
   FIFO#(MemReq#(TAdd#(PicWidthSz,1),20)) memReqQ  <- mkFIFO;
37
   FIFO#(MemResp#(20))                    memRespQ <- mkFIFO;
38
   Bit#(1) bit1 = 1;
39
   Bit#(1) bit0 = 0;
40
 
41
   rule currMbHorUpdate( !(currMbHor
42
      Bit#(PicAreaSz) temp = zeroExtend(picWidth);
43
      if((currMbHor >> 3) >= temp)
44
         currMbHor <= currMbHor - (temp << 3);
45
      else
46
         currMbHor <= currMbHor - temp;
47
   endrule
48
 
49
   rule sendReq ( waiting == 1 && reqCount > 0 );
50
      Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
51
      Bit#(TAdd#(PicWidthSz,1)) temp  = {bit1,temp2};
52
      memReqQ.enq(tagged LoadReq temp );
53
      reqCount <= reqCount-1;
54
   endrule
55
 
56
   rule receiveResp ( waiting == 1 &&& respCount > 0 &&& memRespQ.first() matches tagged LoadResp .data );
57
      if( respCount == 2 )
58
         topVal <= data;
59
      else
60
         begin
61
            topValChroma0 <= data[9:0];
62
            topValChroma1 <= data[19:10];
63
            waiting <= 0;
64
         end
65
      memRespQ.deq();
66
      respCount <= respCount - 1;
67
   endrule
68
 
69
   rule ipcmReq ( waiting == 1 && ipcmCount > 0 );
70
      currMb <= currMb+1;
71
      currMbHor <= currMbHor+1;
72
      Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
73
      Bit#(TAdd#(PicWidthSz,1)) temp  = {bit1,temp2};
74
      memReqQ.enq(tagged StoreReq {addr:temp,data:20'b10000100001000010000} );
75
      ipcmCount <= 0;
76
      waiting <= 0;
77
   endrule
78
 
79
   rule pskipReq ( waiting == 1 && pskipCount > 0 && currMbHor
80
      if(pskipCount[0] == 1)
81
         begin
82
            currMb <= currMb+1;
83
            currMbHor <= currMbHor+1;
84
            Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
85
            Bit#(TAdd#(PicWidthSz,1)) temp  = {bit1,temp2};
86
            memReqQ.enq(StoreReq {addr:temp,data:20'b00000000000000000000} );
87
            if(pskipCount == 1)
88
               waiting <= 0;
89
         end
90
      else
91
         begin
92
            Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
93
            Bit#(TAdd#(PicWidthSz,1)) temp  = {bit0,temp2};
94
            memReqQ.enq(StoreReq {addr:temp,data:20'b00000000000000000000} );
95
         end
96
      pskipCount <= pskipCount - 1;
97
   endrule
98
 
99
   method Action initialize_picWidth( Bit#(PicWidthSz) picWidthInMb ) if( waiting == 0 && currMbHor
100
      picWidth  <= picWidthInMb;
101
   endmethod
102
 
103
   method Action initialize( Bit#(PicAreaSz) firstMbAddr ) if( waiting == 0 && currMbHor
104
      firstMb   <= firstMbAddr;
105
      currMb    <= firstMbAddr;
106
      currMbHor <= firstMbAddr;
107
      leftVal   <= 0;
108
      leftValChroma0 <= 0;
109
      leftValChroma1 <= 0;
110
   endmethod
111
 
112
   method Action loadMb( Bit#(PicAreaSz) mbAddr ) if( waiting == 0 && currMbHor
113
      if( mbAddr != currMb )
114
         $display( "ERROR EntropyDec: mkCalc_nC loadMb wrong mbAddr" );
115
      else
116
         begin
117
            if( currMbHor == 0 || currMb == firstMb)
118
               begin
119
                  leftVal <= 20'b11111111111111111111;
120
                  leftValChroma0 <= 10'b1111111111;
121
                  leftValChroma1 <= 10'b1111111111;
122
               end
123
            if( currMb-firstMb < zeroExtend(picWidth) )
124
               begin
125
                  topVal <= 20'b11111111111111111111;
126
                  topValChroma0 <= 10'b1111111111;
127
                  topValChroma1 <= 10'b1111111111;
128
               end
129
            else
130
               begin
131
                  waiting <= 1;
132
                  reqCount <= 1;
133
                  respCount <= 2;
134
                  Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
135
                  Bit#(TAdd#(PicWidthSz,1)) temp  = {bit0,temp2};
136
                  memReqQ.enq(tagged LoadReq temp );
137
                  //$display( "ERROR EntropyDec: mkCalc_nC loadMb incomplete" );
138
               end
139
         end
140
   endmethod
141
 
142
   method Bit#(5) nCcalc_luma( Bit#(4) microBlockNum ) if( waiting == 0 && currMbHor
143
      Bit#(6) templeft = 0;
144
      Bit#(6) temptop  = 0;
145
      if(microBlockNum[3]==0 && microBlockNum[1]==0)
146
         templeft = zeroExtend(leftVal[4:0]);
147
      else if(microBlockNum[3]==0 && microBlockNum[1]==1)
148
         templeft = zeroExtend(leftVal[9:5]);
149
      else if(microBlockNum[3]==1 && microBlockNum[1]==0)
150
         templeft = zeroExtend(leftVal[14:10]);
151
      else
152
         templeft = zeroExtend(leftVal[19:15]);
153
      if(microBlockNum[2]==0 && microBlockNum[0]==0)
154
         temptop = zeroExtend(topVal[4:0]);
155
      else if(microBlockNum[2]==0 && microBlockNum[0]==1)
156
         temptop = zeroExtend(topVal[9:5]);
157
      else if(microBlockNum[2]==1 && microBlockNum[0]==0)
158
         temptop = zeroExtend(topVal[14:10]);
159
      else
160
         temptop = zeroExtend(topVal[19:15]);
161
      if(temptop!=6'b011111 && templeft!=6'b011111)
162
         return truncate((temptop+templeft+1) >> 1);
163
      else if(templeft!=6'b011111)
164
         return truncate(templeft);
165
      else if(temptop!=6'b011111)
166
         return truncate(temptop);
167
      else
168
         return 0;
169
   endmethod
170
 
171
   method Bit#(5) nCcalc_chroma( Bit#(3) microBlockNum ) if( waiting == 0 && currMbHor
172
      Bit#(6) templeft = 0;
173
      Bit#(6) temptop  = 0;
174
      if(microBlockNum[2]==0)
175
         begin
176
            if(microBlockNum[1]==0)
177
               templeft = zeroExtend(leftValChroma0[4:0]);
178
            else
179
               templeft = zeroExtend(leftValChroma0[9:5]);
180
            if(microBlockNum[0]==0)
181
               temptop = zeroExtend(topValChroma0[4:0]);
182
            else
183
               temptop = zeroExtend(topValChroma0[9:5]);
184
         end
185
      else
186
         begin
187
            if(microBlockNum[1]==0)
188
               templeft = zeroExtend(leftValChroma1[4:0]);
189
            else
190
               templeft = zeroExtend(leftValChroma1[9:5]);
191
            if(microBlockNum[0]==0)
192
               temptop = zeroExtend(topValChroma1[4:0]);
193
            else
194
               temptop = zeroExtend(topValChroma1[9:5]);
195
         end
196
      if(temptop!=6'b011111 && templeft!=6'b011111)
197
         return truncate((temptop+templeft+1) >> 1);
198
      else if(templeft!=6'b011111)
199
         return truncate(templeft);
200
      else if(temptop!=6'b011111)
201
         return truncate(temptop);
202
      else
203
         return 0;
204
   endmethod
205
 
206
   method Action  nNupdate_luma( Bit#(4) microBlockNum, Bit#(5) totalCoeff ) if( waiting == 0 && currMbHor
207
      Bit#(20) topValTemp = topVal;
208
      if(microBlockNum[3]==0 && microBlockNum[1]==0)
209
         leftVal <= {leftVal[19:5] , totalCoeff};
210
      else if(microBlockNum[3]==0 && microBlockNum[1]==1)
211
         leftVal <= {{leftVal[19:10] , totalCoeff} , leftVal[4:0]};
212
      else if(microBlockNum[3]==1 && microBlockNum[1]==0)
213
         leftVal <= {{leftVal[19:15] , totalCoeff} , leftVal[9:0]};
214
      else
215
         leftVal <= {totalCoeff , leftVal[14:0]};
216
      if(microBlockNum[2]==0 && microBlockNum[0]==0)
217
         topValTemp = {topVal[19:5] , totalCoeff};
218
      else if(microBlockNum[2]==0 && microBlockNum[0]==1)
219
         topValTemp = {{topVal[19:10] , totalCoeff} , topVal[4:0]};
220
      else if(microBlockNum[2]==1 && microBlockNum[0]==0)
221
         topValTemp = {{topVal[19:15] , totalCoeff} , topVal[9:0]};
222
      else
223
         topValTemp = {totalCoeff , topVal[14:0]};
224
      topVal <= topValTemp;
225
      if(microBlockNum == 15)
226
         begin
227
            Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
228
            Bit#(TAdd#(PicWidthSz,1)) temp  = {bit0,temp2};
229
            memReqQ.enq(StoreReq {addr:temp,data:topValTemp} );
230
         end
231
      //$display( "TRACE nNupdate_luma old leftVal %b", leftVal );
232
      //$display( "TRACE nNupdate_luma old topVal %b", topVal );
233
      //$display( "TRACE nNupdate_luma microBlockNum %0d", microBlockNum );
234
      //$display( "TRACE nNupdate_luma totalCoeff %0d", totalCoeff );
235
   endmethod
236
 
237
   method Action  nNupdate_chroma( Bit#(3) microBlockNum, Bit#(5) totalCoeff ) if( waiting == 0 && currMbHor
238
      Bit#(10) topValChroma0Temp = topValChroma0;
239
      Bit#(10) topValChroma1Temp = topValChroma1;
240
      if(microBlockNum[2]==0)
241
         begin
242
            if(microBlockNum[1]==0)
243
               leftValChroma0 <= {leftValChroma0[9:5] , totalCoeff};
244
            else
245
               leftValChroma0 <= {totalCoeff , leftValChroma0[4:0]};
246
            if(microBlockNum[0]==0)
247
               topValChroma0Temp = {topValChroma0[9:5] , totalCoeff};
248
            else
249
               topValChroma0Temp = {totalCoeff , topValChroma0[4:0]};
250
         end
251
      else
252
         begin
253
            if(microBlockNum[1]==0)
254
               leftValChroma1 <= {leftValChroma1[9:5] , totalCoeff};
255
            else
256
               leftValChroma1 <= {totalCoeff , leftValChroma1[4:0]};
257
            if(microBlockNum[0]==0)
258
               topValChroma1Temp = {topValChroma1[9:5] , totalCoeff};
259
            else
260
               topValChroma1Temp = {totalCoeff , topValChroma1[4:0]};
261
         end
262
      topValChroma0 <= topValChroma0Temp;
263
      topValChroma1 <= topValChroma1Temp;
264
      if(microBlockNum == 7)
265
         begin
266
            currMb <= currMb+1;
267
            currMbHor <= currMbHor+1;
268
            Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
269
            Bit#(TAdd#(PicWidthSz,1)) temp  = {bit1,temp2};
270
            memReqQ.enq(StoreReq {addr:temp,data:{topValChroma1Temp,topValChroma0Temp}} );
271
         end
272
   endmethod
273
 
274
   method Action  nNupdate_pskip( Bit#(PicAreaSz) inmb_skip_run ) if( waiting == 0 && currMbHor
275
      //$display( "TRACE nNupdate_pskip mb_skip_run = %0d", inmb_skip_run );
276
 
277
      if(inmb_skip_run > 0)
278
         begin
279
            waiting <= 1;
280
            pskipCount <= (inmb_skip_run << 1)-1;
281
            Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
282
            Bit#(TAdd#(PicWidthSz,1)) temp  = {bit0,temp2};
283
            memReqQ.enq(StoreReq {addr:temp,data:20'b00000000000000000000} );
284
            leftVal <= 0;
285
            leftValChroma0 <= 10'b0000000000;
286
            leftValChroma1 <= 10'b0000000000;
287
         end
288
   endmethod
289
 
290
   method Action  nNupdate_ipcm() if( waiting == 0 && currMbHor
291
      leftVal <= 20'b10000100001000010000;
292
      leftValChroma0 <= 10'b1000010000;
293
      leftValChroma1 <= 10'b1000010000;
294
      //$display( "TRACE nNupdate_ipcm");
295
 
296
      waiting <= 1;
297
      ipcmCount <= 1;
298
      Bit#(PicWidthSz)          temp2 = truncate(currMbHor);
299
      Bit#(TAdd#(PicWidthSz,1)) temp  = {bit0,temp2};
300
      memReqQ.enq(StoreReq {addr:temp,data:20'b10000100001000010000} );
301
   endmethod
302
 
303
   interface Client mem_client;
304
      interface Get request  = fifoToGet(memReqQ);
305
      interface Put response = fifoToPut(memRespQ);
306
   endinterface
307
 
308
 
309
endmodule
310
 
311
 
312
 
313
endpackage

powered by: WebSVN 2.1.0

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