Skip to main content

Sequence Detector - abab_baba (Behavioural Modelling)

Behavioural Description of abab_baba Sequence Detector

library ieee;
use ieee.std_logic_1164.all;

entity seqDetector is
port(clock : in std_logic;
reset: in std_logic;
inp : in std_logic; --inp=0 => a inp=1 => b
y: out std_logic --y=1 =>Y y=0=> N
);
end seqDetector;

architecture behav of seqDetector is
--type declaration synthesis tool will assign state encoding
type statetype is (RST,s_a,s_ab,s_aba,s_b,s_ba,s_bab);
signal pr_state,nx_state : statetype;


--state binary encoding
--signal pr_state,nx_state:std_logic_vector(2 downto 0);
--constant RST:std_logic_vector(2 downto 0):="000";
--constant s_a:std_logic_vector(2 downto 0):="001";
--
--constant s_b:std_logic_vector(2 downto 0):="010";
--
--constant s_ab:std_logic_vector(2 downto 0):="011";
--
--constant s_ba:std_logic_vector(2 downto 0):="100";
--constant s_aba:std_logic_vector(2 downto 0):="101";
--
--constant s_bab:std_logic_vector(2 downto 0):="110";
begin

next_state_output_logic: process (pr_state, inp)
begin
case pr_state is

--RST
when RST=>
y<='0';
if inp='1'then nx_state<=s_b;
else nx_state<=s_a;end if;

--S_a
when s_a=>
y<='0';
if inp='1'then nx_state<=s_ab;
else nx_state<=s_a;end if;

--S_b
when s_b=>
y<='0';
if inp='1'then nx_state<=s_b;
else nx_state<=s_ba;end if;

--S_ab
when s_ab=>
y<='0';
if inp='1'then nx_state<=s_b;
else nx_state<=s_aba;end if;

--S_ba
when s_ba=>
y<='0';
if inp='1'then nx_state<=s_bab;
else nx_state<=s_a;end if;

--S_aba
when s_aba=>
y<=inp; --abab detected
if inp='1'then nx_state<=s_bab;
else nx_state<=s_a;end if;

--S_bab
when s_bab=>
y<=not inp; -- baba detected
if inp='1'then nx_state<=s_b;
else nx_state<=s_aba;end if;

--DEFAULT CASE
when others=>
y<='0';
nx_state<=RST;
end case;
end process next_state_output_logic;

reg_process: process(clock,reset)
begin
if(reset='1')then
pr_state<=RST;
elsif(clock'event and clock='1')then
pr_state<=nx_state;
end if;
end process reg_process;
end behav;


TRACEFILE

TRACEFILE