create account

Logic Design - VHDL Finite-State Machines by drifter1

View this thread on: hive.blogpeakd.comecency.com
· @drifter1 · (edited)
$13.05
Logic Design - VHDL Finite-State Machines
<html>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/CPT-FSM-abcd.svg/326px-CPT-FSM-abcd.svg.png" width="326" height="268"/></p>
<p>&nbsp;&nbsp;&nbsp;Hi, its me again! Today we will talk about Flying Spaghetti Monsters...just joking. We will talk about <strong>Finite-State Machines</strong> or <strong>FSM's</strong> for short! It would be a great idea to check the <a href="https://steemit.com/logic/@drifter1/logic-design-vhdl-sequential-circuits">Sequential Circuits in VHDL</a> first if you haven't already! I will this time just talk about the <strong>differences </strong>when writing a <strong>mealy </strong>and a <strong>moore </strong>FSM and also <strong>implement </strong>some <strong>Example Diagrams</strong> and <strong>simulate </strong>them <strong>in Modelsim</strong>. So, without further do let's get straight into it!</p>
<p><br></p>
<h1>Quick Recap:</h1>
<p>&nbsp;&nbsp;&nbsp;&nbsp;A Sequential Circuit works as a Finite-State Machine mostly. We start from a specific State and depending on the Previous State and the Input we then go to the Next State! This transition happens only during a specific clock event. For us this clock event will be a Edge from '0' to '1' and so all our FMS's will contain DFF (that we will not actually see) that will be Positive Edge-Triggered. So, such an FSM contains Memory Elements and Combinational Logic to connect those together with the Inputs and Outputs.&nbsp;</p>
<h1>FMS Types:</h1>
<p>&nbsp;&nbsp;&nbsp;&nbsp;We have <strong>2 types of FMM's</strong>. A <strong>Mealy </strong>and a <strong>Moore </strong>FSM <strong>differentiate in </strong>the way we get the <strong>Output</strong>! A <strong>Mealy Output</strong> <strong>depends on </strong>the whole <strong>transition </strong>and so <strong>Previous State and Input</strong>. A <strong>Moore </strong>in the other hand <strong>has </strong>a <strong>specific Output depending</strong> <strong>on </strong>the <strong>State </strong>we currently are in and so the Output is binded together with the <strong>Current State</strong>.&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;You might remember how we can separate them when getting a Diagram, but because I will use this concept in all our Examples I rewind your memory really quick. A Mealy FSM Diagram will contain the Input/Output value in each Arrow-Transition and the States will just have the number/name that separates them from the others. A Moore on the other hand will have only the Input on the Arrow and the Output will be inside of the State.</p>
<p>This looks like this (picture from <a href="https://steemit.com/logic/@drifter1/logic-design-sequential-synchronous-circuits">Sequential Circuits Theory</a>):</p>
<p><img src="https://s26.postimg.org/xltid3b1l/fsms.jpg" width="274" height="353"/></p>
<p>We will have such Diagrams in our Example Section!</p>
<p><br></p>
<h1>Implementing FSM's in VHDL:</h1>
<p>&nbsp;&nbsp;&nbsp;&nbsp;The basic structure of Coding a Sequential Circuit stays the same. A Code for such a Sequential Circuit needs to have a <strong>Clock and </strong>a <strong>Reset </strong>in the <strong>Input </strong>and <strong>1 or 2 processes</strong> inside of the architecture that only <strong>change </strong>the <strong>state</strong> (and output when mealy) when a <strong>positive edge event</strong> is triggered. To get from one state to another we will use a lot of if and when statements that check the input value and Previous State. The output can be found directly from the Transition and so in the same process when we have a Mealy machine, but when having a Moore we will need a separate process for that. Sometimes we can get to a specific State starting off from different States that can make our Code smaller when having a Moore FMS for example. Some States may also be Starting States or Final States and so we can either don't return to them or can't get away from them.</p>
<p><br></p>
<p>The <strong>States </strong>will be defined as <strong>new Datatypes</strong>! We can do it pretty simple by using this line of code:</p>
<p><em><strong>type state_type is (s0, ..., sn);</strong></em></p>
<p>After that we <strong>define </strong>a <strong>signal </strong>of <strong>type state_type </strong>like that:</p>
<p><em><strong>signal state: state_type;</strong></em></p>
<p><br></p>
<p>So, our <strong>Code Layout </strong>looks like this:</p>
<pre><code>-- define entity entity_name</code></pre>
<pre><code>entity entity_name is</code></pre>
<pre><code>port(&nbsp;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;clk: in std_logic;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;reset: in std_logic;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;-- inputs</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;-- outputs</code></pre>
<pre><code>);</code></pre>
<pre><code>end A;</code></pre>
<pre><code>-- define architecture</code></pre>
<pre><code>architecture arch of entity_name is</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;-- define state type</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;type state_type is (s0, ..., sn)</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;signal state: state_type</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;begin</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;-- process for state switching</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;process(clk, reset)</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;begin</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;-- asynchronous if to reset fsm</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;-- if/when statements for each transition</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;-- if mealy we also set the output</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;end process;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;-- process for output if moore</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;process(state)</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;begin</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;-- if/when statement for each state/output combination</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;end process;</code></pre>
<pre><code>end arch;</code></pre>
<p><br></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;Hope that you can see that it's actually not quite difficult to write a FSM. Knowing how a Mealy and Moore differentiate can save you a lot of time, cause the coding process is actually always the same. Only the States, Transitions and Outputs will change. The rest stays mostly the same!</p>
<p><br></p>
<p>So, let's now get into 1 example for each FSM Type!</p>
<h1>Example Section:</h1>
<h3>Example1:</h3>
<p>We will first implement a <strong>Mealy </strong>FSM! The <strong>Diagram </strong>looks like this:</p>
<p><img src="https://s26.postimg.org/lm480k1bd/mealy.jpg" width="244" height="195"/></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;Because else we would end up with a lot of statements we will use a second process for the output, cause if you check out the Diagram you will see that the Output depends on the previous State only that can make our Code smaller then when having a output assignment in each Transition(Previous State + Input)!</p>
<p>So, our <strong>Code </strong>looks like this:</p>
<pre><code>library ieee;</code></pre>
<pre><code>use ieee.std_logic_1164.all;</code></pre>
<pre><code>entity A is</code></pre>
<pre><code>port(&nbsp;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;clk: in std_logic;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;input: in std_logic;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;areset: in std_logic;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;output: out std_logic</code></pre>
<pre><code>);</code></pre>
<pre><code>end A;</code></pre>
<pre><code><br></code></pre>
<pre><code>architecture arch of A is</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;type state_type is (s0, s1, s2);</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;signal state: state_type;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;begin</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;process(clk, areset)</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;begin</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;if areset='1' then state&lt;=s0;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;elsif(clk'event and clk='1') then</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;case state is</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;when s0=&gt;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if input='1' then state&lt;=s1;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;else state&lt;=s2;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;end if;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;when s1=&gt;</code></pre>
<pre><code>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if input='0' then state&lt;=s2;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;end if;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;when s2=&gt;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if input='1' then state&lt;=s1;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;end if;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;end case;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;end if;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;end process;</code></pre>
<pre><code><br></code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;process(state)</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;begin</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;case state is</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;when s0=&gt;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;output&lt;='0';</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;when s1=&gt;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;output&lt;='1';</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;when s2=&gt;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;output&lt;='0';</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;end case;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;end process;</code></pre>
<pre><code>end arch;</code></pre>
<p><br></p>
<p>Running it in <strong>Modelsim </strong>and assigning the Clock value using right-click we get the following results:</p>
<p><img src="https://s26.postimg.org/cfqir0lgp/mealy_2.jpg" width="331" height="180"/></p>
<p><img src="https://s26.postimg.org/wbmi6k2i1/mealy_3.jpg" width="590" height="115"/></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;You can see that it actually can get a lot of fun running a FSM in Modelsim, cause you can jump from one state to another much easier by simply running the Circuit Simulation after assigning the correct input value!</p>
<p><br></p>
<h3>Example 2:</h3>
<p>Let's now implement also a <strong>Moore Diagram</strong> that looks like this:</p>
<p><img src="https://s26.postimg.org/n3yv3lktl/moore.jpg" width="237" height="187"/></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;This time we will also define constants for the outputs! I will also make the State_type we define ourselfes be a subtype of std_logic_vector! You will see that this time we have to make the Output assignment in a separate process anyway, cause it doesn't work else.</p>
<p>So, our <strong>Code </strong>looks like this:</p>
<pre><code>library ieee;</code></pre>
<pre><code>use ieee.std_logic_1164.all;</code></pre>
<pre><code>entity B is</code></pre>
<pre><code>port(&nbsp;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;clk: in std_logic;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;input: in std_logic;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;areset: in std_logic;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;output: out std_logic_vector(1 downto 0)</code></pre>
<pre><code>);</code></pre>
<pre><code>end B;</code></pre>
<pre><code><br></code></pre>
<pre><code>architecture arch of B is</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;subtype state_type is std_logic_vector(1 downto 0);</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;signal state: state_type;</code></pre>
<pre><code><br></code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;constant s0: state_type:="00";</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;constant s1: state_type:="01";</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;constant s2: state_type:="10";</code></pre>
<pre><code><br></code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;begin</code></pre>
<pre><code><br></code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;process(clk, areset)</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;begin</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;if areset='1' then state&lt;=s0;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;elsif(clk'event and clk='1') then</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;case state is</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;when s0=&gt;</code></pre>
<pre><code>		if input='1' then state&lt;=s1;</code></pre>
<pre><code>		else state&lt;=s2;</code></pre>
<pre><code>		end if;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;when s1=&gt;</code></pre>
<pre><code>		if input='1' then state&lt;=s2;</code></pre>
<pre><code>		end if;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;when s2=&gt;</code></pre>
<pre><code>		if input='0' then state&lt;=s1;</code></pre>
<pre><code>		else state&lt;=s0;</code></pre>
<pre><code>		end if;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;when others=&gt;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;end case;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;end if;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;end process;</code></pre>
<pre><code><br></code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;process(state)</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;begin</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;case state is</code></pre>
<pre><code>&nbsp;&nbsp;&n&gt;bsp;&nbsp;when s0=&gt;</code></pre>
<pre><code>&nbsp;&nbsp;&nbs&lt;p;&nbsp;output&lt;="01";</code></pre>
<pre><code>&nbsp;&nbsp;&n&gt;bsp;&nbsp;when s1=&gt;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&n&lt;bsp;output&lt;="11";</code></pre>
<pre><code>&nbsp;&nbsp;&n&gt;bsp;&nbsp;when s2=&gt;</code></pre>
<pre><code>&nbsp;&nbsp;&lt;&nbsp;&nbsp;output&lt;="11";</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;when others=&gt;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;end case;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;end process;</code></pre>
<pre><code>end arch;</code></pre>
<p><br></p>
<p>The simulation in <strong>Modelsim </strong>looks as following:</p>
<p><img src="https://s26.postimg.org/kcflczmax/moore_2.jpg" width="345" height="144"/></p>
<p><img src="https://s26.postimg.org/8yt21sbs9/moore_3.jpg" width="570" height="109"/></p>
<p><br></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;You can see that it was a little more complicated that way, but the whole logic behind writing the Code stays the same! Also, yes I had all that already finished up inside of a word document :)</p>
<p><br></p>
<p>And this is actually it! Hope you enjoyed this post!</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;From now in VHDL we will implement Computer Components like ALU's, RAM and also simulate more advanced FSM's! So, VHDL gets now into the Assembly-Like phase also :D</p>
<p>Until next time...Bye!</p>
</html>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authordrifter1
permlinklogic-design-vhdl-finite-state-machines
categoryvhdl
json_metadata{"tags":["vhdl","logic","design","fsm","finite-state"],"image":["https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/CPT-FSM-abcd.svg/326px-CPT-FSM-abcd.svg.png","https://s26.postimg.org/xltid3b1l/fsms.jpg","https://s26.postimg.org/lm480k1bd/mealy.jpg","https://s26.postimg.org/cfqir0lgp/mealy_2.jpg","https://s26.postimg.org/wbmi6k2i1/mealy_3.jpg","https://s26.postimg.org/n3yv3lktl/moore.jpg","https://s26.postimg.org/kcflczmax/moore_2.jpg","https://s26.postimg.org/8yt21sbs9/moore_3.jpg"],"links":["https://steemit.com/logic/@drifter1/logic-design-vhdl-sequential-circuits","https://steemit.com/logic/@drifter1/logic-design-sequential-synchronous-circuits"],"app":"steemit/0.1","format":"html"}
created2017-09-29 08:56:36
last_update2017-09-29 09:00:00
depth0
children0
last_payout2017-10-06 08:56:36
cashout_time1969-12-31 23:59:59
total_payout_value9.954 HBD
curator_payout_value3.100 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length15,057
author_reputation98,202,866,830,354
root_title"Logic Design - VHDL Finite-State Machines"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id16,259,043
net_rshares4,709,730,626,382
author_curate_reward""
vote details (49)