//检测10010序列
module fsm(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;//状态为E且输入为0,则输出1
always @(posedge clk) begin
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
end
endmodule
`timescale 1ns/1ns
module fsm_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]};//把24位的数据循环移位,采用的是时钟周期加拼接的方法
initial
begin
clk=0;
rst=1;
#2 rst=0;
#30 rst=1;
data ='b1100_1001_0000_1001_0100;
#500 $stop;
end
fsm m(x,z,clk,rst,state);
endmodule