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.
第一种:
module top_module(
input [254:0] in,
output [7:0] out );
integer i;
always@(*)begin
out = 0;
for(i=0;i<255;i=i+1)begin
if(in[i])begin
out = out + 1;
end
else begin
out = out;
end
end
end
endmodule
第二种:
module top_module(
input [254:0] in,
output [7:0] out );
integer i;
always@(*)begin
out = 1'b0; // out = 8'b00000000;也可以 就是一定要对out使用之前赋初值
for(i=0;i< 255;i=i+1)begin
out = out + in[i];
end
end
endmodule