R11.4.2 A One-Hot Controller by Reverse Case

MediumImplementIcarus Verilog + Yosys · serverReview the topic

Write a one-hot motor controller with a reverse case. Give every illegal state a safe answer, and keep the outputs off until the state is legal again.

Hardware context: The controller of a serial sender with load, shift, wait and done states and a bit counter; and a one-hot motor controller with a debug load port on its state register. Write the one-hot motor controller with a reverse case and recovery from every illegal state.

A motor controller has five states, one flop each: STOP, SPINUP, RUN, BRAKE and TRIP. One-hot was chosen so that drive and brake come from single state bits. A motor is also a place where a flipped state bit must end safely. The safety review asks what the controller does for all 32 values of the state register. The five legal ones are not enough.

This is an Implement problem: write motor_ctrl from its brief. Use a reverse case, case (1'b1), for the five legal states. A debug port can load any value into the state register. The tests use it to put the machine in illegal states.

Brief: rising-edge clk; rst_n asynchronous and active-low, to STOP. The state register is the output state, one bit per state. Transitions, their priority and the rules for illegal states are in the specification. Synthesis check: the design must build on the site's synthesis flow with no latch. The cell and level counts come from an open-source flow and are a proxy for area and depth, not a foundry result.

Signals in the Timeline

SignalWidthRole
script108input of each check
Full specification
The controller's ports (keep the name and the ports)
module motor_ctrl (
  input  logic       clk,
  input  logic       rst_n,       // asynchronous, active-low
  input  logic       go,
  input  logic       halt,
  input  logic       fault,
  input  logic       at_speed,
  input  logic       still,       // the rotor has stopped
  input  logic       clear,       // acknowledge a trip
  input  logic       dbg_load,    // 1: load the state register from dbg_state at this edge
  input  logic [4:0] dbg_state,
  output logic [4:0] state,       // one bit per state: STOP 0, SPINUP 1, RUN 2, BRAKE 3, TRIP 4
  output logic       drive,
  output logic       brake,
  output logic       alarm
);
  // your design
endmodule
State (bit)Next state, first rule that applies
STOP (0)fault: TRIP. go: SPINUP. Otherwise stay.
SPINUP (1)fault: TRIP. halt: BRAKE. at_speed: RUN. Otherwise stay.
RUN (2)fault: TRIP. halt: BRAKE. Otherwise stay.
BRAKE (3)fault: TRIP. still: STOP. Otherwise stay.
TRIP (4)clear with no fault: STOP. Otherwise stay.
State registerAt the next edgeOutputs meanwhile
exactly one bit setthe table abovedrive in SPINUP and RUN, brake in BRAKE, alarm in TRIP
no bit setSTOP, whatever the inputsdrive, brake and alarm all 0
two or more bits setTRIP, whatever the inputsdrive, brake and alarm all 0

dbg_load wins over everything except reset: at an edge with dbg_load = 1 the state register takes dbg_state, legal or not.

Each test first loads an illegal state, then pulses rst_n low with no clock edge and reads the outputs. It then applies 9 steps of one edge each and reads state, drive, brake and alarm after every edge.

The tests check behaviour, not spelling. The reverse case is part of the brief; a reviewer, not the grader, checks that you used it.

Examples

Example 1

Start, Reach Speed, Halt, Stop
Input
go, idle, at_speed, idle, halt, idle, still, idle, idle
Output
state_in_reset = 'b00001, state_0 = 'b00010, drive_0 = yes, brake_0 = no, alarm_0 = no, state_1 = 'b00010, drive_1 = yes, brake_1 = no, alarm_1 = no, state_2 = 'b00100, drive_2 = yes, brake_2 = no, alarm_2 = no, state_3 = 'b00100, drive_3 = yes, brake_3 = no, alarm_3 = no, state_4 = 'b01000, drive_4 = no, brake_4 = yes, alarm_4 = no, state_5 = 'b01000, drive_5 = no, brake_5 = yes, alarm_5 = no, state_6 = 'b00001, drive_6 = no, brake_6 = no, alarm_6 = no, state_7 = 'b00001, drive_7 = no, brake_7 = no, alarm_7 = no, state_8 = 'b00001, drive_8 = no, brake_8 = no, alarm_8 = no

go moves STOP to SPINUP, at_speed to RUN, halt to BRAKE and still back to STOP. drive is 1 in SPINUP and RUN, brake in BRAKE.

Example 2

No State Bit Set Returns to STOP
Input
go, load 00000+go, go, go, load 00000, fault, idle, clear, idle
Output
state_in_reset = 'b00001, state_0 = 'b00010, drive_0 = yes, brake_0 = no, alarm_0 = no, state_1 = 'b00000, drive_1 = no, brake_1 = no, alarm_1 = no, state_2 = 'b00001, drive_2 = no, brake_2 = no, alarm_2 = no, state_3 = 'b00010, drive_3 = yes, brake_3 = no, alarm_3 = no, state_4 = 'b00000, drive_4 = no, brake_4 = no, alarm_4 = no, state_5 = 'b00001, drive_5 = no, brake_5 = no, alarm_5 = no, state_6 = 'b00001, drive_6 = no, brake_6 = no, alarm_6 = no, state_7 = 'b00001, drive_7 = no, brake_7 = no, alarm_7 = no, state_8 = 'b00001, drive_8 = no, brake_8 = no, alarm_8 = no

The debug port loads 00000 while go is 1. The next edge gives STOP, not SPINUP, and all outputs are 0 in between.

Example 3

Two State Bits Set Goes to TRIP
Input
go, load 00110, at_speed, clear, load 01001+go, go, clear, go, idle
Output
state_in_reset = 'b00001, state_0 = 'b00010, drive_0 = yes, brake_0 = no, alarm_0 = no, state_1 = 'b00110, drive_1 = no, brake_1 = no, alarm_1 = no, state_2 = 'b10000, drive_2 = no, brake_2 = no, alarm_2 = yes, state_3 = 'b00001, drive_3 = no, brake_3 = no, alarm_3 = no, state_4 = 'b01001, drive_4 = no, brake_4 = no, alarm_4 = no, state_5 = 'b10000, drive_5 = no, brake_5 = no, alarm_5 = yes, state_6 = 'b00001, drive_6 = no, brake_6 = no, alarm_6 = no, state_7 = 'b00010, drive_7 = yes, brake_7 = no, alarm_7 = no, state_8 = 'b00010, drive_8 = yes, brake_8 = no, alarm_8 = no

00110 has SPINUP and RUN set. drive stays 0, and the next edge gives TRIP with alarm, whatever the inputs are.

Constraints and Contract

  • Inputs are known 0/1 values and change while clk is low.
  • Keep the module name and the ports, and the bit number of each state.

Hints

Hint 1

Start the next-state block with all zeros, and set exactly one bit on every path.

Hint 2

A reverse case alone treats two set bits as the first matching state. The brief wants TRIP there, so test for a legal state before the case.

Hint 3

Count the set bits with $onehot(state), or with a small loop. Use the same signal to hold the three outputs at 0.

What the grader checks: Eight directed and twelve seeded scripts of nine edges run after an illegal previous life and a reset pulse. The state and the three outputs after every edge are compared with an independent model. Legal and illegal states are loaded through the debug port. Passing shows the behaviour for these states and inputs; the coding style is not graded. The synthesis check adds the latch count.

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.

20 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

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Start, Reach Speed, Halt, Stop

Input
go, idle, at_speed, idle, halt, idle, still, idle, idle
Expected output
state_in_reset = 'b00001, state_0 = 'b00010, drive_0 = yes, brake_0 = no, alarm_0 = no, state_1 = 'b00010, drive_1 = yes, brake_1 = no, alarm_1 = no, state_2 = 'b00100, drive_2 = yes, brake_2 = no, alarm_2 = no, state_3 = 'b00100, drive_3 = yes, brake_3 = no, alarm_3 = no, state_4 = 'b01000, drive_4 = no, brake_4 = yes, alarm_4 = no, state_5 = 'b01000, drive_5 = no, brake_5 = yes, alarm_5 = no, state_6 = 'b00001, drive_6 = no, brake_6 = no, alarm_6 = no, state_7 = 'b00001, drive_7 = no, brake_7 = no, alarm_7 = no, state_8 = 'b00001, drive_8 = no, brake_8 = no, alarm_8 = no

go moves STOP to SPINUP, at_speed to RUN, halt to BRAKE and still back to STOP. drive is 1 in SPINUP and RUN, brake in BRAKE.

Run samples executes all 3 public checks. Check all tests runs 20 checks on the server.

Icarus Verilog + Yosys · server