others in state machine

  • Thread starter Jerker Hammarberg \(DST\)
  • Start date
J

Jerker Hammarberg \(DST\)

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
 
T

Tim Hubberstey

Jerker Hammarberg (DST) said:
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.).
 
J

Jerker Hammarberg \(DST\)

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
 
M

Mike Treseler

Tim said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top