此次实验为使用三段式状态机设计一款5位序列检测器
本次实验以设计“10010”检测器为例
quartus II输出的状态转移图
modelsim的仿真波形
module schk(input clk,din,rst,output sout); parameter s0=6'b000001,s1=6'b000010,s2=6'b000100,s3=6'b001000,s4=6'b010000,s5=6'b100000;//独热码 reg [5:0] current_state,next_state; always@(posedge clk or negedge rst) if(rst==0) current_state<=s0; else current_state<=next_state; always@(current_state or din) begin case(current_state) s0:if(din==1'b1) next_state<=s1;else next_state<=s0;//进位是1,进入s1,反之则回到s0 s1:if(din==1'b0) next_state<=s2;else next_state<=s1;//进位是0,进入s2,反之则回到s1 s2:if(din==1'b0) next_state<=s3;else next_state<=s1;//进位是0,进入s3,反之则回到s1 s3:if(din==1'b1) next_state<=s4;else next_state<=s0;//进位是1,进入s4,反之则回到s0 s4:if(din==1'b0) next_state<=s5;else next_state<=s1;//进位是0,进入s5,反之则回到s1 default:next_state<=s1;//if语句内条件以外的情况回到s1 endcase end assign sout=(current_state==s5); //输出现态s5 endmodule
//搭建testbench并用modelsim进行仿真验证状态机序列检测器是否正确实现
`timescale 1ns/1ns
module schk_tb;
reg clk;
reg rst;
reg din;
wire sout;
schk u_schk(
.clk (clk),
.rst (rst),
.din (din),
.sout (sout)
);
initial begin
clk=0;
forever begin
#10;clk=~clk;
end
end
initial begin
rst=0;
#20;rst=1;
end
initial begin
din=0;
forever begin
din=1;#20;
din=0;#20;
din=0;#20;
din=1;#20;
din=0;#20;
end
end
endmodule