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

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

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;
parameter S1 = 2'b01;
parameter S2 = 2'b10;
parameter S3 = 2'b11;

reg [1:0] c_state;
reg [1:0] n_state;
always@(posedge clk or negedge rst) begin
	if(!rst) begin
		c_state <= S0;
	end
	else begin
		c_state <= n_state;
	end
end

always @(*) begin
	if(!rst) begin
		n_state <= S0;
	end
	else begin
		case(c_state) 
			S0: n_state <= (data == 1)? S1:S0;
			S1: n_state <= (data == 1)? S2:S1;
			S2: n_state <= (data == 1)? S3:S2;
			S3: n_state <= (data == 1)? S0:S3;
		endcase
	end
end

always @(posedge clk or negedge rst) begin
	if(!rst) begin
		flag <= 0;
	end
	else if (c_state == S3 && data == 1) begin
		flag <= 1;
	end
	else begin
		flag <= 0;
	end
end



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

全部评论

相关推荐

LemontreeN:有的兄弟有的我今天一天面了五场,4个二面一个hr面
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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