搜索
bottom↓
回复: 1

VHDL写状态机的一个问题,希望大家注意

[复制链接]

出0入0汤圆

发表于 2010-11-22 20:37:45 | 显示全部楼层 |阅读模式
本人选择的FPGA是 EP2C8114,开发环境QUARTUS 9.0. 综合器用的是软件集成的,在用VHDL 构建状态机时发现一个容易犯的的错误。此错误,导致状态机状态转换的混乱。

状态机一般常用的是2段式和3段式的,在第二段中描述状态的转移。当设计的状态机是mealy型(状态的转移取决于输入的条件),在用if --else --end if,case  when end case:描述时.一定要明确的描述状态在相应的输入条件的条件转移情况。

实例

图一 正确的状态转换图和条件 (原文件名:图一.png)
代码

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity fsmtest is
port(
        clk: in std_logic;
        rst :in std_logic;
        a,b,c: in std_logic

);
end entity;

architecture rtl of fsmtest is
                TYPE state is(idle,s1,s2);
                signal cs,ns:state;
               
                begin
                        process(clk,rst) --state trasmit
                                begin
                                        if(clk'event and clk='1') then
                                                if(rst='0') then
                                                        cs<=idle;
                                                else
                                                        cs<=ns;
                                                end if;
                                        end if;
                        end process;
                       
                        process(cs,a,b,c)
                                        begin
                                        case cs is
                                               
                                                when idle =>
                                                                if(a='1') then
                                                                        ns<=s1;
                                                                 else
                                                                        ns<=idle;
                                                                end if;
                                                when s1 =>
                                                                        if(b='1') then
                                                                         ns<=s2;
                                                                         else
                                                                          --ns<=s1;
                                                                        end if;
                                                when s2 =>
                                                                case c  is
                                                                 when '1'=>  ns <=idle;
                                                                 when others=>
                                                                     ns<=s2;
                                                                end case;
                                                               
                                                when others =>
                                                                        ns <= idle;
                                                end case;         
                                end process;

end rtl;


在上述代码中有 3个条件状态的转移



                                          when idle =>

                                                        if(a='1') then

                                                               ns<=s1;

                                                         else

                                                               ns<=idle;

                                                        end if;

                                          when s1 =>

                                                               if(b='1') then

                                                                ns<=s2;

                                                                else

                                                                 ns<=s1;

                                                               end if;

                                          when s2 =>

                                                

                                                        case c  is

                                                         when '1'=>  ns <=idle;

                                                         when others=>

                                                             ns<=s2;

                                                        end case;



在不满足转移条件是也需要明确的用ns<= (本状态).不然状态机就出现混乱。



如将                                                 if(a='1') then

                                                               ns<=s1;

                                                         else

                                                               ns<=idle;

                                                        end if;

改成

                                                        if(a='1') then

                                                               ns<=s1;

                                                        end if;



综合的结果是


图二 错误的状态装换图和转移条件 (原文件名:图2.png)
       把case c  is

             when '1'=>  ns <=idle;

              when others=>

               ns<=s2;

               end case;

中的NS<=S2注释掉以后,结果和上面一样。

个人感觉,在用VHDL描述状态迁移时,即使这次的状态转移条件不满足,也需要明确的描述出将当前的状态作为下一个转移状态。

       If (条件满足) then

           NS<=(下一个状态)

       Else

       NS<=(当前状态)

       End if;



      case 条件 is

       case 满足=> cs<=(下一个状态)

       case others=> ns<=(当前状态)

       end case
不知道别的综合器综合处的是啥效果

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2010-11-22 22:53:29 | 显示全部楼层
process(cs,a,b,c)

是不是敏感信号多了
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 17:28

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表