Salta al contenuto
Note per Studenti VHDL - Finite State Machines and Sequential Testbenches Report

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).

  1. State register (sequential part): on the clock edge state <= next_state; reset brings the machine to a known initial state.
  2. Next state (combinational logic): computes next_state from state and the inputs.
  3. Output (combinational logic): computed from state (Moore) or from state and 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

vhdl
type state_type is (S1, S2, S3, S4);          -- new type with symbolic values
signal state, next_state : state_type;

Definition (enumerated type). With type a 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_logic itself is an enumeration (9 values). The synthesiser chooses the binary encoding of the states unless it is imposed (for example with std_logic_vector constants, as in the traffic light example).

Example. type state_type is (S1, S2, S3, S4); creates a type with 44 symbolic values; the synthesiser encodes it with 22 bits.

Complete example: 1101 sequence recogniser (Mealy)

Input XX, output ZZ; Z=1Z=1 when 110110 has been seen and X=1X=1; asynchronous reset to S1S_1. The state diagram is the one of the synthesis note.

vhdl
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.

Testbench

Two processes in parallel (VHDL - Concurrent Statements, Processes and TestbenchesConcurrent VHDL statements are the plain signal assignment, when-else (priority logic, conditions evaluated in order) and with-select (parallel logic, every case covered once, others required). A process is a concurrent statement with a sequential body (if, case, loop) that wakes up when a signal in its sensitivity list changes; for combinational logic the list holds all inputs; signals are updated when the process suspends and the last assignment wins. An if without else or an uncovered case creates unwanted memory. A testbench has an empty entity, the instantiated DUT and a stimulus process with wait statements.VHDL - Concurrent Statements, Processes and Testbenches →):

vhdl
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 0,100,200,…0,100,200,\dots ns and falls at 50,150,…50,150,\dots; inputs change at 150,250,…150,250,\dots ns, i.e. on the falling edge, away from the rising edge. The initial reset lasts 150150 ns and brings the state to S1S_1.

Expected with sequence 0111010110001110101100: states (S1S_1 at the start) S1,S2,S3,S3,S4,S2,S1,S2,S3,S4,S1S_1,S_2,S_3,S_3,S_4,S_2,S_1,S_2,S_3,S_4,S_1 after successive edges, and Z=1Z=1 only during the sixth bit (the bit completing 11011101). Being a Mealy machine, ZZ rises as soon as the input changes (at 650650 ns, falling edge), not at the next clock edge.

Moore example with output vector: traffic light controller

Input XX = car waiting in the E-W direction; six output lights (RNS,GNS,VNS,REO,GEO,VEO)(R_{NS},G_{NS},V_{NS},R_{EO},G_{EO},V_{EO}) (R red, G yellow, V green). Reset: green N-S, red E-W. If X=1X=1 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 X=0X=0 if X=1X=1
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
vhdl
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; X=0X=0 for 2 cycles (stays in VNS); X=1X=1 for 3 cycles (enters the sequence); X=0X=0 for 2 cycles; X=1X=1 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_state on the edge; (2) next state, combinational from state and inputs; (3) output, combinational from state (Moore) or state and 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 state in process 2 with S1S_1: 0→S10\to S_1, 1→S21\to S_2; S2S_2: 0→S10\to S_1, 1→S31\to S_3; S3S_3: 0→S40\to S_4, 1→S31\to S_3; S4S_4: 0→S10\to S_1, 1→S21\to S_2. 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 state only, no input in the case.
  • Testbench: clock process (PERIOD/2 high, PERIOD/2 low); input process: reset first (3*PERIOD/2), then a bit of test_sequence per cycle on the falling edge, std.env.stop. With 01110101100: Z=1Z=1 only during the sixth bit; Mealy output rises when the input changes (650 ns).
  • Traffic light (Moore): states VNS 001100, GNS 010100, VEO 100001, GEO 100010; X=1X=1 in VNS goes to GNS; then VEO, GEO, VNS.

Teoria collegata