状态机分为moore状态机和mealy状态机。
Mealy状态机的输出信号是当前状态和所有输入信号的函数,mealy状态机的输出时在输入变化后立即发生变化,且输入变化可能出现在时钟周期的任何时候,mealy型状态机对输入的响应比moore型状态机对输入的响应早一个时钟周期。
Moore型状态机的输出只与当前状态有关,而与输入无关,moore型状态机时钟跳变后的有限个门延迟之后,输出达到稳定值,输出会在一个完整的周期内稳定,即使在该时钟周期内输入信号有变化,输出也不会变化。输入对输出的影响要到下个周期才能反映出来。
下面以序列检测程序11010011
moore型状态机:
module SCHK(input clk,input din,input rst,output sout);
parameter s0=0,s1=1,s2=2,s3=3,
s4=4,s5=5,s6=6,s7=7,s8=8;
reg[8:0] st,nst;
always@(posedge clk,posedge rst)
if(rst) st<=s0;
else st<=nst;
always@(st,din)
begin
case (st)
s0:if(din==1'b1) nst<=s1;else nst<=s0;
s1:if(din==1'b1) nst<=s2;else nst<=s0;
s2:if(din==1'b0) nst<=s3;else nst<=s0;
s3:if(din==1'b1) nst<=s4;else nst<=s0;
s4:if(din==1'b0) nst<=s5;else nst<=s0;
s5:if(din==1'b0) nst<=s6;else nst<=s0;
s6:if(din==1'b1) nst<=s7;else nst<=s0;
s7:if(din==1'b1) nst<=s8;else nst<=s0;
s8:if(din==1'b0) nst<=s3;else nst<=s0;
default :nst<=s0;
endcase
end
assign sout=(st==8);
endmodule
mealy状态机:
module test1(input clk,input din,input rst,output reg sout);
parameter s0=0,s1=1,s2=2,s3=3,
s4=4,s5=5,s6=6,s7=7,s8=8;
reg[8:0] st,nst;
always@(posedge clk,posedge rst)
if(rst) st<=s0;
else st<=nst;
always@(st,din)
begin
sout=0;
case (st)
s0:if(din==1'b1) nst<=s1;else nst<=s0;
s1:if(din==1'b1) nst<=s2;else nst<=s0;
s2:if(din==1'b0) nst<=s3;else nst<=s0;
s3:if(din==1'b1) nst<=s4;else nst<=s0;
s4:if(din==1'b0) nst<=s5;else nst<=s0;
s5:if(din==1'b0) nst<=s6;else nst<=s0;
s6:if(din==1'b1) nst<=s7;else nst<=s0;
s7:if(din==1'b1) begin sout=1;
nst<=s8;end
else nst<=s0;
s8:if(din==1'b0) nst<=s3;else nst<=s0;
default :nst<=s0;
endcase
end
endmodule
用户1673212 2013-5-2 12:38