普通三段式,根据状态转移图写即可。
`timescale 1ns/1ns
module fsm1(
input wire clk ,
input wire rst ,
input wire data ,
output reg flag
);
//*************code***********//
localparam S0 = 2'b00 ;
localparam S1 = 2'B01 ;
localparam S2 = 2'B10 ;
localparam S3 = 2'b11 ;
reg [1:0] state,nxt_state ;
always @(posedge clk or negedge rst) begin
if(!rst)
state <= S0 ;
else
state <= nxt_state ;
end
always @(*) begin
case(state)
S0 : nxt_state = data ? S1 : S0 ;
S1 : nxt_state = data ? S2 : S1 ;
S2 : nxt_state = data ? S3 : S2 ;
S3 : nxt_state = data ? S0 : S3 ;
default : nxt_state = 2'b00 ;
endcase
end
always @(posedge clk or negedge rst) begin
if(!rst)
flag <= 1'b0 ;
else if(state==S3 && nxt_state==S0)
flag <= 1'b1 ;
else
flag <= 1'b0 ;
end
//*************code***********//
endmodule
标签:wire,进阶,三段式,19,state,状态机,localparam,rst,input
From: https://www.cnblogs.com/icwangpu/p/17038586.html