G7.6.1 One-Hot Rewrite with a One-Hot Check

MediumImplementIcarus Verilog + Yosys · serverReview the topic

Rewrite a garage-door controller with a one-hot state register that flags any bad code and returns to CLOSED.

Hardware context: A garage-door controller and a frame receiver that must survive a glitch in their state registers. Rewrite a controller with a one-hot state register that checks itself and recovers.

A garage-door controller passed every functional test. The safety review then asked for a one-hot state register that checks itself, because an upset in a binary register went unnoticed.

This is an Implement problem. The starter works, but s is only a decode of a binary register. Make s the one-hot state register itself. Set err while s is not one-hot, and go to CLOSED at the next edge after a bad code.

The check runs 16 cycles of button and limit-switch inputs. Between some edges it forces a code into s, legal or not. It records s and err in every cycle. The design must synthesize with no latches.

Signals in the Timeline

SignalWidthRole
script32input of each check
Full specification
The module (keep the name and the ports)
module door (
  input  logic       clk,
  input  logic       rst,    // synchronous reset, active high: CLOSED
  input  logic       btn,    // the wall button
  input  logic       lim,    // the limit switch: the door has reached the end of its travel
  output logic [3:0] s,      // the state register itself, one-hot
  output logic       err     // 1 while s is not one-hot
);
  ...
endmodule
StatesNext state
CLOSED0001OPENING if btn, else CLOSED
OPENING0010OPEN if lim, else OPENING
OPEN0100CLOSING if btn, else OPEN
CLOSING1000OPENING if btn (the button wins); else CLOSED if lim; else CLOSING
any other code—CLOSED, whatever the inputs
RuleDetail
Resetrst = 1 at an edge gives s = 0001.
err1 in any cycle where s is not one-hot, 0 otherwise. It is combinational: it reads s in the same cycle.
UpsetsA forced code lasts until the next edge. From a legal code the machine carries on from that state.

Examples

Example 1

Open, Then Close
Input
btn/lim per cycle: 00 10 00 00 01 00 00 10 00 00 01 00 00 00 00 00
Output
s_cycle0 = 'b0001, err_cycle0 = 'b0, s_cycle1 = 'b0001, err_cycle1 = 'b0, s_cycle2 = 'b0010, err_cycle2 = 'b0, s_cycle3 = 'b0010, err_cycle3 = 'b0, s_cycle4 = 'b0010, err_cycle4 = 'b0, s_cycle5 = 'b0100, err_cycle5 = 'b0, s_cycle6 = 'b0100, err_cycle6 = 'b0, s_cycle7 = 'b0100, err_cycle7 = 'b0, s_cycle8 = 'b1000, err_cycle8 = 'b0, s_cycle9 = 'b1000, err_cycle9 = 'b0, s_cycle10 = 'b1000, err_cycle10 = 'b0, s_cycle11 = 'b0001, err_cycle11 = 'b0, s_cycle12 = 'b0001, err_cycle12 = 'b0, s_cycle13 = 'b0001, err_cycle13 = 'b0, s_cycle14 = 'b0001, err_cycle14 = 'b0, s_cycle15 = 'b0001, err_cycle15 = 'b0

btn in cycle 1 starts OPENING from cycle 2. lim in cycle 4 gives OPEN in 5. btn in 7 gives CLOSING in 8, and lim in 10 gives CLOSED in 11. err stays 0.

Example 2

The Button Reverses a Closing Door
Input
btn/lim per cycle: 10 00 01 00 10 00 10 00 01 00 00 00 00 00 00 00
Output
s_cycle0 = 'b0001, err_cycle0 = 'b0, s_cycle1 = 'b0010, err_cycle1 = 'b0, s_cycle2 = 'b0010, err_cycle2 = 'b0, s_cycle3 = 'b0100, err_cycle3 = 'b0, s_cycle4 = 'b0100, err_cycle4 = 'b0, s_cycle5 = 'b1000, err_cycle5 = 'b0, s_cycle6 = 'b1000, err_cycle6 = 'b0, s_cycle7 = 'b0010, err_cycle7 = 'b0, s_cycle8 = 'b0010, err_cycle8 = 'b0, s_cycle9 = 'b0100, err_cycle9 = 'b0, s_cycle10 = 'b0100, err_cycle10 = 'b0, s_cycle11 = 'b0100, err_cycle11 = 'b0, s_cycle12 = 'b0100, err_cycle12 = 'b0, s_cycle13 = 'b0100, err_cycle13 = 'b0, s_cycle14 = 'b0100, err_cycle14 = 'b0, s_cycle15 = 'b0100, err_cycle15 = 'b0

A press while CLOSING sends the door back to OPENING at the next edge. In CLOSING the button is looked at before the limit switch.

Example 3

An Upset to 0000 While Open
Input
btn/lim per cycle: 10 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00; 0000 forced in cycle 4
Output
s_cycle0 = 'b0001, err_cycle0 = 'b0, s_cycle1 = 'b0010, err_cycle1 = 'b0, s_cycle2 = 'b0010, err_cycle2 = 'b0, s_cycle3 = 'b0100, err_cycle3 = 'b0, s_cycle4 = 'b0000, err_cycle4 = 'b1, s_cycle5 = 'b0001, err_cycle5 = 'b0, s_cycle6 = 'b0001, err_cycle6 = 'b0, s_cycle7 = 'b0001, err_cycle7 = 'b0, s_cycle8 = 'b0001, err_cycle8 = 'b0, s_cycle9 = 'b0001, err_cycle9 = 'b0, s_cycle10 = 'b0001, err_cycle10 = 'b0, s_cycle11 = 'b0001, err_cycle11 = 'b0, s_cycle12 = 'b0001, err_cycle12 = 'b0, s_cycle13 = 'b0001, err_cycle13 = 'b0, s_cycle14 = 'b0001, err_cycle14 = 'b0, s_cycle15 = 'b0001, err_cycle15 = 'b0

In cycle 4 the door is OPEN when 0000 is forced. err is 1 in that cycle, and the next edge loads 0001: CLOSED.

Constraints and Contract

  • All inputs are known 0/1 values and change only between edges.
  • Keep the module name and the ports. s must be the state register itself, written by the clocked block.

Hints

Hint 1

Write one equation per bit of s: the OR of the arcs that enter that state. Each arc is its source bit ANDed with its input condition.

Hint 2

err is !$onehot(s), or a count of the ones compared with 1. Test it on 0000 and on two hot bits.

Hint 3

When err is 1, the next code is 0001 whatever the equations say. Override the equations; do not add terms to them.

What the grader checks: Ten directed and twelve seeded runs of 16 cycles, many with codes forced into s between edges, legal and illegal. s and err are compared with a JavaScript model in every cycle. Synthesis must report no latches.

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.

22 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 3 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

Open, Then Close

Input
btn/lim per cycle: 00 10 00 00 01 00 00 10 00 00 01 00 00 00 00 00
Expected output
s_cycle0 = 'b0001, err_cycle0 = 'b0, s_cycle1 = 'b0001, err_cycle1 = 'b0, s_cycle2 = 'b0010, err_cycle2 = 'b0, s_cycle3 = 'b0010, err_cycle3 = 'b0, s_cycle4 = 'b0010, err_cycle4 = 'b0, s_cycle5 = 'b0100, err_cycle5 = 'b0, s_cycle6 = 'b0100, err_cycle6 = 'b0, s_cycle7 = 'b0100, err_cycle7 = 'b0, s_cycle8 = 'b1000, err_cycle8 = 'b0, s_cycle9 = 'b1000, err_cycle9 = 'b0, s_cycle10 = 'b1000, err_cycle10 = 'b0, s_cycle11 = 'b0001, err_cycle11 = 'b0, s_cycle12 = 'b0001, err_cycle12 = 'b0, s_cycle13 = 'b0001, err_cycle13 = 'b0, s_cycle14 = 'b0001, err_cycle14 = 'b0, s_cycle15 = 'b0001, err_cycle15 = 'b0

btn in cycle 1 starts OPENING from cycle 2. lim in cycle 4 gives OPEN in 5. btn in 7 gives CLOSING in 8, and lim in 10 gives CLOSED in 11. err stays 0.

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

Icarus Verilog + Yosys · server