module top_module ( input [99:0] in, outputreg [99:0] out ); always @(*) begin for (int i=0;i<$bits(out);i++) // $bits() is a system function that returns the width of a signal. out[i] = in[$bits(out)-i-1]; // $bits(out) is 100 because out is 100 bits wide. end endmodule
population count
计算输入中1的个数
1 2 3 4 5 6 7 8 9 10 11 12
module top_module ( input [254:0] in, outputreg [7:0] out );
always @(*) begin// Combinational always block out = 0; for (int i=0;i<255;i++) out = out + in[i]; end endmodule