There is a special Coding style for State Machines in VHDL as well as in Verilog.
Let us consider below given state machine which is a “1011” overlapping sequence detector. Output becomes ‘1’ when sequence is detected in state S4 else it remains ‘0’ for other states.
VHDL Code for FSM:
library IEEE;use IEEE.STD_LOGIC_1164.ALL;
--Sequence detector for detecting the sequence "1011".
--Overlapping type.
entity seq_det is
port( clk : in std_logic; --clock signal
reset : in std_logic; --reset signal
S_in : in std_logic; --serial bit Input sequence
S_out : out std_logic); -- Output
end seq_det;
architecture Behavioral of seq_det is
--Defines the type for states in the state machine
type state_type is (S0,S1,S2,S3,S4);
--Declare the signal with the corresponding state type.
signal Current_State, Next_State : state_type;
begin
-- Synchronous Process
process(clk)
begin
if( reset = '1' ) then --Synchronous Reset
Current_State <= 'S0';
elsif (clk'event and clk = '1') then --Rising edge of Clock
Current_State <= Next_State
end if;
end process;
-- Combinational Process
Process(Current_State, S_in)
begin
case Current_State is
when S0 =>
S_out <= '0';
if ( s_in = '0' ) then
Next_State <= S0;
else
Next_State <= S1;
end if;
when S1 =>
S_out <= '1';
if ( S_in = '0' ) then
Next_State <= S3;
else
Next_State <= S2;
end if;
when S2 =>
S_out <= '0';
if ( S_in = '0' ) then
Next_State <= S0;
else
Next_State <= S3;
end if;
when S3 =>
S_out <= '1';
if (S_in = '0' ) then
Next_State <= S2;
else
Next_State <= S4;
end if;
when S4 =>
S_out <= '1';
if ( S_in = '0' ) then
Next_State <= S2;
else
Next_State <= S1;
end if;
when others =>
NULL;
end case;
end if;
end process;
superb :)
ReplyDeletei totally understand the programming.
thanks a lot.
totally right!
DeleteLove it.
ReplyDeletehelped me alot, thanks :)
ReplyDeletewas in search of it..thank u..:)
ReplyDeleteHi, Where is the "output state" logic defined to detect the sequence "1011"
ReplyDeleteThanks, there was a typo in the code... We updated it.
DeleteRegards,
Team VLSI Encyclopedia
very good
ReplyDeleteThanks for appreciation :)
Deleteafter "end case;", there is an "end if;" too much, i think
ReplyDeletethank you !
ReplyDeleteHere the second process will not be executed if the s_in in the first clock cycle is 0. The sate machine will not move forward as processes react to only events and there will never be an event on Current_State. Sorry if i am wrong.
ReplyDeleteVamsi, Thanks for writing.
DeleteYou are correct, the sensitivity list should also contain input s_in.
why is the S_out for states S1 and S3 1? Should they not be 0?
ReplyDeleteIf suppose we draw a mealy FSM for this detector, I guess we would be saving one extra state as instead of going from S3 to S4 the FSM can go to the state S1, for detecting the overlapping sequence 1011, the last digit 1 can serve as the beginning of new sequence 1011, am I right?
ReplyDeleteYes you are right... the melay implementation will save 1 state as the output of melay is function of present state and value of inputs.
DeleteHow can I run this code in Quartus II?
ReplyDeletePlease refer below guide for Quartus!!
Deletehttps://goo.gl/FGO8Ow
Thanks,
Team VLSI Encyclopedia