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

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

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

全部评论

相关推荐

05-12 17:28
已编辑
门头沟学院 硬件开发
ldf李鑫:不说公司名祝你以后天天遇到这样的公司
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务