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

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

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

全部评论

相关推荐

美团 后端开发 总包n(15%是股票)
点赞 评论 收藏
分享
shtdbb_:还不错,没有让你做了笔试再挂你
点赞 评论 收藏
分享
叮咚鸭:群众里面有坏人
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务