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

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

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'b00,
			s1	=	2'b01,
			s2	=	2'b10,
			s3	=	2'b11;

reg	[1:0]	cur_state,nxt_state;

always@(posedge	clk or negedge rst)
	if(!rst)
		cur_state	<=	s0;
	else	
		cur_state	<=	nxt_state;

always@(*)	begin
	if(!rst)
		nxt_state	<=	s0;
	else	case(cur_state)	
		s0:begin
			if(data==1'b1)
				nxt_state	<=	s1;
			else
				nxt_state	<=	s0;
		end
		s1:begin
			if(data==1'b1)
				nxt_state	<=	s2;
			else
				nxt_state	<=	s1;
		end
		s2:begin
			if(data==1'b1)
				nxt_state	<=	s3;
			else
				nxt_state	<=	s2;			
		end	
		s3:begin
			if(data==1'b1)
				nxt_state	<=	s0;
			else
				nxt_state	<=	s3;			
		end
	default:	nxt_state	<=	s0;
	endcase	
end

always@(posedge clk or negedge rst)	begin
	if(!rst)
		flag	<=	1'b0;
	else	case(cur_state)
		s3:begin
			if(data==1'b1)
				flag	<=	1'b1;
			else	
				flag	<=	1'b0;
		end
	
	default:	flag	<=	1'b0;
	endcase
end

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

全部评论

相关推荐

赏个offer求你了:友塔HR还专门加我告诉我初筛不通过😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务