7 FSM Sda
7 FSM Sda
DESCRIPTION IN VHDL
Cristian Sisterna
UNSJ
FSM Review
2
RS-232 Tx format
start_tx
Idle
stop
start
parity
shift
8_bits
Inputs
Current Outputs
Next State
Next Current
State
Output
Output
Logic
State Next State Logic
Logic Current Logic
Logic
Logic State
State
Clk
Rst
Inputs Outputs
Next State Logic
&
Output Logic
Current
Current
State
State Logic
Logic
Clk
Rst
Inputs Current
Next
Next Current
Current Synchr. Outputs
State Output
Output Sync
State
State State
State Logic Output
Output
Logic Next Logic Logic
Logic State Logic Logic
FFs
Clk
Rst
Output A
Synchronous
Output Logic
Inputs
Next
Next
Next Current
Current Current
State
State
State State
State State ...
Logic
Logic Logic
Logic ...
Synchronous Output Z
Output Logic
Clk
Rst
Synchronous
Sync Outputs
Output
Output
Logic
FFs
Inputs
Next
Next Current
Current
State
State State
State
Logic Next
Logic State Logic
Logic
Current
State
Clk
Rst
Specifications
Understand the
Problem Traditional Steps
Define an FSM +
Enumerated Type
Define FSM
Signals
VHDL Steps
Select an Encoding
Technique (optional)
Declare the signals for the next state and current state of the state
machine as signal of the enumerated data type already defined
for the state machine
The only values that current_state and next_state can hold are:
IDLE,START,STOP_1BIT,PARITY,SHIFT
FSM - DSDA Cristian Sisterna
FSM – Encoding Techniques
FSM Encoding Techniques
18
State Assignment
During synthesis each symbolic state name has to be
states
One-hot: assigns one ‘hot’ bit for each state
Possible values:
default: assign an encoding style based on the number of states:
Sequential encoding for 0 - 4 enumerated types
One-hot encoding for 5 – 50 enumerated types
Gray encoding for > 50 enumerated types
sequential: 000 001 010 011 100 101 110 111
one-hot: 0000001 00000010 00000100 . . .
gray: 000 001 011 010 110 111 101 100
johnson : 0000 0001 0011 0111 1111 1110 1100 1000
safe: default encoding + reset logic to a known state
attribute syn_encoding: string;
attribute syn_encoding of my_fms_states: type is “one-hot, safe”;
Specify as follows:
attribute fsm_encoding of {entity_name|signal_name }:
{entity |signal} is "{auto | one-hot | compact|
sequential| gray| Johnson | speed1|user}";
==============================================
* Advanced HDL Synthesis *
==============================================
Analyzing FSM <FSM_0> for best encoding.
Optimizing FSM <rs232_tx/tx_current_state/FSM> on signal
<tx_current_state[1:3]> with sequential encoding.
-----------------------------
State | Encoding
-----------------------------
idle | 000
start | 001
shift | 010
parity | 011
stop_1bit | 100
-----------------------------
Inputs
Next
Next Current
Current Output Outputs
State
State
Next
State
State Output
Logic
Logic
Logic
Logic State Logic
Logic Current
State
CLK
RST
S0 S1
X=0 Z=0 Z=1 X=1
X=1
Mealy FSM
X=0
Z=1
X=0 S0 S1 X=1
Z=0 Z=1
X=1
Z=0
FSM - DSDA Cristian Sisterna
Style A - Moore State Machine
40
Next State Logic
process (state, X)
begin
case state is Present State Logic Output Logic
when S0 =>
if(X=‘0’) then
next_state <= S0; process (clk, rst)
else(X=‘1’) then begin process (state)
next_state <= S1; if(rst = ‘1’) then begin
X end if; state <= S0; case state is
when S1 => elsif (rising_edge(clk)) then when S0 => Z <=‘0’;
Z
if …. state <= next_state; when S1 => Z <=‘1’;
next_state <=..; end if; when others => Z <=‘0’;
else .. end process; end case;
….
end process;
when others =>
….
end case;
end process;
Clock
Reset
Clk
Rst
process (state, X)
begin
case state is
Seq. Present State Logic Comb.Output Logic
when S0 =>
if(X=‘0’) then
next_state <= S0; process (clk, rst) process (state)
else(X=‘1’) then begin begin
X next_state <= S1; if(rst = ‘1’) then Z <= ‘0’; -- default value Z
end if; state case state is
state <= S0;
when S1 => elsif (rising_edge(clk)) when S0 => Z <= ‘0’;
if …. then when S1 => Z <= ‘1’;
next_state <= .. ; state <= next_state; when others => Z <= ‘0’;
…. end if; end case;
when others => end process; end process;
….
end case;
end process;
Clock
Reset
process (state, X)
begin Comb. Output Logic
case state is
when S0 => process (state, X)
if(X=‘0’) then begin
Seq. Present State Logic
next_state <= S0; case state is
else(X=‘1’) then when S0 =>
X next_state <= S1; process (clk, rst) if (X = ‘0’) then
end if; begin Z <=…;
when S1 => if(rst = ‘1’) then else
if …. state <= S0; Z <=... ; Z
next_state <= .. ; elsif (rising_edge(clk)) then end if;
…. state <= next_state; when S1 =>
when others => end if; if (X = ‘1’) then
…. end process; .... ;
end case; else
end process; .... ;
end if;
when others =>
Z<=‘0’;
end case;
end process;
Clock
Reset
Mealy
process(clk, rst)
begin
if(rst = ‘1’) then
state <= S0;
Z <= ‘0’;
elsif (rising_edge(clk)) then
case state is
when S0 =>
if (X=’0’) then
X state <= S0; Z
elsif (X=’1’) then
state <= S1;
end if;
Z <=‘0’;
when S1 =>
if (X = ‘0’) then
...
end if;
Z <= ‘1’ ;
end case;
end if;
end process;
Clk
Rst
process(clk, rst)
begin
if(rst = ‘1’) then
state <= S0;
Z <= ‘0’;
elsif (rising_edge(clk)) then
case state is
when S0 =>
if (X=’0’) then
state <= .... ;
Z <= ... ;
Z
X
elsif (X=’1’) then
state <= .... ;
Z <= ... ;
end if;
when S1 =>
if (X = ‘0’) then
...
end if;
end case;
end if;
end process;
Reset
Clock
Clk
Rst
Clk
Rst
process (state, X)
begin Z
next_state <= state;
case state is
when S0 => Seq. Present State Logic
if(X =’1’) then
X next_state <=S1;
end if; process (clk, rst)
Z <=‘0’; begin
when S1 => if(rst = ‘1’) then
if( X = ‘1’) then state <= S0;
next_state <=S0; elsif (rising_edge(clk)) then
end if; state <= next_state;
Z <=‘1’; end if;
end case; end process;
end process;
Clk
Rst
Z
process (state, X)
begin
next_state <= state;
Z <= ... ; Seq. Present State Logic
case state is
when S0 => process (clk, rst)
X if(X =’1’) then begin
next_state <= ... ; if(rst = ‘1’) then
Z <= ... ; state <= S0;
end if; elsif (rising_edge(clk)) then
when S1 => state <= next_state;
if( X = ‘1’) then end if;
next_state <= ... ; end process;
Z <= ... ;
end if;
end case;
end process;
Clk
Rst
Let’s try to obtain an state diagram of a hypothetical memory controller FSM that has the
following specifications:
The controller is between a processor and a memory chip, interpreting commands from the
processor and then generating a control sequence accordingly. The commands, mem, rw and
burst, from the processor constitute the input signals of the FSM. The mem signal is asserted to
high when a memory access is required. The rdwr signal indicates the type of memory access,
and its value can be either ’1’ or ’0’, for memory read and memory write respectively. The
burst signal is for a special mode of a memory read operation. If it is asserted, four
consecutive read operations will be performed. The memory chip has two control signals, oe
(for output enable) and we (for write enable), which need to be asserted during the memory
read and memory write respectively. The two output signals of the FSM, oe and we, are
connected to the memory chip’s control signals. For comparison purpose, let also add an
artificial Mealy output signal, we_mealy , to the state diagram. Initially, the FSM is in the idle
state, waiting for the mem command from the processor. Once mem is asserted, the FSM
examines the value of rdwr and moves to either the read1 or the write state. The input
conditions can be formalized to logic expressions, as shown below:
mem’ : represents that no memory operation is required (mem=‘0’)
mem.rdwr: represents that a memory read operation is required (mem=rdwr=‘1’).
mem.rdwr’: represents that a memory write operation is required (mem=‘1’; rdwr=‘0’)
Based on an example from the “RTL Hardware Design Using VHDL” book, By Pong Chu
Address Bus
Data Bus
idle
burst’
mem.rdwr’
mem.rdwr we_mealy
read1
oe
write
burst
we
read2
oe
read3
oe
read4
oe
idle
mem
rdwr
read1
oe we_mealy
burst write
read2
we
oe
read3
oe
read4
oe
Entity-Architecture Declarations
FSM enumeration type declaration, FSM signal declarations
library ieee ;
use ieee.std_logic_1164.all;
entity mem_ctrl is
port (
clk, reset : in std_logic;
mem, rdwr, burst: in std_logic;
oe, we, we_mealy: out std_logic
);
end mem_ctrl ;
architecture mult_seg_arch of mem_ctrl is
type fsm_states_type is
(idle, read1, read2, read3, read4, write);
signal crrnt_state, next_state: fsm_states_type;
begin
1 bit de start
8 bits de datos
Paridad programable: paridad/no paridad, par/impar
1 bit de stop
Frecuencia de transmisión por defecto es de 9600 Bauds, pero
el código debe permitir también otras frecuencias como: 4800,
38400, 115200 Bauds.
RS-232 Tx format
SysReset
Dato(7:0)
DatoSent
FSM DatoSerie
RS232 Tx Controller
StartTx
??
RTL Viewer
Double click
SysClock Div.
Parity 00
Frec. En
SysReset DatoSerie
01
Dato(7:0)
E1 Reg
E0 Desplaz 10
amiento
DatoSent
Done
StartTx FSM
?? RS232 Tx Controller
StartStopBit
Sel1, Sel0
CS1
TS1
Tx_busy’
TS4
CS2 Tx_busy
Ld_Tx
TS2
Tx_busy
CS3
Tx_busy’ TS3
Tx_busy
Tx_busy
Ld_Tx’
Tx_busy
Clk
Rst Ld_Tx ctrl_tx.vhd
Inputs
Current Outputs
Next Current State Output
State Next
State Process
Process State Process
Transmitter
FSM
Processes
FSM - DSDA Cristian Sisterna
Interacting FSM
87
cntrl_fsm.vhd
Inputs
Clk
Rst
Tx_busy
Inputs
Current Outputs
Next Current State Output
State Next
State Process
Process State Process
tx_fsm.vhd
FSM - DSDA Cristian Sisterna
88
90
Quartus II Software State Machine Viewer
91
State Declaration
ENTITY wm IS
PORT (
clk, reset, door_closed, full : in std_logic;
heat_demand, done, empty : in std_logic;
water, spin, heat, pump : out std_logic);
END ENTITY wm;
ARCHITECTURE behave OF wm IS
TYPE state_type IS
(idle, fill, heat_w, wash, drain); Empty = 1
IDLE
Door_closed = 1
Water = 0
SIGNAL current_state, next_state : Spin = 0
Heat = 0
state_type; Pump = 0
Water = 0 Water = 1
Spin = 1 Spin = 0
Heat = 0 Heat = 0
Pump = 1 Pump = 0
Full = 1
Heat_demand = 1
WASH HEAT_W
Done = 1
Water = 0 Water = 0
Spin = 1 Spin = 1
Heat = 0 Heat = 1
Pump = 0 Pump = 0
Heat_demand = 0
92
Next State Logic
PROCESS (clk, reset) Empty = 1
IDLE
Door_closed = 1
Water = 0
BEGIN Spin = 0
Heat = 0
IF reset = '1' THEN Pump = 0
END PROCESS;
WASH HEAT_W
Done = 1
Water = 0 Water = 0
PROCESS (current_state, door_closed, Spin = 1
Heat = 0
Spin = 1
Heat = 1
full, heat_demand, done, empty) Pump = 0 Pump = 0
BEGIN
next_state <= current_state; Heat_demand = 0
93
Moore Combinatorial Outputs
Default output conditions
PROCESS (current_state)
BEGIN
water <= '0';
IDLE
spin <= '0'; Empty = 1
Water = 0
Door_closed = 1
heat <= '0'; Spin = 0
Heat = 0
pump <= '0'; Pump = 0
DRAIN FILL
CASE current_state IS
Water = 0 Water = 1
WHEN idle => Spin = 1 Spin = 0
Heat = 0 Heat = 0
WHEN fill => Pump = 1 Pump = 0
Water = 0 Water = 0
heat <= '1'; Spin = 1 Spin = 1
Heat = 0 Heat = 1
WHEN wash => Pump = 0 Pump = 0
94
Mealy Combinatorial Outputs
PROCESS (current_state, heat_demand)
BEGIN
water <= '0'; IDLE
spin <= '0'; Empty = 1
Water = 0
Door_closed = 1
Water = 0
spin <= '1'; Spin = 1
Heat =
heat <= heat_demand; Heat_demand
Full = 1
Pump = 0
WHEN drain => Done = 1
95
State Machine Encoding Styles
Grey-
Binary One-Hot Custom
State Code
Encoding Encoding Encoding
Encoding
Idle 000 000 00001 ?
96
Quartus II Encoding Style
Options:
Apply Assignment to • Auto
State Variable • Gray
• Johnson
• Minimal Bits
• One-Hot
• Sequential
• User-Encoded
97
Undefined States
98
'Safe' Binary State Machine?
TYPE state_type IS IDLE
Empty = 1 Door_closed =
(idle, fill, heat_w, wash, drain); Water = 0
1
Spin = 0
SIGNAL current_state, next_state : state_type; Heat = 0
BEGIN Pump = 0
DRAIN FILL
PROCESS (current_state, door_closed, full,
Water = 0 Water = 1
heat_demand, done, empty) Spin = 1 Spin = 0
BEGIN Heat = 0 Heat = 0
Pump = 1 Pump = 0
next_state <= current_state;
Full = 1
CASE current_state is Heat_demand = 1
99
Creating "Safe" State Machines
100
Registering Outputs
Next State
Outputs
Next
Inputs State Output Output
State
Registers Logic Registers
Logic
101
Registered Outputs w/o Latency
Base outputs on next state vs. current state
Output logic uses next state to determine what the next
outputs will be
On next rising edge, outputs change along with state registers
Current State
Next State
Next
State
Inputs State
Registers
Logic
Next
State Output Outputs
Output Registers
Logic
102
Registered Outputs w/o Latency
PROCESS (clk)
BEGIN
IF rising_edge(clk) THEN IDLE
Empty = 1
water <= '0'; Water = 0
Door_closed = 1
Spin = 0
spin <= '0'; Heat = 0
Pump = 0
heat <= '0'; DRAIN FILL
103
Using Custom Encoding Styles
Pump
Water
State
Eliminate combinatorial
Spin
Heat
Encoding
104
Quartus II Custom State Encoding
IDLE
ENTITY wm IS Empty = 1 Door_closed =
PORT ( Water = 0
Spin = 0
1
BEGIN
Heat_demand = 0
105
Writing Efficient State Machines
106
Precision State Encoding Techniques
107
• Onehot
• Twohot
• Gray
• Random
-- Declare the (state-machine) enumerated type
type my_state_type is (SEND, RECEIVE, IGNORE, HOLD, IDLE);
-- Set the type_encoding_style of the state type
attribute type_encoding_style of my_state_type:type is ONEHOT;
State
Table
Note: Precision allows to use ‘-’. It can be used to reduce the size of the circuit