题解 | #状态机-重叠序列检测#

状态机-重叠序列检测

https://www.nowcoder.com/practice/10be91c03f5a412cb26f67dbd24020a9

重新写了个摩尔机。
`timescale 1ns/1ns

module sequence_test2(
	input wire clk  ,
	input wire rst  ,
	input wire data ,
	output reg flag
);
//*************code***********//

	parameter IDEL=0, S0=1, S1=2, S2=3, S3=4;
	reg [2:0] cstate, nstate;

	always@(posedge clk or negedge rst) begin
		if(!rst) begin
			cstate <= IDEL;
		end
		else begin
			cstate <= nstate;
		end
	end

	always@(*) begin
		case(cstate)
			IDEL:   nstate = data?S0:IDEL;
			S0:		nstate = data?S0:S1;
			S1:		nstate = data?S2:IDEL;
			S2:		nstate = data?S3:S1;
			S3:		nstate = data?S0:S1;
			default:nstate = IDEL;
		endcase
	end

	always@(posedge clk or negedge rst) begin
		if(!rst) begin
			flag <= 1'b0;
		end
		else begin
			flag <= (cstate==S3);
		end
	end

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


全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务