0101/110 序列检测器仿真 1 . 实验目的 熟悉Modelsim 仿真软件的使用方法,了解状态机的建模方法,使用ModelSim 仿真Qu artu sII 工程。 2 . 实验内容 用HDL 语言的输入方式,实现 0101/110 序列检测器。 用modelsim 进行仿真 下载至 DE0 开发板上观察实验结果 3 . 代码分析(以 0 1 0 1 序列检测器为例) 1) 状态图如下: 2) 主模块中首先定义了本次实验的所有输入输出接口及各个状态。其中,因为有 4 种状态,所以 cu rrent 为 2 位。 //0101 Sequ ential detector modu le lab1a (v in,cp,ncr,v ou t); inpu t v in,cp,ncr; ou tpu t v ou t; reg v ou t; reg [1:0] cu rrent,nex t; parameter s0=2'b00,s1=2'b01,s2=2'b10,s3=2'b11; 3) 然后设置异步清零,在 cp 上升沿则沿触发器状态翻转。 St0 St1 St3 St2 St0 St1 St2 0/0 1/0 0/0 1/1 1/0 0/0 0/0 1/0 0/0 0/0 1/0 1/0 1/0 0/1 图 3.1 0101(左)/110(右)序列检测状态图 always @(posedge cp or negedge ncr) begin if (~ncr) current <= s0; else current <= next; end 4) 接着编写组合逻辑部分,设定下一状态产生和输出的信号。 always @(current or vin) begin next=2'bxx; case (current) s0:begin next = (vin==1)?s0:s1; end s1:begin next = (vin==1)?s2:s1; end s2:begin next = (vin==1)?s0:s3; end s3:begin next = (vin==1)?s2:s1; end endcase end 5) 最后为输出部分,本程序中设置让输出信号经过一个寄存器再输出,可以消除 vout 信号中的毛刺。 always @(posedge cp or negedge ncr) begin if (~ncr) vout = 1'b0; else begin vout = 1'b0; case (current) s0,s1,s2:vout =1'b0; s3:if (vin==1) vout = 1'b1; else vout = 1'b0; endcase end end endmodule 6) 测试模块中同样先定义了各个变量,并将它们与主模块一一对应后进行初始化。 module test(); reg cp,clr,en; wire q; lab1a d (.cp(cp), .ncr (clr), .vin(en), .vout(q)); initial begin cp = 0; clr = 0; en = 0; end 7) 接着设置各信号波形:clr 在20 个单位时间后变为1,en 的数据变为有效,cp,,en 则分别在每10 个和16 个单位时间翻转一次。在420 个单位...