Projektowanie automatów z użyciem VHDL struktura automatu i jego modelu w VHDL przerzutnik T jako automat przykłady automatów z wyjściami typu: Moore'a Mealy stanu kodowanie stanów automatu Wykorzystano przykłady z: The Low-Carb VHDL Tutorial 2004 by Bryan Mealy (08-27-2004) PUE-w05 1
Schemat blokowy automatu i jego model w VHDL PUE-w05 2
Szablon opisu architektury dowolnego automatu architecture szablon of nazwa_modelu is deklaracje sygnalow wewnetrznych proces stan opisuje czesc sekwencyjną (synchroniczną) stan: process (lista wrazliwosci clk,rst,nextstate) instrukcje VHDL dla przerzutnikow end process stan; proces komb definiuje część kombinatoryczną komb: process (lista wrazliwosci wszystkie wejścia) instrukcje VHDL opisujace część kombinacyjną end process komb; -- (opcjonalna instrukcja wyprowadzająca stan jako wyjście) end architecture szablon; PUE-w05 3
Przerzutnik T jako automat Moore'a FSM - Finite State Machine ; TOG_EN - Toggle Enable ; CLR - Clear; CLK - Clock ; ST0, ST1 - State 0, State 1 entity my_fsm1 is port ( TOG_EN : in std_logic; CLK,CLR : in std_logic; Z1 : out std_logic); end entity my_fsm1; PUE-w05 4
Przerzutnik T jako automat Moore'a architecture fsm1 of my_fsm1 is type state_type is (ST0,ST1); signal PS,NS : state_type; sync: process(clk,ns,clr) if (CLR = 1 ) then PS <= ST0; elsif (rising_edge(clk)) then PS <= NS; end if; end process sync_proc; --(proces kombinacyjny na następnym slajdzie) end architecture fsm1; PUE-w05 5
Przerzutnik T jako automat Moore'a comb: process(ps,tog_en) case PS is when ST0 => Z1 <= 0 ; if (TOG_EN = 1 ) then NS <= ST1; else NS <= ST0; end if; when ST1 => Z1 <= 1 ; if (TOG_EN = 1 ) then NS <= ST0; else NS <= ST1; end if; when others => Z1 <= 0 ; NS <= ST0; end case; end process comb; PUE-w05 6
Przerzutnik T jako automat Moore'a- uwagi Zadeklarowano typ wyliczeniowy state_type do reprezentowania stanów: PS - Present State, NS - Next State Proces sync opisuje asynchroniczne zerowanie i synchroniczne przejście do nowego stanu; proces comb obsługuje wyjście Z1 oraz oblicza stan NS Oba procesy są współbieżne; zmiana NS wymusza obliczenia procesu sync, ale w nie powoduje to natychmiastowej zmiany żadnego innego sygnału; przypisanie PS<=NS następuje dopiero gdy narasta sygnał clk; zmiana PS uruchamia proces comb w którym obliczany jest następny stan NS Klauzula when others w procesie comb obejmuje przypadki, które nie powinny się zdarzyć, (np. po awaryjnym zakłóceniu, zaniku zasilania itp.) Można wyprowadzić stan PS jako wyjście nie w comb ale przy pomocy współbieżnej z oboma procesami instrukcji, np.: with PS select Z1 <= 0 when ST0, 1 when ST1, 0 when others; PUE-w05 7
Automat z wyprowadzonym stanem Y i wyjściem Mealy Z2 entity my_fsm3 is port ( X : in std_logic; CLK : in std_logic; SET : in std_logic; Z2 : out std_logic; Y : out std_logic_vector(1 downto 0)); end entity my_fsm3; PUE-w05 8
Architektura automatu my_fsm3 (1) architecture fsm3 of my_fsm3 is type state_type is (ST0,ST1,ST2); signal PS,NS : state_type; sync: process(clk,ns,set) if (SET = 1 ) then PS <= ST2; elsif (rising_edge(clk)) then PS <= NS; end if; end process sync; -- (opis procesu comb na nastepnym slajdzie) with PS select Y <= 00 when ST0, 10 when ST1, 11 when ST2, 00 when others; end architecture fsm3; PUE-w05 9
Architektura automatu my_fsm3 (2) comb: process(ps,x) case PS is when ST0 => Z2 <= 0 ; if (X = 0 ) then NS <= ST0; else NS <= ST1; end if; when ST1 => Z2 <= 0 ; if (X = 0 ) then NS <= ST0; else NS <= ST2; end if; when ST2 => if (X = 0 ) then NS <= ST0; Z2 <= 0 ; else NS <= ST2; Z2 <= 1 ; end if; when others => NS <= ST0; Z2 <= 1 ; end case; end process comb; PUE-w05 10
Automat z trzema rodzajami wyjść (1) entity my_fsm4 is port ( X,CLK,RESET : in std_logic; Y : out std_logic_vector(1 downto 0); Z1,Z2 : out std_logic); end entity my_fsm4; PUE-w05 11
Automat z trzema rodzajami wyjść (2) architecture fsm4 of my_fsm4 is type state_type is (ST0,ST1,ST2,ST3); signal PS,NS : state_type; sync: process(clk,ns,reset) if (RESET = 1 ) then PS <= ST0; elsif (rising_edge(clk)) then PS <= NS; end if; end process sync; with PS select Y <= 00 when ST0, 01 when ST1, 10 when ST2, 11 when ST3, 00 when others; --(process comb na nastepnym slajdzie) end architecture fsm4; PUE-w05 12
Automat z trzema rodzajami wyjść (3) comb: process(ps,x) case PS is when ST0 => Z1 <= 1 ; if (X = 0 ) then NS <= ST2; Z2 <= 0 ; else NS <= ST1; Z2 <= 1 ; end if; when ST1 => Z1 <= 1 ; if (X = 0 ) then NS <= ST2; Z2 <= 0 ; else NS <= ST1; Z2 <= 1 ; end if; when ST2 => Z1 <= 0 ; if (X = 0 ) then NS <= ST3; Z2 <= 0 ; else NS <= ST2; Z2 <= 1 ; end if; when ST3 => Z1 <= 1 ; if (X = 0 ) then NS <= ST0; Z2 <= 0 ; else NS <= ST3; Z2 <= 1 ; end if; when others => Z1 <= 1 ; Z2 <= 0 ; NS <= ST0; end case; end process comb; PUE-w05 13
Kodowanie stanów automatu 1. Minimalna liczba przerzutników (np. kod NB) N FF log2 N ST 2. Kod 1 z N (tyle przerzutników ile stanów) N FF N ST Dlaczego 1 z N? prostsze funkcje wyjść dziś przerzutniki nie są kosztowne układy programowalne PLD mają przerzutniki blisko końcówek wyjściowych mniej logiki kombinacyjnej => szybsze działanie PUE-w05 14
Zmiana sposobu kodowania stanów -- full encoded approach entity my_fsm4 is port ( X,CLK,RESET : in std_logic; Y : out std_logic_vector(1 downto 0); Z1,Z2 : out std_logic); end entity my_fsm4; -- one-hot encoding approach entity my_fsm4 is port ( X,CLK,RESET : in std_logic; Y : out std_logic_vector(3 downto 0); Z1,Z2 : out std_logic); end my_fsm4; PUE-w05 15
Zmiana sposobu kodowania stanów architecture fsm4 of my_fsm4 is type state_type is (ST0,ST1,ST2,ST3); attribute ENUM_ENCODING: STRING; attribute ENUM_ENCODING of state_type: type is 1000 0100 0010 0001 ; signal PS,NS : state_type; -- (oba procesy bez zmian) -- full encoded approach -- one-hot encoded approach with PS select with PS select Y <= 00 when ST0, Y <= 1000 when ST0, 01 when ST1, 0100 when ST1, 10 when ST2, 0010 when ST2, 11 when ST3, 0001 when ST3, 00 when others; 1000 when others; end architecture fsm4; end architecture fsm4; PUE-w05 16