Salta al contenuto
Note per Studenti Esercizio 15 · descrizione VHDL behavioral di una macchina a tre stati e testbench (tema d'esame settembre 2025)

Esercizio 15descrizione VHDL behavioral di una macchina a tre stati e testbench (tema d'esame settembre 2025)

Esame
In questa pagina 4

Testo (tema d'esame settembre 2025, esercizio 3 della parte di pratica). 1) Proporre una descrizione VHDL di tipo behavioral del circuito il cui diagramma degli stati è riportato in figura. Usare D-FF positive edge triggered con reset asincrono come elementi di memoria [NB: non è richiesta la sintesi]. 2) Creare un testbench che faccia arrivare il sistema nello stato C con il minor numero possibile di transizioni.

Diagramma (dalla figura del tema): macchina di Moore con tre stati, uscita scritta nel cerchio: A/0A/0 (stato iniziale, freccia con "Reset = 0"), B/0B/0, C/1C/1; ingresso a un bit;

  • A→0AA\xrightarrow{0}A e A→1BA\xrightarrow{1}B;
  • B→1BB\xrightarrow{1}B e B→0CB\xrightarrow{0}C;
  • C→1BC\xrightarrow{1}B e C→0AC\xrightarrow{0}A.

Teoria usata: VHDL - macchine a stati finiti e testbench sequenzialeUna macchina a stati finiti (FSM) si descrive in VHDL, in stile behavioral, con un tipo enumerato per gli stati e tre process: (1) registro di stato con reset (asincrono) e aggiornamento state<=next_state sul fronte; (2) stato futuro come funzione combinatoria di stato e ingresso (case state, default next_state<=state); (3) uscita come funzione combinatoria di stato (Moore) o di stato e ingresso (Mealy). Il testbench ha il process del clock e il process degli ingressi (reset iniziale, sequenza di test che percorre tutti gli stati).VHDL - macchine a stati finiti e testbench sequenziale →, VHDL - latch, flip-flop e reset sincrono e asincronoIn VHDL un elemento di memoria si descrive con un process il cui ramo non assegna sempre il segnale: latch D = process(D,C) con if C='1' then Q<=D; flip-flop positive-edge-triggered = process(CLK) con if CLK'event and CLK='1'. Reset asincrono: RST nella sensitivity list e testato per primo (if RST='1' ... elsif fronte); reset sincrono: solo CLK in lista e RST testato dentro al ramo del fronte (serve un fronte per averne effetto). Il testbench di un circuito sincrono ha un process che genera il clock e uno che applica gli ingressi lontano dal fronte attivo.VHDL - latch, flip-flop e reset sincrono e asincrono →.

Passo 1: lettura del diagramma

  • Moore: l'uscita è dentro lo stato (Y=1Y=1 solo in CC), quindi il process dell'uscita ha solo state nella lista di sensibilità.
  • Significato: AA = "l'ultimo bit non ha avviato una sequenza"; BB = "l'ultimo bit è stato 11"; CC = "ultimo bit 00 dopo un 11", cioè la macchina riconosce la sequenza 1010 (con sovrapposizioni: da CC un 11 riparte da BB).
  • Il reset: la freccia iniziale è etichettata "Reset = 0": si legge come reset attivo basso (con reset_n = '0' lo stato diventa AA), asincrono come richiesto dal tema. Se invece il reset fosse attivo alto basta cambiare la condizione del process.

Tabella (stato futuro per ingresso x=0x=0 / x=1x=1):

stato uscita yy x=0x=0 x=1x=1
AA 0 AA BB
BB 0 CC BB
CC 1 AA BB

Passo 2: descrizione behavioral

vhdl
library IEEE;
use IEEE.std_logic_1164.all;

entity fsm_abc is
  port ( clk, reset_n : in  std_logic;
         x            : in  std_logic;
         y            : out std_logic );
end fsm_abc;

architecture beh of fsm_abc is
  type state_type is (A, B, C);
  signal state, next_state : state_type;
begin
  -- D-FF positive edge triggered con reset asincrono (attivo basso)
  state_register : process (clk, reset_n)
  begin
    if reset_n = '0' then
      state <= A;
    elsif clk'event and clk = '1' then
      state <= next_state;
    end if;
  end process;

  -- stato futuro
  next_state_comb : process (state, x)
  begin
    case state is
      when A =>
        if x = '1' then next_state <= B; else next_state <= A; end if;
      when B =>
        if x = '1' then next_state <= B; else next_state <= C; end if;
      when C =>
        if x = '1' then next_state <= B; else next_state <= A; end if;
    end case;
  end process;

  -- uscita di Moore
  output_comb : process (state)
  begin
    case state is
      when C      => y <= '1';
      when others => y <= '0';
    end case;
  end process;
end beh;

Note: la sintesi non è richiesta, quindi non conta come si codificano gli stati. Ogni ramo assegna next_state: nessuna memoria involontaria.

Passo 3: testbench che porta il sistema in CC

Percorso più breve. Da AA (stato dopo il reset) per arrivare in CC bisogna passare da BB (l'unico arco entrante in CC parte da BB): la sequenza minima è x=1x=1 (A →\to B) seguito da x=0x=0 (B →\to C): 2 transizioni, 2 cicli di clock. Non esistono percorsi più brevi.

vhdl
library IEEE;
use IEEE.std_logic_1164.all;

entity tb_fsm_abc is
end tb_fsm_abc;

architecture test of tb_fsm_abc is
  signal clk, reset_n, x, y : std_logic;
  constant PERIOD : time := 10 ns;
begin
  DUT : entity work.fsm_abc(beh) port map (clk, reset_n, x, y);

  generate_clock : process
  begin
    clk <= '1'; wait for PERIOD/2;
    clk <= '0'; wait for PERIOD/2;
  end process;

  apply_inputs : process
  begin
    reset_n <= '0'; x <= '0';          -- reset: stato A
    wait for 3*PERIOD/2;               -- 15 ns, sul fronte di discesa
    reset_n <= '1';
    x <= '1';                          -- 1 -> A passa a B al fronte di salita di 20 ns
    wait for PERIOD;
    x <= '0';                          -- 0 -> B passa a C al fronte di salita di 30 ns
    wait for PERIOD;                   -- 35 ns: la macchina è in C
    assert y = '1' report "il sistema non è nello stato C" severity error;
    std.env.stop;
  end process;
end test;

Evoluzione: t=15t=15 ns reset rilasciato e x=1x=1; a 2020 ns stato BB (y=0y=0); a 2525 ns x=0x=0; a 3030 ns stato CC (y=1y=1): a 3535 ns l'assert verifica y=1y=1.

Errori comuni

  • Leggere "Reset = 0" come reset attivo alto: la freccia indica la condizione che porta nello stato iniziale.
  • Descrivere l'uscita come dipendente da xx: è Moore.
  • Testbench con più transizioni del necessario (per esempio passando da AA con x=0x=0): la richiesta era il percorso minimo.
  • Cambiare xx sul fronte di salita del clock.

Versione ripasso

Teoria collegata