题解 | #不重叠序列检测#(两种方法 题目没错)

不重叠序列检测

https://www.nowcoder.com/practice/9f91a38c74164f8dbdc5f953edcc49cc

`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input data,
	output wire match,      //状态机方法改为reg型
	output wire not_match  //状态机方法改为reg型
	);

/*
   reg [7:0] cur_state;
   reg [7:0] nex_state;

   parameter  IDLE   =  8'b00_000_001 ,
              S0     =  8'b00_000_010 ,
			  S1     =  8'b00_000_100 ,
			  S2     =  8'b00_001_000 ,
			  S3     =  8'b00_010_000 ,
              S4     =  8'b00_100_000 ,
              S5     =  8'b01_000_000 ,
              error  =  8'b10_000_000 ;
              
    
     reg [2:0] cnt ;
   always@(posedge clk or negedge rst_n)begin
       if(!rst_n)
           cnt <= 'd0;
       else if(cnt == 'd5)
           cnt <= 'd0;
       else 
           cnt <= cnt + 1'b1 ; 
   end
   
   //////三段式状态机/////////////////////////////
   always@(posedge clk or negedge rst_n)begin
       if(!rst_n)
           cur_state <= IDLE      ;
       else 
           cur_state <= nex_state ; 
   end
   
   always@(*)begin
      case(cur_state)
          IDLE: begin
              if(data == 1'b0)
                  nex_state <= S0    ;
              else
                  nex_state <= error ;
          end
          S0: begin
              if(data == 1'b1)
                  nex_state <= S1    ;
              else
                  nex_state <= error ;
          end
          S1: begin
              if(data == 1'b1)
                  nex_state <= S2    ;
              else
                  nex_state <= error ;
          end
          S2: begin
              if(data == 1'b1)
                  nex_state <= S3    ;
              else
                  nex_state <= error ;
          end
          S3: begin
              if(data == 1'b0)
                  nex_state <= S4    ;
              else
                  nex_state <= error ;
          end
          S4: begin
              if(data == 1'b0)
                  nex_state <= S5    ;
              else
                  nex_state <= error ;
          end
          S5: begin
              if(data == 1'b0)
                  nex_state <= S0    ;
              else
                  nex_state <= error ;
          end
          error: begin
              if(cnt == 'd5)
                  nex_state <= IDLE    ;
              else
                  nex_state <= error   ;
          end
          
          default: nex_state <= IDLE    ;
      endcase
   end
   
   always@(posedge clk or negedge rst_n)begin
       if(!rst_n)begin
           match     <= 1'b0;
           not_match <= 1'b0;
       end
       else if(cnt == 'd5)begin
           if(nex_state == S5)begin
               match     <= 1'b1;
               not_match <= 1'b0;
           end
           else begin
               match     <= 1'b0;
               not_match <= 1'b1;
           end 
       end
       else begin
           match     <= 1'b0;
           not_match <= 1'b0;    
       end
   end
 */  
   
  
   

//题目没有错 移位实现 要考虑时序
   reg [5:0] reg_num;
   reg [2:0] cnt    ;
  //移位 + 拼接实现寄存
  always@(posedge clk or negedge rst_n)begin
     if(!rst_n)
         reg_num <= 'd0;
     else 
         reg_num <= {reg_num[4:0], data};
  end
  
  //计数器
  always@(posedge clk or negedge rst_n)begin
     if(!rst_n)
         cnt <= 'd0;
     else if(cnt == 'd6)
         cnt <= 'd1;
     else 
         cnt <= cnt + 1'b1;
  end
/*  
  always@(posedge clk or negedge rst_n)begin
      if(!rst_n)begin
          match     <= 1'b0;
          not_match <= 1'b0;
      end
      else if(cnt == 'd6)begin
          if(reg_num == 6'b011100)begin
              match     <= 1'b1;
              not_match <= 1'b0; 
          end              
          else begin
              match     <= 1'b0;
              not_match <= 1'b1;  
          end  
      end
      else begin
          match     <= 1'b0;
          not_match <= 1'b0;
      end 
  end
 */
  assign  match     = (cnt == 'd6 & reg_num == 6'b011100)?1'b1:1'b0;
  assign  not_match = (cnt == 'd6 & reg_num != 6'b011100)?1'b1:1'b0;
  
endmodule

全部评论

相关推荐

爪哇沉淀ing:哎 感觉很丰富 其实没啥含金量 我本科也是理工的 实话实说这个学校真的没啥竞争力 建议还是提升学历
今天你投了哪些公司?
点赞 评论 收藏
分享
评论
6
4
分享

创作者周榜

更多
正在热议
更多
# 长得好看会提高面试通过率吗? #
2950次浏览 42人参与
# HR最不可信的一句话是__ #
985次浏览 32人参与
# MiniMax求职进展汇总 #
24824次浏览 321人参与
# 春招至今,你的战绩如何? #
14348次浏览 133人参与
# AI面会问哪些问题? #
874次浏览 21人参与
# 你的实习产出是真实的还是包装的? #
2588次浏览 52人参与
# 米连集团26产品管培生项目 #
7002次浏览 224人参与
# 沪漂/北漂你觉得哪个更苦? #
1076次浏览 29人参与
# 你做过最难的笔试是哪家公司 #
1084次浏览 20人参与
# AI时代,哪个岗位还有“活路” #
2630次浏览 49人参与
# XX请雇我工作 #
51141次浏览 171人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7950次浏览 43人参与
# 简历第一个项目做什么 #
32035次浏览 357人参与
# 简历中的项目经历要怎么写? #
310850次浏览 4257人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152795次浏览 888人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187527次浏览 1123人参与
# AI时代,哪些岗位最容易被淘汰 #
64474次浏览 860人参与
# 如果重来一次你还会读研吗 #
229960次浏览 2011人参与
# 投格力的你,拿到offer了吗? #
178175次浏览 889人参与
# 你怎么看待AI面试 #
180611次浏览 1291人参与
# 正在春招的你,也参与了去年秋招吗? #
364105次浏览 2640人参与
# 腾讯音乐求职进展汇总 #
160808次浏览 1114人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务