// tb_popcount8.sv: exhaustive self-checking testbench for popcount8. `timescale 1ns/1ps module tb_popcount8; logic [7:0] data; logic [3:0] count; int unsigned errors = 0; popcount8 dut (.data(data), .count(count)); // Reference model: count the bits one at a time, like a software loop. function automatic logic [3:0] ref_count(input logic [7:0] d); logic [3:0] n = '0; for (int i = 0; i < 8; i++) n = n + {3'b000, d[i]}; return n; endfunction initial begin for (int v = 0; v < 256; v++) begin data = v[7:0]; #1; if (count !== ref_count(data)) begin $display("FAIL data=%08b count=%0d expected=%0d", data, count, ref_count(data)); errors++; end end if (errors == 0) $display("PASS: all 256 input values checked"); else $display("FAILED with %0d errors", errors); $finish; end endmodule