Search code examples
verilogsystem-verilogfsmstate-machineoperator-precedence

FSM: next state precedence


When being in a state "a1" how can I show that the next arrows will have a precedence over each other, without having an overhead of extra states?

Full example:

  • We are at a1 state and signals x && y are asserted: we go to state b1
  • If that condition is not asserted but x && z is asserted then we go to state b2
  • If the above conditions are not asserted but x is asserted then we go to state b3

Visual concept:

enter image description here

In the above "FSM" we can't see that x && y is checked before the other two.

Code snippet:

always_comb begin
  case (states)
    a1: begin
      if (x && y)
        next_state = b1;
      else if (x && z)
        next_state = b2;
      else if (x)
        next_state = b3;
      else
        next_state = a1;
    end
  endcase
end

Solution

  • Ideally, you'd need to cover all the possible combinations of input events in each state to get a proper DFA (deterministic FSM).

    However, you can get away by fully specifying the triggers in terms of input signals, and let your HDL default to "no transition". In that case:

    • transition from a1 to b1 may be triggered by x && y && !z
    • transition from a1 to b2 may be triggered by x && !y && z
    • transition from a1 to b3 may be triggered by x && !y && !z

    (with ! denoting logical 'not').

    With an alphabet of 3 symbols (your three input signals), you get 2^3 = 8 possible combinations in every state. Ask yourself: in your current design, what happens if all of x, y and z get asserted ? You need to be specific about that.

    EDIT

    Let me be more specific.

    Let's consider A, B, C, ... H as events, each representing one possible combination of input signals, such as:

      x y z
    A 0 0 0
    B 0 0 1
    C 0 1 0
    D 0 1 1
    E 1 0 0
    F 1 0 1
    G 1 1 0
    H 1 1 1
    

    Then try to express your transitions in terms of A, B, C, ... H. If you can, the resulting FSM is suitable to your task. If you can't, you should probably rethink your logic.