`timescale 1ns/1ns

module huawei7(
	input wire clk  ,
	input wire rst  ,
	output reg clk_out
);

//*************code***********//
parameter    S0 = 4'b0001  ;
parameter    S1 = 4'b0010  ;
parameter    S2 = 4'b0100  ;
parameter    S3 = 4'b1000  ;

reg [3:0] cur_state ;
reg [3:0] nex_state ;
//三段式状态机--第一段 时序逻辑描述状态转移关系
always@(posedge clk or negedge rst)begin
    if(!rst)
	    cur_state <= S0        ;
    else 
	    cur_state <= nex_state ;
end

//三段式状态机--第二段 组合逻辑描述状态转移逻辑
always@(*)begin
	case(cur_state)
	    S0: nex_state = S1        ;
		S1: nex_state = S2        ;
		S2: nex_state = S3        ;
        S3: nex_state = S0        ;
		default :  nex_state = S0 ;
	endcase
end

//三段式状态机--第三段 时序逻辑描述输出
always@(posedge clk or negedge rst)begin
    if(!rst)
	    clk_out <= 1'b0              ;
    else begin
		case(nex_state)
		    S1: clk_out <= 1'b1      ;
			default: clk_out <= 1'b0 ;
		endcase
	end
end

//*************code***********//
endmodule

三段式状态机优点

  • 将组合逻辑与时序逻辑分开,所写代码层次清晰,方便理解和后续的维护
  • 相较两段式状态机,解决了输出毛刺的影响。