SystemVerilog #66 · 2026-11-28 · Enums · Easy · about 12 min

A lamp level that walks off the end of its enum

Write level_ctl, the brightness control of a lamp. It has five levels, kept in the enum level_t that the skeleton declares: OFF (0), DIM (1), MED (2), HIGH (3) and MAX (4). The output level shows the current level as a number.

Requirements

  1. After a synchronous, active-low reset (rst_n 0 at a rising edge), the level is OFF.
  2. At a rising edge where up is 1 and down is 0, the level steps to the next one: OFF, DIM, MED, HIGH, MAX, and from MAX it goes round to OFF.
  3. At a rising edge where down is 1 and up is 0, it steps the other way, and from OFF it goes round to MAX.
  4. If up and down are both 0, or both 1, the level does not change.
  5. level is the registered level itself, so a change shows right after the edge. It is never a value outside 0 to 4.

Example: from reset, three clocks of up give 1, 2, 3. Then one clock with both up and down stays at 3, and one clock of down gives 2.

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