1

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
user2692669
  • 461
  • 8
  • 23
  • Is the question how to draw the digram correctly rather than code it? – Morgan Nov 21 '14 at 09:03
  • The third transition, probably should be labelled as `X && !y && !z` and the second priority should be `x && y && !z` I do not think there is a from for noting precedence in a state diagram. – Morgan Nov 21 '14 at 09:06
  • @Morgan Yes the question is mostly about the diagram. – user2692669 Nov 21 '14 at 23:17

2 Answers2

2

Since you tagged the question with SystemVerilog, I just wanted to mention an alternative way of describing your next state logic:

always_comb begin
  case (states)
    a1: 
        priority casez ({x,y,z})
         3'b11?:  next_state = b1; // x && y
         3'b1?1:  next_state = b2; // x && z
         3'b1??:  next_state = b3; // x
         default: next_state = a1; 
        endcase
  endcase
end

Also, the commonly used state diagram does not specify priority, so you need to completely specify the transition conditions as in xbug's answer. But I don't see why you can't extend this notation, for example, mark each arc with labels 1, 2, 3, and 4, where lower numbers indicate higher priority.

Ari
  • 7,251
  • 11
  • 40
  • 70
  • I can't test the code right now, but I think you summed up the hole topic. P.S. I think xbug should get the green tick, is it ok with you? – user2692669 Nov 20 '14 at 18:44
1

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.

xbug
  • 1,394
  • 1
  • 11
  • 18
  • 1
    How can I state that for all of the rest possibilities we stay in the current state? (I usually draw a pointer pointing both edges to the same state and I write the condition that I want to emphasize {meaning not all of them} ). – user2692669 Nov 20 '14 at 15:11
  • Just make an arrow such as a state points to itself under the default set of events. Consider state 3 on [this example](http://www.cs.berkeley.edu/~bh/v3ch1/fsm3-reject.gif). – xbug Nov 20 '14 at 15:18
  • I think we're getting... our signals crossed :) . I updated the question with a code example to be more clear. – user2692669 Nov 20 '14 at 15:32
  • Why do my signals conflict? I use ordering in my code (if-else if-else statements). – user2692669 Nov 20 '14 at 16:06
  • 1
    xbug is trying to make your state diagram completely deterministic by fully specifying the conditions for each arc. Ie, if `x` and `z` are true, both the arcs to `b2` and `b3` are satisfied so you dont know which to follow. Ideally, this is never the case, so xbug wants to add more logic to ensure no two arcs (even a self arc) are both true for any combination of inputs. – Unn Nov 20 '14 at 16:12
  • 1
    However, while this is typically fine for small examples, larger FSMs with more complicated potential overlap makes this a bit impractical imo. I dont mind the MATLAB Stateflow solution to this for prioritizing arcs ( http://www.mathworks.com/help/stateflow/ug/implicit_ordering_by_angular_position_source_state.png ). They add numbers to the arcs to illustrate priority. I feel this is more practical than adding a ton of not terms, though others may disagree. – Unn Nov 20 '14 at 16:15
  • @Unn + xbug: I'm trying to make the diagramm reflect the code, not the other way around. The purpose should be that if I pick a random colleague and show him/her the diagram, he/she should be able to understand that the code that would be written is not a NFA but a DFA. Unn's sollution is approaching the desired result but I'm not sure if that method is widely recognized ( I personally love it but that's not the point :) ). – user2692669 Nov 20 '14 at 16:39
  • @user2692669 Having it be DFA will reflect the code (implicit in the else if is that `x && y` was false). However, if you want is simple, the Stateflow solution is probably your best bet. Im also unsure how instantly recognizable it is; but its pretty easy to understand once you realize your arcs are not mutually exclusive and try to figure out which arc to take. – Unn Nov 20 '14 at 17:18
  • @user2692669: You have to remember that you only enter an else block if the condition is false, hence the first condition being `(x & y)` means the second condition is `(x & z) & !(x & y)`. That is simplified to `(x & !y & z)`. In the same way the final condition is `x & !(x & y) & !(x & z)` or `(x & !y & !z)` – Paul S Nov 22 '14 at 12:01