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.
vis aW-bit input (Wis a parameter, default 16).longestis a 5-bit output.longestis the length of the longest stretch of adjacent 1 bits inv, counted over any position (bit 0 up to bitW-1). A word of all zeros gives 0, and a word of all ones givesW.- Write the counting as a function named
longest_runinside the module, and drivelongestfrom it. The function is called every timevchanges, 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.