combinational for-loop:255-bit population count

A "population count" circuit counts the number of '1's in an input vector. Build a population count circuit for a 255-bit input vector.

Popcount255 - HDLBits (01xz.net)

 1 module top_module( 
 2     input [254:0] in,
 3     output [7:0] out );
 4  integer i;
 5     always @(*) begin
 6         out = 0;
 7         for(i=0;i<$bits(in);i=i+1) begin    // $bits()为计算位宽函数
 8             if(in[i]) out = out+8'b1;  
 9         end
10     end   
11 
12 endmodule

 另一种写法:

module top_module( 
    input [254:0] in,
    output [7:0] out );
    integer i;
    integer temp;
    always @(*) begin
        out = 8'd0;
        for(i=0;i<255;i++) begin
            out = out + in[i];
        end
    end
endmodule

此题我理解为输入input每进来一个,output就加一个计数

posted @ 2023-04-20 10:52  江左子固  阅读(30)  评论(0)    收藏  举报