写在前面的话
序列检测器也算是数字IC笔面中常考的一个知识点了,这里包含有移位寄存器解法和状态机解法,还有重复序列检测和不重复序列检测。但是,现在的好多笔试都要求采用状态机的解法来实现。
为什么?你以为他考的是序列检测,结果考得还是状态机。为啥?因为状态机重要啊,不是所有设计都需要序列检测,但所有设计都肯定需要状态机。只要和状态机扯上关系,那能聊的就多了去了,状态机分米利和摩尔,也分一段、两段、三段式、阻塞和非阻塞区别等等。分分钟就能了解到你的基本功是不是扎实的,如果只是看序列检测,那你可太年轻了,还是得多看看状态机。
序列检测器示例
序列检测器就是将指定的序列从数字码流中检测出来, 收到输出为1,否则输出为0
移位寄存器解法
重复序列检测
重复检测1101序列
// -----------------------------------------------------------------------------
// Copyright (c) 2014-2022 All rights reserved
// -----------------------------------------------------------------------------
// Author : HFUT904 1320343336@qq.com
// File : seq_detect1.v
// Create : 2022-11-02 19:55:16
// Revise : 2022-11-02 19:55:16
// Editor : HFUT Integrated Circuit Design & Research Center
// Verdion: v1.0
// Description: 重复检测序列1101 移位寄存器法
// -----------------------------------------------------------------------------
module seq_detect1 (
input clk , // Clock
input rst_n , // Asynchronous reset active low
input din , // Data input
output flg // detect flag
);
reg [3:0 ] din_t ;
always @(posedge clk or negedge rst_n) begin : proc_din_t
if(~rst_n) begin
din_t <= 4'd0;
end
else begin
din_t <= {din_t[2:0],din};
end
end
assign flg =(din_t == 4'b1101 )?1'b1:1'b0 ;
endmodule
不重复序列检测
不重复检测1101序列
// -----------------------------------------------------------------------------
// Copyright (c) 2014-2022 All rights reserved
// -----------------------------------------------------------------------------
// Author : HFUT904 1320343336@qq.com
// File : seq_detect2.v
// Create : 2022-11-02 20:04:36
// Revise : 2022-11-02 20:04:36
// Editor : HFUT Integrated Circuit Design & Research Center
// Verdion: v1.0
// Description: 不重复检测序列1101 移位寄存器法
// -----------------------------------------------------------------------------
module seq_detect2 (
input clk , // Clock
input rst_n , // Asynchronous reset active low
input din , // Data input
output flg // detect flag
);
reg [1:0] cnt ;
reg [3:0 ] din_t ;
always @(posedge clk or negedge rst_n) begin
if(~rst_n) begin
cnt <= 2'd0;
end
else if(cnt == 2'd3) begin
cnt <= 2'd0;
end
else begin
cnt <= cnt +1'b1 ;
end
end
always @(posedge clk or negedge rst_n) begin : proc_din_t
if(~rst_n) begin
din_t <= 4'd0;
end
else begin
din_t <= {din_t[2:0],din};
end
end
assign flg =((din_t == 4'b1101) &&(cnt == 2'd3) )?1'b1:1'b0 ;
endmodule
状态机解法(重要)
状态机按照类型分可分为米利型(Mealy)和摩尔型(Moore)两类。
其中:
Moore状态机:输出只和当前状态相关,与输入无关。
Mealy状态机:输出和当前状态和输入状态同时有关。
按照写法又可分为一段式、两段式和三段式,强烈建议学习三段式写法。
下面列举的写法采用的是三段式Moore状态机写法,感兴趣的同学可以接着
重复序列检测
重复检测1101序列
// -----------------------------------------------------------------------------
// Copyright (c) 2014-2022 All rights reserved
// -----------------------------------------------------------------------------
// Author : HFUT904 1320343336@qq.com
// File : seq_detect3.v
// Create : 2022-11-02 20:49:09
// Revise : 2022-11-02 20:49:09
// Editor : HFUT Integrated Circuit Design & Research Center
// Verdion: v1.0
// Description: 重复检测序列1101 三段式Moore状态机解法
// -----------------------------------------------------------------------------
module seq_detect3 (
input clk , // Clock
input rst_n , // Asynchronous reset active low
input din , // Data input
output reg flg // detect flag
);
//状态定义
reg [4:0] cs ; //当前状态
reg [4:0] ns ; //下一状态
localparam IDLE = 5'b00001 ;
localparam Seq1 = 5'b00010 ;
localparam Seq11 = 5'b00100 ;
localparam Seq110 = 5'b01000 ;
localparam Seq1101 = 5'b10000 ;
//第一段,描述状态转移,时序逻辑
always @(posedge clk or negedge rst_n) begin : proc_
if(~rst_n) begin
cs <= IDLE;
end
else begin
cs <= ns;
end
end
//第二段,描述状态转移条件,组合逻辑
always @(*) begin
case(cs)
IDLE : ns = din? Seq1 : IDLE ;
Seq1 : ns = din? Seq11 : IDLE ;
Seq11 : ns = din? Seq11 : Seq110 ;
Seq110 : ns = din? Seq1101 : IDLE ;
Seq1101 : ns = din? Seq11 : IDLE ;
default : ns = IDLE ;
endcase
end
//第三段,描述状态输出,可组合逻辑 可时序逻辑
always @(posedge clk or negedge rst_n) begin
if(~rst_n)
flg <= 1'b0 ;
else if(cs == Seq1101)
flg <= 1'b1 ;
else
flg <= 1'b0 ;
end
endmodule
不重复序列检测
不重复检测1101序列
// -----------------------------------------------------------------------------
// Copyright (c) 2014-2022 All rights reserved
// -----------------------------------------------------------------------------
// Author : HFUT904 1320343336@qq.com
// File : seq_detect4.v
// Create : 2022-11-02 21:10:10
// Revise : 2022-11-02 21:10:10
// Editor : HFUT Integrated Circuit Design & Research Center
// Verdion: v1.0
// Description: 不重复检测序列1101 三段式Moore状态机解法
// -----------------------------------------------------------------------------
module seq_detect4 (
input clk , // Clock
input rst_n , // Asynchronous reset active low
input din , // Data input
output reg flg // detect flag
);
//状态定义
reg [4:0] cs ; //当前状态
reg [4:0] ns ; //下一状态
localparam IDLE = 5'b00001 ;
localparam Seq1 = 5'b00010 ;
localparam Seq11 = 5'b00100 ;
localparam Seq110 = 5'b01000 ;
localparam Seq1101 = 5'b10000 ;
//第一段,描述状态转移,时序逻辑
always @(posedge clk or negedge rst_n) begin : proc_
if(~rst_n) begin
cs <= IDLE;
end
else begin
cs <= ns;
end
end
//第二段,描述状态转移条件,组合逻辑
always @(*) begin
case(cs)
IDLE : ns = din? Seq1 : IDLE ;
Seq1 : ns = din? Seq11 : IDLE ;
Seq11 : ns = din? IDLE : Seq110 ;
Seq110 : ns = din? Seq1101 : IDLE ;
Seq1101 : ns = IDLE ;
default : ns = IDLE ;
endcase
end
//第三段,描述状态输出,可组合逻辑 可时序逻辑
always @(posedge clk or negedge rst_n) begin
if(~rst_n)
flg <= 1'b0 ;
else if(cs == Seq1101)
flg <= 1'b1 ;
else
flg <= 1'b0 ;
end
endmodule
总结
序列检测器也是数字IC笔试面试中重要的知识点,用移位寄存器的写法要简单不少,但是近几年的笔试都会要求采用状态机的解法,本质上还是测试对状态机的理解和熟练程度,甚至有些面试官会询问状态机的画法,大家关注的知识点还是应该放在状态机上,加深对状态机的理解。感兴趣的同学可以再找找例题做做。