// popcount8.sv: count the number of 1 bits in an 8-bit word. // The whole count is combinational: on an FPGA it becomes a small // adder tree in LUTs and produces a new result every clock cycle. `timescale 1ns/1ps module popcount8 ( input logic [7:0] data, output logic [3:0] count ); // Level 1: add bit pairs (each sum is 0..2, two bits wide) logic [1:0] s0, s1, s2, s3; // Level 2: add pairs of pairs (each sum is 0..4, three bits wide) logic [2:0] t0, t1; assign s0 = {1'b0, data[0]} + {1'b0, data[1]}; assign s1 = {1'b0, data[2]} + {1'b0, data[3]}; assign s2 = {1'b0, data[4]} + {1'b0, data[5]}; assign s3 = {1'b0, data[6]} + {1'b0, data[7]}; assign t0 = {1'b0, s0} + {1'b0, s1}; assign t1 = {1'b0, s2} + {1'b0, s3}; // Level 3: final sum is 0..8, four bits wide assign count = {1'b0, t0} + {1'b0, t1}; endmodule