题解 | #含有无关项的序列检测#
含有无关项的序列检测
http://www.nowcoder.com/practice/cba67d06d6834a5d9b93e1087b56c8d8
`timescale 1ns/1ns
module sequence_detect(
input clk,
input rst_n,
input a,
output reg match
);
parameter S0 =10'b0000_0000_01;
parameter S1 =10'b0000_0000_10;
parameter S2 =10'b0000_0001_00;
parameter S3 =10'b0000_0010_00;
parameter S4 =10'b0000_0100_00;
parameter S5 =10'b0000_1000_00;
parameter S6 =10'b0001_0000_00;
parameter S7 =10'b0010_0000_00;
parameter S8 =10'b0100_0000_00;
parameter S9 =10'b1000_0000_00;
reg [9:0] CS;
reg [9:0] NS;
always@(posedge clk or negedge rst_n) begin
if(!rst_n) begin
CS <=S0;
end
else begin
CS <=NS;
end
end
always@(*) begin
case(CS)
S0: begin
if(a==1'b0) begin
NS <=S1;
end
else begin
NS <=S0;
end
end
S1: begin
if(a==1'b0) begin
NS <=S1;
end
else begin
NS <=S2;
end
end
S2: begin
if(a==1'b0) begin
NS <=S1;
end
else begin
NS <=S3;
end
end
S3: begin
NS <=S4;
end
S4: begin
NS <=S5;
end
S5: begin
NS <=S6;
end
S6: begin
if(a==1'b0) begin
NS <=S1;
end
else begin
NS <=S7;
end
end
S7: begin
if(a==1'b0) begin
NS <=S1;
end
else begin
NS <=S8;
end
end
S8: begin
if(a==1'b1) begin
NS <=S0;
end
else begin
NS <=S9;
end
end
S9: begin
if(a==1'b0) begin
NS <=S1;
end
else begin
NS <=S2;
end
end
default: NS <=S0;
endcase
end
always@(posedge clk or negedge rst_n) begin
if(!rst_n) begin
match<=1'b0;
end
else begin
case(CS) //注意:这个地方应该是NS,三段式状态机时序输出应该case next_state,从时序图也可以看出,都采用moore状态机了,输出延迟了两个时钟周期,这是有问题的。但是答案是 case current_state
S9: match<=1'b1;
default: match<=1'b0;
endcase
end
end
endmodule
module sequence_detect(
input clk,
input rst_n,
input a,
output reg match
);
parameter S0 =10'b0000_0000_01;
parameter S1 =10'b0000_0000_10;
parameter S2 =10'b0000_0001_00;
parameter S3 =10'b0000_0010_00;
parameter S4 =10'b0000_0100_00;
parameter S5 =10'b0000_1000_00;
parameter S6 =10'b0001_0000_00;
parameter S7 =10'b0010_0000_00;
parameter S8 =10'b0100_0000_00;
parameter S9 =10'b1000_0000_00;
reg [9:0] CS;
reg [9:0] NS;
always@(posedge clk or negedge rst_n) begin
if(!rst_n) begin
CS <=S0;
end
else begin
CS <=NS;
end
end
always@(*) begin
case(CS)
S0: begin
if(a==1'b0) begin
NS <=S1;
end
else begin
NS <=S0;
end
end
S1: begin
if(a==1'b0) begin
NS <=S1;
end
else begin
NS <=S2;
end
end
S2: begin
if(a==1'b0) begin
NS <=S1;
end
else begin
NS <=S3;
end
end
S3: begin
NS <=S4;
end
S4: begin
NS <=S5;
end
S5: begin
NS <=S6;
end
S6: begin
if(a==1'b0) begin
NS <=S1;
end
else begin
NS <=S7;
end
end
S7: begin
if(a==1'b0) begin
NS <=S1;
end
else begin
NS <=S8;
end
end
S8: begin
if(a==1'b1) begin
NS <=S0;
end
else begin
NS <=S9;
end
end
S9: begin
if(a==1'b0) begin
NS <=S1;
end
else begin
NS <=S2;
end
end
default: NS <=S0;
endcase
end
always@(posedge clk or negedge rst_n) begin
if(!rst_n) begin
match<=1'b0;
end
else begin
case(CS) //注意:这个地方应该是NS,三段式状态机时序输出应该case next_state,从时序图也可以看出,都采用moore状态机了,输出延迟了两个时钟周期,这是有问题的。但是答案是 case current_state
S9: match<=1'b1;
default: match<=1'b0;
endcase
end
end
endmodule