题解 | #根据状态转移写状态机-三段式#
根据状态转移写状态机-三段式
https://www.nowcoder.com/practice/d8394a6d31754e73ace8c394e9465e2a
`timescale 1ns/1ns module fsm1( input wire clk , input wire rst , input wire data , output reg flag ); //*************code***********// parameter s0 = 2'd0, s1 = 2'd1, s2 = 2'd2, s3 = 2'd3; reg [1:0] cur_st,nxt_st; always @(posedge clk or negedge rst) begin if(rst == 1'b0) cur_st <= s0; else cur_st <= nxt_st; end always @(*)(1444584) begin case(cur_st) s0:begin if(data == 1'b1) nxt_st = s1; else nxt_st = s0; end s1:begin if(data == 1'b1) nxt_st = s2; else nxt_st = s1; end s2:begin if(data == 1'b1) nxt_st = s3; else nxt_st = s2; end s3:begin if(data == 1'b1) nxt_st = s0; else nxt_st = s3; end default: nxt_st = s0; endcase end always @(posedge clk or negedge rst)begin if(rst == 1'b0) flag <= 1'b0; else if(cur_st == s3 && data == 1'b1) flag <= 1'b1; else flag <= 1'b0; end //*************code***********// endmodule