题解 | #根据状态转移写状态机-三段式#

根据状态转移写状态机-三段式

https://www.nowcoder.com/practice/d8394a6d31754e73ace8c394e9465e2a

`timescale 1ns/1ns

module fsm1(
	input wire clk  ,
	input wire rst  ,
	input wire data ,
	output reg flag
);
//*************code***********//
reg		[2:0]		cur_state ;
reg		[2:0]		next_state ;
parameter S0 = 0 ,S1 = 1,S2 = 2,S3 = 3;
always @(posedge clk or negedge rst) begin
	if(!rst) begin
		cur_state <= S0 ;
	end
	else begin
		cur_state <= next_state ;
	end
end
always @(*) begin
	case(cur_state)
		S0 : begin
			if(data) begin
				next_state = S1;
			end
			else begin
				next_state = S0;
			end
		end
		S1: begin
			if(data) begin
				next_state = S2;
			end
			else begin
				next_state = S1;
			end
		end
		S2: begin
			if(data) begin
				next_state = S3;
			end
			else begin
				next_state = S2;
			end
		end
		S3: begin
			if(data) begin
				next_state = S0;
			end
			else begin
				next_state = S3;
			end
		end
	endcase
end
	always @(posedge clk or negedge rst ) begin
		if(!rst) begin
			flag <= 1'b0 ;
		end
		else if(cur_state == S3 && data) begin
			flag <= 1'b1 ;
		end
		else begin
			flag <= 1'b0 ;
		end
	end

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-26 15:46
已编辑
字节国际 电商后端 24k-35k
点赞 评论 收藏
分享
一颗宏心:华为HR晚上过了十二点后还给我法消息。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务