转载:序列检测机

代码实现:

//功能:实现111序列检测器(使用FSM状态机)
module sequence_111(
    input en,              //输入使能(高位有效)
    input clk,             //输入时钟
    input rst,             //复位信号(高位有效)
    input m_sequence,      //输入序列信号
    output reg out_en,     //输出使能信号(高位有效)
    output reg detection   //输出检测信号(高位有效)
    );

//所有十种状态
parameter s0 = 4'b0000;
parameter s1 = 4'b0001;
parameter s2 = 4'b0010;
parameter s3 = 4'b0011;
parameter s4 = 4'b0100;
parameter s5 = 4'b0101;
parameter s6 = 4'b0110;
parameter s7 = 4'b0111;
parameter s8 = 4'b1000;
parameter s9 = 4'b1001;

reg [3:0] state;    //当前状态
reg [1:0] counter;  //计数(确保有足够序列读入)
reg out_en_temp;    //用于输出使能信号延迟

//计数(确保有足够序列读入),确保有至少三个输入
always @(posedge clk)
begin
    if(rst)
        counter <= 2'b0;
    else
        begin
            if(en && (counter <= 2'b01))
                counter <= counter + 2'b01;
            else
                counter <= counter;
        end
end

//根据计数器当前数值作输出使能判断
always @(posedge clk)
begin
if(rst)
    out_en_temp <= 1'b0;
else
    begin
        if(en && (counter == 2'b10))
            out_en_temp <= 1'b1;
        else
            out_en_temp <= 1'b0;
    end
end

//时钟延迟
always @(posedge clk)
begin
if(rst)
    out_en <= 1'b0;
else
    out_en <= out_en_temp;
end

//状态转移
always @(posedge clk)
begin
    if(rst)
        state <= s0;
    else
        begin
            if(en)
                begin
                case (state)
                    s0: begin
                        if(m_sequence)
                        state <= s1;
                        else
                        state <= s0;
                        end
                    s1: begin
                        if(m_sequence)
                        state <= s3;
                        else
                        state <= s2;
                        end
                    s2: begin
                        if(m_sequence)
                        state <= s5;
                        else
                        state <= s4;
                        end
                    s3: begin
                        if(m_sequence)
                        state <= s7;
                        else
                        state <= s6;
                        end
                    s4: begin
                        if(m_sequence)
                        state <= s1;
                        else
                        state <= s0;
                        end
                    s5: begin
                        if(m_sequence)
                        state <= s3;
                        else
                        state <= s2;
                        end
                    s6: begin
                        if(m_sequence)
                        state <= s5;
                        else
                        state <= s4;
                        end
                    s7: begin
                        if(m_sequence)
                        state <= s8;
                        else
                        state <= s6;
                        end
                    s8: begin
                        if(m_sequence)
                        state <= s9;
                        else
                        state <= s6;
                        end
                    s9: begin
                        if(m_sequence)
                        state <= s7;
                        else
                        state <= s6;
                        end
                    default: state <= state;
                endcase
                end
            else
                state <= state;
        end
end

//输出检测信号
always @(posedge clk)
begin
    if(rst)
    detection <= 1'b0;
    else
    begin
        case(state)
        s7: detection <= 1'b1;
        default:detection <= 1'b0;
        endcase
    end 
end

状态转移图:
在这里插入图片描述
三、TestBench
左边导航栏(Flow Navigator)选择 AddSources→Add or create simulation sources→Create File Add Sources \rightarrow Add\ or\ create\ simulation\ sources\rightarrow Create\ FileAddSources→Add or create simulation sources→Create File 命名后即可添加Verilog文件(可以在主文件名后加_tb),点击 Sources(导航栏右上)→Hierarchy→simulation Sources→sim_1 Sources(导航栏右上) \rightarrow Hierarchy\rightarrow simulation\ Sources\rightarrow sim_1Sources(导航栏右上)→Hierarchy→simulation Sources→sim_1 即可找到刚刚兴建的文件,双击打开。

代码示例:

`timescale 1ns / 1ps            //定义模块的仿真时的时间单位和时间精度
module sequence_111_tb();

//一般将待测试模块的输入设置为reg,输出设置为wire即可
reg en;
reg clk;
reg rst;
reg m_sequence;
wire out_en;
wire detection;
            
//待测试模块例化 ,第一个名称为待测试模块名称,第二个名称符合命名规范随意设置一个名称即可    
//输入输出设置按如下形式设置,testbench中名称最好沿用待测试文件中名称,方便编写      
sequence_111 sequence_111_1 (
	.en(en),
	.clk(clk),
	.rst(rst),
	.m_sequence(m_sequence),
    .out_en(out_en),
    .detection(detection)
);

//起始语句,只执行一次
//#10表示样式十个单位时间(本文一个单位时间设置为1ns)
initial                                                
begin   
clk <= 0;
en <= 0;
rst <= 1;
m_sequence <= 1;
#10 
begin
    en <= 1;
    rst <= 0;
end                                              
#10	m_sequence = 1; 
#10	m_sequence = 1;
#10 m_sequence = 0;
#10 m_sequence = 0;    
#10 m_sequence = 1;
#10 m_sequence = 1;
#10 m_sequence = 1;
#10 m_sequence = 1;
#10 m_sequence = 0;
#10 m_sequence = 0;
#10 m_sequence = 0;         
end 

//设置时钟
always #5 clk <= ~clk;                                                   
                                                   
endmodule

其中s8 s8s8与s9 s9s9状态为避免检测到1111或者11111连续输出检测有效而设置的状态。

————————————————
版权声明:本文为CSDN博主「xidian_hxc」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xidian_hxc/article/details/103771131

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、实验目的: 1、深入了解与掌握同步时序逻辑电路的设计过程; 2、了解74LS74、74LS08、74LS32及74LS04芯片的功能; 3、能够根据电路图连接好实物图,并实现其功能。学会设计过程的检验与完善。 二、实验内容描述: 题目:“1 1 1”序列检测器。 原始条件:使用D触发器( 74 LS 74 )、“与”门 ( 74 LS 08 )、“或”门( 74 LS 32 )、非门 ( 74 LS 04 ),设计“1 1 1”序列检测器。 集成电路引脚图: D触发器( 74 LS 74 ) “与”门 ( 74 LS 08 ) “或........ 三、实验设计过程: 第1步,画出原始状态图和状态表。 根据任务书要求,设计序列检测器有一个外部输入x和一个外部输出Z。输入和输出的逻辑关系为:当外部输入x第一个为“1”,外部输出Z为“0”;当外部输入x第二个为“1”,外部输出Z为“0”;当外部输入x第三个为“1”,外部输出Z才为“1”。假定有一个外部输入x序列以及外部输出Z为: 输入x: 0 1 0 1 1 1 0 1 1 1 1 0 1 输出Z: 0 0 0 0 0 1 0 0 0 1 1 0 0 要判别序列检测器是否连续接收了“111”,电路必须用不同的状态记载外部输入x的值。假设电路的初始状态为A,x输入第一个“1”,检测状态由A装换到B,用状态B记载检测器接受了111序列的第一个“1”,这时外部输出Z=0;x输入第二个“1”,检测状态由B装换到C,用状态C记载检测器接受了111序列的第二个“1”,外部输出Z=0;x输入第三个“1”,检测状态由C装换到D,外部输出Z=1。然后再根据外部输入及其他情况时的状态转移,写相应的输出。以上分析了序列检测器工作,由此可画出图7-1所示的原始状态图。根据原始状态图可列原始状态表,如表7-2所示。 现态 次态/输出 x = 0 x = 1 A A / 0 B / 0 B A / 0 C / 0 C A / 0 D / 1 D A / 0 D / 1 (表 7-2 原始状态表) (图

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值