Home › Practice › Formal Verification › One-hot state machine: exactly one bit set

One-hot state machine: exactly one bit set

MediumFormalassertFSM

A one-hot state machine keeps one flip-flop per state, and only the flip-flop of the current state is 1. The whole design relies on that. If two bits are ever 1 together, or all bits are 0, the machine is lost. This is a classic safety property: something bad must never happen.

Concept first. "Exactly one bit set" is two facts, and a checker needs both. Fact one: not all zero. Fact two: not two or more set. A trick for the second: for a number s, the value s & (s - 1) clears the lowest 1 bit. If s has at most one 1 bit, the result is 0. Example with other numbers: s = 3'b100 gives 100 & 011 = 000 (one bit, good), but s = 3'b110 gives 110 & 101 = 100 (two bits, bad).

The design is a three-step machine in one-hot form: bit 0 is idle, bit 1 is run, bit 2 is done. go moves idle to run, fin moves run to done, and done returns to idle.

module dut(input clk, input rst, input go, input fin, output reg [2:0] state);
  initial state = 3'b001;
  always @(posedge clk) begin
    if (rst) state <= 3'b001;
    else if (state[0]) begin if (go) state <= 3'b010; end
    else if (state[1]) begin if (fin) state <= 3'b100; end
    else if (state[2]) state <= 3'b001;
  end
endmodule
Solve in your browser →

No install and no sign-up needed. You write your answer in the editor and Nishkam checks it.

More in this track

← Proof or only a short search? Help the inductioncocotb first steps 1: Test an AND gate →