硬汉嵌入式论坛

 找回密码
 立即注册
查看: 3198|回复: 0
收起左侧

Verilog设计练习进阶(8)-------利用有限状态机进行复杂时序逻辑的设计

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107122
QQ
发表于 2013-1-30 19:53:32 | 显示全部楼层 |阅读模式
         在数字电路中我们已经学习过通过建立有限状态机来进行数字逻辑的设计,而在
Verilog HDL 硬件描述语言中,这种设计方法得到进一步的发展。通过Verilog HDL 提供的
语句,我们可以直观地设计出适合更为复杂的时序逻辑的电路。关于有限状态机的设计方法
在教材中已经作了较为详细的阐述,在此就不赘述了。
      下例是一个简单的状态机设计,功能是检测一个 5 位二进制序列“10010”。考虑到序列
重叠的可能,有限状态机共提供8 个状态(包括初始状态IDLE)
模块源代码:
seqdet.v
module seqdet(x,z,clk,rst,state);
input  x,clk,rst;
output z;
output[2:0] state;
reg[2:0] state;
wire z;
parameter IDLE='d0,  A='d1,  B='d2,
                     C='d3,  D='d4,
                     E='d5,  F='d6,
                     G='d7;
            
assign  z = ( state==E && x==0 )? 1 : 0;    //当x=0 时,状态已变为E,
                                          //状态为D 时,x仍为1。因此
                      //输出为1 的条件为( state==E && x==0 )。
always @(posedge clk)
   if(!rst)
          begin
          state <= IDLE;
          end
   else
          casex(state)
            IDLE : if(x==1)
                       begin   
                          state <= A;
                       end
            A:     if(x==0)
                       begin
                          state <= B;
                       end
            B:     if(x==0)
                       begin
                          state <= C;
                       end
                    else
                       begin
                          state <= F;
                       end
            C:      if(x==1)
                       begin
                          state <= D;
                   end
                    else
                       begin
                          state <= G;
                       end
            D:      if(x==0)
                       begin
                          state <= E;
                      end
                   else
                      begin
                         state <= A;
                      end
           E:      if(x==0)
                      begin
                         state <= C;
                      end
                   else
                      begin
                         state <= A;
                      end
           F:      if(x==1)
                      begin
                         state <= A;
                       end
                    else
                       begin
                          state <= B;
                       end
            G:      if(x==1)
                       begin
                          state <= F;
                       end
           default:state=IDLE;      //缺省状态为初始状态。
           endcase
endmodule
测试模块源代码:
//------------------ seqdet.v ------------------
`timescale 1ns/1ns
`include "./seqdet.v"
module seqdet_Top;
  reg clk,rst;
  reg[23:0] data;
  wire[2:0] state;
  wire z,x;
  assign x=data[23];
  always  #10 clk = ~clk;
  always @(posedge clk)
         data={data[22:0],data[23]};
   
  initial
     begin
       clk=0;
       rst=1;
       #2 rst=0;
       #30 rst=1;
       data ='b1100_1001_0000_1001_0100;
       #500 $stop;
     end
   
  seqdet  m(x,z,clk,rst,state);
         
endmodule                  
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-19 04:16 , Processed in 0.243051 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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