What is the system that I want to design? I assume that I have an input sequence of bits, arriving at the input port, one bit a time. Whenever the last 4 bits has the value "1011", I want a single pulse to be generated at the output port.
State Machine Diagram:
The following state machine diagram shows how this works. The VHDL code shared further down in the article is a direct implementation of this state diagram. You can see that the initial state is called "A" and as each incoming bit matches the bit in the sequence "1011", we move to the next state, finally arriving at state "D". The output pulse is generated if the next input bit is '1', matching with the right most bit in the sequence, "1011".
library ieee; use ieee.std_logic_1164.all; --Sequence detector for detecting the sequence "1011". --Non overlapping type. entity sequence_detector is port(clk : in std_logic; --clock signal reset : in std_logic; --reset signal bit_in : in std_logic; --serial bit input seq_detected : out std_logic --A '1' indicates the pattern "1011" is detected in the sequence. ); end sequence_detector; architecture Behavioral of sequence_detector 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,reset) begin if(reset = '1') then --resets state and output signal when reset is asserted. seq_detected <= '0'; state <= A; --initial state 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. seq_detected <= '0'; if(bit_in = '0') then state <= A; else state <= B; --first bit matches with the 1st bit in "1011" from left. end if; when B => --when the current state is B. if(bit_in = '0') then state <= C; --2nd bit matches with the 2nd bit in "1011" from left. else state <= B; end if; when C => --when the current state is C. if(bit_in = '0') then state <= A; else state <= D; --3rd bit matches with the 3rd bit in "1011" from left. end if; when D => --when the current state is D. if(bit_in = '0') then state <= C; else state <= A; --assert pulse on output signal seq_detected <= '1'; --4th bit matches with the final bit in "1011". end if; when others => NULL; end case; end if; end process; end Behavioral;
I believe that the code is well commented, so I wont explain it any further. Note that, the output depends on the current state and the current input. So what we have just implemented is a Mealy state machine.
The following testbench code was written for testing the code. The sequence "11011101011" was sent to our sequence detector entity, one bit at a time starting from the leftmost bit.
What we are trying to detect is the 4 bit sequence "1011". So an output pulse should be generated on the 5th and the 11th input bit.
My input sequence: "11011101011".
Testbench Code for Sequence Detector:
library ieee; use ieee.std_logic_1164.all; entity tb_sequence_detector is end tb_sequence_detector; architecture Behavioral of tb_sequence_detector is signal clk,reset,bit_in,seq_detected : std_logic := '0'; constant clk_period : time := 10 ns; begin -- entity instantiation with named association uut: entity work.sequence_detector port map ( clk => clk, reset => reset, bit_in => bit_in, seq_detected => seq_detected ); -- generate clock clk_process :process begin wait for clk_period/2; clk <= not clk; end process; -- stimulus process : apply the bits in the sequence one by one. stim_proc: process begin bit_in <= '1'; --1 wait for clk_period; bit_in <= '1'; --11 wait for clk_period; bit_in <= '0'; --110 wait for clk_period; bit_in <= '1'; --1101 wait for clk_period; bit_in <= '1'; --11011 wait for clk_period; bit_in <= '1'; --110111 wait for clk_period; bit_in <= '0'; --1101110 wait for clk_period; bit_in <= '1'; --11011101 wait for clk_period; bit_in <= '0'; --110111010 wait for clk_period; bit_in <= '1'; --1101110101 wait for clk_period; wait; end process; end Behavioral;
Simulation Waveform:
The simulated waveform from modelsim is pasted below:
Note:- The code was simulated using modelsim. It was synthesised successfully using AMD Vivado 2023.2 version.
In process(clk) - reset is missing from the sensitivity list.
ReplyDelete@foam : thanks for the note. As you said, it should be process(clk,reset).
ReplyDeleteWhat would be the state diagram for an overlapping sequence? What is the difference?
ReplyDelete@karan : Read any digital book for the state diagram for overlapping sequence detector.
ReplyDeleteFor converting the state diagram into a vhdl code, you can use the same concept used in this post.
Which book is good for vhdl
DeleteHow to draw the internal architecture of 4 bit non overlapping sequence detector????
ReplyDeleteWhen should I use the state machine design used in this example and when should I use the design used in http://vhdlguru.blogspot.com/2010/04/how-to-implement-state-machines-in-vhdl.html?
ReplyDeleteIt seems to me that in this example you change the current state in the same clock as it's verified, but in the other one you let it wait to change in the next clock. Is my interpretation correct?
@Leandro : Really good question and observation. You can use this type of design in all cases.
ReplyDeleteSee the sensitivity list in both the processes. In this design it has Clk.So in each clk edge it will check and change the state.
But in the second code, the sensitivity list contains only next state and input. Which means that if they remain the same for some reason(depends on your state machine design) then the process statemnent may not evaluate in every clock cycle.
@sonica : I think you can find the answer in any good digital design book. The above example deals with a non overlapping sequence. So draw the state machine in a similar way.Once you have the state machine(which is the most important step) coding it in vhdl is a very easy process.
ReplyDeleteHai
ReplyDeleteI have project in vhdl to be completed within next week.The project is to decode irig b pulses.pls help me in doing that
mahesh
i want to ask...when i run the testbench and this error come out : Error (10533): VHDL Wait Statement error at blog_cg.vhd(26): Wait Statement must contain condition clause with UNTIL keyword
ReplyDeletewant sequence detector for 0100 and 1000 sequences
ReplyDeleteplease send me the state diagram with the necessary explanation for the below Question.on my email id (the.beast.master.007@gmail.com)
ReplyDeleteA sequential network has on input (X) and two outputs (Z1 and Z2). An output Z1=1 occurs every time the input sequence 010 is completed provided that the sequence 100 has never occurred.an output Z2=1 occurs every time the iniput sequence 100 is completed. Note that once a Z2=1 output has occurred, Z1=1 can never occur,but not vice versa.
a).Derive a mealy state graph and table with a minimum number of states (8 states).
@beast_boy : Contact me using the contact form and we will discuss about it.
ReplyDeletein the test bench i think is a mistake.... it didn't show the state signal... can you help me to fix the problem... i try to do something but the signal state in simulation is block on state A... some help someone?
ReplyDelete@sabin : contact me using the contact form. I can help for a fee.
ReplyDeletehi i have the same problem, can`t see state change, the state is always A ..
Deletecan you help me, please ?
Hi, the program you have mentioned above seems to me helpful to detect ADS-S signal(https://docs.google.com/document/d/1EzR8wpxjFJhmnaxeSt7_6DNqb-bFPda8emZF6W1kIhI/edit?pli=1) for my project.
ReplyDeleteAll I need to detect only pattern of the signal (only 10 microsecond from the beginning not the entire 120 microsecond in attached figure).
As I am very new to VHDL and so difficult to proceed.
Can I get some more Idea how I can proceed to do simulation to detect this signal?
Thanks in Advance
Sheikh
thank u bt in simulation we are not getting 'state' ;
ReplyDeleteHello to everybody . Please tell me what i must do for having "state" in TEST BENCH SIMULATION ??? Thanks ...
ReplyDeleteI am eager to see the answer. I want to to see that too.
Deleteif it is a overlapping sequence in the last state D after encountering '1' it must go to state B RIGHT?Please correct me if I am wrong
ReplyDeleteWhat if I wanted to detect this sequence five times
ReplyDeleteCan you please send the code for moore sequence detector for sequence-0111010
ReplyDelete