Professional Documents
Culture Documents
support@allaboutfpga.com +91-9789377039
Menu
Sequence Detector using Mealy and Moore State Machine VHDL Codes
July 5, 2017 by shahul akthar
The Output of the state machine depends on both present state and current input. When the input changes,the output of the state machine
The Output of the State machine depends only on present state. The output of state machine are only updated at the clock edge.
https://allaboutfpga.com/sequence-detector-using-mealy-and-moore-state-machine-vhdl-codes/ 1/14
5/8/2019 Sequence Detector using Mealy and Moore State Machine VHDL Codes
Menu
Let’s construct the sequence detector for the sequence 101 using both mealy state machine and moore state machine.
Moore state require to four states st0,st1,st2,st3 to detect the 101 sequence.
Mealy state machine require only three states st0,st1,st2 to detect the 101 sequence.
https://allaboutfpga.com/sequence-detector-using-mealy-and-moore-state-machine-vhdl-codes/ 2/14
5/8/2019 Sequence Detector using Mealy and Moore State Machine VHDL Codes
Menu
VHDL code for Sequence detector (101) using moore state machine
1 library IEEE;
2 use IEEE.STD_LOGIC_1164.ALL;
3
4 entity moore is
5 Port ( clk : in STD_LOGIC;
6 din : in STD_LOGIC;
7 rst : in STD_LOGIC;
8 dout : out STD_LOGIC);
9 end moore;
10
11 architecture Behavioral of moore is
12 type state is (st0, st1, st2, st3);
13 signal present_state, next_state : state;
14 begin
15
16 synchronous_process: process (clk)
17 begin
18 if rising_edge(clk) then
19 if (rst = '1') then
20 present_state <= st0;
21 else
22 present_state <= next_state;
23 end if;
24 end if;
25 end process;
26
27 output_decoder : process(present_state, din)
https://allaboutfpga.com/sequence-detector-using-mealy-and-moore-state-machine-vhdl-codes/ 3/14
5/8/2019 Sequence Detector using Mealy and Moore State Machine VHDL Codes
28 begin
29 next_state <= st0; case (present_state) is when st0 =>
30 if (din = '1') then
31 next_state <= st1;
32 else
33 next_state <= st0; end if; when st1 =>
34
Menu if (din = '1') then
35 next_state <= st1;
36 else
37 next_state <= st2; end if; when st2 =>
38 if (din = '1') then
39 next_state <= st3;
40 else
41 next_state <= st0; end if; when st3 =>
42 if (din = '1') then
43 next_state <= st1;
44 else
45 next_state <= st2; end if; when others =>
46 next_state <= st0; end case; end process; next_state_decoder : process(present_state) begin case
(present_state) is when st0 =>
47 dout <= '0'; when st1 =>
48 dout <= '0'; when st2 =>
49 dout <= '0'; when st3 =>
50 dout <= '1'; when others =>
51 dout <= '0';
52 end case;
53 end process;
54
55 end Behavioral;
VHDL code for Sequence detector (101) using mealy state machine
1 library IEEE;
2 use IEEE.STD_LOGIC_1164.ALL;
3
4 entity mealy is
5 Port ( clk : in STD_LOGIC;
6 din : in STD_LOGIC;
7 rst : in STD_LOGIC;
8 dout : out STD_LOGIC);
9 end mealy;
10
https://allaboutfpga.com/sequence-detector-using-mealy-and-moore-state-machine-vhdl-codes/ 4/14
5/8/2019 Sequence Detector using Mealy and Moore State Machine VHDL Codes
TestBench VHDL code for sequence detector using Moore State Machine
1 LIBRARY ieee;
2 USE ieee.std_logic_1164.ALL;
3
4 ENTITY tb_moore IS
Menu
5 END tb_moore;
6
7 ARCHITECTURE behavior OF tb_moore IS
8
9 -- Component Declaration for the Unit Under Test (UUT)
10
11 COMPONENT moore
12 PORT(
13 clk : IN std_logic;
14 din : IN std_logic;
15 rst : IN std_logic;
16 dout : OUT std_logic
17 );
18 END COMPONENT;
19
20 --Inputs
21 signal clk : std_logic := '0';
22 signal din : std_logic := '0';
23 signal rst : std_logic := '0';
24
25 --Outputs
26 signal dout : std_logic;
27
28 -- Clock period definitions
29 constant clk_period : time := 20 ns;
30
31 BEGIN
32
33 -- Instantiate the Unit Under Test (UUT)
34 uut: moore PORT MAP (
35 clk => clk,
36 din => din,
37 rst => rst,
38 dout => dout
39 );
40
https://allaboutfpga.com/sequence-detector-using-mealy-and-moore-state-machine-vhdl-codes/ 6/14
5/8/2019 Sequence Detector using Mealy and Moore State Machine VHDL Codes
Note: Same testbench code can be used for Mealy VHDL code by simply changing the component name to mealy.
From the above shown waveform, sequence 101 is detected twice from the testbench VHDL code.
Share this:
Related
https://allaboutfpga.com/sequence-detector-using-mealy-and-moore-state-machine-vhdl-codes/ 8/14
5/8/2019 Sequence Detector using Mealy and Moore State Machine VHDL Codes
Menu
VHDL Code for 4-bit Ring Counter and Johnson Tutorial 1: Binary Counter FPGA Implementation VHDL Testbench Tutorial
Counter November 4, 2018 April 22, 2014
May 22, 2016 In "FPGA" In "VHDL"
In "VHDL"
VHDL
Mealy state machine vhdl code, moore state machine vhdl code, sequence detector vhdl
Carry Select Adder VHDL Code
BCD to 7 Segment Decoder VHDL Code
2 thoughts on “Sequence Detector using Mealy and Moore State Machine VHDL Codes”
nia
November 8, 2017 at 1:48 am | Reply
https://allaboutfpga.com/sequence-detector-using-mealy-and-moore-state-machine-vhdl-codes/ 9/14
5/8/2019 Sequence Detector using Mealy and Moore State Machine VHDL Codes
coder
September 1, 2017 at 11:56 pm | Reply
Menu
hello, mealy machine is not working good, the dout becomes ‘1’ on falling edge of clock and not rising edge. thanks
Leave a Reply
HOTTEST PRODUCT
https://allaboutfpga.com/sequence-detector-using-mealy-and-moore-state-machine-vhdl-codes/ 10/14
5/8/2019 Sequence Detector using Mealy and Moore State Machine VHDL Codes
Menu
8500 INR/ 132 USD
Buy Now
https://allaboutfpga.com/sequence-detector-using-mealy-and-moore-state-machine-vhdl-codes/ 11/14
5/8/2019 Sequence Detector using Mealy and Moore State Machine VHDL Codes
Menu
VHDL Code for Clock Divider (Frequency Divider)
Recent Posts
https://allaboutfpga.com/sequence-detector-using-mealy-and-moore-state-machine-vhdl-codes/ 12/14
5/8/2019 Sequence Detector using Mealy and Moore State Machine VHDL Codes
About Us
AllAboutFPGA.com is commercial site for selling FPGA development products and it is part of Invent Logics. We are the developers of
high quality and low cost FPGA development kits. Our aim is to provide the best FPGA learning platform to the students, research
Menu and young engineers.
scholars
Like us on Facebook
Subscribe to Newsletter
Email Address
Subscribe
My Accounts
My account
Terms of Service
https://allaboutfpga.com/sequence-detector-using-mealy-and-moore-state-machine-vhdl-codes/ 13/14
5/8/2019 Sequence Detector using Mealy and Moore State Machine VHDL Codes
Privacy Policy
Store A liates
A liate Login
Menu
© 2018 • All About FPGA
https://allaboutfpga.com/sequence-detector-using-mealy-and-moore-state-machine-vhdl-codes/ 14/14