SystemVerilog #73 · 2026-12-05 · Static vs automatic functions · Easy · about 12 min

A bit counter that must forget

Write ones_cnt, a module that counts the 1 bits in an 8-bit value.

  • On every rising edge of clk, n takes the number of 1 bits in v (0 to 8). So n shows the count for the value v had at the previous edge.
  • Do the counting in a function named count_ones that you write inside the module, with a local variable that holds the running count. The function is called on every clock, and every call must give the right answer for its own argument, whatever values came before.

Example: with v = 8'b0000_0110, n is 2 after the next edge. Then with v = 8'b1111_1111, n is 8. Then with v = 8'b0000_0001, n is 1.

Fill in ones_cnt.sv. Run applies three values.

Write and run your answer in the browser. The hidden tests check it, and it’s free.