VHDL - Finite State Machines and Sequential Testbenches
In questa pagina 3
A finite state machine (FSM) is a state register plus two combinational networks, next state and output (Finite State Machines - SynthesisSynthesis of a synchronous sequential circuit follows specifications, state diagram, state table (merging equivalent states), binary state encoding, choice of D flip-flop, flip-flop input and output equations, minimisation, circuit and verification. A state summarises the useful part of history; a sequence recogniser has one state per recognised prefix, reusing states for overlaps. A reset gives a known initial state. Gray encoding (minimum flip-flops) versus one-hot (one flip-flop per state, simpler logic): for the 1101 recogniser Gray costs about half.Finite State Machines - Synthesis →). In VHDL the structure shows directly in the code, with three processes working in parallel. It is the standard scheme for writing an FSM, and the typical exam exercise.
The three-process model
Definition (three-process FSM model).
- State register (sequential part): on the clock edge
state <= next_state; reset brings the machine to a known initial state.- Next state (combinational logic): computes
next_statefromstateand the inputs.- Output (combinational logic): computed from
state(Moore) or fromstateand input (Mealy).
Processes 2 and 3 describe purely combinational logic (sensitivity lists with all inputs, complete assignments); only the first has memory. The three processes interact through shared signals (state, next_state).
Enumerated type
type state_type is (S1, S2, S3, S4); -- new type with symbolic values
signal state, next_state : state_type;Definition (enumerated type). With
typea type is defined by listing its values in parentheses, separated by commas: for signals with a finite number of values, given names rather than numbers, for readability.std_logicitself is an enumeration (9 values). The synthesiser chooses the binary encoding of the states unless it is imposed (for example withstd_logic_vectorconstants, as in the traffic light example).
Example. type state_type is (S1, S2, S3, S4); creates a type with symbolic values; the synthesiser encodes it with bits.
Complete example: 1101 sequence recogniser (Mealy)
Input , output ; when has been seen and ; asynchronous reset to . The state diagram is the one of the synthesis note.
library IEEE;
use IEEE.std_logic_1164.all;
entity seq_rec is
port ( clk, rst, X : in std_logic;
Z : out std_logic );
end seq_rec;
architecture beh of seq_rec is
type state_type is (S1, S2, S3, S4);
signal state, next_state : state_type;
begin
-- process 1: state register with asynchronous reset
state_register : process (rst, clk)
begin
if rst = '1' then
state <= S1;
elsif clk'event and clk = '1' then
state <= next_state;
end if;
end process;
-- process 2: next state = f(state, input)
next_state_comb : process (X, state)
begin
case state is
when S1 =>
if X = '0' then next_state <= S1; else next_state <= S2; end if;
when S2 =>
if X = '0' then next_state <= S1; else next_state <= S3; end if;
when S3 =>
if X = '0' then next_state <= S4; else next_state <= S3; end if;
when S4 =>
if X = '0' then next_state <= S1; else next_state <= S2; end if;
end case;
end process;
-- process 3: output = f(state, input) (Mealy)
output_comb : process (X, state)
begin
case state is
when S4 =>
if X = '1' then Z <= '1'; else Z <= '0'; end if;
when others =>
Z <= '0';
end case;
end process;
end beh;Remarks.
- The code is behavioral: it does not say how to encode the states or build the logic, only the behaviour.
- In process 1 reset is tested first (asynchronous, VHDL - Latches, Flip-Flops and ResetsIn VHDL a memory element is a process in which some branch does not assign the signal: the D latch is process(D,C) with if C='1' then Q<=D, the positive-edge-triggered flip-flop is process(CLK) with if CLK'event and CLK='1'. Asynchronous reset: RST is in the sensitivity list and tested first (if RST='1' ... elsif clock edge); synchronous reset: only CLK in the list and RST tested inside the clock-edge branch, so a clock edge is needed for it to act. The testbench of a synchronous circuit has a process generating the clock and one applying inputs away from the active edge.VHDL - Latches, Flip-Flops and Resets →).
- In processes 2 and 3 every branch assigns the outputs (
next_state,Z): no unwanted memory. - Moore instead of Mealy: the sensitivity list of the output process contains only
stateand thecasedoes not look at the input:case state is when R4 => Z <= '1'; when others => Z <= '0'; end case;. - If the text says "in all other cases the system does not change state", write as the first statement of process 2 the default
next_state <= state;and then only the cases that change state: the last assignment wins.
Testbench
library IEEE;
use IEEE.std_logic_1164.all;
entity test_seq_rec is
end test_seq_rec;
architecture test of test_seq_rec is
signal clock, reset, X, Z : std_logic;
signal test_sequence : std_logic_vector(0 to 10) := "01110101100";
constant PERIOD : time := 100 ns;
begin
DUT : entity work.seq_rec(beh) port map (clock, reset, X, Z);
generate_clock : process
begin
clock <= '1'; wait for PERIOD/2;
clock <= '0'; wait for PERIOD/2;
end process;
apply_inputs : process
begin
reset <= '1'; X <= '0';
wait for 3*PERIOD/2; -- initial reset: the system starts from a known state
reset <= '0';
for i in 0 to 10 loop
X <= test_sequence(i); -- one bit per cycle, applied on the falling edge
wait for PERIOD;
end loop;
std.env.stop;
end process;
end test;The clock rises at ns and falls at ; inputs change at ns, i.e. on the falling edge, away from the rising edge. The initial reset lasts ns and brings the state to .
Expected with sequence : states ( at the start) after successive edges, and only during the sixth bit (the bit completing ). Being a Mealy machine, rises as soon as the input changes (at ns, falling edge), not at the next clock edge.
Moore example with output vector: traffic light controller
Input = car waiting in the E-W direction; six output lights (R red, G yellow, V green). Reset: green N-S, red E-W. If the sequence goes to green E-W and then returns to green N-S; before every red there is a yellow. States and outputs (Moore):
| state | output luci |
meaning | if | if |
|---|---|---|---|---|
| VNS | 001100 |
green N-S, red E-W | VNS | GNS |
| GNS | 010100 |
yellow N-S | VEO | VEO |
| VEO | 100001 |
green E-W, red N-S | GEO | GEO |
| GEO | 100010 |
yellow E-W | VNS | VNS |
entity semaforo is
port ( clk, rst, X : in std_logic;
luci : out std_logic_vector(5 downto 0) ); -- (RNS, GNS, VNS, REO, GEO, VEO)
end semaforo;
architecture impl_beh of semaforo is
type state_type is (VNS, GNS, VEO, GEO);
signal state, next_state : state_type;
begin
state_register : process (clk, rst)
begin
if rst = '1' then state <= VNS;
elsif clk'event and clk = '1' then state <= next_state;
end if;
end process;
next_state_comb : process (X, state)
begin
case state is
when VNS => if X = '0' then next_state <= VNS; else next_state <= GNS; end if;
when GNS => next_state <= VEO;
when VEO => next_state <= GEO;
when GEO => next_state <= VNS;
end case;
end process;
output_comb : process (state) -- Moore: state only
begin
case state is
when VNS => luci <= "001100";
when GNS => luci <= "010100";
when VEO => luci <= "100001";
when GEO => luci <= "100010";
end case;
end process;
end impl_beh;A testbench that visits all states and transitions: reset; for 2 cycles (stays in VNS); for 3 cycles (enters the sequence); for 2 cycles; again for a new full cycle.
Simulation can be functional (zero delay) or timing (real delays); see Timing of Sequential Systems - Setup, Hold and Maximum Clock FrequencyA flip-flop stores the right value only if its input is stable for the setup time before and the hold time after the clock edge, and its output changes after the propagation delay. In a synchronous circuit the clock period must satisfy T >= t_pd,FF + t_COMB + t_s,FF + t_slack, which fixes the maximum frequency; slowing the clock cures setup violations but not hold violations, which require t_pd,FF,min + t_COMB,min >= t_h. Clock skew modifies both constraints.Timing of Sequential Systems - Setup, Hold and Maximum Clock Frequency →. A larger FSM-based system (control unit plus datapath) written in VHDL is in Datapath and Control UnitA register cell is a flip-flop with logic for microoperations, shared across the n cells of a register. In a programmable system (CPU) the control unit executes instructions fetched from memory using a program counter; in a non-programmable system the control unit decides the microoperations only from inputs and datapath status signals. Complete example: summing five values with start, res and busy; the datapath has ACC_REG, OUT_REG and CNT_REG (decrementer, MUX, status signal zero); microoperations are combinations of control signals; the control unit is a 3-state Mealy machine (ready, acc, init).Datapath and Control Unit →.
Versione ripasso
- Three processes: (1) state register: reset to initial state, else
state <= next_stateon the edge; (2) next state, combinational fromstateand inputs; (3) output, combinational fromstate(Moore) orstateand input (Mealy). Only process 1 has memory. - Enumerated type:
type state_type is (S1, S2, S3, S4);symbolic values; the synthesiser picks the encoding (2 bits for 4 states) unless imposed. - Recogniser 1101 (Mealy):
case statein process 2 with : , ; : , ; : , ; : , . Output:when S4 => Z <= '1' if X='1', else'0'. - Good practice: reset tested first (async); assign every output in every branch; default
next_state <= state;for "stay" cases. - Moore: output process sensitive to
stateonly, no input in thecase. - Testbench: clock process (
PERIOD/2high,PERIOD/2low); input process: reset first (3*PERIOD/2), then a bit oftest_sequenceper cycle on the falling edge,std.env.stop. With01110101100: only during the sixth bit; Mealy output rises when the input changes (650 ns). - Traffic light (Moore): states VNS
001100, GNS010100, VEO100001, GEO100010; in VNS goes to GNS; then VEO, GEO, VNS.