others in state machine

Discussion in 'VHDL' started by Jerker Hammarberg \(DST\), Aug 28, 2003.

  1. Hello! I have noticed that if I add a "when others" branch in a state
    machine case statement, the resulting FPGA becomes considerably smaller. To
    be clear,

    type state_type is (s0, s1);
    signal state: state_type;
    ....
    process(clk, rst)
    begin
    if (rst = '1') then
    state <= s0;
    elsif clk'event and clk = '1' then
    case state of
    when s0 =>
    -- do something
    state <= s1;
    when s1 =>
    -- do something else
    state <= s0;
    when others =>
    state <= s0;
    end case;
    end if;
    end process;

    is smaller than if I remove the "when others" branch. What's the reason? My
    second question is how this branch can be reached in the FPGA? It happens to
    me. I understand that it can be reached during simulation if "state" is not
    initialized, but how can it happen in reality?

    I use Xilinx XST with FSM encoding set to "auto".

    Thanks in advance for any help!

    /Jerker
     
    Jerker Hammarberg \(DST\), Aug 28, 2003
    #1
    1. Advertising

  2. "Jerker Hammarberg (DST)" wrote:
    >
    > Hello! I have noticed that if I add a "when others" branch in a state
    > machine case statement, the resulting FPGA becomes considerably smaller. To
    > be clear,
    >
    > type state_type is (s0, s1);
    > signal state: state_type;
    > ...
    > process(clk, rst)
    > begin
    > if (rst = '1') then
    > state <= s0;
    > elsif clk'event and clk = '1' then
    > case state of
    > when s0 =>
    > -- do something
    > state <= s1;
    > when s1 =>
    > -- do something else
    > state <= s0;
    > when others =>
    > state <= s0;
    > end case;
    > end if;
    > end process;
    >
    > is smaller than if I remove the "when others" branch. What's the reason? My
    > second question is how this branch can be reached in the FPGA? It happens to
    > me. I understand that it can be reached during simulation if "state" is not
    > initialized, but how can it happen in reality?
    >
    > I use Xilinx XST with FSM encoding set to "auto".


    Actually, the "when others" is unreachable in simulation since you are
    using enumerated types and have included all possible values.

    It should also be unreachable in an FPGA unless the synthesizer decides
    to use some form of encoding that has possible illegal states in the
    state register. For a 2-state machine like you've shown, it only needs
    to have a 1-bit state register and so there should be no illegal states.
    It's possible that with the "when others" the tool decides to use binary
    encoding and without it uses one-hot encoding (I'm guessing here). FPGA
    synthesizers like one-hot encoding (it maps better to FPGA
    architectures), so it is always a good idea to include a "when others"
    state to reset the machine to something sane in case the state vector
    gets trashed by something (noise, gremlins, etc.).
    --
    Tim Hubberstey, P.Eng. . . . . . Hardware/Software Consulting Engineer
    Marmot Engineering . . . . . . . VHDL, ASICs, FPGAs, embedded systems
    Vancouver, BC, Canada . . . . . . . . . . . http://www.marmot-eng.com
     
    Tim Hubberstey, Aug 28, 2003
    #2
    1. Advertising

  3. > It should also be unreachable in an FPGA unless the synthesizer decides
    > to use some form of encoding that has possible illegal states in the
    > state register. For a 2-state machine like you've shown, it only needs
    > to have a 1-bit state register and so there should be no illegal states.
    > It's possible that with the "when others" the tool decides to use binary
    > encoding and without it uses one-hot encoding (I'm guessing here). FPGA
    > synthesizers like one-hot encoding (it maps better to FPGA
    > architectures), so it is always a good idea to include a "when others"
    > state to reset the machine to something sane in case the state vector
    > gets trashed by something (noise, gremlins, etc.).


    That makes sense, thanks a lot! You're right, the actual state machine where
    it happens has more states. I suppose you mean that with "when others" it
    uses one-hot encoding and without it it uses binary encoding.

    /Jerker
     
    Jerker Hammarberg \(DST\), Aug 28, 2003
    #3
  4. Tim Hubberstey wrote:

    > so it is always a good idea to include a "when others"
    > state to reset the machine to something sane in case the state vector
    > gets trashed by something (noise, gremlins, etc.).


    The when others clause also provides some protection
    in case the type state_type is ever edited.

    -- Mike Treseler
     
    Mike Treseler, Aug 28, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lamb
    Replies:
    1
    Views:
    694
  2. Weng Tianxiang
    Replies:
    7
    Views:
    1,129
    Mike Treseler
    Nov 25, 2003
  3. Weng Tianxiang
    Replies:
    3
    Views:
    1,452
    Weng Tianxiang
    Jul 25, 2006
  4. fenster
    Replies:
    3
    Views:
    1,173
    jeppe
    Dec 23, 2011
  5. Travis
    Replies:
    3
    Views:
    381
Loading...

Share This Page