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
- After a synchronous, active-low reset (
rst_n0 at a rising edge), the level isOFF. - At a rising edge where
upis 1 anddownis 0, the level steps to the next one:OFF,DIM,MED,HIGH,MAX, and fromMAXit goes round toOFF. - At a rising edge where
downis 1 andupis 0, it steps the other way, and fromOFFit goes round toMAX. - If
upanddownare both 0, or both 1, the level does not change. levelis 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.