SystemVerilog #64 · 2026-11-26 · Static and automatic functions · Easy · about 15 min

Longest run of ones, call after call

Write run_len in run_len.sv: a combinational module that finds the longest run of consecutive 1s in a word.

  • v is a W-bit input (W is a parameter, default 16). longest is a 5-bit output.
  • longest is the length of the longest stretch of adjacent 1 bits in v, counted over any position (bit 0 up to bit W-1). A word of all zeros gives 0, and a word of all ones gives W.
  • Write the counting as a function named longest_run inside the module, and drive longest from it. The function is called every time v changes, and it must give the right answer for every call, whatever words came before.

Example (W = 16): v = 16'b0000_0000_0111_0110 has runs of 3 and 2, so longest = 3. Then v = 16'b0000_0000_0000_0000 gives 0 and v = 16'b1111_1111_1111_1111 gives 16.

Fill in run_len.sv. Run applies two words and prints the result for each.

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