题解 | #根据状态转移写状态机-二段式#居然没人说好奇怪可恶啊 我看不懂。
根据状态转移写状态机-二段式
http://www.nowcoder.com/practice/5b2ff27610d04993ae92374d51bfc2e6
``` `timescale 1ns/1ns
module fsm2( input wire clk , input wire rst , input wire data , output reg flag );
//**code//
/reg[2:0] s;
always @(posedge clk or negedge rst )begin
if (!rst)begin
s<=0;
flag<=0;
end
else begin
if(data==1)begin
if(s<=3)begin
s<=s+1;
flag<=0;
end
if(s==4)begin
s<=1;
flag<=1;
end
end
else begin
flag<=0;
if(s==4)begin
flag<=1;
s<=0;
end
end
end
end/
parameter S0=0, S1=1, S2=2, S3=3, S4=4;
reg [2:0] state, nstate;
always@(data or rst or state) begin
if(~rst) begin
nstate <= S0;
flag <= 0;
end
else
case(state)
S0: begin
nstate <= data? S1: S0;
flag <= 0;
end
S1: begin
nstate <= data? S2: S1;
flag <= 0;
end
S2: begin
nstate <= data? S3: S2;
flag <= 0;
end
S3: begin
nstate <= data? S4: S3;
flag <= 0;
end
S4: begin
nstate <= data? S1: S0;
flag <= 1;
end
default: begin
nstate <= S0;
flag <= 0;
end
endcase
end
always@(posedge clk or negedge rst) begin
if(~rst)
state <= S0;
else
state <= nstate;
end
//**code// endmodule