How to Code a State Machine in Verilog

7

Not to long ago, I wrote a post about what a state machine is. That post covered the state machine as a concept and way to organize your thoughts. Well, if you are looking to use state machines in FPGA design, the idea isn’t much help without knowing how to code it.

As you know from the last post, a state machine is made up of three components. The next state logic, state register and output logic.

The first component I’ll go through is the next state logic. This is coded directly from the state diagram.

I’m going to put the state diagram here for reference.

Stepper motor controller state diagram.

Stepper motor controller state diagram.

This block of code creates the state variables. This way each state can be referenced by name.

This block of code creates the state variables. This way each state can be referenced by name.

This block of code creates the state registers that hold the next_state and present_state variables. This is which circle you are on, on the state diagram.

This block of code creates the state registers that hold the next_state and present_state variables. This is which circle you are on on the state diagram.

This is the first chunk of the state logic. It shows, if you are in state sig4, based on the input what the next state is. This encapsulates the arrows on the state diagram.

This is the first chunk of the state logic. It shows, if you are in state sig4, based on the input what the next state is. This encapsulates the arrows on the state diagram.

This is the second chunk of the next state logic. It defines where you go next if you are in sig3 or sig2.

This is the second chunk of the next state logic. It defines where you go next if you are in sig3 or sig2.

This is the third chunk of next state logic. It defines what to do if you are in state sig1 or sig0.

This is the third chunk of next state logic. It defines what to do if you are in state sig1 or sig0.

This is the output logic. This is represented on the state diagram as the purple text.

This is the output logic. This is represented on the state diagram as the purple text.

This is the state register. It moves the next_state to the present_state on the positive clock edge. This is what makes movement between the states in the state diagram happen.

This is the state register. It moves the next_state to the present_state on the positive clock edge. This is what makes movement between the states in the state diagram happen.

For more information on this state diagram and full code. You can view the instructable on driving a stepper motor with FPGA.

Share on FacebookTweet about this on TwitterShare on Google+Pin on PinterestShare on RedditShare on TumblrShare on LinkedInShare on StumbleUponEmail this to someone
Share.

About Author

When I started school I wasn’t interested in any of things I’m passionate about now. In fact originally I started out wanting to study art. But then I decided I didn’t want to have people telling me what to create, so I changed to music. Then I decided I didn’t want to ruin a hobby by making it my career. At the same time my Physics class was teaching a unit on the physics of music, and I thought that was way interesting, so I changed to physics. Then by the time physics was over I decided that the coolest part of physics was electricity and magnetism, and I may as well get a degree that transfers directly into a career. So while all this was happening, I was attending Shoreline Community College, and during that time I found my passion, or rather presented itself repeatedly, until I realized, maybe I should take a hint from the universe. While at community college, I was asked to help at the high school by tutoring chemistry students. Then I was asked to help at the elementary school by being a math Olympiad coach. I continued both because I found I really enjoyed it. I also had an opportunity, and was hired to be a tutor in the Math Learning Center at the Community College, a job I really loved. At the same time I was working as a Nanny, which I had been doing for several years, the main reason because I could and would answer the hard questions the kids asked honestly (i.e. why is the sky blue). I then was recommended by the patrons of the MLC to the transfer tutoring center (private tutoring,) and developed a wait list of students. Through all these opportunities at some point I realized that I loved watching people go from totally lost, to masters of a subject. I was also forced to admit that having all these opportunities continually renewed, I must have been somewhat good at it. So I decided I wanted to teach, which fits with my mission oriented personality. I saw a serious lack of passionate ECE professors in the institutions I attended. At WSU I continued this trend by being ask to TA for computer science and electrical engineering, being a TA for a total of 4 semesters. This continued by getting an amazing opportunity in my first semester at Washington State University to work at Digilent, an educational company. So even if I didn’t want to teach, turns out I can’t avoid it. Luckily it is my main passion.

7 Comments

    • I’ve translated Kaitlyn’s example into VHDL:

      library IEEE;
      use IEEE.STD_LOGIC_1164.ALL;

      entity state_machine is
      Port (
      CLK : in STD_LOGIC;
      RST : in STD_LOGIC;
      dir : in STD_LOGIC;
      en : in STD_LOGIC;
      sigout : out STD_LOGIC_VECTOR(3 downto 0)
      );
      end state_machine;

      architecture Behavioral of state_machine is

      –define a new type to hold state values
      type state_type is (STATE_0, STATE_1, STATE_2, STATE_3, STATE_4);

      –present state will turn into the state register
      –present state is driven by next state at the clock edge
      signal present_state, next_state : state_type;

      begin

      –controls transitions to next state
      state_register_logic:
      process(CLK, RST) begin
      if (RST = ‘1’) then –reset logic
      present_state <= STATE_0;
      elsif (CLK'event and CLK = '1') then –if rising edge of clock
      present_state
      if (dir = ‘0’ and en = ‘1’) then
      next_state <= STATE_3;
      elsif (dir = '1' and en = '1') then
      next_state <= STATE_1;
      else
      next_state
      if (dir = ‘0’ and en = ‘1’) then
      next_state <= STATE_2;
      elsif (dir = '1' and en = '1') then
      next_state <= STATE_4;
      else
      next_state
      if (dir = ‘0’ and en = ‘1’) then
      next_state <= STATE_1;
      elsif (dir = '1' and en = '1') then
      next_state <= STATE_3;
      else
      next_state
      if (dir = ‘0’ and en = ‘1’) then
      next_state <= STATE_4;
      elsif (dir = '1' and en = '1') then
      next_state <= STATE_2;
      else
      next_state
      if ( en = ‘1’) then
      next_state <= STATE_1;
      else
      next_state
      sigout
      sigout
      sigout
      sigout
      sigout <= "0000";
      end case;
      end process;

      end Behavioral;

  1. Hey,

    Great article, thanks.

    I was wondering if you can provide a blog post about designing a simple digital phase modulator, I’m interested in building my own Software Defined Radio, applying my FPGA knowledge in designing most of the digital components of the radio would be amazing.

    Much Regards
    Abdullah

  2. Hi Fredrik,

    Unfortunately I don’t know VHDL well enough to confidently write up a similar post, I’m in the process of learning. However, I found this blog post that has a VHDL example.
    http://vhdlguru.blogspot.com/2010/04/how-to-implement-state-machines-in-vhdl.html

    If you get stuck at any point in writing your state machine please feel free to post on forum.digilentinc.com. There’s a whole community of people like me willing to help you out!

    Kaitlyn

  3. Hi Abdullah,
    I haven’t done any Digital Phase Modulation myself but it looks like from my brief internet search that it’s similar to PWM. If this is the case I’m working on a instructable and blog post on using servos with FPGA which uses PWM. That should be posted relatively soon.

    If not, and in the mean time I would post on forum.digilentinc.com. One of the members of the Digilent Forum may have already done this and likely would be better help than me.

    Kaitlyn

Leave A Reply