题解 | #输入序列连续的序列检测#

输入序列连续的序列检测

https://www.nowcoder.com/practice/d65c2204fae944d2a6d9a3b32aa37b39

`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input a,
	output reg match
	);

	reg [8:0]cur_state;
	reg [8:0]nex_state;

	parameter IDLE=9'b0_0000_0001;
	parameter s0=9'b0_0000_0010;
	parameter s1=9'b0_0000_0100;
	parameter s2=9'b0_0000_1000;
	parameter s3=9'b0_0001_0000;
	parameter s4=9'b0_0010_0000;
	parameter s5=9'b0_0100_0000;
	parameter s6=9'b0_1000_0000;
	parameter s7=9'b1_0000_0000;

	always@(posedge clk or negedge rst_n)begin
		if(!rst_n)begin
			cur_state<=IDLE;
		end
		else begin
			cur_state<=nex_state;
		end
	end

	always@(*)begin
		case(cur_state)
		IDLE:nex_state=(a==1'b0)?s0:IDLE;
		s0:nex_state=(a==1'b1)?s1:s0;
		s1:nex_state=(a==1'b1)?s2:s0;
		s2:nex_state=(a==1'b1)?s3:s0;
		s3:nex_state=(a==1'b0)?s4:s0;
		s4:nex_state=(a==1'b0)?s5:s0;
		s5:nex_state=(a==1'b0)?s6:s0;
		s6:nex_state=(a==1'b1)?s7:s0;
		s7:nex_state=(a==1'b1)?IDLE:s0;
		endcase
	end

	//由图可见,match的输出比a要慢一拍,所以应该使用时序逻辑电路判断
	//因为在always块中要使用reg类型,且题目已经给出的定义output match是reg类型
	//所以在不改动原先代码的前提下,应在always中使用match,然后用assign在always块外对另一个match_wire进行工作
	//等于always单纯是用来打一拍的延后赋值
	wire match_wire;
	always@(posedge clk or negedge rst_n)begin
		if(!rst_n)begin
			match<=0;
		end
		else begin
			match<=match_wire;
		end
	end

	assign match_wire=(cur_state==s7)?1:0;
  
endmodule

要点在代码注释中

全部评论

相关推荐

小叮当411:应该是1-3个月吧
点赞 评论 收藏
分享
程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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