Sunteți pe pagina 1din 2

Sequence detector using state machine in VHDL

Some readers were asking for more examples related with state machine and some where asking for codes related with sequence detector.This article will be helpful for state machine designers and for people who try to implement sequence detector circuit in VHDL. I have created a state machine for non-overlapping detection of a pattern "1011" in a sequence of bits. The state machine diagram is given below for your reference.

The VHDL code for the same is given below. I have added comments for your easy understanding.

library IEEE; use IEEE.STD_LOGIC_1164.ALL; --Sequence detector for detecting the sequence "1011". --Non overlapping type. entity seq_det is port( clk : in std_logic; --clock signal reset : in std_logic; --reset signal seq : in std_logic; --serial bit sequence det_vld : out std_logic --A '1' indicates the pattern "1011" is detected in the sequence. ); end seq_det; architecture Behavioral of seq_det is type state_type is (A,B,C,D); --Defines the type for states in the state machine signal state : state_type := A; --Declare the signal with the corresponding state type. begin process(clk) begin if( reset = '1' ) then --resets state and output signal when reset is asserted. det_vld <= '0'; state <= A; elsif ( rising_edge(clk) ) then --calculates the next state based on current state and input bit.

case state is when A => --when the current state is A. det_vld <= '0'; if ( seq = '0' ) then state <= A; else state <= B; end if; when B => --when the current state is B. if ( seq = '0' ) then state <= C; else state <= B; end if; when C => --when the current state is C. if ( seq = '0' ) then state <= A; else state <= D; end if; when D => --when the current state is D. if ( seq = '0' ) then state <= C; else state <= A; det_vld <= '1'; --Output is asserted when the pattern "1011" is found in the sequence. end if; when others => NULL; end case; end if; end process; end Behavioral;