三段式状态机分析
编写三段式状态机,第一步是需要分析状态,并编制状态参数。
Parameter [1:0]S0=2’b00; Parameter [1:0]S1=2’b01; Parameter [1:0]S2=2’b10; Parameter [1:0]S3=2’b11;
第一段:
采用时序逻辑:描述状态转换
reg [1:0]current_state; reg [1:0]next_state; always@(posedge clk) begin if(!rst) current_state <= 2'b00; else current_state <= next_state; end
第二段:
采用组合逻辑,描述下一状态
always@(*) begin case(current_state) S0: next_state = S1; S1: next_state = S2; S2: next_state = S3; S3: next_state = next_state //锁在了S3状态 default: next_state = S0; endcase end
第三段:
输出逻辑
reg out_r; always@(posedge clk) begin if(!rst) out_r <= 1'b0; else begin case(current_state) S0,S2: out_r <= 1'b0; S1,S3: out_r <= 1'b1; default: out_r <= out_r; endcase end end assign out_o = out_r;