HDL-Bits Solution : Popcount255

For this quiz, we need to write code to create a Population Count circuit. This circuit counts logic 1s on each bit and outputs the number in binary form. For example, if it finds three logic ones in the circuit, it outputs 011 (binary form), which is equivalent to the number three. I use a for-loop in this quiz.

Here is my code for this quiz.

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

Reference: https://hdlbits.01xz.net/wiki/Popcount255

Leave a Reply

Your email address will not be published. Required fields are marked *