Fix a fan-speed selector whose enum state register accepts numbers that name no speed. Every assignment to it goes through an unchecked cast.
Hardware context: A six-state bus-burst sequencer whose three-bit state register has two unnamed codes. Fix the fan-speed selector whose enum register takes numbers through unchecked casts.
A fan controller keeps its speed in an enum of five names: OFF, LOW, MID, HIGH and TURBO. Software can set a speed code, and two buttons step it up and down. In the field the fan sometimes stopped answering the buttons. The log line that prints the speed by name was empty.
The first version would not compile: the tools refused a number assigned to the enum. Its author wrapped each number in a cast, and the errors went away. This is a Fix problem. Find what those casts let through, and make the selector follow the brief.
Brief: rising-edge clk; rst_n asynchronous and active-low, to OFF with bad at 0. A set wins over the buttons. A set with a code from 0 to 4 loads that speed. A set with code 5, 6 or 7 is refused: the speed holds, and bad is 1 for the next cycle. With no set, up alone moves one speed up and holds at TURBO. down alone moves one speed down and holds at OFF. up and down together hold. Synthesis check: the design must build on the site's synthesis flow with no latch.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
script | 96 | input of each check |
Full specification
module fan_sel (
input logic clk,
input logic rst_n, // asynchronous, active-low
input logic set, // 1: load the speed from set_code
input logic [2:0] set_code,
input logic up, // 1: one speed up
input logic down, // 1: one speed down
output logic [2:0] speed,
output logic bad // 1 for the cycle after a refused set
);
typedef enum logic [2:0] {OFF, LOW, MID, HIGH, TURBO} speed_e; // codes 0 to 4
speed_e speed_q;
// your fix
endmodule
| At a rising edge out of reset | speed | bad after the edge |
|---|---|---|
| set = 1, set_code 0 to 4 | becomes set_code | 0 |
| set = 1, set_code 5 to 7 | holds | 1 |
| set = 0, up = 1, down = 0 | one speed up; TURBO holds | 0 |
| set = 0, up = 0, down = 1 | one speed down; OFF holds | 0 |
| set = 0, up = down | holds | 0 |
Each test pulses rst_n, then applies 16 steps of one edge each. After every edge it records speed and bad. It also reads speed_q.name() inside your design and compares it with the name of the code on the speed port. The result field named_steps lists the steps where the two agree.
Examples
Example 1
Up to the Top, and Two More- Input
up x6, down x2, idle x8
- Output
speed_step0 = LOW (1), speed_step1 = MID (2), speed_step2 = HIGH (3), speed_step3 = TURBO (4), speed_step4 = TURBO (4), speed_step5 = TURBO (4), speed_step6 = HIGH (3), speed_step7 = MID (2), speed_step8 = MID (2), speed_step9 = MID (2), speed_step10 = MID (2), speed_step11 = MID (2), speed_step12 = MID (2), speed_step13 = MID (2), speed_step14 = MID (2), speed_step15 = MID (2), named_steps = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
Four ups reach TURBO. The fifth and sixth must hold there. The shipped design counts on to codes 5 and 6, which have no name.
Example 2
A Set to a Code with No Name- Input
set 2, set 6, idle, up, set 7, set 5, down, idle x9
- Output
speed_step0 = MID (2), speed_step1 = MID (2), bad_step1 = yes, speed_step2 = MID (2), speed_step3 = HIGH (3), speed_step4 = HIGH (3), bad_step4 = yes, speed_step5 = HIGH (3), bad_step5 = yes, speed_step6 = MID (2), speed_step7 = MID (2), speed_step8 = MID (2), speed_step9 = MID (2), speed_step10 = MID (2), speed_step11 = MID (2), speed_step12 = MID (2), speed_step13 = MID (2), speed_step14 = MID (2), speed_step15 = MID (2), named_steps = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
The set of 6 is refused: the speed stays MID and bad is 1 for one cycle. The up that follows gives HIGH.
Example 3
Down from OFF- Input
down x2, up, up+down x2, up, down x3, idle x7
- Output
speed_step0 = OFF (0), speed_step1 = OFF (0), speed_step2 = LOW (1), speed_step3 = LOW (1), speed_step4 = LOW (1), speed_step5 = MID (2), speed_step6 = LOW (1), speed_step7 = OFF (0), speed_step8 = OFF (0), speed_step9 = OFF (0), speed_step10 = OFF (0), speed_step11 = OFF (0), speed_step12 = OFF (0), speed_step13 = OFF (0), speed_step14 = OFF (0), speed_step15 = OFF (0), named_steps = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
Two downs from OFF hold at OFF. up and down together hold as well.
Constraints and Contract
- Inputs are known 0/1 values and change while clk is low.
- Keep the module name, the ports, the type speed_e with its five names, and the register speed_q of that type.
Hints
Hint 1
Run the second example. After the set of 6, what does the speed port show, and which name does that code have?
Hint 2
A cast changes the type and checks nothing. Each cast in this design needs a question in front of it: can this value be a speed?
Hint 3
There are three casts. The set needs a range check. The two steps need an end stop: the brief says hold, where a count or next() would run on or wrap.
What the grader checks: Seven directed and ten seeded scripts of sixteen steps drive set, set_code, up and down. speed and bad after every edge are compared with an independent model. The name of speed_q is read through the hierarchy at every step. Lint is silent on the shipped file too. A cast hides the defect from every tool, so only the tests find it.
The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.
17 checks on Icarus Verilog + Yosys · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 2 steps and the final capstone