题解 | #输入序列连续的序列检测#
输入序列连续的序列检测
https://www.nowcoder.com/practice/d65c2204fae944d2a6d9a3b32aa37b39
核心就是如图所示的状态机,状态用的是one-hot code 独热码
用独热码对状态进行编码,这样在状态判断中更省组合逻辑(我并没有深入研究原理)
写代码时我参考了这个视频,作者思路是正确的,就是状态机画错了
我自己画了一个正确的状态机
`timescale 1ns/1ns module sequence_detect( input clk, input rst_n, input a, output reg match ); parameter IDLE =9'b000000001; parameter s0 =9'b000000010; parameter s1 =9'b000000100; parameter s2 =9'b000001000; parameter s3 =9'b000010000; parameter s4 =9'b000100000; parameter s5 =9'b001000000; parameter s6 =9'b010000000; parameter s7 =9'b100000000; reg [8:0] cs,ns; always @(posedge clk or negedge rst_n) begin if(~rst_n) cs<=IDLE; else cs<=ns; end //01110001 always @(*) begin case(cs) IDLE: ns=(a==0)? s0:IDLE; s0 : ns=(a==1)? s1:s0; s1 : ns=(a==1)? s2:s0; s2 : ns=(a==1)? s3:s0; s3 : ns=(a==0)? s4:s0; s4 : ns=(a==0)? s5:s1; s5 : ns=(a==0)? s6:s1; s6 : ns=(a==1)? s7:s0; s7 : ns=(a==0)? s2:s0; endcase end wire match_tmp; assign match_tmp=(cs==s7); always @(posedge clk or negedge rst_n)begin if(~rst_n) match<=1'b0; else match<=match_tmp; end endmodule