要求:设计一个10010 序列检测器
目录
1、按照要求画出状态转移图
2、根据状态转移图书写代码
端口说明:
输入端口:时钟、复位、输入的序列in
输出端口: 标志符号flag
代码如下
module seq_test(
input wire clk,
input wire rst_n,
input wire in,
output wire flag
);
reg [5:0] current_state;
reg [5:0] next_state;
parameter IDLE = 6'b000_001,
A = 6'b000_010,
B = 6'b000_100,
C = 6'b001_000,
D = 6'b010_000,
E = 6'b100_000;
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
current_state <= IDLE;
else
current_state <= next_state;
end
always @(*) begin
case(current_state)
IDLE: if(in == 1'b1)
next_state = A;
A: if(in == 1'b0)
next_state = B;
B: if(in == 1'b0)
next_state = C;
else
next_state = A;
C: if(in == 1'b1)
next_state = D;
else
next_state = IDLE;
D: if(in == 1'b0)
next_state = E;
else
next_state = A;
E: if(in == 1'b0)
next_state = C;
else
next_state = A;
default: next_state = IDLE;
endcase
end
assign flag = (current_state == E)? 1'b1:1'b0;
endmodule
3、书写testbench,测试功能
module tb_seq_test();
reg clk;
reg rst_n;
reg in;
wire flag;
reg [15:0] data;
integer i;
initial begin
rst_n =0;
clk =0;
in=0;
data = 16'b1100100100010010;
#100;
rst_n =1;
for(i = 0; i <16 ;i=i+1) begin
@(posedge clk) begin
in = data[15-i];
end
end
end
always #10 clk =~clk;
seq_test seq_test_inst(
.clk(clk),
.rst_n(rst_n),
.in(in),
.flag(flag)
);
endmodule
4、modelsim 仿真结果
仿真输入的序列为1100100100010010,根据仿真,正确指示出10010序列
5、总结
10010序列检测器,是比较基础的设计之一,主要问题在于需要画出正确的状态转移图,本设计主要考虑的问题在于,如何考虑重复序列的检测,否则容易出现状态遗漏。