OpenCores
URL https://opencores.org/ocsvn/rtf_sprite_controller/rtf_sprite_controller/trunk

Subversion Repositories rtf_sprite_controller

[/] [rtf_sprite_controller/] [trunk/] [rtl/] [verilog/] [cntpop.v] - Blame information for rev 10

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

Line No. Rev Author Line
1 9 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      cntpop.v
9
//              - count number of one bits in a byte
10
//              - simple fast approach - lookup table
11
//
12
// BSD 3-Clause License
13
// Redistribution and use in source and binary forms, with or without
14
// modification, are permitted provided that the following conditions are met:
15
//
16
// 1. Redistributions of source code must retain the above copyright notice, this
17
//    list of conditions and the following disclaimer.
18
//
19
// 2. Redistributions in binary form must reproduce the above copyright notice,
20
//    this list of conditions and the following disclaimer in the documentation
21
//    and/or other materials provided with the distribution.
22
//
23
// 3. Neither the name of the copyright holder nor the names of its
24
//    contributors may be used to endorse or promote products derived from
25
//    this software without specific prior written permission.
26
//
27
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
//
38
// ============================================================================
39
 
40
module cntpop8(
41
        input [7:0] i,
42
        output reg [3:0] o
43
);
44
 
45
        always @(i)
46
                case (i)
47
                8'b00000000:    o = 0;
48
                8'b00000001:    o = 1;
49
                8'b00000010:    o = 1;
50
                8'b00000011:    o = 2;
51
                8'b00000100:    o = 1;
52
                8'b00000101:    o = 2;
53
                8'b00000110:    o = 2;
54
                8'b00000111:    o = 3;
55
                8'b00001000:    o = 1;
56
                8'b00001001:    o = 2;
57
                8'b00001010:    o = 2;
58
                8'b00001011:    o = 3;
59
                8'b00001100:    o = 2;
60
                8'b00001101:    o = 3;
61
                8'b00001110:    o = 3;
62
                8'b00001111:    o = 4;
63
 
64
                8'b00010000:    o = 1;
65
                8'b00010001:    o = 2;
66
                8'b00010010:    o = 2;
67
                8'b00010011:    o = 3;
68
                8'b00010100:    o = 2;
69
                8'b00010101:    o = 3;
70
                8'b00010110:    o = 3;
71
                8'b00010111:    o = 4;
72
                8'b00011000:    o = 2;
73
                8'b00011001:    o = 3;
74
                8'b00011010:    o = 3;
75
                8'b00011011:    o = 4;
76
                8'b00011100:    o = 3;
77
                8'b00011101:    o = 4;
78
                8'b00011110:    o = 4;
79
                8'b00011111:    o = 5;
80
 
81
                8'b00100000:    o = 1;
82
                8'b00100001:    o = 2;
83
                8'b00100010:    o = 2;
84
                8'b00100011:    o = 3;
85
                8'b00100100:    o = 2;
86
                8'b00100101:    o = 3;
87
                8'b00100110:    o = 3;
88
                8'b00100111:    o = 4;
89
                8'b00101000:    o = 2;
90
                8'b00101001:    o = 3;
91
                8'b00101010:    o = 3;
92
                8'b00101011:    o = 4;
93
                8'b00101100:    o = 3;
94
                8'b00101101:    o = 4;
95
                8'b00101110:    o = 4;
96
                8'b00101111:    o = 5;
97
 
98
                8'b00110000:    o = 2;
99
                8'b00110001:    o = 3;
100
                8'b00110010:    o = 3;
101
                8'b00110011:    o = 4;
102
                8'b00110100:    o = 3;
103
                8'b00110101:    o = 4;
104
                8'b00110110:    o = 4;
105
                8'b00110111:    o = 5;
106
                8'b00111000:    o = 3;
107
                8'b00111001:    o = 4;
108
                8'b00111010:    o = 4;
109
                8'b00111011:    o = 5;
110
                8'b00111100:    o = 4;
111
                8'b00111101:    o = 5;
112
                8'b00111110:    o = 5;
113
                8'b00111111:    o = 6;
114
 
115
                // 44 - 1       
116
                8'b01000000:    o = 1;
117
                8'b01000001:    o = 2;
118
                8'b01000010:    o = 2;
119
                8'b01000011:    o = 3;
120
                8'b01000100:    o = 2;
121
                8'b01000101:    o = 3;
122
                8'b01000110:    o = 3;
123
                8'b01000111:    o = 4;
124
                8'b01001000:    o = 2;
125
                8'b01001001:    o = 3;
126
                8'b01001010:    o = 3;
127
                8'b01001011:    o = 4;
128
                8'b01001100:    o = 3;
129
                8'b01001101:    o = 4;
130
                8'b01001110:    o = 4;
131
                8'b01001111:    o = 5;
132
 
133
                8'b01010000:    o = 2;
134
                8'b01010001:    o = 3;
135
                8'b01010010:    o = 3;
136
                8'b01010011:    o = 4;
137
                8'b01010100:    o = 3;
138
                8'b01010101:    o = 4;
139
                8'b01010110:    o = 4;
140
                8'b01010111:    o = 5;
141
                8'b01011000:    o = 3;
142
                8'b01011001:    o = 4;
143
                8'b01011010:    o = 4;
144
                8'b01011011:    o = 5;
145
                8'b01011100:    o = 4;
146
                8'b01011101:    o = 5;
147
                8'b01011110:    o = 5;
148
                8'b01011111:    o = 6;
149
 
150
                8'b01100000:    o = 2;
151
                8'b01100001:    o = 3;
152
                8'b01100010:    o = 3;
153
                8'b01100011:    o = 4;
154
                8'b01100100:    o = 3;
155
                8'b01100101:    o = 4;
156
                8'b01100110:    o = 4;
157
                8'b01100111:    o = 5;
158
                8'b01101000:    o = 3;
159
                8'b01101001:    o = 4;
160
                8'b01101010:    o = 4;
161
                8'b01101011:    o = 5;
162
                8'b01101100:    o = 4;
163
                8'b01101101:    o = 5;
164
                8'b01101110:    o = 5;
165
                8'b01101111:    o = 6;
166
 
167
                8'b01110000:    o = 3;
168
                8'b01110001:    o = 4;
169
                8'b01110010:    o = 4;
170
                8'b01110011:    o = 5;
171
                8'b01110100:    o = 4;
172
                8'b01110101:    o = 5;
173
                8'b01110110:    o = 5;
174
                8'b01110111:    o = 6;
175
                8'b01111000:    o = 4;
176
                8'b01111001:    o = 5;
177
                8'b01111010:    o = 5;
178
                8'b01111011:    o = 6;
179
                8'b01111100:    o = 5;
180
                8'b01111101:    o = 6;
181
                8'b01111110:    o = 6;
182
                8'b01111111:    o = 7;
183
 
184
                //  - 2 
185
                8'b10000000:    o = 1;
186
                8'b10000001:    o = 2;
187
                8'b10000010:    o = 2;
188
                8'b10000011:    o = 3;
189
                8'b10000100:    o = 2;
190
                8'b10000101:    o = 3;
191
                8'b10000110:    o = 3;
192
                8'b10000111:    o = 4;
193
                8'b10001000:    o = 2;
194
                8'b10001001:    o = 3;
195
                8'b10001010:    o = 3;
196
                8'b10001011:    o = 4;
197
                8'b10001100:    o = 3;
198
                8'b10001101:    o = 4;
199
                8'b10001110:    o = 4;
200
                8'b10001111:    o = 5;
201
 
202
                8'b10010000:    o = 2;
203
                8'b10010001:    o = 3;
204
                8'b10010010:    o = 3;
205
                8'b10010011:    o = 4;
206
                8'b10010100:    o = 3;
207
                8'b10010101:    o = 4;
208
                8'b10010110:    o = 4;
209
                8'b10010111:    o = 5;
210
                8'b10011000:    o = 3;
211
                8'b10011001:    o = 4;
212
                8'b10011010:    o = 4;
213
                8'b10011011:    o = 5;
214
                8'b10011100:    o = 4;
215
                8'b10011101:    o = 5;
216
                8'b10011110:    o = 5;
217
                8'b10011111:    o = 6;
218
 
219
                8'b10100000:    o = 2;
220
                8'b10100001:    o = 3;
221
                8'b10100010:    o = 3;
222
                8'b10100011:    o = 4;
223
                8'b10100100:    o = 3;
224
                8'b10100101:    o = 4;
225
                8'b10100110:    o = 4;
226
                8'b10100111:    o = 5;
227
                8'b10101000:    o = 3;
228
                8'b10101001:    o = 4;
229
                8'b10101010:    o = 4;
230
                8'b10101011:    o = 5;
231
                8'b10101100:    o = 4;
232
                8'b10101101:    o = 5;
233
                8'b10101110:    o = 5;
234
                8'b10101111:    o = 6;
235
 
236
                8'b10110000:    o = 3;
237
                8'b10110001:    o = 4;
238
                8'b10110010:    o = 4;
239
                8'b10110011:    o = 5;
240
                8'b10110100:    o = 4;
241
                8'b10110101:    o = 5;
242
                8'b10110110:    o = 5;
243
                8'b10110111:    o = 6;
244
                8'b10111000:    o = 4;
245
                8'b10111001:    o = 5;
246
                8'b10111010:    o = 5;
247
                8'b10111011:    o = 6;
248
                8'b10111100:    o = 5;
249
                8'b10111101:    o = 6;
250
                8'b10111110:    o = 6;
251
                8'b10111111:    o = 7;
252
 
253
                // 44 - 3       
254
                8'b11000000:    o = 2;
255
                8'b11000001:    o = 3;
256
                8'b11000010:    o = 3;
257
                8'b11000011:    o = 4;
258
                8'b11000100:    o = 3;
259
                8'b11000101:    o = 4;
260
                8'b11000110:    o = 4;
261
                8'b11000111:    o = 5;
262
                8'b11001000:    o = 3;
263
                8'b11001001:    o = 4;
264
                8'b11001010:    o = 4;
265
                8'b11001011:    o = 5;
266
                8'b11001100:    o = 4;
267
                8'b11001101:    o = 5;
268
                8'b11001110:    o = 5;
269
                8'b11001111:    o = 6;
270
 
271
                8'b11010000:    o = 3;
272
                8'b11010001:    o = 4;
273
                8'b11010010:    o = 4;
274
                8'b11010011:    o = 5;
275
                8'b11010100:    o = 4;
276
                8'b11010101:    o = 5;
277
                8'b11010110:    o = 5;
278
                8'b11010111:    o = 6;
279
                8'b11011000:    o = 4;
280
                8'b11011001:    o = 5;
281
                8'b11011010:    o = 5;
282
                8'b11011011:    o = 6;
283
                8'b11011100:    o = 5;
284
                8'b11011101:    o = 6;
285
                8'b11011110:    o = 6;
286
                8'b11011111:    o = 7;
287
 
288
                8'b11100000:    o = 3;
289
                8'b11100001:    o = 4;
290
                8'b11100010:    o = 4;
291
                8'b11100011:    o = 5;
292
                8'b11100100:    o = 4;
293
                8'b11100101:    o = 5;
294
                8'b11100110:    o = 5;
295
                8'b11100111:    o = 6;
296
                8'b11101000:    o = 4;
297
                8'b11101001:    o = 5;
298
                8'b11101010:    o = 5;
299
                8'b11101011:    o = 6;
300
                8'b11101100:    o = 5;
301
                8'b11101101:    o = 6;
302
                8'b11101110:    o = 6;
303
                8'b11101111:    o = 7;
304
 
305
                8'b11110000:    o = 4;
306
                8'b11110001:    o = 5;
307
                8'b11110010:    o = 5;
308
                8'b11110011:    o = 6;
309
                8'b11110100:    o = 5;
310
                8'b11110101:    o = 6;
311
                8'b11110110:    o = 6;
312
                8'b11110111:    o = 7;
313
                8'b11111000:    o = 5;
314
                8'b11111001:    o = 6;
315
                8'b11111010:    o = 6;
316
                8'b11111011:    o = 7;
317
                8'b11111100:    o = 6;
318
                8'b11111101:    o = 7;
319
                8'b11111110:    o = 7;
320
                8'b11111111:    o = 8;
321
 
322
                endcase
323
 
324
 
325
endmodule
326
 
327
 
328
module cntpop16(
329
        input [15:0] i,
330
        output [4:0] o
331
);
332
 
333
        wire [3:0] cnt1, cnt2;
334
 
335
        cntpop8 u1 (i[ 7:0],cnt1);
336
        cntpop8 u2 (i[15:8],cnt2);
337
 
338
        assign o = cnt1 + cnt2;
339
 
340
endmodule
341
 
342
 
343
// 76 slices / 147 LUTs / 19 ns
344
module cntpop32(
345
        input [31:0] i,
346
        output [5:0] o
347
);
348
 
349
        wire [3:0] cnt1, cnt2, cnt3, cnt4;
350
 
351
        // cntpop8 results in faster result than cntpop16
352
        cntpop8 u1 (i[ 7: 0],cnt1);
353
        cntpop8 u2 (i[15: 8],cnt2);
354
        cntpop8 u3 (i[23:16],cnt3);
355
        cntpop8 u4 (i[31:24],cnt4);
356
 
357
        assign o = cnt1+cnt2+cnt3+cnt4;
358
 
359
endmodule
360
 
361
 
362
// 156 slices / 300 LUTs / 22.2 ns
363
module cntpop64(
364
        input [63:0] i,
365
        output [6:0] o
366
);
367
 
368
        wire [4:0] cnt1, cnt2, cnt3, cnt4;
369
 
370
        cntpop16 u1 (i[15: 0],cnt1);
371
        cntpop16 u2 (i[31:16],cnt2);
372
        cntpop16 u3 (i[47:32],cnt3);
373
        cntpop16 u4 (i[63:48],cnt4);
374
 
375
        assign o = cnt1+cnt2+cnt3+cnt4;
376
 
377
endmodule
378
 
379
module cntpop80(
380
        input [79:0] i,
381
        output [6:0] o
382
);
383
 
384
        wire [4:0] cnt1, cnt2, cnt3, cnt4, cnt5;
385
 
386
        cntpop16 u1 (i[15: 0],cnt1);
387
        cntpop16 u2 (i[31:16],cnt2);
388
        cntpop16 u3 (i[47:32],cnt3);
389
        cntpop16 u4 (i[63:48],cnt4);
390
        cntpop16 u5 (i[79:64],cnt5);
391
 
392
        assign o = cnt1+cnt2+cnt3+cnt4+cnt5;
393
 
394
endmodule
395
 
396
module cntpop128(
397
        input [127:0] i,
398
        output [7:0] o
399
);
400
 
401
        wire [6:0] cnt1, cnt2;
402
 
403
        cntpop64 u1 (i[63: 0],cnt1);
404
        cntpop64 u2 (i[127:64],cnt2);
405
 
406
        assign o = cnt1+cnt2;
407
 
408
endmodule
409
 
410
 
411
module cntpop16reg (
412
  input clk,
413
  input ce,
414
        input [15:0] i,
415
        output reg [4:0] o
416
);
417
 
418
        wire [3:0] cnt1, cnt2;
419
 
420
        cntpop8 u1 (i[ 7:0],cnt1);
421
        cntpop8 u2 (i[15:8],cnt2);
422
 
423
  always @(posedge clk)
424
          if (ce) o <= cnt1 + cnt2;
425
 
426
endmodule
427
 
428
module cntpop64reg(
429
  input clk,
430
  input ce,
431
        input [63:0] i,
432
        output reg [6:0] o
433
);
434
 
435
        wire [4:0] cnt1, cnt2, cnt3, cnt4;
436
 
437
        cntpop16reg u1 (clk, ce, i[15: 0],cnt1);
438
        cntpop16reg u2 (clk, ce, i[31:16],cnt2);
439
        cntpop16reg u3 (clk, ce, i[47:32],cnt3);
440
        cntpop16reg u4 (clk, ce, i[63:48],cnt4);
441
 
442
  always @(posedge clk)
443
          if (ce) o <= cnt1+cnt2+cnt3+cnt4;
444
 
445
endmodule

powered by: WebSVN 2.1.0

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