{"id":11934,"date":"2016-02-01T09:15:13","date_gmt":"2016-02-01T17:15:13","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=11934"},"modified":"2025-01-03T00:45:35","modified_gmt":"2025-01-03T08:45:35","slug":"how-to-code-a-state-machine-in-verilog","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/how-to-code-a-state-machine-in-verilog\/","title":{"rendered":"How to Code a State Machine in Verilog"},"content":{"rendered":"<p>Not to long ago, I wrote a <a href=\"https:\/\/digilent.com\/blog\/index.php\/what-is-a-state-machine\/\">post about what a state machine is<\/a>. 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&#8217;t much help without knowing how to code it.<\/p>\n<p>As you know from the last post, a state machine is made up of three\u00a0components. The next state logic, state register and output logic.<\/p>\n<p>The first component I&#8217;ll go through is the next state logic. This is coded directly from the state diagram.<\/p>\n<p>I&#8217;m going to put the state diagram here for reference.<\/p>\n<figure id=\"attachment_12093\" aria-describedby=\"caption-attachment-12093\" style=\"width: 618px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.instructables.com\/id\/How-to-Control-a-Stepper-Motor-With-an-FPGA\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-12093 size-full\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/01\/StepperMotor_fullstep_statediagran.png\" alt=\"Stepper motor controller state diagram.\" width=\"618\" height=\"467\" data-wp-pid=\"12093\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/01\/StepperMotor_fullstep_statediagran.png 618w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/01\/StepperMotor_fullstep_statediagran-600x453.png 600w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/a><figcaption id=\"caption-attachment-12093\" class=\"wp-caption-text\">Stepper motor controller state diagram.<\/figcaption><\/figure>\n<figure id=\"attachment_12106\" aria-describedby=\"caption-attachment-12106\" style=\"width: 337px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.instructables.com\/id\/How-to-Control-a-Stepper-Motor-With-an-FPGA\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-12106 size-full\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/01\/1-state-variables.jpg\" alt=\"This block of code creates the state variables. This way each state can be referenced by name.\" width=\"337\" height=\"145\" data-wp-pid=\"12106\" \/><\/a><figcaption id=\"caption-attachment-12106\" class=\"wp-caption-text\">This block of code creates the state variables. This way each state can be referenced by name.<\/figcaption><\/figure>\n<figure id=\"attachment_12107\" aria-describedby=\"caption-attachment-12107\" style=\"width: 274px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.instructables.com\/id\/How-to-Control-a-Stepper-Motor-With-an-FPGA\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-12107 size-full\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/01\/2-state-register-variables.jpg\" alt=\"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.\" width=\"274\" height=\"68\" data-wp-pid=\"12107\" \/><\/a><figcaption id=\"caption-attachment-12107\" class=\"wp-caption-text\">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.<\/figcaption><\/figure>\n<figure id=\"attachment_12108\" aria-describedby=\"caption-attachment-12108\" style=\"width: 377px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.instructables.com\/id\/How-to-Control-a-Stepper-Motor-With-an-FPGA\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-12108 size-full\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/01\/3-state-logic-1.jpg\" alt=\"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.\" width=\"377\" height=\"386\" data-wp-pid=\"12108\" \/><\/a><figcaption id=\"caption-attachment-12108\" class=\"wp-caption-text\">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.<\/figcaption><\/figure>\n<figure id=\"attachment_12109\" aria-describedby=\"caption-attachment-12109\" style=\"width: 364px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.instructables.com\/id\/How-to-Control-a-Stepper-Motor-With-an-FPGA\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-12109 size-full\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/01\/4-state-logic-2.jpg\" alt=\"This is the second chunk of the next state logic. It defines where you go next if you are in sig3 or sig2.\" width=\"364\" height=\"476\" data-wp-pid=\"12109\" \/><\/a><figcaption id=\"caption-attachment-12109\" class=\"wp-caption-text\">This is the second chunk of the next state logic. It defines where you go next if you are in sig3 or sig2.<\/figcaption><\/figure>\n<figure id=\"attachment_12110\" aria-describedby=\"caption-attachment-12110\" style=\"width: 331px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.instructables.com\/id\/How-to-Control-a-Stepper-Motor-With-an-FPGA\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-12110 size-full\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/01\/5-state-logic-3.jpg\" alt=\"This is the third chunk of next state logic. It defines what to do if you are in state sig1 or sig0.\" width=\"331\" height=\"479\" data-wp-pid=\"12110\" \/><\/a><figcaption id=\"caption-attachment-12110\" class=\"wp-caption-text\">This is the third chunk of next state logic. It defines what to do if you are in state sig1 or sig0.<\/figcaption><\/figure>\n<figure id=\"attachment_12111\" aria-describedby=\"caption-attachment-12111\" style=\"width: 255px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.instructables.com\/id\/How-to-Control-a-Stepper-Motor-With-an-FPGA\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-12111 size-full\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/01\/6-output-logic.jpg\" alt=\"This is the output logic. This is represented on the state diagram as the purple text. \" width=\"255\" height=\"294\" data-wp-pid=\"12111\" \/><\/a><figcaption id=\"caption-attachment-12111\" class=\"wp-caption-text\">This is the output logic. This is represented on the state diagram as the purple text.<\/figcaption><\/figure>\n<figure id=\"attachment_12112\" aria-describedby=\"caption-attachment-12112\" style=\"width: 287px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.instructables.com\/id\/How-to-Control-a-Stepper-Motor-With-an-FPGA\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-12112 size-full\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/01\/6-state-register.jpg\" alt=\"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.\" width=\"287\" height=\"194\" data-wp-pid=\"12112\" \/><\/a><figcaption id=\"caption-attachment-12112\" class=\"wp-caption-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.<\/figcaption><\/figure>\n<p>For more information on this state diagram and full code. You can view the instructable on <a href=\"https:\/\/digilent.com\/blog\/controlling-a-stepper-motor-with-an-fpga\/\" target=\"_blank\" rel=\"noopener\">driving a stepper motor with FPGA<\/a>. This works with the <a href=\"https:\/\/digilent.com\/shop\/basys-3-artix-7-fpga-trainer-board-recommended-for-introductory-users\/\">Basys 3<\/a>, \u00a0<a href=\"https:\/\/digilent.com\/shop\/nexys-a7-fpga-trainer-board-recommended-for-ece-curriculum\/\">Nexys A7<\/a>, <a href=\"https:\/\/digilent.com\/shop\/cmod-a7-35t-breadboardable-artix-7-fpga-module\/\">Cmod A7<\/a>, and <a href=\"https:\/\/digilent.com\/shop\/cmod-s7-breadboardable-spartan-7-fpga-module\/\">Cmod S7.<\/a><\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-11934 jlk' data-task='like' data-post_id='11934' data-nonce='3c15ebf169' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-11934 lc'>+18<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-11934 jlk' data-task='unlike' data-post_id='11934' data-nonce='3c15ebf169' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-11934 unlc'>-13<\/span><\/a><\/div><\/div> <div class='status-11934 status align-left'><\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>Now we know what a state machine is. But how do we code one?<\/p>\n","protected":false},"author":18,"featured_media":12111,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35,1563],"tags":[144],"ppma_author":[4466],"class_list":["post-11934","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fpga","category-guide","tag-tutorial-2"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/01\/6-output-logic.jpg","authors":[{"term_id":4466,"user_id":18,"is_guest":0,"slug":"kaitlyn","display_name":"Kaitlyn Franz","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/3f16f6159d3666c6fd05a4b73a18c286?s=96&d=mm&r=g","author_category":"","user_url":"","last_name":"Franz","last_name_2":"","first_name":"Kaitlyn","first_name_2":"","job_title":"","description":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/11934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=11934"}],"version-history":[{"count":4,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/11934\/revisions"}],"predecessor-version":[{"id":31267,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/11934\/revisions\/31267"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/12111"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=11934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=11934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=11934"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=11934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}